$(function(){
	
	// Home page touts //////////////////////////
	$('#home_tout_list li').click(function(){
		// Vars
		var $me = $(this); 
		
		// Am I already "active?"
		if($me.hasClass('active')) {
			document.location = $('a',$me).attr('href');
		} else {
			syn.masthead.rotate_to($me);
		}
	}).hover(function(){
		// Hovering over one of the tout items on the left should change the tout
		syn.masthead.rotate_to($(this));
	});
});

//// Rotating home page touts
var syn = {
	masthead: {
		interval: 5000, //5k = 5sec
		item_count: 0,
		position: 0,
		start_animation: function(){
			if ( ! syn.masthead.timeout) {
				syn.masthead.timeout = window.setTimeout(syn.masthead.rotate, syn.masthead.interval);
			}
		},
		stop_animation: function(){
			if (syn.masthead.timeout) {
				window.clearTimeout(syn.masthead.timeout);
				syn.masthead.timeout = null;
			}
		},
		rotate: function(){
			if(syn.masthead.data !== 0){
				var next = syn.masthead.position+1;
				if(next >= syn.masthead.item_count) next = 0;
				syn.masthead.position = next;
			
				syn.masthead.rotate_to(jQuery('#home_tout_list #home_tout_'+next));
				
				syn.masthead.timeout = null;
				syn.masthead.start_animation();
			}
		},
		rotate_to: function($me) {
			// Un-mark as active the current, that is formerly, active item
			$('#home_tout_list li').removeClass('active');
			// Make self active
			$me.addClass('active');
			// Rotate big image and what it links to
			var tout_image = $me.attr('data-image');
			var tout_image_alt = $me.find('.tout_desc').text();
			var tout_link = $me.attr('data-link');
			$('#home_tout_image_link').attr('href',tout_link);
			$('#home_tout_image').attr('src',tout_image);
			$('#home_tout_image').attr('alt',tout_image_alt);
			
			// Allow auto-rotation to resume nicely (with the one after this one)
			var my_pos = parseInt($me.attr('data-tout-id'));
			if(typeof my_pos === 'number') { syn.masthead.position = my_pos; }
			//else { console.log('my_os was not a number. ' + my_pos +' (' + (typeof my_pos) + ')'); }
		},
		timeout: null
	}
};
$(function(){
	// Initialize tout rotation
	var $touts = $('#home_tout_list .tout');
	var tout_count = $touts.size();
	if (tout_count > 0){
		syn.masthead.item_count = tout_count;
		syn.masthead.start_animation();
	}
	// Stop animation on hover 
	$('#home_touts').hover(
		function() { // mouse over
			syn.masthead.stop_animation();
		},
		function() { // mouse out
			syn.masthead.start_animation();
		}
	);
});



/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

// Nav menu drop-downs ///////////////

$(document).ready(function(){

	// JS loaded!
	$('#nav_root').removeClass('nojs');
	
	$("#nav_root li").hoverIntent({
		over: function(){
			$(this).parentsUntil('#nav_root').andSelf().siblings().removeClass('open');
			$(this).addClass('open');
		},
		out: function() {
			$(this).removeClass('open');
			$('#nav_root li:hover').addClass('open');
		},
		"timeout": 600, //before "out"
		sensitivity: 8,
		interval: 100
	});


// Sidebar accordion behavior ////

	//hide child page
	$('#keri_product_lines-3 ul li.page_item ul').hide().parent('li').children('a').prepend('<span class="sidebar_more">&nbsp;</span>').addClass('toggleable');
	//when navigate to a child page show all pages
	$('#keri_product_lines-3 ul li.current_page_item').addClass('active').parents("li").addClass('active');
	// show page list when toggled
	$('#keri_product_lines-3 ul li.active').children('ul').show();
 
	$('#keri_product_lines-3 ul li .sidebar_more').click(function(event) { 

		$me = $(this);
		$this_li = $me.parent().parent();
		$this_li.toggleClass("active");
        $this_li.children('ul').slideToggle("fast"); 
		// Don't follow hyperlink
		event.stopPropagation(); //doesn't work
		return false; // does work
	});
});

