////////// Initialize the current page //////
var currentPage = '';
var currentMainSection = '';
var scrollLeftAmount = '';

////////// Is the shadowbox open ? /////////
var isShadowboxOpen = false;

$(function() {

	
	///////////////// Override an href's clicked on event /////////////////////
	$('a.hijackLink').livequery('click', function(event) {
		if(flashDoneTransitionReadyForNext) {
		
			saveScrollLeftFromVisuals();
			
			///////// Send x location of polaroids to flash, so they can cover them /////////////////
			if(currentPage == "agency-employment") {
				//alert(numberOfPolaroidsOnScreen);
				sendFlashPolaroidLocation($('#polaroidLiteCarouselContainer').offset().left+8,numberOfPolaroidsOnScreen);
			}
			
			//////// The content is not ready to load yet, until flash gives the ok //////
			loadNewContent = false;
			
			//////// Close shadowbox if open //////////
			if(isShadowboxOpen) {Shadowbox.close(); isShadowboxOpen = false;}
			
			/////// Close the polaroid if necessary //////
			closePolaroidModal();
			
			var pathToContent = this.href;
			pathToContent = formatFullUrl(pathToContent);
			
			////////////////////// Path Formatting /////////////////////////////////////////////
			var pathArray = pathToContent.split("/"); //An array of the path seperated by the backslash
			var targetPage = pathArray.pop(); //The page with the extension
			targetPage = targetPage.slice(0,targetPage.lastIndexOf(".")); //Take of the extension
			//////////////////////////////////////////////////////////////////////////////////////
			
			if(targetPage != currentPage) {checkIfTransitionCanStart(currentPage,targetPage,pathToContent,1);} //This will verify that flash is loaded and we can indeed call for the transition
			//////////////////////////////////////////////////////////////////////////////////
		}
		
		this.blur(); 
		return false; //Stop the link from working
	});
	
	//This function will verify that flash has loaded, if not it will wait 500 milliseconds and try again. After 4 unsuccessfull loads (2sec), it will move on
	function checkIfTransitionCanStart(currentPage,targetPage,pathToContent,counter) {
		if(!flashDoneInitialLoad || counter > 2) {
			loadNewContent = true;				//Tell the script its OK to load content, this usually comes from flash
			SWFAddress.setValue(pathToContent); //Call the SWFAddress to do the heavy lifting. 
		} else if(flashDoneInitialLoad) {
			loadFlash(currentPage,targetPage);	//Start teh flash transition
			SWFAddress.setValue(pathToContent); //Call the SWFAddress to do the heavy lifting. 
		} else {
			return setTimeout(function() {checkIfTransitionCanStart(currentPage,targetPage,pathToContent,++counter);},500);
		}
	}
	
	//This function will verify that flash has loaded, if not it will wait 500 milliseconds and try again. After 4 unsuccessfull loads (2sec), it will move on
	function checkIfMovieCanStart(currentPage,targetPage,counter) {
		if(!flashDoneInitialLoad || counter > 2) {
			alert('Please upgrade your flash player to view these portfolios!');
			return false;
		} else if(flashDoneInitialLoad) {
			loadFlash(currentPage,targetPage);	//Start teh flash transition
		} else {
			return setTimeout(function() {checkIfMovieCanStart(currentPage,targetPage,++counter);},500);
		}
	}
	
	////////// On click of the return to news article in the colleague modal, we close the modal here, and open the news bucket in openClose.js and the aritlce in news-individual.js
	/*$('#returnToArticle').livequery('click',function() {
		//////// Close shadowbox if open //////////
		if(isShadowboxOpen) {; isShadowboxOpen = false;}
		
		return false;
	});*/
	
	$('.socialMediaLink, .noFlashTransitionLink').livequery('click',function(event) {
		
		saveScrollLeftFromVisuals();
		
		//////// Close shadowbox if open //////////
		if(isShadowboxOpen) {Shadowbox.close(); isShadowboxOpen = false;}
		
		/////// Hide any current content ///////////
		if(currentMainSection == "agency-interactive") {$('#content').html(''); }
		
		loadNewContent = true;
		SWFAddress.setValue(formatFullUrl(this.href));
		return false;
	});
	
	/////// Save the scroll left in case the user comes back to the page
	function saveScrollLeftFromVisuals() {
		if(currentPage == "visuals") {scrollLeftAmount = $('.listcontainer').scrollLeft();}
	}
	
	$('.newsLink').livequery('click',function(event) {
		loadNewContent = false;
		neverLoadContent = true;
		
		//////// Since this is a news story, load the article in via an ajax request here /////
		$('#content').load('news/news-article.php',"sid="+Math.random()+"&id="+$(this).attr('rel'));
		
		SWFAddress.setValue(formatFullUrl(this.href));
		
		return false;
	});
	
	$('.storyLink').livequery('click',function(event) {
		loadNewContent = false;
		neverLoadContent = true;
		
		var pathToStory = formatFullUrl(this.href);
		
		//////// Since this is a news story, load the article in via an ajax request here /////
		$('#client-info').load(pathToStory,"sid="+Math.random()+"&newStory=1");
		
		///////// Get the current Page //////////
		var pathArray = pathToStory.split("/"); //An array of the path seperated by the backslash
		currentPage = pathArray.pop(); //The page with the extension
		currentPage = currentPage.slice(0,currentPage.lastIndexOf("."));
		
		////////////// Update the background image ////////////////
		$('#client-info').removeClass().addClass(currentPage+'-story-bg');
		
		SWFAddress.setValue(pathToStory);
		
		return false;
	});
	
	
	$('.viewPortfolioStory').livequery('click',function(event) {
		//////// Close shadowbox if open //////////
		if(isShadowboxOpen) {Shadowbox.close(); isShadowboxOpen = false;}
		
		////////////////////// Path Formatting /////////////////////////////////////////////
		targetPage = "view-portfolio";
		checkIfMovieCanStart(currentPage,targetPage,1); //This will verify that flash is loaded and we can indeed call for the transition
		//////////////////////////////////////////////////////////////////////////////////
		
		this.blur(); 
		return false; //Stop the link from working
	});
	
	
	
	/////////////// Polaroid's image click ///////////////////////////
	$('#polaroids a').livequery('click', function(event) {
		var pathToContent = this.href;
		var pathArray = pathToContent.split("/"); //An array of the path seperated by the backslash
		openPolaroidModal(pathArray.pop(),event.pageX);
		return false;
	});
	
	///////////////// Privacy Policy and Sitemap and colleague /////////////////////
	$('a.callOpenModal').livequery('click', function(event) {
		var modalPage = ""
		var newsID = "";
		if($(this).attr('rel') == "privacy") {modalPage = "privacy-policy/privacy-policy.php";}
		if($(this).attr('rel') == "sitemap") {modalPage = "sitemap.php";}
		if($(this).attr('rel') == "colleague") {modalPage = "include/ajaxFiles/colleague/your-info.php";}
		
		//When opening this modal, we also close both news buckets. The article is closed in the news-individual.js and the group of 3 is closed in openClose.js
		if($(this).attr('rel') == "news") {
			modalPage = "include/ajaxFiles/colleague/your-info.php";
			newsID = $(this).attr('id');
		} 
		
		///////////////// Load the data and open modal ///////////
		$.get(modalPage+'?sid='+Math.random()+"&currentPage="+currentPage+"&newsID="+newsID,function(data){
			
			////// Open the modal //////
			Shadowbox.open({
				content:    data,
				player:     "html",
				height:     350,
				width:      800
			});
			
			isShadowboxOpen = true;
		});
		
		return false;
	});
	
});

