/*===================================	
	common.js
	www.strangerstudio.co.uk    
    by Julien Decaudin http://www.juliendecaudin.com
    11/10/2010
===================================*/

$('html').addClass('js');

/************* GLOBAL VARIABLES *************/
var debug = false;
var projectRolloverSpeed = 200;
var projectRolloutSpeed = 300;
var filterFadeoutSpeed = 300;
var filterFadeinSpeed = 500;

$(document).ready(function() {			
	/*////////////////////////////////////// INIT */ 
	$('#content, #content_footer_list').localScroll();	
	initTooltip();	
    initProjectRollover();    
    
    //Update href of ajax target links if required    
    if($('body#project_list').length == 0){
    	$('a.ajax_link').each(function(){
    		currentHref = $(this).attr('href');
    		if(currentHref != '/'){
    			$(this).attr('href', '/#' + currentHref);		
    		}    		
    	});    	
	}			
    
    /*////////////////////////////////////// INTERACTIONS */     
    /* PROJECT FILTERING */    
    if($('body#project_list').length > 0){	    
    	$('#secondary_nav li a').click(function(){
	    	$.address.value($(this).attr('href'));    	
	    	filterProjects($(this).attr('href'));
	    	return false;
		});
	}else if($('body').hasClass('ajax')){		
		$('#secondary_nav li a').click(function(){
	    	$.address.value($(this).attr('href'));    	
	    	updatePageContent($(this).attr('href'));
	    	return false;
		});
	}
		
	/*////////////////////////////////////// DEEP LINKING */ 
	if($('body#project_list').length > 0){
		$.address.init(function(event) {	
		}).change(function(event) {   
			 
	    }).externalChange(function(event) {	    	    	
	    	eventValueFixed = event.value;	    	
	    	if(event.value.lastIndexOf('/') != event.value.length - 1){
	    		eventValueFixed = event.value + '/';
	    	}	    	
	    	if(eventValueFixed.indexOf('/projects/') > -1 || eventValueFixed == "/"){    			    	
		    	filterProjects(eventValueFixed);	    		    	
	    	}
	    });         
	}else if($('body').hasClass('ajax')){
		$.address.init(function(event) {	
		}).change(function(event) {   
			 
	    }).externalChange(function(event) {	    	    	
	    	eventValueFixed = event.value;	    	
	    	if(event.value.lastIndexOf('/') != event.value.length - 1){
	    		eventValueFixed = event.value + '/';
	    	}	 	    	
	    	if(eventValueFixed != "/"){  		    	
		    	updatePageContent(eventValueFixed);
	    	}else{
	    		
	    	}
	    }); 
	}
});

//Initialise the tooltips on banner images
function initTooltip(){
	$('.banner a, .project_image a, .copy_image a').tipTip({
		defaultPosition: 'top',
		delay: 0
	});	
}

//Initialise the rollover effect on project thumbnails
function initProjectRollover(){
	var fullOpacity = 1;
	
	if($.browser.msie){
		//set opacity to 0
		$('.project_thumb a span.panel').css('opacity', 0);
		fullOpacity = 0.9;
	}
	
	//event handler
	$('.project_thumb a').live('mouseover', function(){
		$(this).find('span.panel').stop().animate({
			opacity: fullOpacity
		}, projectRolloverSpeed);		
	}).live('mouseout', function(){
		$(this).find('span.panel').stop().animate({
			opacity: 0
		}, projectRolloutSpeed);
	});
}

