jsFrontend = {
	debug: true,
	
	// constructor
	init: function() {
		jsFrontend.layout.init();
		jsFrontend.controls.init();
		jsFrontend.facebook.init();
		jsFrontend.events.init();
	},
	
	// end of object
	eof: true
}

jsFrontend.controls = {
	// constructor
	init: function() {
		jsFrontend.controls.introbox();
		jsFrontend.controls.forms();
	},

	// forms
	forms: function() {
		$('.inputText').focus(function() {
			if($(this).val() == 'Bv.: Jane Doe.') $(this).val('').removeClass('isDefault');
			if($(this).val() == 'Bv.: jane@example.com') $(this).val('').removeClass('isDefault');
		})
		$('.inputText').blur(function() {
			if($(this).attr('id') == 'email' && $(this).val() == '') $(this).val('Bv.: jane@example.com').addClass('isDefault');
			else if($(this).attr('id') == 'newsletter' && $(this).val() == '') $(this).val('Bv.: jane@example.com').addClass('isDefault');
			else if($(this).attr('id').substr(0, 5) == 'email' && $(this).val() == '') $(this).val('Bv.: jane@example.com').addClass('isDefault');
			else if($(this).val() == '') $(this).val('Bv.: Jane Doe.').addClass('isDefault');
		})
		
		$('.inputText').each(function() {
			if($(this).val() == 'Bv.: Jane Doe.') $(this).addClass('isDefault');
			if($(this).val() == 'Bv.: jane@example.com') $(this).addClass('isDefault');
			if($(this).val() == 'Vul je e-mailadres in...') $(this).addClass('isDefault');
		});
	},
	
	// introbox
	introbox: function() {
		// introbox
		$('#introBoxClose').click(function() {
			// hide it first
			$('#introbox').css('visibility', 'hidden');

			// slide introbox away
			$('#introBox').slideUp();

			// and make sure we don't see it again by setting a cookie which should last for a week
			var date = new Date();
			date.setTime(date.getTime() + 7 * 24 * 60 * 60 * 1000);
			document.cookie = 'introbox=1;expires=' + date.toUTCString();
		});
	},
	
	// end of object
	eof: true
}
	
jsFrontend.facebook = {
	appId: null,
	loggedIn: false,
	queue: [],
		
	// constructor
	init: function() {
		// initialize
		FB.init({appId: jsFrontend.facebook.appId, status: true, cookie: true, xfbml: true});
		
		// get current status
		FB.getLoginStatus(function(response) {
			// logged in?
			if(response.session) {
				// set property
				jsFrontend.facebook.loggedIn = true;
				
				// populate
				jsFrontend.facebook.populate();
				
				// show correct divs
				$('#facebookLoggedIn').show();
				$('#facebookLoggedOut').hide();
				
				if(jsFrontend.facebook.queue.length > 0) {
					for(var i in jsFrontend.facebook.queue) eval(jsFrontend.facebook.queue[i]);
				}
			}
	
			// logged out
			else
			{
				// set property
				jsFrontend.facebook.loggedIn = false;

				// show correct divs
				$('#facebookLoggedIn').hide();
				$('#facebookLoggedOut').show();
			}
		});
	
		// when the session changes, we should reload
		FB.Event.subscribe('auth.sessionChange', function(response) { window.location.reload(true); });
	},
	
	// attend event
	attend: function(eventId, title, url) {
		if(jsFrontend.facebook.loggedIn) {
			// publish in stream
			FB.api('/me/feed', 'post', { message: 'Ik ga naar '+ title +'!', link: url +'?utm_source=facebook&utm_medium=feed&utm_campaign='+ eventId, name: title });

			// attend the facebook event
			if(eventId != '') FB.api('/'+ eventId + '/attending', 'post');
		}
	},
	
	// populate formfield
	populate: function() {
		// populate fields
		FB.api('/me', function(response) {
			if($('.fbName')) $('.fbName').html(response.name);
			if($('#name0').length > 0) $('#name0').val(response.name);
			if($('#name').length > 0) $('#name').val(response.name);
			if($('#email0').length > 0) $('#email0').val(response.email);
			if($('#email').length > 0) $('#email').val(response.email);
		});
	},
	
	// end of object
	eof: true
}


