/**
 * @author Samuel Goodwin
 * v. 1
 */


(function($) {
    
    
    var viewing_area = 1150;
    var scroll_time = 2300; //default time
    var scroll_long = 4000; //when its a long scroll
    var scroll_medium = 3000; //when its a long scroll
    var scroll_short = 1000; //when its a long scroll
    var animate_header = false;
    
    $(document).ready(function() {
	
	
	bg_position = parseInt($( "#container" ).css('backgroundPosition'));
	
	/**************************************************************/
	//agency area
	$("#agency_section").click( function() {
	    //for the main nav
	    $(this).addClass("current");
	    start_transition("our_agency", 3990, "home/home-creative");
	    Cufon.replace(this,{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});

	});

	$("#difference_section").click( function() {
	    start_transition("our_agency", 3990, "our-agency/the-difference");    
	});
	
	$("#behindagency_section").click( function() {
	    //$(this).addClass("current");
	    start_transition("our_agency", 3990, "our-agency/behind-the-agency");
	});

/**************************************************************/

	$("#client_section").click( function() {
	    $(this).addClass("current");
	    start_transition("clients", 1685, "a-few-of-our-friends");
	    Cufon.replace(this,{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	});

/**************************************************************/

	$("#services_section").click( function() {
	    $(this).addClass("current");
	    start_transition("services", 2950, "what-were-good-at");
	    Cufon.replace(this,{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	});
	
	
/**************************************************************/
	$("#blog_section").click( function() {
	    $(this).addClass("current");
	    start_transition("blog", 415, "blog");
	    Cufon.replace(this,{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	});
	
/**************************************************************/
	//case studies area
	$("#casestudies_section").click( function() {
	    $(this).addClass("current");
	    start_transition("casestudies", 4980, "casestudies/article/2");
	    Cufon.replace(this,{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	});
	
	//case studies area
	$("#corporate_section").click( function() {
	    start_transition("casestudies", 4980, "casestudies/article/2");
	});
	
	//case studies area
	$("#pharmaceutical_section").click( function() {
	    start_transition("casestudies", 6565, "casestudies/article/3");
	});
	
	//case studies area
	$("#animal_section").click( function() {
	    start_transition("casestudies", 9985, "casestudies/article/4");
	});
	
	//case studies area
	$("#communication_section").click( function() {
	    start_transition("casestudies", 11435, "casestudies/article/1");
	});
	
	$("#sme_section").click( function() {
	    start_transition("casestudies", 13070, "casestudies/article/5");
	});
/**************************************************************/

	
	
	$("#contact_section").click( function() {
	    $(this).addClass("current");
	    //Cufon.replace('#nav a.first');
	    start_transition("contact", 16085, "give-us-a-bell");
	});


    });
   
    
    function fix_cufon_navigation(layer_id)
    {
	$('[name$="_section"]').removeClass('current');
	Cufon.replace('#nav a.first');
	//Cufon.replace('[name$="_section"]',{fontFamily:'ClarendonLTStd',hover:false, textShadow: '0'});
	
	if (layer_id == 'our_agency') {
	    $('#agency_section').addClass("current");
	    Cufon.replace('#agency_section',{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	}
	
	if (layer_id == 'clients') {
	    $('#client_section').addClass("current");
	    Cufon.replace('#client_section',{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	}
	
	if (layer_id == 'blog') {
	    $('#blog_section').addClass("current");
	    Cufon.replace('#blog_section',{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	}
	
	if (layer_id == 'casestudies') {
	    $('#casestudies_section').addClass("current");
	    Cufon.replace('#casestudies_section',{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	}
	
	if (layer_id == 'contact') {
	    $('#contact_section').addClass("current");
	    Cufon.replace('#contact_section',{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	}
	
	if (layer_id == 'services') {
	    $('#services_section').addClass("current");
	    Cufon.replace('#services_section',{fontFamily:'ClarendonLTStd',hover:false, textShadow: '#fff 2px 0px, #fff 2px 2px, #fff 0px -2px, #fff -2px -2px'});
	}
    }
    
    
    function start_transition(layer_id, new_pos, url)
    {
	
	fix_cufon_navigation(layer_id);
	
	$('[name$="_area"]').fadeOut('fast', function() { 
	    $(this).remove();
	    $('[name$="_modal"]').remove();
	    
	    //hide submenus
	    //if ($('#'+layer_id+'_submenu').length == 0)
	    //{
		$('[name$="_submenu"]').fadeOut('fast', function() { $(this).hide();});
	    //}
	    
	    //if heading not exist, and close rest
	    if ($('#'+layer_id+'_heading').length == 0)
	    {
		$('[name$="_heading"]').animate({
			'top' : -630,
			queue:false 
		    }, 
		    300,
		    'linear',
		    function() {
			$('#content_wrapper').append('<div id="'+layer_id+'_heading" name="'+layer_id+'_heading"></div>');
			$(this).remove();
			move_to_section(layer_id, new_pos, url);
		    } //end function
		);//end animate
	    } else {
		move_to_section(layer_id, new_pos, url);
	    } //end if
	}); //end fade out
    }
    
    
    
    function move_to_section(layer_id, new_pos, url)
    {
	start_scroll = (($(document).width() - viewing_area ) / 2) - new_pos;

	bg_position = bg_position - new_pos;
	
	//console.log('bg: ' + bg_position + 'start_scroll: ' + start_scroll);
	var pos = $('#container').css('background-position');
	if (pos == 'undefined' || pos == null) {
	    pos = $('#container').css("background-position-x"); //die in 
	} else {
	    pos = pos.split(' ');
	    pos = pos[0];
	}
	
	
	
	
	
	
	//console.log('bg pos: ' + $('#container').css('background-position-y'));
	//console.log('bg pos: ' + $('#container').css('backgroundPosition'));
	//console.log('bg pos: ' + pos[0]);
	
	
	var time_length = bg_position - parseFloat(pos);
	
	//console.log('final: ' + time_length);
	
	if (time_length > 0 )
	    {
		if  (bg_position > 1) { scroll_time = scroll_short; }
		if  (bg_position > 4000) { scroll_time = scroll_time; }
		if  (bg_position > 8000) { scroll_time = scroll_medium; }
		if  (bg_position > 13000) { scroll_time = scroll_long; }
	    }
	if (time_length < 0 )
	    {
		if  (bg_position < -1) { scroll_time = scroll_short; }
		if  (bg_position < -4000) { scroll_time = scroll_time; }
		if  (bg_position < -8000) { scroll_time = scroll_medium; }
		if  (bg_position < -13000) { scroll_time = scroll_long; }
	    }

		bg_position = 0;

	
	$( "#container" ).animate({
		backgroundPosition:"("+start_scroll+"px 0px)",
		queue: true
	    },
	    scroll_time, 
	    'easeOutCirc', 
	    function() {
		new_scroll_time = setup_area(url, layer_id, scroll_time);
		//return new_scroll_time;
	    }// end function
	); //end animate
    /**
	    //$('#container').click(function() {
	      $('#container').animate({
		backgroundPositionX: start_scroll + "px"
	      }, scroll_time, "easeOutCirc",function() {
		// Animation complete.
		new_scroll_time = setup_area(url, layer_id, scroll_time);
	      });
	    //});
	    **/
    }
    
    function setup_area(url, layer_id, scroll_time)
    {
	
	
	
	if (url == "home/home-creative")
	{
	    $('<div id="home_area" name="home_area" style="display: none"></div>').appendTo('#content_wrapper');
	} else if ( $('#'+layer_id+'_area').length == 0 ) {
	    $('<div id="'+layer_id+'_area" name="'+layer_id+'_area" style="display: none"></div>').appendTo('#content_wrapper');
	}
		
	$.ajax({
	   type: "POST",
	   url: url,
	   success: function(data){
	   
		//setup heading
		if (layer_id == 'our_agency') {
		    head_position = -250;
		} else { head_position = -330;}
				
		//home hack
		if (url == "home/home-creative") {  layer_id = "home"; }

				
		//add data
		$('#'+layer_id+"_area").html(data);	
		
		//cufon replace
		Cufon.replace('h1, h2, h3, ul.servicelist li, .pagination a',{fontFamily:'Georgia', hover:true} );
		
		$('#'+layer_id+'_area').fadeIn('slow', function() {
		    
		    //home hack
		    if (url == "home/home-creative") {  layer_id = "our_agency"; }
		    
		    $('#'+layer_id+'_heading').animate({'top' : head_position}, {queue:true, duration:700, easing: 'easeOutBounce'});
		    
		    //show sub menu if exists
		    if ( $("#"+layer_id+"_submenu").length != 0 ) {
			$('#'+layer_id+'_submenu').fadeIn('slow',function(){});
			
		    }
		    
		    //home hack
		    if (url == "home/home-creative") {  layer_id = "home"; }
		    
		    //remove display
		    $('#'+layer_id+"_area").remove('display');
		    
		    if (layer_id == 'clients') {
			$('#panel_clients').jScrollPane();
		    }else if (layer_id == 'casestudies') {
			$('#panel_case').jScrollPane();
		    }

		}); //end fade in
	   } //end success
	}); //end ajax
    } // end setup


    
})(jQuery);
