/**
 * @namespace FC
 * 
 * call jQuery
 */

var server="/";


//Top navigation background fix
FC.navRollover = function(o) {
	var element= o.element
	var theClass= o.theClass
	
	//Navigation background fix
	$(element).bind("mouseover", function(){
		$(this).parent().next().addClass(theClass)
	});
	
	$(element).bind("mouseout", function(){
		$(this).parent().next().removeClass(theClass)
	});
}





FC.rollovers = function(){

	if(document.images){

	ImLeftRoll = new Image();
	ImLeftRoll.src = server+"images/buttons/images-left-on.gif";
	ImLeftOff = new Image();
	ImLeftOff.src = server+"images/buttons/images-left.gif";
	ImLeftInactive = new Image();
	ImLeftInactive.src = server+"images/buttons/images-left-inactive.gif";
	
	ImRightRoll = new Image();
	ImRightRoll.src = server+"images/buttons/images-right-on.gif";
	ImRightOff = new Image();
	ImRightOff.src = server+"images/buttons/images-right.gif";
	ImRightInactive = new Image();
	ImRightInactive.src = server+"images/buttons/images-right-inactive..gif";
	
	ArticlesLeftRoll = new Image();
	ArticlesLeftRoll.src = server+"images/buttons/archive-left-on.gif";
	ArticlesLeftOff = new Image();
	ArticlesLeftOff.src = server+"images/buttons/archive-left-off.gif";
	ArticlesLeftInactive = new Image();
	ArticlesLeftInactive.src = server+"images/buttons/archive-left-inactive.gif";
	
	ArticlesRightRoll = new Image();
	ArticlesRightRoll.src = server+"images/buttons/archive-right-on.gif";
	ArticlesRightOff = new Image();
	ArticlesRightOff.src = server+"images/buttons/archive-right-off.gif";	
	ArticlesRightInactive = new Image();
	ArticlesRightInactive.src = server+"images/buttons/archive-right-inactive.gif";
	
	YearsLeftRoll = new Image();
	YearsLeftRoll.src = server+"images/buttons/archive-left-on.gif";
	YearsLeftOff = new Image();
	YearsLeftOff.src = server+"images/buttons/archive-left-off.gif";
	YearsLeftInactive = new Image();
	YearsLeftInactive.src = server+"images/buttons/archive-left-inactive.gif";
	
	YearsRightRoll = new Image();
	YearsRightRoll.src = server+"images/buttons/archive-right-on.gif";
	YearsRightOff = new Image();
	YearsRightOff.src = server+"images/buttons/archive-right-off.gif";
	YearsRightInactive = new Image();
	YearsRightInactive.src = server+"images/buttons/archive-right-inactive.gif";

	}

	
}


	
	
	
	
	
FC.imgRoll = function(n){
	if(document.getElementById(n)){document.getElementById(n).src = eval(n+"Roll").src}
}

FC.imgOff = function(n){
	if(document.getElementById(n)){document.getElementById(n).src = eval(n+"Off").src}
}



















//Contact us countries
FC.contact = function(o){
	$(".contact-content ul li").removeClass("selected");
	$(".contact-content ul li div div").hide();
	$(".contact-content ul li a").bind("click", function(){														  
		var currentClass = $(this).parent().parent().attr("class");
		var currentCountry = $(this);
		if(currentClass=="selected"){	
			currentCountry.parent().parent().find("div div").animate({height:"toggle",opacity:0}, 
				function(){currentCountry.parent().parent().removeClass("selected");}
			);
		}
		else{
			$(".contact-content ul li").removeClass("selected");
			$(".contact-content ul li div div").hide();
															   
			currentCountry.parent().parent().find("div div").animate({opacity:0},1);
			currentCountry.parent().parent().addClass("selected");	
			currentCountry.parent().parent().find("div div").animate({height:"toggle",opacity:1});	
		}
		return false;
	});
	$(".contact-content ul.country-link li a").unbind();
}




