//Create all the mouseover buttons, remember to preload the images
var srcImg = '';
var imgExt = '';

var swapIn = function(lmnt) { 
		srcImg = $(lmnt).attr("src");
		imgExt = srcImg.split(".");
		imgExt = "."+imgExt.pop();
		srcImg = srcImg.replace(imgExt,"-on"+imgExt);
		$(lmnt).attr("src",srcImg);
}

var swapOut = function(lmnt) { 
	srcImg = srcImg.replace("-on"+imgExt,imgExt);
	$(lmnt).attr("src",srcImg);
}

//hides the button and shows the preloading image.
var hideButton = function(id){
	btnWidth = $('#'+id+'').width();
	if( $('#'+id+'').attr('class') == 'formbutton') { 
		btnLt = $('#'+id+'').css('padding-left').replace('px','');
		btnRt = $('#'+id+'').css('padding-right').replace('px','');
		btnTotalPad = parseInt(btnLt) + parseInt(btnRt);
		btnTotal = btnTotalPad + btnWidth;
	} else { 
		btnTotal = btnWidth;
	}
	loaderPad = (btnTotal - 16) / 2;
	$('#'+id+'').css('display','none');
	loaderHTML = '<img class="loadingmini" id="ajaxloader" height="16" width="16" style="display: inline; padding: 6px ' + loaderPad + 'px;" src="/images/common/ajax-loader-mini.gif"/>';
	$('#'+id+'').after(loaderHTML);
}

var showButton = function(id){
	$("#ajaxloader").remove();
  $("#"+id+"").show();
}


var loadVideo = function(vidPath,framePath,playOnLoad) { 
	//framePath = movPath.replace("flv","jpg");
	randNum = Math.random();
  var flashVars = {
		videoPath: vidPath,
		framePath: framePath,
		videoWidth: "640",
		videoHeight: "360",
		autoPlayButton: "true",
		autoStart: playOnLoad,
		displayPlayPause: "true",
		displayTimeline: "true",
		displayTimeClock: "true",
		displayVolume: "true",
		displayFullScreen: "true",
		displayColor: "0xFFFFFF",
		backFillColor: "0x000000",
		backFillAlpha: "0.60",
		loaderColor: "0xFFFFFF"
  };
  var flashParams = {
      wmode: "transparent",
      allowFullScreen: "true"
  };
  var flashAttributes = {
      id: "flashvideo",
      name: "flashvideo"
  };
  swfobject.embedSWF("/flash/gen_flv_player.swf?" + randNum, 'videocontainer', "640", "360", "9.0.0","/flash/expressInstall.swf", flashVars, flashParams, flashAttributes);
}

var viewAllCopy = function() { 
		copyHeight = $("#copytruncate").height();
		var fullCopy = $("#copyfull").html();
		//alert(fullCopy);
		$("#copytruncate").html(fullCopy);
		$("#featureimg2").css("display","block");
		$("#featureimg3").css("display","block");
		$("#featureimg4").css("display","block");
}

//specify the id of the element you would like to scroll to as a parameter. 
var scrollMe = function(lmnt){
	currentPos = $(window).scrollTop();
	winHeight = $(window).height();
	divOffset = $("#"+lmnt+"").offset().top;
	divHeight = $("#"+lmnt+"").height();
	scrollPos = divOffset + divHeight - winHeight + 30;
	if(currentPos < scrollPos){
		$('html, body').animate({
		scrollTop: scrollPos
		}, 500);
	}
}


//DOM Ready
$(function(){
	ajaxLoad= new Image(); 
  ajaxLoad.src="/images/common/ajax-loader.gif"; 
	miniAjaxLoad= new Image(); 
  miniAjaxLoad.src="/images/common/ajax-loader-mini.gif"; 
		 
					 
	//Create all the hover states for the menus and subnav
	$(".nav").hover(
		function(){
			$(this).addClass("navhover");
			$(this).next('.nav').addClass("navnextsibling");
			$(this).prev('.nav').addClass("navprevsibling");
			$(this).children('.subnav').css("left",'-5px');
		},
		function(){
			$(this).removeClass("navhover");
			$(this).next('.nav').removeClass("navnextsibling");
			$(this).prev('.nav').removeClass("navprevsibling");
			$(this).children('.subnav').css("left",'-10000px');
		}
	);
	
	//mouseover images on all .swap classes
	$(".swap").mouseover(
		function(){
			swapIn(this);
		});
	$(".swap").mouseout(
		function(){
			swapOut(this);
		});
	
	//mouseover for category hero thumbs
	$(".catgrid a img").hover(
		function(){
			$(this).addClass("hover");
			swapIn(this);
		},
		function(){
			swapOut(this);
			$(this).removeClass("hover");
		}
	);
	
	
	//Clear the Search Terms on Focus
	$("#searchterms").focus(function(){
		if($("#searchterms").val() == 'Product Search') { 
			$("#searchterms").val('');
		}
	});
	//Switch the footer email address
	var emailLink = $('#footeremaillink').attr('title');
	emailLink = "mailto:"+emailLink.replace("[at]", "@");
	$('#footeremaillink').attr('href',emailLink);	
});
