/* Declare a namespace for the site */
var Site = window.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {
	
	//same as $(document).ready();
	$(function() {
	
		initExternalLinks();
		//initLoaderCanvas();
		initBackground();
		initMenuMods();	
		initLightroom();
		onPageChange();
	    initAjax();
			
	});


	$(window).bind("load", function() {
	});
	
})(jQuery);

var $intro;
var $imgs = [];

function initBackground(){
	
	if($imgs.length <= 0)
	{
		$.each($("#background a"), function(index, img) {
			$imgs.push(img);	
		});
	}
	
	var $bg = $imgs[Math.floor(Math.random()*$imgs.length)];
	
	$("#background").css("background", "transparent url("+$bg+") top center no-repeat");
	$("#background").fadeIn("slow");

	$(window).resize(function() {
		if($(window).width() < 960)
		{
			$("#background").css("background-position", "top left");
			$("#background").css("width", "1200px");
		}
		else
		{
			$("#background").css("background-position", "top center");
			$("#background").css("width", "100%");
		}
	});
}

function initLoaderCanvas()
{
	 //var loader = document.getElementById("loader");
    // var ctx = loader.getContext("2d");

    // layer1/Path
      ctx.save();
      ctx.beginPath();
      ctx.moveTo(64.5, 32.2);
      ctx.bezierCurveTo(64.5, 50.1, 50.1, 64.5, 32.2, 64.5);
      ctx.bezierCurveTo(14.4, 64.5, 0.0, 50.1, 0.0, 32.2);
      ctx.bezierCurveTo(0.0, 14.4, 14.4, 0.0, 32.2, 0.0);
      ctx.bezierCurveTo(50.1, 0.0, 64.5, 14.4, 64.5, 32.2);
      ctx.closePath();
      ctx.fillStyle = "rgb(4, 4, 4)";
      ctx.fill();

      // layer1/Path
      ctx.beginPath();
      ctx.moveTo(19.6, 52.9);
      ctx.lineTo(46.4, 31.5);
      ctx.lineTo(33.3, 28.5);
      ctx.lineTo(43.5, 14.0);
      ctx.lineTo(35.2, 12.1);
      ctx.lineTo(22.5, 31.3);
      ctx.lineTo(32.0, 33.6);
      ctx.lineTo(19.6, 52.9);
      ctx.closePath();
      ctx.fillStyle = "rgb(255, 255, 255)";
      ctx.fill();
      ctx.restore();
}

function initLightroom()
{
	$("#single_post #mediabox a").click(function() {
		return false;
	});
}

function onPageChange()
 {
 	
 	$.address.change(function(event) {
 		
 		if(event.value != "/")
 		{
 			//$("#loader").css("margin-top", "-34px");
 			
 			$.ajax({ 	url: $.address.baseURL() + event.value, 
 						beforeSend: function() {
 							//$("#loader").show();
 							$("#main").hide();
 							$("html").css("cursor", "wait");
 						},
						success: function(data){
        			   	 $("#main").html(data);
        			   //	 $("#loader").hide();
        			   	 $("#main").fadeIn("fast");
	       			   	 $("html").css("cursor", "default");
        			   	 $('html, body').animate({scrollTop: '0px'}, 400, 'linear');
      				}
      		});
 		}
 		else
 		{
 			//$("#loader").hide();
 			$("#main").html($intro);
 			$("#nav a").removeClass("active");
 		}
 	});
 	
 	$.address.init(function(event) {
 		$intro = $("#main").html();
 	});
 }
 
 function initAjax()
 {
 	$("#nav a").click(function() {
		var $cell = $(this).parent().html();
		var $url = $($cell).attr("href");
		$.address.title($($cell).text() + " - The Portfolio of Benoit Nolens - Web and Mobile");
		var path = $url.replace(base_url, "");
		
		//location.hash = path;
		window.location.href = base_url + "/#!" + path;
		      	
 		return false;
 		
 	});
 }
 
 function initMenuMods() {
 	$("#nav a").click(function() {
 			$("#nav a").removeClass("active");
 			$(this).addClass("active");
 	});
 }
 
  function initExternalLinks()
 {
    $('a[rel*=external]').click( function() {
        window.open(this.href);
        return false;
    });
 }


