var navpos = 100;
var navposHover = 92;
var navshow = 0;
var $navhover,$navselect,$selected,$subnav,$hometitle;
var subnavHover = 0;
var titleCount = 0;
var titleCur = 0;
var signInHide = true;
$(document).ready(function(){
	
	//$(".mailto").emailencode().find('i').remove();
	$(".mailto i").remove();
	
	$navhover = $("#navhover");
	$navselect = $("#navselect");

	
	if ($("#hometitle").length > 0) { 
		$hometitle = $("#hometitle h2");

		titleTextLoop();
		
	 }
	
	if ($("#nav li.active").length){
		$selected = $("#nav li.active a").first();
		var selectedOffset = $selected.offset();

		$navselect
			.css("left",selectedOffset.left+"px")
			.css("width",$selected.outerWidth()+"px");				
	}


	$("#nav ul.topnav>li>a").hover(
		//Mouseover...
		function(){
			var pos = $(this).offset();
			
			var w = $(this).outerWidth();
			
			if (!navshow){
				$navhover.css("left",pos.left+"px");
				navshow = 1;
			}
			$navhover.stop(true,false).animate(
				{  left: pos.left+'px', top:navposHover+'px', width:w+'px' },
				200,
				function() {
				}
			);
		},
		
		//Mouseout...
		function(){
			$navhover.delay(300).animate(
				{  top:navpos+'px' },
				200,
				function() { navshow=0; }
			);
				
		
		}
	);
	

	if ($("#signInLink").length){ 
		$("#signInLink").click(function(){
			$("#login").focus();
			signInHide = false;
		});		
	}



	function megaHoverOver(){
		$(this).parent().find(".sub").hide();
		$(this).find(".sub").stop().fadeTo(300, 1).show();

		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).outerWidth(); 
				});
				$(this).find("div#navSignIn").each(function() {					
					rowWidth += $(this).outerWidth(); 
				});						

			};
		})(jQuery); 

		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});

		} else { //If row does not exist...

			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});

		}
	}

	function megaHoverOut(){ 
		if (signInHide){
			$(this).find(".sub").stop().fadeTo(200, 0, function() {
				$(this).hide(); 
			});			
		}

	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul.topnav li .sub").css({'opacity':'0'});
	$("ul.topnav li").hoverIntent(config);

	function titleTextLoop(){

		var titleCount = $hometitle.length;
		if (titleCur >= titleCount){ titleCur = 0;}
		if (jQuery.support.opacity == true){
			$("#hometitle h2:eq("+titleCur+")").fadeIn(800).delay(4000).fadeOut(500, 'linear', function(){titleTextLoop()});		
			titleCur++;
		}
		else {
			
			//$("#hometitle h2:eq("+titleCur+")").show().delay(5000).hide(0, function(){titleTextLoop()});
				
			$("#hometitle h2").css('right','450px').hide();

			
			$("#hometitle h2:eq("+titleCur+")")
				
				.show()
				.animate(	{  right: '0px' },300,'swing',function(){} )
				.delay(5000)
				.animate(	{  right: '450px' },300,'linear',function(){ titleTextLoop();} );

			
			
			titleCur++;
		}



	}
});	
	
(function($) {
	$.fn.emailencode = function(options) {
		var settings = jQuery.extend({
			atsign: "*"
		}, options);
		

		return this.each(function() {
			var address = $(this).attr("href");
			var formatedAddress = address.replace(settings.atsign, "@");
			$(this).attr("href", "mailto:" + formatedAddress);
			
		});
	};
})(jQuery);




function randomRange(minVal,maxVal)
{
  var floatVal = 0;
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);

}

