$(document).ready(function(){
	$('a[rel*=lightbox]').lightBox();
	
	$("#exchange_rates").load("http://www.abatti.com/modules/exchange_rates.php");
	$("#weather").load("http://www.abatti.com/modules/weather.php");
	

	/* Expand/Collapse Category Products
	 **************************************************/
		//Javascript degredation.
		$(".product_forage_module_applications ul").css({display: "none"});
		$(".product_forage_module ul h3 span").html("(click to expand)");
		$(".product_seed_module_applications ul").css({display: "none"});
		$(".product_seed_module ul h3 span").html("(click to expand)");
		$(".product_fertilizer_module_applications ul").css({display: "none"});
		$(".product_fertilizer_module ul h3 span").html("(click to expand)");
		$(".product_wheat_module_applications ul").css({display: "none"});
		$(".product_wheat_module ul h3 span").html("(click to expand)");

		/* Forage
		 **************************************************/
			$(".product_forage_module_applications").click(function(){
				var expand_note;
				$("ul", this).slideToggle("normal", function(){
					if($(this).css("display") == "none"){
						expand_note = "(click to expand)";
					} else {
						expand_note = "(click to collapse)";
					}
					$(this).parent().children().children("span").html(expand_note);
				});
			});

		/* Seed
		 **************************************************/
			$(".product_seed_module_applications").click(function(){
				var expand_note;
				$("ul", this).slideToggle("normal", function(){
					if($(this).css("display") == "none"){
						expand_note = "(click to expand)";
					} else {
						expand_note = "(click to collapse)";
					}
					$(this).parent().children().children("span").html(expand_note);
				});
			});

		/* Fertilizer
		 **************************************************/
			$(".product_fertilizer_module_applications").click(function(){
				var expand_note;
				$("ul", this).slideToggle("normal", function(){
					if($(this).css("display") == "none"){
						expand_note = "(click to expand)";
					} else {
						expand_note = "(click to collapse)";
					}
					$(this).parent().children().children("span").html(expand_note);
				});
			});
			
		/* Wheat
		 **************************************************/
			$(".product_wheat_module_applications").click(function(){
				var expand_note;
				$("ul", this).slideToggle("normal", function(){
					if($(this).css("display") == "none"){
						expand_note = "(click to expand)";
					} else {
						expand_note = "(click to collapse)";
					}
					$(this).parent().children().children("span").html(expand_note);
				});
			});


	/* Home Banner
	 **************************************************/
		//BUG FIX
		//Set initial width of timeline.
		$("#progress_bar").width(0);
	
		//General Configuration
		var menuItems 			= 4;		//Number of menu items/stops
		var animationEasing 	= "swing";	//Options: linear, swing (default)
		var intervalDistance 	= 150;		//The number of pixels between timeline items
		var a 					= 1;		//Do not change
		var rotationTimer;					//Do not change
		
		//Navigation Configuration
		var navInterval			= 250;		//Milliseconds it take to transition between timeline states
		
		//Transition Configuration
		var intervalSpeed 		= 10000;	//Milliseconds it takes the scubber to move between timeline items
		var intervalPause 		= 2000;		//Milliseconds the scrubber spends stopped on timeline items
		var transitionInterval 	= 500;		//Milliseconds it takes to transition between timeline items


		//Register navigation click and move the AnimateTimeline() function
		//into the navigation setting (no automatic transition between banners).
		$(".navigation_item").click(function(){
			a = $(this).attr("class");
			a = a.slice(0, 1);

			//Stop the current animation on the progress bar and existing timers.
			$("#progress_bar").stop();
			$("#category_1").stop();
			$("#category_2").stop();
			$("#category_3").stop();
			$("#category_4").stop();
			clearTimeout(rotationTimer);

			//Set navigation item color start new animation.
			$(".navigation_item").each(function(){$(this).css({color: "#FFFFFF"});});
			$("."+a).css({color: "#3aa839"});
			AnimateTimeline(a, false);
		});

		//The animation function has two states; timeline items automatically transition at timed intervals,
		//or timeline items transition only with user input (clicking on timeline items).
		function AnimateTimeline(a, transition){
			//Controlled transition.
			if(transition === false){
				var animateToNext = intervalDistance*(a-1);
				$("#progress_bar").animate({width: animateToNext}, navInterval, animationEasing);

				//Loop through and find the banner currently being displayed by checking if the css
				//property 'display' is set to block. Animate all 'blocks' out.
				for(var b = 1; b <= menuItems; b++){
					if($("#category_"+b).css("display") === "block" && a != b){
						//Animate banner out and set 'display' to none.
						$("#category_"+b).animate({opacity: "0"}, (navInterval/2), function(){
							$(this).css({"display": "none"});
						});
					}
				}

				//Animate selected menu item in.
				$("#category_"+a).css({"opacity": "0", "display": "block"}).animate({opacity: "1"}, navInterval*2, animationEasing);
			//Timed transition.
			} else {
				var animateToNext = intervalDistance*a;
				//End of timeline. Animate back to begining.
				if(a === menuItems){
					$("#progress_bar").animate({width: 0}, intervalPause, animationEasing, function(){
						a=1;
						$(".navigation_item").each(function(){$(this).css({color: "#FFFFFF"});});
						$("."+a).animate({color: "#3aa839"}, 250);
						//Transition between categories.
						$("#category_"+(menuItems)).animate({opacity: "0"}, transitionInterval, function(){
							$(this).css({"display": "none"});
							$("#category_"+a).css({"opacity": "0", "display": "block"}).animate({opacity: "1"}, (transitionInterval*4));
						});
						rotationTimer = setTimeout(function(){AnimateTimeline(a)}, intervalPause);
					});
				//Next to last timeline item.Add additional pause before animating back to begining.
				} else if(a === (menuItems-1)){
					$("#progress_bar").animate({width: animateToNext}, intervalSpeed, animationEasing, function(){
						a++;
						$(".navigation_item").each(function(){$(this).css({color: "#FFFFFF"});});
						$("."+a).animate({color: "#3aa839"}, 250);
						//Transition between categories.
						$("#category_"+(a-1)).animate({opacity: "0"}, transitionInterval, function(){
							$(this).css({"display": "none"});
							$("#category_"+a).css({"opacity": "0", "display": "block"}).animate({opacity: "1"}, transitionInterval);
						});
						rotationTimer = setTimeout(function(){AnimateTimeline(a)}, intervalSpeed);
					});
				} else {
					$("#progress_bar").animate({width: animateToNext}, intervalSpeed, animationEasing, function(){
						a++;
						$(".navigation_item").each(function(){$(this).css({color: "#FFFFFF"});});
						$("."+a).animate({color: "#3aa839"}, 250);
						//Transition between categories.
						$("#category_"+(a-1)).animate({opacity: "0"}, transitionInterval, function(){
							$(this).css({"display": "none"});
							$("#category_"+a).css({"opacity": "0", "display": "block"}).animate({opacity: "1"}, (transitionInterval*4));
						});
						rotationTimer = setTimeout(function(){AnimateTimeline(a)}, intervalPause);
					});
				}
			}
		}
		AnimateTimeline(1);


	/* Navigation
	 **************************************************/
		//Degradable javascript navigation enhancements.
		//Set absolute widths and fix css padding.
		$(".top_menu_item").each(function(){
			//Original width.
			//var width = $(this).width();
			
			//Width fix. Safari and Opera return incorrect widths
			//(appears to be browser width). Fixed by selecting the width of
			//the menu item anchor tag.
			var width = $(this).children("a").width()+30;
			
			$(this).css({"width": width});
			$(this).children("a").css({"padding": "4px 0px 6px"});
			$(this).find("a").css({"padding": "4px 0px 6px"});
		});
		
		$(".top_menu_item").hover(function(){
			var width = $(this).width();
			$(this).css({"width": width-2});
		}, function(){
			var width = $(this).width();
			$(this).css({"width": width+2});
		});
	
		//Add rounded corners to elements. Exclude IE.
		var test = false;
		jQuery.each(jQuery.browser, function(i, val){
			if(i=="msie" && val==true && parseInt(jQuery.browser.version.substr(0,3)) <= 8){
				test = false;
				return false;
			} else {
				test = true;
			}
		});
		
		if(test == true){
			$(".top_menu_item").corner("7px");
			$("#language_selection ul li").corner("7px bottom");
			
			$(".product_forage_module_content").corner("7px");
			$(".product_seed_module_content").corner("7px");
			$(".product_fertilizer_module_content").corner("7px");
			$(".product_wheat_module_content").corner("7px");
			$(".product_forage_module_applications").corner("7px bottom");
			$(".product_seed_module_applications").corner("7px bottom");
			$(".product_fertilizer_module_applications").corner("7px bottom");
			$(".product_wheat_module_applications").corner("7px bottom");
		}


	/* Category Modules
	 **************************************************/
		$(".applications li").hover(function(){
			$(this).stop().animate({opacity: ".75"}, 125);
		}, function(){
			$(this).stop().animate({opacity: "1"}, 125);
		});


	/* Product Modules
	 **************************************************/
		$(".product_module").hover(function(){
			$(this).stop().animate({opacity: ".75"}, 250);
		}, function(){
			$(this).stop().animate({opacity: "1"}, 125);
		});
});