//Filter project thumbnails using an AJAX request
function filterProjects(currentLinkUrl){	
	var updatedProjectList;
	var currentLink = $('#secondary_nav li a[href=' + currentLinkUrl + ']');	   
	
	//display loading message
	$('#loading').show();
	
	//Hide back to top link
	$('#content_footer_list').css('visibility', 'hidden');
	
	//Flag the existing content holder and deactivate it visually
	$('#content_fluid .content_holder').attr('id', 'current');	
	$('div.content_holder#current', '#content_fluid').css('opacity', '0.5');	
	
	//Update the navigation
	updateNavigation(currentLink);		   
	
	$.ajax({
	  url: currentLinkUrl,
	  type: "GET",
	  dataType: "html",		  			  
	
	  success: function(html) {
	  	//Grab the HTML of the filtered projects
		updatedProjectList = $(html).find('div.project_thumb');	 
	 },
	 
	 complete: function() {
	 	
	 	//Add the new list content_holder
	 	$('#content_fluid').append('<div id="new" class="content_holder" style="display:none"></div>');
	 	
	 	//Hide current list
	 	$('div.content_holder#current', '#content_fluid').fadeOut(filterFadeoutSpeed, function(){
	 		//Remove current list
	 		$('div.content_holder#current').remove();
	 		
	 		//Append new list
	 		$('#content_fluid .content_holder#new').append(updatedProjectList);		 
	 		$('#content_fluid .content_holder#new').append("<div class='clearer'></div>");		 
	 		initProjectRollover();		
	 		
	 		//Preload new list images
	 		var arrayImages = new Array();
	 		$('#content_fluid .content_holder#new .project_thumb img').each(function(n){
	 			arrayImages[n] = $(this).attr('src');
	 		});	 		
	 		
	 		$.preLoadImages(arrayImages, function(){
	 			//hide loading message
				$('#loading').hide();
	 			
				//Display back to top link
		 		$('#content_footer_list').css('visibility', 'visible');
				
		 		//Display new list
		 		$('#content_fluid .content_holder#new').fadeIn(filterFadeinSpeed, function(){
		 			//Refresh Cufon replacement		 			
		 			Cufon.refresh('.panel .title, .panel .category');		 					 			
		 		});	
	 		}); 			 		
	 	});	
	  },
	
	  error: function() {
	    //called when there is an error
	  }
	});					
}

//Update the page content using an AJAX request
function updatePageContent(currentLinkUrl){
	var updatedContent;
	var currentLink = $('#secondary_nav li a[href=' + currentLinkUrl + ']');	   
	
	//display loading message
	$('#loading').show();
	
	//Hide back to top link
	$('#next').css('visibility', 'hidden');
	
	//Flag the existing content holder and deactivate it visually
	$('#content .content_holder').attr('id', 'current');	
	$('div.content_holder#current', '#content').css('opacity', '0.5');	
	
	//Update the navigation
	updateNavigation(currentLink);		   
	
	$.ajax({
	  url: currentLinkUrl,
	  type: "GET",
	  dataType: "html",		  			  
	
	  success: function(html) {
	  	//Grab the HTML of the loaded content
		updatedContent = $(html).find('#content div.content_holder');	 
		updatedContent.attr('class', '');
	 },
	 
	 complete: function() {
	 	
	 	//Add the new content_holder
	 	$('#content').append('<div id="new" class="content_holder" style="display:none"></div>');
	 	
	 	//Hide current content
	 	$('div.content_holder#current', '#content').fadeOut(filterFadeoutSpeed, function(){
	 		//Remove current list
	 		$('div.content_holder#current').remove();
	 		
	 		//Append new list
	 		$('#content .content_holder#new').append(updatedContent);		 	 				
	 		
	 		//Preload new list images	 		
	 		var arrayImages = new Array();
	 		$('#content .content_holder#new img').each(function(n){
	 			arrayImages[n] = $(this).attr('src');
	 		});	 		
	 		
	 		if(arrayImages.length){
	 			$.preLoadImages(arrayImages, function(){
		 			//hide loading message
					$('#loading').hide();
		 			
					//Display back to top link
		 			$('#next').css('visibility', 'visible');
					
			 		//Display new list
			 		//Refresh Cufon replacement		 			
			 		Cufon.refresh();		
			 		$('#content .content_holder#new').fadeIn(filterFadeinSpeed);			 		
		 		}); 		 			 			
	 		}else{
	 			//hide loading message
				$('#loading').hide();
	 			
				//Display back to top link
		 		$('#next').css('visibility', 'visible');
				
		 		//Display new list
		 		//Refresh Cufon replacement		 			
		 		Cufon.refresh();		 	
		 		$('#content .content_holder#new').fadeIn(filterFadeinSpeed);			 			
	 		}	
	 		
	 		//Reinit JS features
	 		$('#content, #content_footer_list').localScroll(); 		
	 		initTooltip();
	 	});	
	  },
	
	  error: function() {
	    //called when there is an error
	  }
	});		
}

//Update navigation item dynamically
function updateNavigation (link) {
	link.parents('ul').find('li').removeClass('current');
	link.parent('li').addClass('current');
	//Refresh Cufon replacement		 			
	Cufon.refresh();
}

















