
// remap jQuery to $
(function($){
	$("#work li small").hide();
	var show = false;

	$("#splitter").draggable({ containment: "#container", axis: 'x' });

	$("#splitter").bind("drag dragstop", function(){

		var pos = $("#splitter").position();
		nyPos = pos.left + 175;
		$("#work").css("clip","rect(0px, 2000px, 2000px, " + nyPos + "px )");			

	});
	
	$("#container").width( $("#container").width() - 50 );	
	
	
	$(window).resize(function(){
		if (show) {
			$("#show").height($(window).height());
			$("#work_content").height($(window).height()-110);	
			$("#work_content").jScrollPane(); 	
		}
		
	});
	
	$("#awork").click(function(){
		if ( show ) {
			$("#show #close").click();
		}

		$("#awork").addClass("selected");
		$("#acontact").removeClass("selected");

	
		$("#work").animate({
	    	clip: "rect(0px, 2000px, 2000px, 100px)"
	    }, {
	    	duration: 1000,
	    	easing: 'easeOutExpo'			
	    });			
	
		$("#splitter").animate({
	    	left: '-10px'
	    }, {
	    	duration: 1000,
	    	easing: 'easeOutExpo'			
	    });			
	});
	
	$("#acontact").click(function(){
		$("#acontact").addClass("selected");
		$("#awork").removeClass("selected");
		
		if( show ) {
			api.getContentPane().empty();
			$(".jspContainer").hide();			
		}
		
		maxX = $(window).width() - 190;
		clip = maxX + 175;
	
		$("#work").animate({
	    	clip: "rect(0px, 2000px, 2000px, "+ clip +"px)"
	    }, {
	    	duration: 1000,
	    	easing: 'easeOutExpo'			
	    });	
	
		$("#splitter").animate({
	    	left: maxX + 'px'
	    }, {
	    	duration: 1000,
	    	easing: 'easeOutExpo'			
	    });				
	});

	$('#work li').hover(function(){
		$(this).find("small").show();				
		$(this).children("img").animate({
    		clip: "rect(0px, 240px, 150px, 0px)"
    	}, {
    		duration: 500,
    		easing: 'easeOutExpo'			
    	});
	}, function(){
		$(this).find("small").hide();				
    					
    	$(this).children("img").animate({
    		clip: "rect(0px, 240px, 150px, 240px)"
    	}, {
    		duration: 500,
    		easing: 'easeOutExpo',
    		complete: function() { 
    			$("#work li").children("img").css('clip','rect(0px 0px 150px 0px)');
    		}			
    	});
	});
	
	function doneLoading() {
		list = $("#work_content li").length;				
	}
	
	$('#work li').click(function(){
		$(".jspContainer").show();	
		show = true;
		var h = $(window).height();		
		
		// Load case
		title = $(this).children("div").children("h3").text();
		$("#show header h1").text( title );
		
		page = $(this).attr("id");
		$("#work_content").height($(window).height()-110);	
		

		//$("#work_content").load("data.html?v=3 #" + page, doneLoading );
		
		//$("#work_content").jScrollPane(); 		
	
		api = $('#work_content').jScrollPane({
			showArrows:false,
			maintainPosition: false
		}).data('jsp');
			
		api.getContentPane().load(
			"data.html?v=3 #" + page,
			function() {
				api.reinitialise();
				doneLoading();
		});

		$("#show").animate({
			height: h+'px'
		}, {
			duration: 1000,
			easing: 'easeOutExpo'
		});		
				
	});
 
	
	
	
	
	/******************************
	
		Show
	
	******************************/
	
	$('#show #close').click(function(){
		updateUrl("work", "Work");	
		show = false;
		
		$("#show").animate({
			height: '0px'
		}, {
			duration: 500,
			easing: 'easeOutExpo'
		});
		
		api.getContentPane().empty();
		$(".jspContainer").hide();	
	});
	
	
	$("#show #work_content #right").live("click", function(){


	});
	
	
	
	/******************************
	
		URL
	
	******************************/	

	var main_url_raw = window.location.href;
    function updateUrl(url, title) {
    	window.location.href = main_url + "/#/" + url;					
    	document.title = "Lasse Korsgaard - " + title;
    }
    
    String.prototype.capitalize = function() {
       return this.charAt(0).toUpperCase() + this.slice(1);
    }

    if ( main_url_raw.indexOf("contact") !== -1 ) {					
	    //alert(main_url_raw.indexOf("contact"));
    	// contact
    	
		$("#acontact").click();
    	slash_check = main_url_raw.indexOf("/#/");
		main_url = main_url_raw.slice( 0, slash_check );    	
    	updateUrl("contact", "Contact");



    }else{
    	// check for subpages				
    	main_url = window.location.href.slice( 0, window.location.href.indexOf('/#') );
    	page_url_raw = window.location.hash.substr(2);

    	if ( page_url_raw.indexOf("/") == -1 ) {
    		// work
    		//$("li:contains('work')").addClass("sel");
    		updateUrl("work", "Work");
    	}else{
    		//get subpage
    		sub_slash_check = page_url_raw.indexOf("/");
    		sub_url = page_url_raw.slice( sub_slash_check+1 );
    		updateUrl( "work/" + sub_url, sub_url.capitalize() );
    		$("#"+ sub_url ).click();
    	}
    }
	
	$("#work menu li").click(function(){
		updateUrl( "work/" + $(this).attr("id"), $(this).text().capitalize() );	
		_gaq.push(['_trackPageview', "Work/"+$(this).attr("id")]);		
	});
	
	$("#acontact").click(function(){
		updateUrl( "contact", "Contact" );	
		_gaq.push(['_trackPageview', 'Contact' ]);					
	});

	$("#awork").click(function(){
		updateUrl( "work", "Work" );	
		_gaq.push(['_trackPageview', 'Work']);					
	});	
	
    /*
    $("li").click(function(){
    	$(".sel").removeClass("sel");
    	if ( $(this).hasClass("work") ) {
    		$("#work").addClass("sel");
    		$(this).addClass("sel");
    		updateUrl( "work/" + $(this).text(), $(this).text().capitalize() );
    	}else{
    		$(this).addClass("sel");
    		updateUrl( $(this).text(), $(this).text().capitalize() );					
    	}
    });
    */
    

	/******************************
	
		Webfont Loader
	
	******************************/
	WebFontConfig = {
	custom: { families: ['Arvo', 'serif'],
	urls: [ 'http://lassekorsgaard.com/css/scroll.css' ] }
	};
	 
	(function() {
	var wf = document.createElement('script');
	wf.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
	wf.type = 'text/javascript';
	wf.async = 'true';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(wf, s);
	})();






})(this.jQuery);


// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};



// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);