//Help tooltips
FC.help = function(o){
	var element= o.element
	var elementToFind= o.elementToFind
	var theClass= o.theClass

	$(element).each(function(){
		
		$(this).find(elementToFind).addClass(theClass)
		$(this).attr("tabindex","0")
		
		$(this).bind("mouseover focus", function(){
			$(this).find(elementToFind).removeClass(theClass)
		});
		
		$(this).bind("mouseout blur", function(){
			$(this).find(elementToFind).addClass(theClass)
		});
	
	});
}



//Site wide flash movie strapline
FC.strapline = function(){
	var flashvars = {};
	var params = {
	  wmode: "transparent"
	};
	var attributes = {};
	swfobject.embedSWF(server+"swf/test.swf", "flash-strapline", "367", "90", "9.0.0", server+"swf/expressInstall.swf", flashvars, params, attributes);
}




FC.rollover = function(targ,id,del){
	
	targ.find("img").attr("id",id);
	if(del!=1){
		
		targ.css({cursor:"pointer"});
		targ.find("img").attr("src",eval(id+"Off").src);
		
		targ.mouseover(function(){
			targ.find("img").attr("src",eval(id+"Roll").src);
		});
		targ.mouseout(function(){
			targ.find("img").attr("src",eval(id+"Off").src);
		});
		
		targ.removeClass("inactive");
	}
	else{
		targ.css({cursor:"default"});
		targ.unbind("mouseover");
		targ.unbind("mouseout");
		targ.addClass("inactive");
		targ.find("img").attr("src",eval(id+"Inactive").src);
	}
}