jsFrontend.events = {
	// constructor
	init: function() {
		jsFrontend.events.effects();
		if($('#signInForm .addRow').length > 0) $('#signInForm .addRow').click(jsFrontend.events.addRow);
		$('a.lightbox').fancybox({ 'transitionIn': 'fade', 'transitionOut':	'fade', 'speedIn': 600, 'speedOut': 200, 'overlayShow': false, 'showNavArrows': true, 'cyclic': true, 'centerOnScroll': true, 'titlePosition': 'over',
									'titleFormat': jsFrontend.events.fancyBoxTitle });

		if($('a.lightbox').length > 0 && window.location.hash != '') {
			var hash = window.location.hash.replace('#', '');
			var position = '';
			var index = 0;

			$('a.lightbox').each(function() {
				if($(this).attr('id') == 'show-'+ hash) {
					$(this).click();
					return;
				}
			});
		}
	},
		
	fancyBoxTitle: function(title, currentArray, currentIndex, currentOpt) {
		var location = window.location.href.replace(window.location.hash, '');
		
		// build html
		var html = '<span id="fancybox-title-over">'+ title;
		html += '<span style="float: right;"><iframe src="http://www.facebook.com/plugins/like.php?href='+ location +'#'+ $(currentArray[currentIndex]).attr('id') +'&amp;layout=button_count&amp;show_faces=false&amp;width=70&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:70px; height:21px;" allowTransparency="true"></iframe><span></span>'
		
		// return
		return html;
	},

	// mouseover-events
	effects: function() {
		// eventBox mouseover
		$('.overview .eventBox').mouseover(function() { $(this).find('.eventBoxHover').css('display', 'block').css('height', $(this).height() + 28); }) // 28 = padding
					  .mouseout(function() 	{ $(this).find('.eventBoxHover').css('display', 'none'); });

		// eventBox click
		$('.overview .eventBox').click(function() { window.location = $(this).attr('rel')});	
		$('.overview .archiveBox').click(function() { window.location = $(this).attr('rel')});	
	},

	addRow: function(evt) {
		evt.preventDefault();
		
		// get highest number
		var newIndex = parseInt($('#rows .inputText:last').attr('id').replace('email', '')) + 1;
		
		
		// build HTML
		var html = 	'<div class="row clearfix">'+
					'	<div class="inputHolder">'+
					'		<input type="text" class="inputText" maxlength="255" value="Bv.: Jane Doe." name="name'+ newIndex +'" id="name'+ newIndex +'">'+
					'	</div>'+
					'	<div class="inputHolder lastInputHolder">'+
					'		<input type="text" class="inputText" maxlength="255" value="Bv.: jane@example.com" name="email'+ newIndex +'" id="email'+ newIndex +'">'+ 
					'	</div>'+
					'	<div class="buttonHolder">'+
					'		<input type="submit" class="inputSubmit" value="Schrijf me in">'+
					'	</div>'+
					'</div>';
		
		$('#rows .buttonHolder').remove();
		$('#rows').append(html);
		
	},
	
	// end of object
	eof: true
}

jsFrontend.layout = {
	// constructor
	init: function() {
		jsFrontend.layout.fix();
	},
	
	// some layout fixes
	fix: function() {
		// add lastChild-class on last childs
		$('.selector li:last-child').addClass('lastChild');
		$('.archiveBox:last').addClass('lastArchiveBox');

		// add firstChild-class on first childs
		$('.selector li:first-child').addClass('firstChild');

		// fix the a/img problem
		$('.content img').each(function() {
			var parentTag = $(this).parent().get(0).tagName;
			if(parentTag == 'a') { $(this).parent().addClass('linkedImage'); };
		});
	},
	
	// end of object
	eof: true
}

$(document).ready(function() { jsFrontend.init(); });
