// JavaScript Document

function rotator(parentNode, delayNormal, delayLong) {
	// How many milliseconds we want it to take between cycles, normally
	if ((typeof delayNormal != "undefined") && (delayNormal != null)) {
		this.delayNormal=delayNormal;
	} else {
		this.delayNormal=2000;
	}
	// How many milliseconds we want it to take between cycles, once a button has been clicked
	if ((typeof delayLong != "undefined") && (delayLong != null)) {
		this.delayLong=delayLong;
	} else {
		this.delayLong=20000;
	}
	
	// Store the timer so we can cancel it, etc.
	this.timer=null;
	
	// Store the current position
	this.position=-1;
	
	// Store the parent node
	this.parentNode=$(parentNode);
	
	// Find all of the child rotatble elements
	this.rotatables=this.parentNode.find('.rotatable');
	
	// Quit out if there were zero or one rotatable elements and therefore no need for rotation
	if (this.rotatables.length<2) {
		this.rotatables.show(); // Ensure that if there is only one, it's at least displayed.
		return;
	}
	
	// Create the paginator
	this.createBullets();
	
	// Start rotating
	this.rotate();
}

rotator.prototype.createBullets=function() {
	// Create the div to hold the paginator
	this.parentNode.append('<div class="rotator_paginator_holder"><div class="rotator_paginator"></div></div>');
	// Store a reference to it
	this.paginator=this.parentNode.find('.rotator_paginator');
	// Store a reference to the current this
	var _that=this;
	// For each rotatable
	this.rotatables.each(function(k, v) {
		// Add a bullet
		_that.paginator.append('<a href="#" rel="'+k+'">'+(k+1)+'</a>');
	});
	// Store all of the bullets
	this.bullets=this.paginator.find('a');
	
	// Bind the click events for each bullet
	this.bullets.click(function(e) {
		e.preventDefault();
		_that.showAndDelay($(this).attr('rel'));
	});
}

rotator.prototype.rotateInTime=function(delay) {
	var _that=this;
	if (this.timer!=null) {
		clearTimeout(this.timer);
	}
	this.timer=setTimeout(function() {
		_that.rotate();
	}, delay);
}

rotator.prototype.show=function(position) {
	// Move to the requested position
	this.position=position;
	// Ensure it can't go out of range
	this.position=this.position % this.rotatables.length; 
	
	// Show the correct rotatable
	this.rotatables.hide();
	$(this.rotatables[this.position]).show();
	
	// Set the bullet to active
	this.bullets.removeClass('current');
	$(this.bullets[this.position]).addClass('current');
}

rotator.prototype.showAndDelay=function(position) {
	// Show the requested image
	this.show(position);
	// Prep to rotate in time
	this.rotateInTime(this.delayLong);
}

rotator.prototype.rotate=function() {
	// Show the next 
	this.show(this.position+1);
	// Prep to rotate in time
	this.rotateInTime(this.delayNormal);
}

var menuAnimating=false;

function initMenu() {
	if ($('#collapse_menu').length>0) {
		$('a.pcsm_primary').click(function(e) {
			e.preventDefault();
			
			// Don't do anything with the always open nodes
			if ($(this).parents('li').is('.pcsm_always_open')) {
				return;
			}
			
			// Don't do anything if another animation is already running
			if (!menuAnimating) {		
				var alreadyOpen=$(this).parents('li').is('.pcsm_primary_open');
				
				menuAnimating=true;
				
				$('.pcsm_primary_open').each(function() {
					$(this).removeClass('pcsm_primary_open');
					$(this).find('ul.pcsm_secondary').slideUp({duration: 500});
				});
				
				if (!alreadyOpen) {
					$(this).parents('li').addClass('pcsm_primary_open');
					$(this).parents('li').find('ul.pcsm_secondary').slideDown({duration: 500});
				}
				
				setTimeout(function() { menuAnimating=false; }, 500);
			}
		});
	}
	$('#pcsm_our_products').parents('li').addClass('pcsm_primary_open');
	$('#pcsm_our_products').parents('li').find('ul.pcsm_secondary').show();
}

