//Document Ready
//Primary Nav
$(function(){
		   
	var BannerQuantity = ($('#home #Data li').length) / 4;
				
	//Delete Initial Values
	if(BannerQuantity > 0){
		$('#home #background-rotator').html('<ul id="RotatorImageList"></ul>');
		$('#home h1').html('<ul id="RotatorHeadingList"></ul>');
		$('#home #summary').html('<ul id="RotatorSummaryList"></ul>');	
	}		
	
	//Put li types in to their corresponding ul's.
	$('#Data li.Image').appendTo('#RotatorImageList');
	$('#Data li.Heading').appendTo('#RotatorHeadingList');
	$('#Data li.Content').appendTo('#RotatorSummaryList');
	$('#Data li.ProjectLink').appendTo('#ViewProject');

	/* -------------------------------------- */
	
	var RunRotate = true;
	var SelectionQueue = false;

	/* Prevents the last image displaying on load. */
	$('#background-rotator ul li').each(function(Index){
		$(this).css('zIndex', '' + ((BannerQuantity - 1) - Index) + '');
	});
	
	//Setup banner navigation.
	$('#background-rotator ul li').each(function(Index){
		if(Index == 0){
			$('ul#project-selector').append('<li><a rel="' + (Index + 1) + '" class="current">&nbsp;</a></li>');
		}
		else{
			$('ul#project-selector').append('<li><a rel="' + (Index + 1) + '">&nbsp;</a></li>');
		}
	});
	
	$('#background-rotator ul li').css('display','block');
	$('h1 ul li:eq(0)').fadeIn(800);
	$('#summary ul li:eq(0)').fadeIn(800);
	$('#ViewProject li:eq(0)').fadeIn(800);
	setTimeout(function() {Rotate(0);}, 9000);
	
	//Handles the automatic rotation.
	function Rotate(Current){		
		if(RunRotate == true){
			
			$('h1 ul li').fadeOut(800);
			$('#summary ul li').fadeOut(800);
			$('#ViewProject li').fadeOut(500);
			
			var ModCurrent = Current % BannerQuantity;				
							
			//Image Rotate
			$('#background-rotator ul li').eq(ModCurrent).fadeOut(800, function(){
				//Reorder the z-index
				$('#background-rotator ul li').each(function(Index){
					//Calculate z-index
					var ZIndexLevel = ((BannerQuantity - Index) + ModCurrent) % BannerQuantity;
					$(this).css({
						'zIndex' : ZIndexLevel,
						'display' : 'block'
					});
				});	
				SelectProject(ModCurrent + 1);
			});
			
			//Heading Rotate
			if(Current == BannerQuantity -1){
				$('h1 ul li:eq(0)').fadeIn(800);
			}
			else if(Current == BannerQuantity){
				$('h1 ul li:eq(1)').fadeIn(800);
			}
			else{
				$('h1 ul li:eq(' + (Current + 1) + ')').fadeIn(800);
			}
			
			//Summary Rotate
			if(Current == BannerQuantity -1){
				$('#summary ul li:eq(0)').fadeIn(800);
			}
			else if(Current == BannerQuantity){
				$('#summary ul li:eq(1)').fadeIn(800);
			}
			else{
				$('#summary ul li:eq(' + (Current + 1) + ')').fadeIn(800);
			}
			
			//ProjectLink Rotate
			if(Current == BannerQuantity -1){
				$('#ViewProject li:eq(0)').fadeIn(500);
			}
			else if(Current == BannerQuantity){
				$('#ViewProject li:eq(1)').fadeIn(500);
			}
			else{
				$('#ViewProject li:eq(' + (Current + 1) + ')').fadeIn(500);
			}
			
			//Change the number to change the amount of time an image is fully visible (unit is milliseconds).
			setTimeout(function() {Rotate(++ModCurrent);}, 9000);
			
		}
		else{
			RestartRotate(RunRotate);
			return false; //Probably this one or the next one is not necessary.
		}
		return false; //Probably this one or the first one is not necessary.
	}
	
	function SelectProject(Current) {
		if (Current == BannerQuantity) {
			Current = 0;
		}
		
		$('ul#project-selector .current').removeClass('current');
		$('ul#project-selector li:eq(' + Current + ') a').addClass("current");
	}
	
	/* Calls "BannerSelected" to display the selected banner. */
	$('ul#project-selector li a').click(function(){		
		if(SelectionQueue == false){
			//Hide current data.
			$('h1 ul li').fadeOut(800);
			$('#summary ul li').fadeOut(800);
			$('#ViewProject li').fadeOut(500);
			
			var SelectedBanner = $(this).attr('rel');
			BannerSelected(SelectedBanner);
		}
		else{
			SelectionQueue = $(this).attr('rel');
		}
	});

	function BannerSelected(SelectedBanner){
		//Ensure only 1 click is being processed at a time.
		SelectionQueue = true;
		//This stop is used to stop all movement caused by previous clicks of the nav buttons. Possibly no longer required.
		$('#background-rotator ul li').each(function(){
			$(this).stop(true, true);
		});
		SelectProject(SelectedBanner - 1);
		//Place selected banner on top and fade in. Possilbly not required.
		$('#background-rotator ul li:eq(' +  (SelectedBanner - 1) + ')').css({
			'zIndex' : BannerQuantity,
			'display' : 'block',
			'opacity' : '1'
		});
		
		//Fade data in for selected project.
		$('h1 ul li:eq(' + (SelectedBanner - 1) + ')').fadeIn(800);
		$('#summary ul li:eq(' + (SelectedBanner - 1) + ')').fadeIn(800);
		$('#ViewProject li:eq(' + (SelectedBanner - 1) + ')').fadeIn(500);

		//Switch the Rotator class off and store selected banner index.
		RunRotate = SelectedBanner;
		
		//Re-allocate z-indexes.
		var ZIndexLevel = 0;
		
		SelectedBanner = (SelectedBanner - 2) % BannerQuantity;
		$('#background-rotator ul li').each(function(Index){
			//Calculate z-index
			 ZIndexLevel = ((BannerQuantity - Index) + SelectedBanner) % BannerQuantity;	
			$(this).css({
				'zIndex' : ZIndexLevel,
				'display' : 'block',
				'opacity' : '1'
			});
		});

		if(SelectedBanner == -1){
			SelectedBanner = 1;
			RunRotate = "First";
		}

		if(SelectionQueue == true){
			SelectionQueue = false;
		}
		else{
			BannerSelected(SelectionQueue);
		}
	}

	function RestartRotate(SelectedBanner){
		if(SelectedBanner == "First"){
			SelectedBanner = 1;
		}
		RunRotate = true;
		setTimeout(function() {Rotate(SelectedBanner - 1);}, 4000);
	}
	
	
	/* -------------------------------------- */
	
	/* Primary Navigation */
	//Fades the menu in and out as required.
	$('ul#nav_548843 li').hover(function(){
		$(this)						 
			.find('ul')
			.stop('true', 'true')
			.fadeIn('400');
		//Hide deeper level lists.
		$(this)	
			.find('ul ul')
			.hide();
	}, function(){
		$(this)
			.find('ul')
			.stop('true', 'true')
			.fadeOut('400');
	});

	/* Project Navigation */
	$('#nav_549996 ul').hide();
	$('#nav_549996 li.selected ul').show();
	// Unrequired transition
	//$('#nav_549996 li.selected ul').delay('2000').slideDown('2000');

	//Sets the "Current" state on the sub nav items that point to the current page/project.
	$('a[href="' + location.pathname + '"]').addClass('Current-Navitem'); 
	
	/*--------------------------------------------------------------------*/
	
	  
	//Run these functions straight away.
	fnResize();
	fnFooter();
	 
	//Run these when the window is resized.
	$(window).resize(function() {
		fnResize();
		fnFooter();
	});
	  
	//Footer vertical alignment. (The footer is 80px.)
	function fnFooter(){
		var WindowHeight = $(window).height();
		var ContainerHeight = $('#container').height() + 80;
		if(WindowHeight > ContainerHeight){
			$('#footer').css('top','' + (WindowHeight - 80)  + 'px')
		}
		else{
			$('#footer').css('top', '' + (ContainerHeight - 80) + 'px');
		}
	}

	function fnResize(){
		
		// Window dimensions.
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		
		// Container dimensions.
		var containerWidth = $("#container").width();	 
		var containerHeight = $("#container").height() + 80;
			 
		// Image dimensions.
		var imageWidth = 1200;
		var imageHeight = 800;
		
		//Window is both Higher and Wider than the container.
		if (windowWidth > containerWidth && windowHeight > containerHeight) {
			if ((windowWidth / imageWidth)>=(windowHeight / imageHeight)) {
				$('#background-rotator img').css('width', '100%');
				$('#background-rotator img').css('height', 'auto');
			}
			else {
				$("#background-rotator img").css('height', '100%');
				$('#background-rotator img').css('width', 'auto');
			}			
		 }
		 
		 //Container is both Higher and Wider than the window.
		 else if(containerWidth > windowWidth && containerHeight > windowHeight){
			 if ((containerWidth / imageWidth)>=(windowHeight / imageHeight)) {
				$('#background-rotator img').css('width', '' + containerWidth + 'px');
				$('#background-rotator img').css('height', 'auto');
			}
			else {
				$("#background-rotator img").css('height', '' + containerHeight + 'px');
				$('#background-rotator img').css('width', 'auto');
			}
		 }
		 
		 //The window is either wider or taller than the container.
		else{		
			var DimensionRatio = new Array();
			DimensionRatio[0] = (windowWidth / imageWidth);
			DimensionRatio[1] = (windowHeight / imageHeight);
			DimensionRatio[2] = (containerWidth / imageWidth);
			DimensionRatio[3] = (containerHeight / imageHeight);
			
			var Counter = 0;
			var TopDimensionValue = 0;
			var TopDimension = "equal"
			while (Counter < 4){
				if (DimensionRatio[Counter] > TopDimensionValue){
					TopDimensionValue = DimensionRatio[Counter];
					TopDimension = Counter;
				}
				Counter++;
			}
			
			switch(TopDimension){
				case 0:
					$('#background-rotator img').css('width', '100%');
					$('#background-rotator img').css('height', 'auto');
				break;
				case 1:
					$("#background-rotator img").css('height', '100%');
					$('#background-rotator img').css('width', 'auto');
				break;
				case 2:
					$('#background-rotator img').css('width', '' + containerWidth + 'px');
					$('#background-rotator img').css('height', 'auto');
				break;
				case 3:
					/*
					if(windowWidth > containerWidth){
						$('#background-rotator img').css('width', '100%');
						$('#background-rotator img').css('height', 'auto');
					}
					else{
						$('#background-rotator img').css('width', '' + containerWidth + 'px');
						$('#background-rotator img').css('height', 'auto');
					}*/
					
					$('#background-rotator img').css({
						'minWidth' : '100%',
						'minHeight' : '100%',
						'width' : 'auto',
						'height' : 'auto'
					});
				
					//$("#background-rotator img").css('height', '' + windowHeight + 'px');
					//$('#background-rotator img').css('width', 'auto');
					//$("#background-rotator img").css('height', '' + containerHeight + 'px');
					//$('#background-rotator img').css('width', 'auto');
				break;
				
			}			 
		 } 
		
	};

	//Footer base position
	function fnFooter() {
	   var windowHeight = $(window).height();
	   var containerHeight = $('#container').height();
	   if (windowHeight > (containerHeight + 80)) {
			$('#footer').css('top', '' + windowHeight - 80 + 'px');
	   }
	}
	  
	 /* Inserts an automatic link in the Project WebApp if a specific value is found. */
	//Data
	if($('#projectdetail-text').length > 0){
		var awardTitles = new Array();
		awardTitles[0]=" 2008 MBA Excellence in Construction Award";
		awardTitles[1]=" 2009 MBA Excellence in Construction Award";
		awardTitles[2]=" 2010 MBA Excellence in Construction Award";
		var projectDetailMarkup = $('#projectdetail-text').html();
		var awardLink;
		var repetition;
		var counter = 0;
		var repCounter = 0;
		while(counter < awardTitles.length){
			repetition = projectDetailMarkup.split(awardTitles[counter]).length -1;
			if(repetition > 0){
				repCounter = 0;
				awardLink = '&nbsp;<a href="\/award.htm">' + awardTitles[counter].substr(1) + '<\/a>'
				while(repCounter < repetition){
					projectDetailMarkup = projectDetailMarkup.replace(awardTitles[counter], awardLink);
					repCounter++;
				}
			}
			counter++;
		}
		//Implement changes.
		$('#projectdetail-text').html('' + projectDetailMarkup + '');
	}

	/* Adds link to project image in the awards page. */
	if($('#content-award').length > 0){
		
		var URL;
		var WinningProjectContent;
		$('.award-list p a').each(function(counter){
			url = $(this).attr('href');
			WinningProjectContent = $('.award-list:eq(' + counter + ') .WinningProject').html();
			WinningProjectContent = '<a href="' + url + '">' + WinningProjectContent + '</a>';
			$('.award-list:eq(' + counter + ') .WinningProject').html('' + WinningProjectContent + '');
		});
		
		//var URL = $
		
	}
	

});
