// JavaScript Document

/* top navigation image roll over state */
$(document).ready( function(){
	homeSlide();
	accordion();
	accordion2();
	accordion3();
	emailPage();
	printPage();
	externalLinks();
	
});

/* homepage slideshow function */
function homeSlide() {
	
	$('#showNav ul').addClass('display');                    // display the nav
	$('#showNav ul li:first').addClass('tabActive');         // Set the class of the first link to active
	
	var c=0;
	var t;
	var slideCount = $('#showNav ul li').length;
	
	// auto play
	function autoPlay (){

		timedCount()

		function timedCount(){                         // delay between slides
			$('#showNav ul li a').click(function(){                  //When any link is clicked
				c= $('#showNav ul li a').index(this);
				stopCount();                                         // stops timer
				$('#showNav ul li').removeClass('tabActive');         // Remove active class from all links
				$(this).parent().addClass('tabActive');               //Set clicked link class to active
				var currentTab = $(this).attr('href');               // Set variable currentTab to value of href attribute of clicked link
				$("#showNav div:visible").fadeOut("fast", function(){ //fade out visible div
					$(currentTab).fadeIn("slow");                     //fade in target div
				});
			});
			
			$('#showNav ul li a:eq('+c+')').click();
			
			t=setTimeout(
				function(){
					if(c < (slideCount - 1)){ 	        // loops through slides
						c=c+1;
						timedCount()
					} else { 					            // rests slide to beginning
						c=0;
						timedCount()
					}
				}, 10000
			)
		}
		
		function stopCount(){
			clearTimeout(t);
		}
	};
	
	if($('#showNav').length){       // starts auto play
		autoPlay ()
	}

}

/* accordian function for center section */
function accordion() {
		//hide the all of the element with class msg_body
	$(".acc-content").hide();
	$('#elm1').show();
	var el;
	var divel=$('#kui-accordion');
	//slides the element with class "msg_body" when paragraph with class "msg_head" is clicked 
	$("#kui-accordion h3").click(function()
    {
		el=$('#kui-accordion')[0].offsetHeight;
		$(this).next(".acc-content").slideToggle('fast').siblings(".acc-content").slideUp("fast");
		 $(this).toggleClass("active");
		 $(this).siblings("h3").removeClass("active");
		//$('#kui-accordion').css('height', el);
	});
}

/* accordian function for right section */
function accordion2() {
		//hide the all of the element with class msg_body
	$(".acc-content2").hide();
	$('#elm2').show();
	var el;
	var divel=$('#kui-accordion2');
	//slides the element with class "msg_body" when paragraph with class "msg_head" is clicked 
	$("#kui-accordion2 h3").click(function()
    {
		el=$('#kui-accordion2')[0].offsetHeight;
		$(this).next(".acc-content2").slideToggle('fast').siblings(".acc-content2").slideUp("fast");
		 $(this).toggleClass("active");
		 $(this).siblings("h3").removeClass("active");
		//$('#kui-accordion2').css('height', el);
	});
}

/* accordian function for right section */
function accordion3() {
		//hide the all of the element with class msg_body
	$(".acc-content3").hide();
	$('#elm3').show();
	var el;
	var divel=$('#kui-accordion3');
	//slides the element with class "msg_body" when paragraph with class "msg_head" is clicked 
	$("#kui-accordion3 h3").click(function()
    {
		el=$('#kui-accordion3')[0].offsetHeight;
		$(this).next(".acc-content3").slideToggle('fast').siblings(".acc-content3").slideUp("fast");
		 $(this).toggleClass("active");
		 $(this).siblings("h3").removeClass("active");
		//$('#kui-accordion2').css('height', el);
	});
}


/* email page function */
function emailPage() {
	$('a.email').click(function(){
	$('a.email').attr({ href: 'mailto:enter email address?subject=Wolseley Annual Report 2009&body=The sender of this email thought that you may find the following page(s) from Wolseley Annual Report 2009 of interest: %0a%0a' + document.title + ': ' + window.location})
 });
}

/* print page function */
function printPage() {
	$('.print').click(function(){
		window.print();
	});
}


/* external links to open in new window */
function externalLinks() { 

	if (!document.getElementsByTagName) return; 

	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		var relvalue = anchor.getAttribute("rel");
		if (anchor.getAttribute("href")) {
			var external = /external/;
			var relvalue = anchor.getAttribute("rel");
			if (external.test(relvalue)) { 
				anchor.target = "_blank";
			}
		} 
	}
} 
//window.onload = externalLinks;