//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() {
	
	//declare globals					   
	var preloadImg = new Image();  //preload the clicked image
	var contentAnchor = document.createElement('img'); //container for the preload image
	var aArray;
	var aIndex;
	//attach behavior to prev image link
	$('#pPrevImg').click(function(){
		if(aIndex != 0){
			openContent(aArray[aIndex-1],true);
		}
		return false;
	})
	//attach behavoir to next image link
	$('#pNextImg').click(function(){
		if(aIndex != aArray.length){
			openContent(aArray[aIndex+1],true);
		}
		return false;
	})
						   
	//Call Index Flash From External JS
	if($('#cFlashSlideshow')){
		var flashvars = {};
		flashvars.fileName = "/flash/slideshow/Mar10/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/']").click(function(){
		openContent(this,false);
		return false;
	});
	
	function openContent(cThis,op){

		if(op){
			prepContainer();
		}else{
			prepContent();	
		}

		var captionTxt = document.createTextNode(cThis.title); //text node for caption
		
		with(contentAnchor){
			src = cThis.href;
			alt = cThis.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 - 40) / 2 + $(window).scrollTop())
			},350, function(){
				$('#pActiveContent').animate({
					"height" : Math.floor(newH)
				},350, function(){
					$('#pActiveContent').animate({
						"width" : Math.floor(newW)
					},350, function(){
						$("#pActiveMedia").css({height : Math.floor(newH)}).fadeIn(350);
						$('#pActiveCaption').css({
							width : Math.floor(newW),
							height : 'auto'
						}).fadeIn(200);
						//Test if image is part of an image table
						if(isPhotoGroup(cThis)){
						//set photo links to current table
						aArray = $(cThis).closest('#cThumb').find('a').filter(function(){
							var result = ($(this).css('display') == 'none') ? false : true;
							return result;
						});
						aIndex = aArray.index(cThis);
						$('#pPrevImg').show();
						$('#pNextImg').show();			
						if(aIndex == 0){
							$('#pPrevImg').hide();
						}else if(aIndex+1 == aArray.length){
							$('#pNextImg').hide();
						}
					}
					});
				});
			});
		}
		preloadImg.src = cThis.href;
	};
	
	//Close Button Functionality
	$("#pActiveOverlay, a[href='#close']").click(function(){
		$('#pActiveOverlay').fadeOut(250,function(){
			$('#pActiveOverlay').hide();
		});
		$("#pActiveOuter").fadeOut(250,function(){
			prepContainer();
		});
		return false;
	});
	
	//Open Flash Video Function
	$("a[href$='.flv'],a[href$='.swf'],a[href$='.mp4']").click(function(){
		openFlash(this);
		return false;
	});
	function openFlash(cThis){
		
		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 = cThis.href;
		var params = {};
		params.quality = "high";
		params.wmode = 'opaque';
		params.scale = 'noscale';
		var attributes = {};
		if(cThis.href.indexOf('.flv') != -1 || cThis.href.indexOf('.mp4') != -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{
			switch(cThis.href.split("/")[cThis.href.split("/").length-1]){
				case 'extremeRotate.swf':
					fWidth = 550;
					fHeight = 400;
					break;
				case 'zbRotate.swf':
					fWidth = 555;
					fHeight = 400;
					break;
			}
			swfobject.embedSWF(cThis, "flashDiv", fWidth, fHeight, "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
		}
		
		$('#pActiveOuter').animate({
			"top" : Math.floor(($(window).height() - fHeight - 40) / 2 + $(window).scrollTop())
		},350, function(){
			$('#pActiveContent').animate({
				"height" : Math.floor(fHeight),
				"width" : Math.floor(fWidth)
			},350, function(){
				$("#pActiveMedia").css({height : Math.floor(fHeight)}).fadeIn(350);
				$('#pActiveCaption').css({
					"width" : Math.floor(fWidth),
					"height" : '1.5em'
				}).fadeIn(200);
			});
		});
		return false;
	};
	
	function prepContent(){
		prepContainer();
		$('#pActiveOverlay').fadeTo(1,0, function(){
			$('#pActiveOverlay').show().fadeTo(350,0.75).css({
				"height" : $(document).height()
			});
		});
		$("#pActiveOuter").css({
			'top' : Math.floor(($(window).height() - $("#pActiveOuter").height()) / 2 + $(window).scrollTop())
		}).fadeIn(350);
	}
	
	function prepContainer(){
		$('#pActiveMedia').hide().empty();
		$('#pActiveCaption').hide().empty();
		$('#pPrevImg').hide();
		$('#pNextImg').hide();
	}
	
	function isPhotoGroup(clicked){
		return $(clicked).parents('#cThumb')[0];
	}
	
	//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);
	
	//Function	
	//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%");
	}
}