// Cybermuse - VERONESE History Slider script
// Developer: Dan Copeland - BV02, Ottawa ON

$(document).ready(function(){
	var previousSection = 0;
	var targetSection;  // an id name
	var targetId = 0;  // an id number (sliced from id name)
	var scrollToX;  // pixel value to scroll horizontally to
	var scrollToY;  // pixel value to scroll vertically to
	var pageName;
	var hashValue;



	/*var commissionDiv = document.clientHeight;
	var commissionHeight = commissionDiv.clientHeight;*/
	var commissionHeight = $('.commissionPage').height() + 20;
	var landingPageHeight = $('.historyLandingPage').height() + 20;
	var fragmentationHeight = $('.fragmentationPage').height() + 20;
	var restorationHeight = $('.restorationPage').height() + 20;
	var reconstructionHeight = $('.reconstructionPage').height() + 20;
	//alert('height: ' + commissionHeight);

	var slidePanels = function(selected){
		$('ul.historyNav li#history-' + selected + ' a').addClass('current');
		if(selected != previousSection){
			if(selected == 1){
				scrollToX = -719;
				$('#historyWrapper').animate(
					{ height: commissionHeight },
					{
						easing: "easeInOutSine", 
						duration: 600, 
						complete: function(){}
					}
				);
				pageName = 'commission';
			}
			if(selected == 2){
				scrollToX = -1438;
				$('#historyWrapper').animate(
					{ height: fragmentationHeight },
					{
						easing: "easeInOutSine", 
						duration: 600, 
						complete: function(){}
					}
				);
				pageName = 'fragmentation';
			}
			if(selected == 3){
				scrollToX = -2157;
				$('#historyWrapper').animate(
					{ height: restorationHeight },
					{
						easing: "easeInOutSine", 
						duration: 600, 
						complete: function(){}
					}
				);
				pageName = 'restoration';
			}
			if(selected == 4){
				scrollToX = -2876;
				$('#historyWrapper').animate(
					{ height: 900 },
					{
						easing: "easeInOutSine", 
						duration: 600, 
						complete: function(){/*resetReconstruction();*/}
					}
				);
				pageName = 'reconstruction';
			}
			$('#historyContainer').animate(
				{ left: scrollToX },
				{
					easing: "easeInOutSine", 
					duration: 600, 
					complete: function(){}
				}
			);
			previousSection = selected;
			previousSubSection = 1;
			if(previousSection == 4){
				$('div#arrowTabs a.previous').css('display','block');
				$('div#arrowTabs a.next').css('display','none');
			} else {
				$('div#arrowTabs a.previous').css('display','block');
				$('div#arrowTabs a.next').css('display','block');
			}
			hashValue = '#' + pageName;
			if(pageName != 'undefined'){
				window.location.hash = hashValue;
			}
			if($('body').hasClass('fr')){
				toggleUrl = 'index_e.jsp#' + pageName;
			} else {
				toggleUrl = 'index_f.jsp#' + pageName;
			}
			$('#textLangNav a').attr('href',toggleUrl);
		}
	};

	var slideToHome = function(){
		$('ul.historyNav li a').removeClass('current');
		scrollToX = 0;
		$('#historyWrapper').animate(
			{ height: landingPageHeight },
			{
				easing: "easeInOutSine", 
				duration: 600, 
				complete: function(){}
			}
		);
		if(previousSection == 1){
			slideDuration = 600;
		}
		if(previousSection == 2){
			slideDuration = 800;
		}
		if(previousSection == 3){
			slideDuration = 1000;
		}
		if(previousSection == 4){
			slideDuration = 1200;
		}
		$('#historyContainer').animate(
			{ left: scrollToX },
			{
				easing: "easeInOutSine", 
				duration: slideDuration, 
				complete: function(){}
			}
		);
		$('div#arrowTabs a.previous').css('display','none');
		$('div#arrowTabs a.next').css('display','block');
		previousSection = 0;

		//hashValue = '';
		window.location.hash = '#';

		if($('body').hasClass('fr')){
			toggleUrl = 'index_e.jsp';
		} else {
			toggleUrl = 'index_f.jsp';
		}
		$('#textLangNav a').attr('href',toggleUrl);
		return false;
	};

	var showPage = function(pageName){
		if(pageName == 'commission'){
			targetId = 1;
		} else if(pageName == 'fragmentation'){
			targetId = 2;
		} else if(pageName == 'restoration'){
			targetId = 3;
		} else if(pageName == 'reconstruction'){
			targetId = 4;
		}
		slidePanels(targetId);
	};
	$('ul.historyLandingPageNav li').hoverIntent(
		function(){
			$(this).children('div').slideToggle('fast');
		},
		function(){
			$(this).children('div').slideToggle('fast');
		}
	);
	$('ul.historyLandingPageNav li#landing-2, ul.historyLandingPageNav li#landing-3, ul.historyLandingPageNav li#landing-4, ul.historyLandingPageNav li#landing-5, ul.historyLandingPageNav li#landing-6').bind('click',function(){
		targetSection = $(this).attr('id');
		targetId = targetSection.replace('landing-','');
		if(targetId == 5){
			targetId = 3;
		} else if(targetId == 4) {
			targetId = 4;
		} else {
			targetId--;
		}
		slidePanels(targetId);
	});

	/*$('ul.historyNav li ul a').bind('click',function(){
		//$('a#history-nav-1,a#history-nav-2,a#history-nav-3,a#history-nav-4').removeClass('current');
		$('ul.historyNav li ul a').removeClass('current');
		$(this).parent().parent().children('a').addClass('current');
		return false;
	});*/
	$('ul.historyNav li#history-1 ul a').bind('click',function(){
		targetSubSection = $(this).parent().attr('id');
		$(this).toggleClass('current');
		subSectionId = targetSubSection.replace('commission-','');
		showMoreCommission(subSectionId);
	});
	$('ul.historyNav li#history-2 ul a').bind('click',function(){
		targetSubSection = $(this).parent().attr('id');
		$(this).toggleClass('current');
		subSectionId = targetSubSection.replace('fragmentation-','');
		showMoreFragmentation(subSectionId);
	});
	$('ul.historyNav li#history-3 ul a').bind('click',function(){
		targetSubSection = $(this).parent().attr('id');
		$(this).toggleClass('current');
		subSectionId = targetSubSection.replace('restoration-','');
		showMoreRestoration(subSectionId);
	});
	$('a#history-nav-1,a#history-nav-2,a#history-nav-3,a#history-nav-4').bind('click',function(){
		targetSection = $(this).parent().attr('id');
		//$('ul.historyNav ul').hide();
		$('a#history-nav-1,a#history-nav-2,a#history-nav-3,a#history-nav-4').removeClass('current');
		$(this).toggleClass('current');
		targetId = targetSection.replace('history-','');
		slidePanels(targetId);
		return false;
	});
	$('div#arrowTabs a.next').bind('click',function(){
		$('ul.historyNav li a').removeClass('current');
		targetId++;
		slidePanels(targetId);
		return false;
	});
	$('div#arrowTabs a.previous').bind('click',function(){
		$('ul.historyNav li a').removeClass('current');
		if(targetId == 1){
			slideToHome(targetId);
		} else{
			targetId--;
			slidePanels(targetId);
		}
		return false;
	});

	$('ul#mainNav li#history a').bind('click',slideToHome);

	if(window.location.hash != 'undefined'){
		var	requestHash = window.location.hash;
		var	requestId = requestHash.replace('#','');
		showPage(requestId);
	}
	//$('#historyWrapper').css('height',landingPageHeight);
	//$('#commissionPage').css('height',landingPageHeight);
});