function initSelectNavs() {
	// Find all of the select navs and bind to their change events
	$('.select_nav').change(function(e) {
		// Don't do anything for empty options
		if ($(this).val()!="") {
			// Go to the url stored in the value of the select
			document.location=$(this).val();
		}
	});
}

function initFlyouts() {
	$('li.phs_primary').mouseover(function(e) {
		$(this).addClass('phs_open');
	});
	
	$('li.phs_primary').mouseout(function(e) {
		$(this).removeClass('phs_open');
	});
}

$(document).ready(function() {
	// Initialize a rotator for every page_feature
	$('#page_feature .page_section_inner').each(function() {
		new rotator(this, 10000, 600000);
	});
	
	// Initialize a rotator for every module_image_rotator's content
	$('.module_image_rotator .module_content').each(function() {
		new rotator(this);
	});
	
	// Initialize the homepage menu
	initMenu();
	
	// Set up the select box navs
	initSelectNavs();
	
	// Set up the flyouts
	// initFlyouts();
});


// 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);

//<![CDATA[
    $(document).ready(function() {      
      function addMega(){
        $(this).addClass("phs_open");
        }

      function removeMega(){
        $(this).removeClass("phs_open");
        }

    var megaConfig = {
 		 interval: 100,
         sensitivity: 7,
         over: addMega,
         timeout: 500,
         out: removeMega
    };

    $("li.phs_primary").hoverIntent(megaConfig)     
    });
//]]>
// -- close hoverIntent r5 --

// var swiss = {
//  src: '/flash/sifr.swf'
//};

//sIFR.activate(swiss); // From revision 209 and onwards

//sIFR.replace(swiss, {
//	selector: '#page_feature_inner h1',
//	css: {
//		'.sIFR-root': { 
//			'color': '#ffffff',
//			'font-size': '30px'
//		}
//	},
//	wmode: 'transparent'
//});

// ANIMATED ANCHOR SCROLLING ------------------
$(document).ready(function(){
  $('a[href*=#]').click(function() {
 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
 && location.hostname == this.hostname) {
   var $target = $(this.hash);
   $target = $target.length && $target
   || $('[name=' + this.hash.slice(1) +']');
   if ($target.length) {
  var targetOffset = $target.offset().top;
  $('html,body')
  .animate({scrollTop: targetOffset}, 1000);
    return false;
   }
 }
  });
});



/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/5780/easy-slider-17-numeric-navigation-jquery-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade: 	false,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			500,
			auto:			false,
			pause: 			20000,
			continuous:		false, 
			numeric: 		true,
			numericId: 		'controls'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			var clickable = true;
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
								
			if(options.controlsShow){
				var html = options.controlsBefore;				
				if(options.numeric){
					html += '<ol id="'+ options.numericId +'"></ol>';
				} else {
					if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
					html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
					html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';				
				};
				
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
			
			if(options.numeric){									
				for(var i=0;i<s;i++){						
					$(document.createElement("li"))
						.attr('id',options.numericId + (i+1))
						.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
						.appendTo($("#"+ options.numericId))
						.click(function(){							
							animate($("a",$(this)).attr('rel'),true);
						}); 												
				};							
			} else {
				$("a","#"+options.nextId).click(function(){		
					animate("next",true);
				});
				$("a","#"+options.prevId).click(function(){		
					animate("prev",true);				
				});	
				$("a","#"+options.firstId).click(function(){		
					animate("first",true);
				});				
				$("a","#"+options.lastId).click(function(){		
					animate("last",true);				
				});				
			};
			
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li", "#" + options.numericId).removeClass("current");
				$("li#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);					
					};
					
					if(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();					
						};
						if(t==0){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			if(options.numeric) setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);