//initialize Global Vars

//Determine User Agent
var IECheck = (navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Opera') == -1) ? true : false;
if(IECheck){
	var IE6Check = (navigator.userAgent.indexOf('MSIE 6') != -1) ? true : false;
}
var FFCheck = (navigator.userAgent.indexOf('Firefox') != -1) ? true : false;
var OperaCheck = (navigator.userAgent.indexOf('Opera') != -1) ? true : false;
var SafariCheck = (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1) ? true : false;
var ChromeCheck = (navigator.userAgent.indexOf('Chrome') != -1) ? true : false;

//jQuery
$(document).ready(function() {
	//Call Index Flash From External JS
	if($('#cFlashSlideshow')){
		var flashvars = {};
		flashvars.fileName = "/flash/slideshow/Feb10/slideshow.swf";
		var params = {};
		params.scale = "noscale";
		params.quality = "high";
		params.salign = "lb";
		params.wmode = "opaque";
		var attributes = {};
		swfobject.embedSWF("/flash/slideshow/slideshowLoader.swf", "cFlashSlideshow", "100%", "250px", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);	
	}
	
	//Open Image Function
	$("a[href^='/images/']:not([href^='/images/wallpaper/']):not([href$='pdf'])").click(function(){
		
		prepContent();
			
		var captionTxt = document.createTextNode(this.title); //text node for caption
		var contentAnchor = document.createElement('img'); //container for the preload image
		var preloadImg = new Image();  //preload the clicked image
		with(contentAnchor){
			src = this.href;
			alt = this.title;
			id = "media";
		}
		preloadImg.onload = function(){
			var imgH = preloadImg.height;
			var imgW = preloadImg.width;
			
			with(contentAnchor){
				height = imgH;
				width = imgW;
			}
			
			$('#pActiveCaption').append(captionTxt);
			$('#pActiveMedia').append(contentAnchor);
			
			var winH = $(window).height() * .8;
			var winW = $(window).width() * .8;
			var scale = imgW / imgH;
			
			if(winH <= imgH){
				newH = winH;
				newW = newH * scale;
			}else if(winW <= imgW){
				newW = winW;
				newH = newW / scale;
			}else{
				newH = imgH;
				newW = imgW;
			}
			
			$('#media').attr({
				height : newH,
				width : newW
			});
			
			$('#pActiveOuter').animate({
				"top" : Math.floor(($(window).height() - newH - 45) / 2 + $(window).scrollTop())
			},500, function(){
				$('#pActiveContainer').animate({
					"height" : Math.floor(newH + 35),
					"width" : Math.floor(newW)
				},500, function(){
					$("#pActiveMedia").css({height : Math.floor(newH)});
					$("#pActiveContent").fadeIn(500);
				});
			});
		}
		preloadImg.src = this.href;
		return false;
	});
	
	//Close Button Functionality
	$("a[href='#close']").click(function(){
		$('#pActiveOverlay').fadeOut(function(){
			$('#pActiveOverlay').hide();
		});
		$("#pActiveOuter").fadeOut(function(){
			$('#pActiveMedia').empty();
			$('#pActiveCaption').empty();
			$("#pActiveContent").hide();
		});
		return false;
	});
	
	//Open Flash Files Function
	$("a[href$='.flv'],a[href$='.swf']").click(function(){	
	
		prepContent();		
	
		var flashDiv = document.createElement('div');//create container div for flash to load into
		with(flashDiv){
			id = 'flashDiv';
		};
		$('#pActiveMedia').append(flashDiv);
		var flashvars = {};
		flashvars.videofile = this.href;
		var params = {};
		params.quality = "high";
		params.wmode = 'opaque';
		params.scale = 'noscale';
		var attributes = {};
		var fHeight;
		var fWidth;
		if(this.href.indexOf('.flv') != -1){
			swfobject.embedSWF("/flash/flvplayer/umplayer2.1.2.swf", "flashDiv", "600", "400", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
			fHeight = 400;
			fWidth = 600;
		}else{
			//Evaluate split string(filename) to make adjustments
			switch(this.href.split("/")[this.href.split("/").length-1]){
				case 'extremeRotate.swf':
					fWidth = 550;
					fHeight = 400;
					break;
				case 'zbRotate.swf':
					fWidth = 555;
					fHeight = 400;
					break;
			}
			swfobject.embedSWF(this.href, "flashDiv", fWidth, fHeight, "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
		}
		$('#pActiveOuter').animate({
			"top" : Math.floor(($(window).height() - fHeight - 45) / 2 + $(window).scrollTop())
		},500, function(){
			$('#pActiveContainer').animate({
				"height" : Math.floor(fHeight + 35),
				"width" : Math.floor(fWidth)
			},500, function(){
				$("#pActiveMedia").css({height : Math.floor(fHeight)});
				$("#pActiveContent").fadeIn(500);
			});
		});

		return false;
	});
	
	//Add gif to indicate PDF Link
	//var pdfImg = new Image();
	//with(pdfImg){
	//	src = '/images/layout/pdf_logo.gif';
	//	height = 20;
	//	width = 20;
	//	alt = 'PDF File Link';
	//	className = 'noformat';
	//}
	//$("a[href$='.pdf']").append(pdfImg);
	
	//Add gif to indicate external sites
	var extImg = new Image();
	with(extImg){
		src = '/images/layout/external.gif';
		height = 10;
		width = 10;
		alt = 'External Link';
		className = 'noformat';
	}
	$("a[rel='external']").append(extImg);
	
	//Functions
	function prepContent(){
		$('#pActiveMedia').children().remove();
		$('#pActiveCaption').children().remove();
		$('#pActiveOverlay').fadeTo(1,0, function(){
			$('#pActiveOverlay').show().fadeTo(500,0.70).css({
				"height" : $(document).height()
			});
		});
		$("#pActiveOuter").css({
			'top' : Math.floor(($(window).height() - $("#pActiveOuter").height()) / 2 + $(window).scrollTop())
		}).fadeIn();
	}
	
	//PopUp Handler
	$("a[rel='popUp']").click(function(){
		return !window.open(this.href);
	});
	//MouseOver Handler
	$("a[rel='mouseUp']").click(function(){
		return false;
	}).mouseenter(function(){
		$('#cShippingRates').removeClass().mouseleave(function(){
			$('#cShippingRates').addClass('hidden');
		});
	});
	
	//Dealer Locator Quick Search Clear
	$("input[name='zip']").focus(function(){
		this.value = "";
	});
});

// onRender
addJSEvent();
//Min-Max Width Handlers for IE6
if(IE6Check){
	checkAvailableWidth();
}

//onResize
window.onresize = function(){
	if(IE6Check){
		checkAvailableWidth();
	}
}

//Attach Javascript to page objects
function addJSEvent(){
	//span array for prod menu
	var spanArray = document.getElementById('productMenu').getElementsByTagName('span');
	//li array for banner menu
	var bannerArray = document.getElementById('Banner').getElementsByTagName('ul')[0].childNodes;
	//anchor array for popup windows
	for(z=0;z<spanArray.length;z++){
		spanArray[z].onclick = function(){openMenu(this);}
		spanArray[z].onmouseover = function(){this.className = 'red';}
		spanArray[z].onmouseout = function(){this.className = '';}
		spanAnchorArray = spanArray[z].parentNode.getElementsByTagName('a');
		for(y=0;y<spanAnchorArray.length;y++){
			if(spanAnchorArray[y].href == window.location.href.split("?",1)){
				openMenu(spanArray[z]);
			}
		}
	}
	for(x=0;x<bannerArray.length;x++){
		bannerArray[x].onmouseover = function(){bannerOpen(this);}
		bannerArray[x].onmouseout = function(){bannerClose(this);}
	}
}
// Functions
//DOM method for innerHTML clear
function clearElement(clearElement){
	while(clearElement.firstChild) clearElement.removeChild(clearElement.firstChild);	
}

//Banner Menu Function
function bannerOpen(thisObj){
	thisObj.className = 'bannerOver';
	if(thisObj.getElementsByTagName('ul')[0]) thisObj.getElementsByTagName('ul')[0].className = 'block';
}
function bannerClose(thisObj){
	thisObj.className = '';
	if(thisObj.getElementsByTagName('ul')[0]) thisObj.getElementsByTagName('ul')[0].className = '';
}
function openMenu(menuItem){
	var pThis = menuItem.parentNode;
	if(pThis.className != 'block'){
		if(pThis.parentNode.parentNode.className == 't2down'){
			closeMenu(pThis);
			pThis.className = 't3down';
		}else{
			closeMenu(pThis.parentNode.parentNode);
			pThis.className = 't2down';
		}
	pThis.getElementsByTagName('ul')[0].className = 'block';
	}else{
		closeMenu(pThis);
	}
}
function closeMenu(closeThis){
	var liArray = closeThis.parentNode.parentNode.getElementsByTagName('li');
	var ulArray = closeThis.parentNode.getElementsByTagName('ul');
	for(x=0;x<liArray.length;x++){
		if(liArray[x].className == 't2down' || liArray[x].className == 't3down'){
			liArray[x].className = '';
		}
	}
	for(x=0;x<ulArray.length;x++){
		if(ulArray[x].className == 'block'){
			ulArray[x].className = '';
		}
	}
}

// Min-Max Width Function for IE6
function checkAvailableWidth(){
	var widthConArray = new Array();
	widthConArray[0] = document.getElementById("pBody");
	widthConArray[1] = document.getElementById("pHeaderContent");
	widthConArray[2] = document.getElementById("pFooterContent");
	for(x=0;x<widthConArray.length;x++){
		widthConArray[x].style.width = (document.body.clientWidth < 994? "989px" : document.body.clientWidth > 1205? "1200px" : "100%");
	}
}