﻿/** 
 * Javascript glue for theintelligentproject.org
 *
 * @author		Tony Dewan ( tdewan{at}bladv.com )
 * @version		1.0 ( 3-09-09 )
 */


/**
 * Things that happen when the DOM is loaded. Using jQuery.
 * @see jquery.com
 *
 */
$(document).ready(function(){		

	// this is a big hack to overcome a deficiency with my nav class + CI list functions
	// should be removed once Nav class is better
	// since relying on JS for active link status is probably not a great idea
	// plus it just feels dirty. :(
	var bid = $("body").attr("id");
	$("li a[title="+bid+"]").parent().addClass("active");
	
	
	// If the browser is Less than IE7 (i.e., IE6 [thats alotta IE]), Fix PNGs and add first-child, last-child support
	if($.browser.msie && $.browser.version < 7){
		$("img[@src$=png]").pngfix(); /** @see scripts/jquery.pngfix.js */

		$("#subnav li:first-child").addClass("firstChild");
		$("#subnav li:last-child").addClass("lastChild");
	}
	
	// Hide the 'skip to content' link
	$("a#skip").css('display','none');
	
	// events and stuff for the homepage only widget
	if($("body").attr("id") == "home"){
	
		// start the SG widget with the correct visual stete
		$("#sg_desc .sg_home, #sg_desc .sg_transmission").css("display", "none");
		$("#sg_consumer").addClass("hover");
		$("#sg_consumer_arrow").css({marginLeft:"72px", marginTop:"0"});

		
		var loc = 1; // current location 
		
		// timer used for automatically looping the SG widget		
		/** @see scripts/jquery.timer.js */
		$(document).everyTime(4000, "sg", function(){
		
			switch(loc){
				
				case 1:
					consumer_trans();
					break;
					
				case 2:
					home_trans();
					break;
					
				case 3:
					transmission_trans();
					break;			
			}
			
			loc = (loc == 3) ? 1 : loc + 1;
		
		}, 7); // call it 7 times (2 loops [2*3], plus end back at the beginning[+1])

		
		// transition effects for the consumer stuff
		function consumer_trans(){
		
			$("#sg_consumer").addClass("hover");
			$("#sg_home_arrow").animate({marginLeft:"56px"}, "fast", function(){$("#sg_home").removeClass("hover")});
			$("#sg_transmission_arrow").animate({marginLeft:"56px"}, "fast", function(){$("#sg_transmission").removeClass("hover")});
			$("#sg_desc .sg_home, #sg_desc .sg_transmission").fadeOut();
			$("#sg_desc .sg_consumer").fadeIn();	
			
			if( $("#sg_consumer_arrow").css("margin-left") != "72px")
				$("#sg_consumer_arrow").css({marginLeft:"56px", marginTop: "0"}).animate({marginLeft:"72px"}, "medium", function(){$("#sg_consumer").addClass("hover")});
		}
		
		// transition for home stuff
		function home_trans(){
			$("#sg_home").addClass("hover");
			$("#sg_consumer_arrow").animate({marginLeft:"56px"}, "fast", function(){$("#sg_consumer").removeClass("hover")});
			$("#sg_transmission_arrow").animate({marginLeft:"56px"}, "fast", function(){$("#sg_transmission").removeClass("hover")});
			$("#sg_desc .sg_consumer, #sg_desc .sg_transmission").fadeOut();
			$("#sg_desc .sg_home").fadeIn();
			
			if( $("#sg_home_arrow").css("margin-left") != "72px")
				$("#sg_home_arrow").css({marginLeft:"56px", marginTop:"72px"}).animate({marginLeft:"72px"}, "medium", function(){$("#sg_home").addClass("hover")});
		}
		
		// transition for transmission stuff
		function transmission_trans(){
			$("#sg_transmission").addClass("hover");
			$("#sg_home_arrow").animate({marginLeft:"56px"}, "fast", function(){$("#sg_home").removeClass("hover")});
			$("#sg_consumer_arrow").animate({marginLeft:"56px"}, "fast", function(){$("#sg_consumer").removeClass("hover")});
			$("#sg_desc .sg_consumer, #sg_desc .sg_home").fadeOut();
			$("#sg_desc .sg_transmission").fadeIn();

			if( $("#sg_transmission_arrow").css("margin-left") != "72px")	
				$("#sg_transmission_arrow").css({marginLeft:"56px", marginTop:"144px"}).animate({marginLeft:"72px"}, "medium", function(){$("#sg_transmission").addClass("hover")});

		}
		
		// bind actions to hover events for the SG widget buttons
		/**@see http://docs.jquery.com/Events/hover#overout */
		$("#sg_consumer").hover(
			function(){
				
				$(document).stopTime("sg");
				
				$(this).addClass("hover");
				consumer_trans();
				
			},
			function(){}
		);
		
		$("#sg_home").hover(
			function(){
				
				$(document).stopTime("sg");
				$(this).addClass("hover");
				home_trans();

			}, 
			function(){}
		);

		$("#sg_transmission").hover(
			function(){
				
				$(document).stopTime("sg");
				$(this).addClass("hover");
				transmission_trans();
				
			},
			function(){}
		);
	
	}
	
});