jQuery(function($) {
	
	// main infographic
	$('#browsable').scrollable({circular:true}).navigator();
	
	// Init jQuery Masonry plugin
	var $boxes = $('#boxes');
	$boxes.masonry({
		itemSelector : '.box',
		/*columnWidth : 298,*/
		singleMode: true,
		gutterWidth : 18
	});
		
	/* Load content into modules */
	
	//Foursquare
	$('#foursqcheckin').load(templateDir + '/functions/foursquare.php', function() {
		var mapdata = $('#mapdata').html();
		var maparray = mapdata.split(',');
		$('#foursqmap').gMap({
			latitude: maparray[0],
			longitude: maparray[1],
			maptype: 'ROADMAP',
			zoom: 12,
			controls: {
				panControl: true,
				zoomControl: true,
				mapTypeControl: true,
				scaleControl: false,
				streetViewControl: false,
				overviewMapControl: false
			},
			markers:[
				{
					latitude: maparray[0],
					longitude: maparray[1]
				}
			]
		});
		processBox($(this),$boxes);
	});

	//Posterous box 01
	$('#posterous01').load(templateDir + '/functions/posterous.php', {'post': 0}, function() {
		processBox($(this),$boxes);
	});

	//Posterous box 02
	$('#posterous02').load(templateDir + '/functions/posterous.php', {'post': 1}, function() {
		processBox($(this),$boxes);
	});

	//Posterous box 03
	$('#posterous03').load(templateDir + '/functions/posterous.php', {'post': 2}, function() {
		processBox($(this),$boxes);
	});
	
	//Twitter
	$('#tweets').load(templateDir + '/functions/twitter.php', function() {
		processBox($(this),$boxes);
	});
	
	//Flickr
	$('#flickpics').load(templateDir + '/functions/flickr.php', function() {
		$(this).removeClass('loading').cycle({
			fx:     'fade',
			speed:   1000, 
			timeout: 8000, 
			next:   '#next1', 
			prev:   '#prev1'
		});
		$boxes.imagesLoaded(function(){
			$boxes.masonry();
		});
	});
	
	//Quora
	$('#quoraquestions').load(templateDir + '/functions/quora.php', function() {
		processBox($(this),$boxes);
	});	
		
	/* Form modal boxes */
	
	// modal box trigger
	$('.modaltrigger').click(function() {
							
		var modalhtml = $(this).attr('href');
		
		// scroll to the top of the page
		$('html, body').animate({scrollTop: '0px'}, 200);
		
		// load the content of the modal box							
		$('div.modalcontent').load(modalhtml, function() {
		
			var overlay = $('#overlay');
			var target = $('div.modalbox');
			var maskHeight = $(document).height();  
			var maskWidth = $(window).width();  
		
			overlay.css({'color': '#ffffff', 'width':maskWidth+'px', 'height':maskHeight+'px', 'opacity':0.8}).fadeIn(500);    

			var winW = $(window).width();  

			target.css({'top':'40px','left': winW/2-target.width()/2+'px'}).fadeIn(500);
			
			$('input.focus').focus();
		
		});
		
		return false;
	});  

	$('.close, #overlay').live('click',function() { 
		$('#overlay, div.modalbox').fadeOut(300, function() {
			$('div.modalcontent').html('');																  
		});
		return false;
	});
	
	/* Process forms */
	
	// join form
	$('#joinuserform').live('submit', function() {
			
		$(this).ajaxSubmit({
			success: function(r) {
				if (r == "invalidcaptcha") {
					$('#results').html('Invalid captcha code');
				} else if (r == "invalidemail") {
					$('#results').html('Invalid email address');
				} else {
					$('html, body').animate({scrollTop: '0px'}, 200, function() {
						$('#joinformcontent').html('').load(templateDir + '/form_join_thanks.php');
					});
				}
			} 
		});
		return false;
		
	});

	// hire form
	$('#hireuserform').live('submit', function() {
			
		$(this).ajaxSubmit({
			success: function(r) {
				if (r == "invalidcaptcha") {
					$('#results').html('Invalid captcha code');
				} else if (r == "invalidemail") {
					$('#results').html('Invalid email address');
				} else {
					$('html, body').animate({scrollTop: '0px'}, 200, function() {
						$('#hireformcontent').html('').load(templateDir + '/form_hire_thanks.php');
					});
				}
			} 
		});
		return false;
		
	});

	// capthca image reload
	$('a.captchareload').live('click', function() {
		var captchapath = templateDir + '/functions/securimage/securimage_show.php?' + Math.random();
		$('#captchaimg').attr('src', captchapath);
		return false;
	});
	
	/* Setup HTML5 video controls */
	
	// play all videos
	$('a.playvideo').click(function() {
		$('video').trigger('play');
		$('a.playvideo').fadeOut(300);
		return false;
	});
	
	// show play button
	$('video').bind('ended', function() {
		$('a.playvideo').fadeIn(300);
	}); 

});

function processBox(item,boxes) {
	item.removeClass('loading');
	boxes.imagesLoaded(function(){
		boxes.masonry();
	});
}

