/*
 * jQuery 1.2.3 - New Wave Javascript
 *
 * Copyright (c) 2008 John Resig (jquery.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2008-02-06 00:21:25 -0500 (Wed, 06 Feb 2008) $
 * $Rev: 4663 $
 */
$(document).ready(function(){
	
	/*******************************/
	/**** Front page slide show ****/
	/*******************************/
	if($("div#homeSlideshow ul li").length > 1) {
		
		var delay = 8000;
		var slideCount = $("div#homeSlideshow ul li").length;
		var currentSlide = 0;
		var liHeights = new Array();
		
		// Get all LI heights
		for(var a=0;a<slideCount;a++) {
			liHeights[a] = $("div#homeSlideshow ul li:eq("+a+")").height();
		}
		
		// Get the tallest LI
		var tallestLi = Math.max.apply(Math,liHeights);
		
		// Set the container UL to the height of its tallest LI child
		$("div#homeSlideshow ul").height(tallestLi+10);
		
		// Hide all but first slide
		$("div#homeSlideshow ul li:not(:first)").hide();		
		
		function nextSlide() {
			$("div#homeSlideshow ul li:eq("+currentSlide+")").hide();			// Hide displayed slide
			currentSlide != slideCount-1 ? currentSlide++ : currentSlide = 0;		// Increment current slide var (or wrap to first slide)
			$("div#homeSlideshow ul li:eq("+currentSlide+")").fadeIn(delay * .2);	// Fade in new slide
		}
		
		function runSlideshow() {
			nextSlide();
			setTimeout(runSlideshow,delay);	// Runs showSlide every 5 seconds
		}
		
		setTimeout(runSlideshow,delay);	// Start the slideshow after 5 seconds

	}
	
	/***************************/
	/**** Hidey-showey list	****/
	/***************************/
	if($("div.solution").length) {
		// Hide all "copy" paragraphs
		$("div.solution div.hs_copy").hide();
	
		// Show all expanders
		$("div.solution a.expander").show();
	
		// Show first "visible" paragraph
		//$("div.solution div.hs_copy:first").show();
	
		// Set first expander to '-'
		//$("div.solution a.expander:first").toggleClass("on");
				
		$("div.solution a.expander").click(function(){
			$(this).next().next().toggle();
			$(this).toggleClass("on");
		});
//				$("div.solution a.expander2").click(function(){
//			$(this).next().next().toggle();
//			$(this).toggleClass("on");
//		});
	}
	
	/**********************/
	/**** Promo slides ****/
	/**********************/
	if($("div.image_div").length) {
		
		//
		// FIXMARGIN
		// This function accomodates for the floating issues created by image_div-right being taller than image_div-left
		//
		function fixMargin(promoToFix) {
			// Get height of content on right side
			var idrHeight = $("div.image_div-right:eq("+promoToFix+")").height();
			
			// If the right side is taller than the left, put the tabswitcher on it's own line.
			if ( idrHeight > 138 )
				$("div.image_navigation").css("clear","right");
			else
				$("div.image_navigation").css("clear","none");
		}

		$("div.image_div").hide();													// Hide all promos
		
		var promoCount = $("div.image_div").length;									// Get number of promos
		var randomPromo = Math.floor((promoCount)*Math.random());					// Generate random promo number
		$("div.image_div:eq("+randomPromo+")").show();								// Show random promo	
		
		fixMargin(randomPromo);
	
		// Insert tab for each promo
		for(var a = 0;a<promoCount;a++) {
			$("div.image_navigation").append('<div class="imageNav"><div></div></div>');		
		}
		
		// Fade in the random promo's orangey tab	
		$("div.imageNav:eq("+randomPromo+")").toggleClass("on");				
		$("div.imageNav:eq("+randomPromo+") div").hide();
		$("div.imageNav:eq("+randomPromo+") div").fadeIn("slow");	
		
		$("div.image_navigation div.imageNav").click(function(){		
			var activeTab = $("div.image_navigation div.imageNav").index(this);		// Get this tab's index
	
			$("div.image_div").hide();												// Hide all promos
			$("div.image_div:eq("+activeTab+")").show();							// Show selected promo
			
			$("div.image_div:eq("+activeTab+") img").hide();						// Hide Images
			$("div.image_div:eq("+activeTab+") img").fadeIn();						// Fade in new Image
			
			fixMargin(activeTab);													// Fix the margin between tabs and CTA if necessary
			
			$("div.imageNav").removeClass("on");									// Turn all tabs off
			$(this).addClass("on");													// Turn this tab on
		});
	}
	
	// Set "_blank" targets for all links inside solution copy (if any exists)
	if($("div.hs_copy p.copy").length) {
		$("div.hs_copy p.copy a").attr({target: "_blank"}); 
	}
});