///////////////////////
// PPS EFFECTS.JS - REQUIRES JQUERY
// Please do not steal.
// Every time you steal, An angry Scotsman punches a puppy in the face! A really cute puppy!

// WAIT FOR THE PAGE TO BE READY
$(document).ready(function(){

// Easing equation for sliding stuff
	// This one for the navigation
	jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
	
	// And this one for the sliding
	jQuery.easing.easeInOutExpo = function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	};


///////////////////////
// SPLASH PAGE STUFF
/*
	speed = 3000;
	imgNum = 2;
	hashPos = 0 + document.location.href.indexOf("#");
	randomSplash = Math.floor(Math.random()*5) + 1;
	newSplash = 'undefined';
	
	//Check if there is a hash in the URL, if there is we don't need the splash.
	if(hashPos < 4){
		$('#splash_page').show();
		$('#splash_title').show();
		
		$('div#splash_page').append("<img src='/images/splash/"+randomSplash+"/splash_3.jpg' alt='splash image' />");
	
		$('#splash_page').everyTime(4000,function(){
			if(imgNum >= 1){
				swapSplash();
				imgNum--;
			}else{
				$('#background').show();
				$('#splash_page').animate({opacity:0},500, function(){$(this).hide()});
				$('#splash_title').animate({opacity:0},500, function(){$(this).hide()});
				$('#splash_page').stopTime();
				nextPage = '#home';
				backgroundImage = '/images/backgrounds/homepage.jpg';
				$('div#home').animate({opacity: 'show'}, 500, swapBackground);
				firstLoad = false;
			}
		});
	}else{
		$('#splash_page').hide();
		$('#splash_title').hide();
		
	}
	
	function swapSplash(){
		newSplash = '/images/splash/'+randomSplash+'/splash_'+imgNum+'.jpg';
		
		var tempSplash = new Image();
		$(tempSplash).load(function () {
			$(this).css('display', 'none');
			$('div#splash_page').css({backgroundImage: 'url('+newSplash+')'});
			$('div#splash_page img').animate({opacity:0}, 300, tidySplash);
		}).error(function () {
			// An error has occured, but we wont tell anyone. Shush...
		}).attr('src', newSplash);
	}
	
	function tidySplash(){
		$('div#splash_page').append("<img src='"+newSplash+"' alt='splash image' />");
	}*/

///////////////////////
// NAVIGATION

// ON LOAD
	// Add the plus/minus icon to the expandable navs
	$('#navigation a.top').prepend('<span></span>');

// EXPANDING AND COLLAPSING
	// Click event
	$('#navigation a.top').click(function(){
		$(this).addClass('on').next('ul').slideDown('slow', 'easeOutQuart').parent('li').siblings('li').children('ul').slideUp('slow', 'easeOutQuart').prev('a').removeClass('on');
		$(this).next('ul').children('li').removeClass('active');
		$('a.home').removeClass('on');
		return false;
	});

///////////////////////
// CONTENT CHANGINGS

	// LOGO CLICKING
	$('h1 a').click(function(){
		document.location.href='#home';
		$('#navigation a.home').parent('li').siblings('li').children('a').removeClass('on').next('ul[class != sector_nav]').slideUp();
		$('ul.sector_nav').slideDown();
		$('ul.sector_nav').siblings('a').addClass('on');
		if(firstLoad == true){
			nextPage = '#home';
			backgroundImage = '/images/backgrounds/homepage.jpg';
			slideContent();
		}
	});
	$('#navigation a.home').click(function(){
		document.location.href='#home';
		$(this).parent('li').siblings('li').children('a').removeClass('on').next('ul[class != sector_nav]').slideUp();
		$('ul.sector_nav').slideDown();
		$('ul.sector_nav').siblings('a').addClass('on');
		if(firstLoad == true){
			nextPage = '#home';
			backgroundImage = '/images/backgrounds/homepage.jpg';
			slideContent();
		}
	});

	$(window).resize(function(){
		$('.sliding .slidenav li.active a').click();
	});

	// INTERNAL SCROLLING
	$.localScroll.defaults.axis = 'xy';
	
	$('.sliding .slidenav').localScroll({
		target: '.sliding .big', // could be a selector or a jQuery object too.
		queue:false,
		duration:800,
		easing: 'easeInOutExpo',
		hash:false
	});
	
	$('.sliding .slidenav li a').click(function(){
		thePerson = $(this).attr('href');
		$(this).parent('li').addClass('active');
		$(this).parent('li').siblings('li').removeClass('active');
	});

	// MAIN SCROLLING
	// IF they get rid of the 'orrible splash page, un comment these.
	//$('div#content_slider div:first').show();
	//$('div#background').css({backgroundImage: 'url(/images/backgrounds/'+backgroundImage+'.jpg)'});
	
	clickEnabled = true;
	currentPage = '#' + $('div#content_slider div:first').attr('id');
	nextPage = 'undefined';
	thePerson = 'undefined';
	backgroundImage = 'default';
	firstLoad = true;
	
	$('#navigation ul li a.people').click(function(){
		nextPage = $(this).attr('href');
		if(currentPage != nextPage){
			// BACKGROUND IMAGE STUFF
			backgroundImage = $(nextPage).children('span.background_link').text();
			
			slideContent();
			clickEnabled = false;
		}
	});
	
	$('#navigation ul li a.careers').click(function(){
		nextPage = $(this).attr('href');
		if(currentPage != nextPage){
			// BACKGROUND IMAGE STUFF
			backgroundImage = $(nextPage).children('span.background_link').text();
			
			slideContent();
			clickEnabled = false;
		}
	});
	
	$('#navigation ul li a.latest_news').click(function(){
		nextPage = $(this).attr('href');
		if(currentPage != nextPage){
			// BACKGROUND IMAGE STUFF
			backgroundImage = $(nextPage).children('span.background_link').text();
			
			slideContent();
			clickEnabled = false;
		}
	});
	
	$('#navigation ul a:not(.top)').click(function(){
		if(clickEnabled == true){
			if($(this).attr('class') != 'external'){
				nextPage = $(this).attr('href');
				if(currentPage != nextPage){
					updateLocation();
					// BACKGROUND IMAGE STUFF
					backgroundImage = $(nextPage).children('span.background_link').text();
					
					slideContent();
					clickEnabled = false;
					
					updateNavigations(nextPage);
				}
				$(this).parent('li').addClass('active').siblings('li').removeClass('active');
			}else{
				return true;
			}
		}
		return false;
	});
	$('a.deep').click(function(){
		if(clickEnabled == true){
			if($(this).parent('li').parent('ul').attr('class') == 'people_list'){
				thePerson = $(this).attr('href');
				nextPage = '#' + $(thePerson).parent('.slider').parent('.big').parent('.content').attr('id');
				openPeopleNav(); // Opens the people nav and main section if needed.
				updatePeopleNav(thePerson);
			}else{
				nextPage = $(this).attr('href');
				updateNavigations(nextPage);
			}
			if(currentPage != nextPage){
				// BACKGROUND IMAGE STUFF
				backgroundImage = $(nextPage).children('span.background_link').text();

				slideContent();
				clickEnabled = false;
			}
		}
		return false;
	});
	
	function updateLocation(){
		document.location.href=nextPage;
	}
	
	onLoadLocation();
	
	function onLoadLocation(){
		hashPos = document.location.href.indexOf("#");
		nextPage = document.location.href.substr(hashPos);
		if(hashPos > 5){
			theBackground = $(nextPage).children('span.background_link').text();
			slideContent();
			$('#navigation a[href = '+nextPage+']').parent('li').addClass('active').parent('ul').show().siblings('a').addClass('on').parent('li').siblings('li').children('ul').hide().siblings('a').removeClass('on');
			//$('#navigation a[href = '+nextPage+']').click();
			$('#background').show().css({backgroundImage: 'url('+theBackground+')'});
		}else{
			$('#navigation a.default').addClass('on').next('ul').show();
			nextPage = '#home';
			backgroundImage = '/images/backgrounds/homepage.jpg';
			$('div#home').animate({opacity: 'show'}, 500, swapBackground);
			firstLoad = false;
		}
	}
	
	function updatePeopleNav(theClicked){
		theSection = $(theClicked).parent('div').parent('div').parent('div').attr('id');
		$('a[href = '+theClicked+']').parent('li').addClass('active').siblings('li').removeClass('active');
		$('a[href = #'+theSection+']').parent('li').addClass('active').siblings('li').removeClass('active');
	}
	
	function updateNavigations(theClicked){
		$('a.deep[href = '+theClicked+']').parent('li').addClass('active');
		$('a.deep[href != '+theClicked+']').parent('li').removeClass('active');
	}
	
	function openPeopleNav(){
		$('#navigation').oneTime(1000,function(){
			if($('#navigation a.main').attr('class') != 'top main on'){
				$('#navigation a.main').click();
			}
		});
		$('#navigation').oneTime(1600,function(){
			if($('#navigation a.people').attr('class') != 'top people on'){
				$('#navigation a.people').addClass('on').next('ul').slideDown().parent('li').siblings('li').children('ul').slideUp().prev('a').removeClass('on');
			}
		});
	}
	
	function slideContent(){
		if(firstLoad != true){
			$(currentPage).show().css({left:0});
		}else{
			$('#splash_page').animate({opacity:0},500, function(){$(this).hide()});
			$('#splash_title').animate({opacity:0},500, function(){$(this).hide()});
			$('#splash_page').stopTime();
		}
		$(nextPage).show().css({left:670});
		$('div' + nextPage + ' div.big').scrollTo(thePerson);
		$('div#content_slider').animate({left: -670}, 1005, 'easeInOutExpo', tidyUp);
	}
	
	// LOAD BACKGROUND

	function swapBackground(){
		$('#background').show();
		currentBackground = $('div#background').css('backgroundImage');
		newBackground = 'url('+backgroundImage+')';
		if(currentBackground != newBackground){
			var tempImage = new Image();
			$(tempImage).load(function () {
				$(this).css('display', 'none');
				$('div#background').animate({opacity:0}, 300, swapImage).animate({opacity:1}, 300);
				function swapImage(){
					theBgHeight = $('body').height();
					$('div#background').css({backgroundImage: 'url('+backgroundImage+')', height:theBgHeight});
				}
			}).error(function () {
				// An error has occured, but we wont tell anyone. Shush...
			}).attr('src', backgroundImage);
		}
	}
	
	function tidyUp(){
		
		swapBackground();
		
		if(firstLoad == true){
			firstLoad = false;
		}else{
			$(currentPage).hide().siblings('div:not(#'+nextPage+')').hide();
		}
		$(nextPage).css({left:0});
		
		$('div#content_slider').css({left:0});
		
		clickEnabled = true;
		
		currentPage = '#' + $(nextPage).attr('id');
		nextPage = 'undefined';
	}

	//SECTOR LIST INTERNAL STUFF

	currentDetails = 'none';
	
	$('ul.sector_list li a').click(function(){
		newDetails = $(this).attr('href');
		//alert(newDetails);
		newContent = $('div'+newDetails).html();
		if(currentDetails != newDetails){
			if(currentDetails == 'none'){
				//just show the new uns
				$('div'+currentPage+' div.sector_contact').slideDown().children('div.slider').html(newContent);
				currentDetails = newDetails;
			}else{
				$('div'+currentPage+' div.sector_contact').fadeOut('fast', function(){
					$(this).children('div.slider').html(newContent);
					$(this).fadeIn();
				});
				currentDetails = newDetails;
			}
			$(this).parent().addClass('active').siblings('li').removeClass('active');
			$(this).parent('li').parent('ul').siblings('ul.sector_list').children('li').removeClass('active');
		}
		return false;
		//update the navigation
	});
	
	$('a.close_details').click(function(){
		$('.sector_contact').slideUp();
		currentDetails = 'none';
		$('ul.sector_list li').removeClass('active');
		return false;
	});
	
	//SECTOR LIST INTERNAL STUFF

	currentDetails = 'none';
	
	$('ul.people_list li a').click(function(){
		newDetails = $(this).attr('href');
		//alert(newDetails);
		newContent = $('div'+newDetails).html();
		if(currentDetails != newDetails){
			if(currentDetails == 'none'){
				//just show the new uns
				$('div'+currentPage+' div.sector_contact').slideDown().children('div.slider').html(newContent);
				currentDetails = newDetails;
			}else{
				$('div'+currentPage+' div.sector_contact').fadeOut('fast', function(){
					$(this).children('div.slider').html(newContent);
					$(this).fadeIn();
				});
				currentDetails = newDetails;
			}
			$(this).parent().addClass('active').siblings('li').removeClass('active');
			$(this).parent('li').parent('ul').siblings('ul.sector_list').children('li').removeClass('active');
		}
		return false;
		//update the navigation
	});
	
	$('a.close_details').click(function(){
		$('.sector_contact').slideUp();
		currentDetails = 'none';
		$('ul.sector_list li').removeClass('active');
		return false;
	});

	///////////////////////
	// HOMEPAGE LOGOS
	
	tLogo = $('#home_logos').children('.set').size();
	cLogo = 1;
	
	$('#home_logos').children('.set:not(:first)').hide();
	
	$('#home_logos').everyTime(4000,function(){
		if(cLogo == tLogo){
			$(this).children('.set:first').fadeIn().siblings().fadeOut();
			cLogo = 1;
		}else{
			$(this).children('.set:eq('+cLogo+')').fadeIn().siblings().fadeOut();
			cLogo++;
		}
	});


// End jQuery goodness
});