//Press Release archive
FC.pressArchive = function(){

	//Set Variables
	var baseurl = "/Templates/Private/Pages/International/ArticleTimeLine.aspx";
	var prevArticle = $(".page .archive .articles a.previous");
	var nextArticle = $(".page .archive .articles a.next");
	var wrapper = $(".page .archive .articles .wrapper");
	var prevYear = $(".page .archive .years a.previous");
	var nextYear = $(".page .archive .years a.next");
	var years = $(".page .archive .years-inner li.link a");
	var months = $(".page .archive .months li.link a");
	var temp = $("#temp");
	var set = 0;
	var prevset = 0;
	var maxset = 0;
	var yearSet = 0;
	var articleCount = $(".page .archive .months li.selected input").val();
	var __waiting = document.createElement("img");
	__waiting.src="/images/waiting.gif";
	__waiting.height="31";
	__waiting.width="31";
	__waiting.style.position="absolute";
	__waiting.style.height="31px";
	__waiting.style.width="31px";
	__waiting.style.left="330px";
	__waiting.style.top="20px";
	__waiting.id="waiting";
	

	nextArticle.attr("disabled","");
	prevArticle.attr("disabled","");
	prevYear.attr("disabled","");
	nextYear.attr("disabled","");
	

	var stepAmount = 2;
	var yearStepAmount = 6;
	
	
	//If the wide version is used, change the step amount
	var wide = $(".page .archive.wide");
	if(wide.length>0){
		stepAmount = 3;
		yearStepAmount = 8;
	}	
	

	
	
	//Setup scroller for years
	FC.scroller({
			
		right : ".timeline .years a.next",
		left : ".timeline .years a.previous",
		container : ".timeline .years-inner",
		amount : yearStepAmount,
		items : ".timeline .years-inner ol li",
		itemClass : ".link",
		rollover : "Years"
		
	});
	
	
	

	

	//disable previous articles button
	FC.rollover(prevArticle,"ArticlesLeft",1);
	
	//If there are fewer articles than the step amount, then make next button inactive
	var articles = $(".page .archive .articles .wrapper .article");
	if(articles.length<stepAmount){
		FC.rollover(nextArticle,"ArticlesRight",1);	
	}
	
	

	
	//Update the articles (based on articles left and right)
	var updateArticles = function(url,state,set){

			var finalUrl = baseurl+url;
			//maxset = articleCount-1;
			
			//If the element numbered with set does not exist, then go and get the next two articles

			
			if(!$('.article:eq('+set+')').length || $('.article:eq('+set+')').length==0){

				if(set<=maxset){

					$.ajax({
						   
						url: finalUrl,
						cache: false,
						success: function(html){
									
									//Load the HTML into a temporary div 
									$("#temp").append(html);
									
									//Put all the articles into the wrapper div
									$(".page .archive .articles .wrapper-inner").append($("#temp .article"));
									
									$(".page .archive .articles a.next").attr("href",$("#temp .archive .articles a.next").attr("href"));
									$(".page .articles .wrapper").scrollTo( '.article:eq('+set+')', 600, {axis:'x'});
									
									$("#temp").empty();
			
						}
		 
					});

				}
				else{
					
					
					//if(set==articleCount){set=set-stepAmount;}
					$(".page .articles .wrapper").scrollTo( '.article:eq('+set+')', 600, {axis:'x'});
				}
			}
			//If the element numbered with set does exist, just jump striaght to it
			else{
				$(".page .articles .wrapper").scrollTo( '.article:eq('+set+')', 600, {axis:'x'});
			}



	}
	
	var previousArticles = function(set){

		$(".page .articles .wrapper").scrollTo( '.article:eq('+set+')', 600, {axis:'x'});
		if(set==0){
			$(".page .articles .previous img").attr("src",server+"images/buttons/archive-left-inactive.png");
			FC.rollover($(".page .articles .previous"),"ArticlesLeft",1);
		}
	}
	


	
	var getMonths = function(url){
		
		$("#temp").empty();	

		$(".archive .months").append(__waiting);
		
		years.unbind("click");
		years.click(function(){return false});
		
		var finalUrl = baseurl+url
		$(".page .archive .months ol").animate({opacity:0}, 250, function(){
																		  
		$.ajax({
			   
			url: finalUrl,
			cache: false,
			success: function(html){
				
				
				$(".archive .months").find("#waiting").remove();
				
				//Load the HTML into a temporary div 
				$("#temp").append(html);
						
				//Put all the months in the years div (removing the existing ones first)
				
					$(".page .archive .months ol li").remove();
					$(".page .archive .months ol").append($("#temp .months ol li"));
					$(".page .archive .months ol").animate({opacity:1}, 250, function(){	
							
						articleCount = $(".page .archive .months ol li.selected input").val();

						//Load the articles for the pre-selected month	
						monthURL = $(".page .archive .months ol li.selected").find("a").attr("href");
						monthURL = getQS(monthURL);
						getArticles(monthURL,articleCount);
						
						//Empty out the temporary div
						$("#temp").empty();	
						
						
						//Re-intialise the year rollovers
						FC.archiveRollover({
							element : ".months li.link",
							element2 : ".years li.link",
							theClass : "hover"
						});	
						
						//Reactivate the months links
						activateMonths();
						activateYears();
					
					});
			
				}
			
			});
		});
	};
	
	
	var getArticles = function(url,articleCount){
		
		$("#temp").empty();	

		$(".archive .articles").append(__waiting);
		
		var finalUrl = baseurl+url
		$.ajax({
			   
			url: finalUrl,
			cache: false,
			success: function(html){
				
				set=0;
				
				$(".archive .articles").find("#waiting").remove();
				
				//Load the HTML into a temporary div 
				$("#temp").append(html);
				
				//Put all the articles into the wrapper div (empty first)
				$(".page .archive .articles .wrapper-inner").animate({opacity:0}, 250, function(){
					$(".page .archive .articles .wrapper-inner").empty();
					$(".page .archive .articles .wrapper-inner").append($("#temp .article"));
					$(".page .archive .articles .wrapper").scrollTo( '.article:eq(0)', 1, {axis:'x'});
					$(".page .archive .articles .wrapper-inner").animate({opacity:1}, 250, function(){
						
						
						//UpDate the "next article" link
						$(".page .articles .next").attr("href",$("#temp .articles .next").attr("href"));
						
						//Empty out the temporary div
						$("#temp").empty();	
						
					});																							

				});
				
				
				
				FC.rollover($(".page .articles .previous"),"ArticlesLeft",1);
				

				if(articleCount<3){
					FC.rollover($(".page .articles .next"),"ArticlesRight",1);
				}
				else{
					FC.rollover($(".page .articles .next"),"ArticlesRight",0);
					activateNextArticle();
				}

			}
			
		});
	};	

	
	
	
	//Button activations
	function activateNextArticle(){

		var nextArticle = $(".page .archive .articles a.next");
		nextArticle.unbind("click");
		nextArticle.click(function(){				   
			
			maxset = Math.floor(articleCount/stepAmount);
			maxset = maxset*stepAmount;
			
			if(maxset==articleCount){
				maxset=articleCount-stepAmount;	
			}
			
			
			url = $(this).attr("href");
			if(url){url = getQS(url);}
			

					if(set<articleCount){
						set=set+stepAmount;
						FC.rollover($(".page .articles .previous"),"ArticlesLeft",0);
						updateArticles(url,"next",set);
					}
			

			if(set==maxset){
				FC.rollover($(".page .articles .next"),"ArticlesRight",1);
				$(".page .articles .next").unbind("click");
				$(".page .articles .next").click(function(){return false});	
			}
			
			

			

			


			
			
			if(articleCount<3){
				FC.rollover($(".page .articles .previous"),"ArticlesLeft",1);
				FC.rollover($(".page .articles .next"),"ArticlesRight",1);
			}
			
			return false;
		});
	}
	activateNextArticle();
	
	
	
	function activatePrevArticle(){

						
		prevArticle.attr("href","#");
		prevArticle.attr("disabled","");
		prevArticle.click(function(){				   
			
			activateNextArticle();
			FC.rollover($(".page .articles .next"),"ArticlesRight",0);

			
			if(set>0){
				
				if(set==articleCount){set=set-(stepAmount*2);}
				else{set=set-stepAmount}
				previousArticles(set);
			}
			
			if(articleCount>3){
				FC.rollover($(".page .articles .next"),"ArticlesRight",0);
			}
			return false;
		});			
	}
	activatePrevArticle();

	
	function activateYears(){
		var years = $(".page .archive .years-inner li.link a");
		years.unbind("click");
		years.click(function(){
			years.parent().removeClass("selected");
			$(this).parent().addClass("selected");
			url = $(this).attr("href");
			url = getQS(url);
			getMonths(url);
			return false;
		});
		var emptyYears = $(".page .archive .years-inner li.empty a");
		emptyYears.click(function(){
			return false;						  
		});	
	}
	activateYears();
	
	
	function activateMonths(){
		var months = $(".page .archive .months li.link a");
		var emptyMonths = $(".page .archive .months li.empty a");
		
		months.click(function(){
			months.parent().removeClass("selected");
			emptyMonths.parent().removeClass("selected");
			$(this).parent().addClass("selected");
			
			//Get URL query string
			url = $(this).attr("href");
			url = getQS(url);
			
			//Get article count
			articleCount = $(this).parent().find("input").val();

			//Get Articles
			getArticles(url,articleCount);
			
			return false;
		});
		//Make "empty" months non-clicable
		emptyMonths.click(function(){
			return false;						  
		});
	}
	activateMonths();
	
	
	function getQS(url){
		var start = url.indexOf("?");
		var end = url.length;
		url = url.substring(start,end);
		return url
	}
	
	
}






