/*
	OnLoad
*/

  function where_we_work(){
		$("#where_we_work").css({"display":"block"});
		$("#csr_text").css({"display":"none"});
    };
    
        function our_community(){
		$("#our_community").css({"display":"block"});
		$("#where_we_work").css({"display":"none"});
    };
    
        function the_big_wide_world(){
		$("#the_big_wide_world").css({"display":"block"});
		$("#our_community").css({"display":"none"});
    };
    
    
  
$ (function() { 

	/*
		Settings
	*/

	var TT_additional = 'easeInOutQuint';
	var TS_additional = 480;
	var TT_header = 'easeInOutExpo';
	var TS_header = 800;

	var menu_opacity = 0.2;
	var busy = 0;
	
	var section = $("#content").attr('class');

	var clientsColorOver = '#E9E9EA';
	var aboutusColorOver = '#f1f1f1';
	var cultureColorOver = '#f8f4e8';
	var workColorOver = '#f2e5e5';
	var newsColorOver = '#e5e2e7';
	var joinusColorOver = '#ecf1ef';
	var familyColorOver = '#dfe4e0';
	var opinionColorOver = '#eae9f2';
	var clientsColor = '#919295';
	var aboutusColor = '#b7b8b8';
	var cultureColor = '#dec68d';
	var workColor = '#bd7f7b';
	var newsColor = '#7f6e89';
	var familyColor = '#5d7665';
	var joinusColor = '#a0baaf';
	var opinionColor = '#968fbe';
	var Color = '#ffffff';
	var ColorOver = '#ffffff';

	var sectionColorName = section + "Color";
	var sectionColor = eval(sectionColorName);
	
	var sectionColorOverName = section + "ColorOver";
	var sectionColorOver = eval(sectionColorOverName);
	
	/*
		Hostname + Section Name
	*/
	
	var basePath = document.URL.split("//")[1];
	var hostName = basePath.split("/")[0];
	
	if(hostName.split(":")[0] == "localhost"){ // If local...
		hostName = hostName + "/AISWebsite2009";
	} else if (hostName.split(".")[0] == "development"){ // If Dev
		hostName = hostName + "/2009/AIS/AISWebsite2009";
	} else if (hostName.split(".")[0] == "staging"){ // If Staging
		hostName = hostName + "/ais/aislondon";
	}
	
	var urlLength = document.URL.split("/").length - 2;
	var sectionName = document.URL.split("/")[urlLength];
	
	/*
		Browser funtions
	*/

	$('a, .submit').click(function(){
		this.blur();
	});
	
	$(document).pngFix();
	
	/*
		Left Navigation
	*/

	if(!$('#navigation ul li#Clients').hasClass('current')){
		$('#navigation ul li#Clients').hover(function() {
			$(this).css({'background':"#f1f1f1"})
		}, function() {
			$(this).css({'background':"#ffffff"})	
		}); 
	}
	if(!$('#navigation ul li#AboutUs').hasClass('current')){
		$('#navigation ul li#AboutUs').hover(function() {
			$(this).css({'background':"#e9e9ea"})
		}, function() {
			$(this).css({'background':"#ffffff"})	
		});
	}
	if(!$('#navigation ul li#Culture').hasClass('current')){
		$('#navigation ul li#Culture').hover(function() {
			$(this).css({'background':"#f8f4e8"})
		}, function() {
			$(this).css({'background':"#ffffff"})	
		});
	}
	if(!$('#navigation ul li#Work').hasClass('current')){
		$('#navigation ul li#Work').hover(function() {
			$(this).css({'background':"#f2e5e5"})
		}, function() {
			$(this).css({'background':"#ffffff"})	
		});
	}
	if(!$('#navigation ul li#News').hasClass('current')){
		$('#navigation ul li#News').hover(function() {
			$(this).css({'background':"#e5e2e7"})
		}, function() {
			$(this).css({'background':"#ffffff"})	
		});
	}
	if(!$('#navigation ul li#JoinUs').hasClass('current')){
		$('#navigation ul li#JoinUs').hover(function() {
			$(this).css({'background':"#ecf1ef"})
		}, function() {
			$(this).css({'background':"#ffffff"})
		});
	}
	if(!$('#navigation ul li#Opinion').hasClass('current')){
		$('#navigation ul li#Opinion').hover(function() {
			$(this).css({'background':"#eae9f2"})
		}, function() {
			$(this).css({'background':"#ffffff"})	
		});
	}	
	
	if(!$('#navigation ul li#NewsOpinion').hasClass('current')){
		$('#navigation ul li#NewsOpinion').hover(function() {
			$(this).css({'background':"#eae9f2"})
		}, function() {
			$(this).css({'background':"#ffffff"})	
		});
	}	

	// Different for the family link as its the foot and theres an image for the rounded bottom edge
	if($('#navigation ul li#Family').hasClass('current')){
		$("#navigation-foot").css({'background':"url(http://" + hostName + "/Images/Generic/navigation_bottom_on.jpg) no-repeat"});
	}else{
		$("#navigation ul li#Family").hover(function() {
			$(this).css({'background':"#dfe4e0"})
			$("#navigation-foot").css({'background':"url(http://" + hostName + "/Images/Generic/navigation_bottom_over.jpg) no-repeat"})
		}, function() {
			$(this).css({'background':"#ffffff"})	
			$("#navigation-foot").css({'background':"url(http://" + hostName + "/Images/Generic/navigation_foot.jpg) no-repeat"})
		});
		$("#navigation-foot").hover(function() {
			$("#navigation ul li#Family").css({'background':"#dfe4e0"})
			$("#navigation-foot").css({'background':"url(http://" + hostName + "/Images/Generic/navigation_bottom_over.jpg) no-repeat"})
			$("#navigation-foot").css({'cursor':"pointer"})
		}, function() {
			$("#navigation ul li#Family").css({'background':"#ffffff"})	
			$("#navigation-foot").css({'background':"url(http://" + hostName + "/Images/Generic/navigation_foot.jpg) no-repeat"})
		});
	}
	

	
	/*
		List Item clicks
	*/
	

	$("#content_body ul.items li, #navigation ul li").click(function(){
		var linkURL = $(this).find('a').attr('href');
		window.location=linkURL
	});


	/*
		Related Content
	*/
	
	$(".related_content_container li").hover(function() {
		$(this).css({'background':"url(http://" + hostName + "/Images/Generic/related_item_top_over.gif) no-repeat"})
		$(this).find(".related_content_item_content").css({'background':"url(http://" + hostName + "/Images/Generic/related_item_bottom_over.gif) no-repeat bottom"})
	}, function() {
		$(this).css({'background':"url(http://" + hostName + "/Images/Generic/related_item_top.gif) no-repeat"})
		$(this).find(".related_content_item_content").css({'background':"url(http://" + hostName + "/Images/Generic/related_item_bottom.gif) no-repeat bottom"})	
	});
	
	$(".related_content_container li").click(function(){
		var linkURL = $(this).find('a').attr('href');
		window.location=linkURL
	});
    
    /*
		Initialise Related Content
    */
    
    initRelatedContent = function(parent){
		
		// Get total children
		var totalChildren = $(parent + " ul").size();
		// Calculate widths
		var childWidth = 203;
		var containerWidth = totalChildren * childWidth;
		// ammount the parent will need to move to display each item
		var difference = childWidth;
		// Get the maximum left point (to stop it from falling off the edge of the world...)
		var endOfLine = containerWidth - difference;
		
		// Remove css scrollbar 
		$('div.related_content_mask').css("overflow","hidden");
		
		if(totalChildren > 1)
		{ 
		    //var lis = $(parent + " ul").children().size();
		    $('div.related_content_mask').height($(parent).find('ul').height());
		}
        else
        {
            var lis = $(parent + " ul").children().size();
            if(lis<4)
            {
                $('div.related_content_mask').height(250);  
            }
            else
            {
                $('div.related_content_mask').height($(parent).find('ul').height());   
            }
                  
        }
		
		// Set container width
		$(parent).width(containerWidth);
		
		if(totalChildren > 1){
		
			// Show controls
			$('#related_content_controls').css("display","block");
			$('#related_content_controls .left').css("opacity","0.2");
			var dot = $('.paging_container').html();
			$('.paging_container').empty();
			
			$(parent).find('ul').each(
				function(i){
					var newDot = $(dot).addClass('dot_' + i).css('opacity','0.2');
					$('.paging_container').append(newDot);
				}
			);
			
			$('.paging_container .dot_0').css('opacity', '1');
			
			$('a.related_scroller').click(function(){
			
				// store the clicked anchor as an object for ease of use in the function
				var selectedLink = $(this);
				
				// check current CSS left value and * 1 so its treated as a number not a string
				var containerPosLeft = $(parent).css('left').split('px')[0] * 1; 
				//alert(containerPosLeft);
						
				if(selectedLink.hasClass('left') && !busy && containerPosLeft != 0) {
					// Prevent further calls
					busy = 1;
					// calculate new location
					var newLoc = containerPosLeft + difference;
					
					// Selection number
					var nextNum =  ((-containerPosLeft + childWidth) / childWidth) - 2;
					var prevNum = nextNum + 1;
					
					$('.paging_container .dot_'+prevNum).animate({opacity: 0.2},200);
					$('.paging_container .dot_'+nextNum).animate({opacity: 1},200);
					
					// if its the last element fade out controls					
					if(newLoc == 0){
						$('#related_content_controls .left').animate({opacity: 0.2},200);
					}
					// animate content
					$(parent).animate({left: newLoc },TS_additional, TT_additional, function(){
						relatedComplete();
					});
					$('#related_content_controls .right').animate({opacity: 1},500);
				}
				if(selectedLink.hasClass('right') && !busy && containerPosLeft != -endOfLine) {
					// Prevent further calls
					busy = 1;
					// calculate new location
					var newLoc = containerPosLeft - difference;
					
					// Selection number
					var nextNum =  ((-containerPosLeft + childWidth) / childWidth);
					var prevNum = nextNum - 1;
					
					$('.paging_container .dot_'+prevNum).animate({opacity: 0.2},200);
					$('.paging_container .dot_'+nextNum).animate({opacity: 1},200);
					
					// if its the last element fade out controls
					if(newLoc == -endOfLine){
						$('#related_content_controls .right').animate({opacity: 0.2},200);
					}
					// animate content
					$(parent).animate({left: newLoc },TS_additional, TT_additional, function(){
						relatedComplete();
					});
					// Lazyness? Setting the opposte arrows oppacity to full evertyime instead of checking
					// this isnt hard to update... just low on time as per...
					$('#related_content_controls .left').animate({opacity: 1},500);
				}
				
				return false;
			});
			
		} else {
			$('a.related_scroller.right, a.related_scroller.left').hide();
		}
		
    };
    
    // Call Init related content function
    initRelatedContent(".related_content_container");
    
	/*
		Related Content scroller
	*/
    
	relatedComplete = function(){
		busy = 0;
	}
	
	// Attach an action for when people click on the specified anchor
	$('a.additional').click(function(){

		// store the clicked anchor as an object for ease of use in the function
		var selectedLink = $(this);
		// grab the related content container by looking at the href of the clicked item
		var selected_id = selectedLink.attr('href');
		// store the related content container as an object for ease of use
		var selectedId = $(selected_id);
		
		// if the clicked anchor has the class more
		if(selectedLink.hasClass('more')) {
			// animate the selected container
			selectedId.animate({height: 'show', opacity: 1}, TS_additional, TT_additional, function(){
				// once animation is complete remove the class more and add the class less
				selectedLink.removeClass('more').addClass('less');
			});
		// if the clicked anchor does not have the class more
		} else {
			// animate the selected container
			selectedId.animate({height: 'hide', opacity: 0}, TS_additional, TT_additional, function(){
				// once animation is complete remove the class less and add the class more
				selectedLink.removeClass('less').addClass('more');
			});
		};
		return false;
	});


	/*
		Filters
	*/

	$('#content.work #content_head #FilterLinks').append ('<p>filter by:</p>');

	var cssMinHeight = 234;
	var allContent = $('.items');
	var filteredContent = allContent.clone();
	var overlay = $("<div></div>").appendTo("#content_body").addClass("overlay").animate({opacity: 0}, 0);

	$('#content_head a').click(function(){

		// Get selected className
		var whichClass = $(this).attr("class");
		
		// Get + set heights for content and overlay
		var contentHeight = $(".items").height();
		var overlayHeight = contentHeight + 9;
		$("#content_body").height(contentHeight);
		overlay.css({display: 'block'});
		overlay.height(overlayHeight).animate({opacity: 1}, 500, function(){
		
			$(".items").empty();
			
			// Operate on cloned element
			$(filteredContent).find('li').each(function(){

				// Get the reviewed LI class's
				var filterClass = $(this).attr("class");
				// Split the class's into an array
				var whichTags = filterClass.split(' ');
				// Check each class in the elements array against the selected class and clone any relevant items into the original container
				for(i=0; i<whichTags.length; i++)
				{
					var testTerm = whichTags[i];
					if(whichClass==testTerm)
					{
						$(this).clone().appendTo(allContent);
					}
				}
				
			});
			// Grab new height of total items so that we can animate the container
			var newHeight = $(".items").height();
			// Ensure height is above a certain value to make sure it animates nicely to the min height
			if(newHeight < cssMinHeight) newHeight = cssMinHeight; 
			overlay.height(newHeight + 9)
			$("#content_body").animate({height: newHeight}, 500, function(){
				overlay.animate({opacity: 0}, 500, function(){
					$(this).css({display: 'none'});
				});
			})
		});
	});
});