/* SCROLLER */
FC.scroller = function(o){

	var __right = $(o.right);
	var __left = $(o.left);
	var __container = $(o.container);
	var __scrollAmount = o.amount;
	var __current = 0;
	var __contentBoxes = $(o.items);
	var __itemClass = o.itemClass;
	var __rollover = o.rollover;
	var __start = 0;
	var __max = Math.floor(__contentBoxes.length/__scrollAmount);
	__max = __max*__scrollAmount;

	//Make left rollover inactive
	FC.rollover(__left,__rollover+"Left",1);
	
	//Activate the right rollover only if there are more items than the scroll amount
	if(__contentBoxes.length<=__scrollAmount){
		FC.rollover(__right,__rollover+"Right",1);
	}
	else{
		__start = __contentBoxes.length - __scrollAmount;
		FC.rollover(__left,__rollover+"Left",0);	
		FC.rollover(__right,__rollover+"Right",1);
	}
	
	//Make sure the scroller is at the start
	__container.scrollTo( __itemClass+':eq('+__start+')', 0, {axis:'x'});
	
	//If the amount of items is divisible by the scroll amount, then the max should be the scrollamount less
	if(__max==__contentBoxes.length){
		__max=__max-__scrollAmount;
	}
	
	
	//Left scroller button
	__left.bind("click keypress", function(){
		
		//If the current is more than the minimum
		if(__current>0){
			
			//Activate right rollever if there only if there are more items than the scroll amount
			if(__contentBoxes.length>__scrollAmount){
				FC.rollover(__right,__rollover+"Right",0);
			}
			//Scroll to the previous items
			__current = __current-__scrollAmount;
			__container.scrollTo( __itemClass+':eq('+__current+')', 250, {axis:'x'});	
		}
		
		//If the current is equal to the minimum, then disable the left rollover and activate the right rollover
		if(__current==0){
			
			if(__start>__current){
				
				__current = __start-__scrollAmount;
				if(__current<0){
					__current=0
					FC.rollover(__right,__rollover+"Right",0);
					FC.rollover(__left,__rollover+"Left",1);
				}
				
				__container.scrollTo( __itemClass+':eq('+__current+')', 250, {axis:'x'});

			}
			else{
				FC.rollover(__left,__rollover+"Left",1);
			
				if(__contentBoxes.length>__scrollAmount){
					FC.rollover(__right,__rollover+"Right",0);
				}					
			}
			

		}

		
		return false;
	});
	

	//Right scroller button
	__right.bind("click keypress", function(){
		
		//If current is less than the max
		if(__current<__max){
		

			//If the max is reached, then disable the right rollover
			if(__current==(__max-__scrollAmount)){
				FC.rollover(__right,__rollover+"Right",1);
			}
			
			//Activate left rollover
			FC.rollover(__left,__rollover+"Left",0);
			
			//Scroll to the next results
			__current = __current+__scrollAmount;
			
			if(__current==__scrollAmount){__container.scrollTo( __itemClass+':eq('+__start+')', 500, {axis:'x'});}
			else{__container.scrollTo( __itemClass+':eq('+__current+')', 500, {axis:'x'});}
			
			

		}
		
		return false;
	});

}












//Archive rollover
	FC.archiveRollover = function(o) {
		var element= o.element
		var element2= o.element2
		var theClass= o.theClass
		
		$(element).hover(
			function () {
				$(this).addClass(theClass)
			},
			
			function () {
				$(this).removeClass(theClass)
			}
		);
		
		$(element2).hover(
			function () {
				$(this).addClass(theClass)
			},
			
			function () {
				$(this).removeClass(theClass)
			}
		);
	},















// nest document.ready() calls in here if jQuery is included FC.$ is jQuery
$(function(){
		   
	$("body").addClass("js");	   
		   
	FC.navRollover({
		element : ".navigation a",
		theClass : "nobg"
	});
	
	FC.rollovers();
	
	FC.pressArchive();
	
	FC.contact();
	
	if($(".case-study").length!=0){
		$(".case-study").css("height","423px");
	}
	
	
	FC.help({
		element : ".helpbutton",
		elementToFind : ".help",
		theClass : "hidden"
	});
	
	FC.archiveRollover({
		element : ".months li.link",
		element2 : ".years li.link",
		theClass : "hover"
	});
	
	FC.pngFix()
});

