$(function(){
$(".leftNavLines a").each(function() {
	if($(this).html() == "MaxxPro")
		$(this).html("MaxxPro&reg;");
});
$("h1").each(function() {
	if($(this).html() == "MaxxPro Utility")
		$(this).html("MaxxPro Utility Variant");
});
});

if(window.location.href.indexOf("navistardefense.com"))
	document.domain = "navistardefense.com";

/*****************************************************************************
* global variables
*****************************************************************************/
var curTab = 0;
var numTabs = 0;
var tabName = "";
var promoNum = 0;
var promoPage = "";
var headline = "";
var headlines = "";
var links = "";

/*****************************************************************************
* showTab - function for switching between tabs (i.e. on the product pages)
*****************************************************************************/
function showTab( tabNum )
{
    curTab = tabNum;
    for( i=0; i<numTabs; i++ ){
        page = document.getElementById( 'tabPage' + i );
        tab = document.getElementById( tabName + '_tab' + i );
        tabOn = document.getElementById( tabName + '_tab' + i + '_on' );
        tabOver = document.getElementById( tabName + '_tab' + i + '_over' );
        if( i == tabNum ){
	        page.className = tabName + '_tabPageTable';
	        tab.className = 'hidden';
	        tabOver.className = 'hidden';
	        tabOn.className = 'tab';
        }
        else{
	        page.className = 'hidden';
	        tab.className = 'tab';
	        tabOn.className = 'hidden';
        }
    }
}

/*****************************************************************************
* highlightTab - function for highlighting tabs (i.e. on the product pages)
*****************************************************************************/
function highlightTab( tabNum, bHighlight )
{
    if( tabNum != curTab ){
        tab = document.getElementById( tabName + '_tab' + tabNum );
        tabOver = document.getElementById( tabName + '_tab' + tabNum + '_over' );
        if( bHighlight ){
            tab.className = 'hidden';
            tabOver.className = 'tab';
        }
        else{
            tab.className = 'tab';
            tabOver.className = 'hidden';
        }
    }
}

/*****************************************************************************
* promoRollover - function for showing promo rollovers (i.e. on the home page)
*****************************************************************************/
function promoRollover( promoNum, bShow )
{
    promo = document.getElementById( promoPage + 'Promo' + promoNum );
    promoOver = document.getElementById( promoPage + 'Promo' + promoNum + '_over' );
    if( bShow ){
        promo.className = 'hidden';
        promoOver.className = promoPage + 'Promo_over';
    }
    else{
        promo.className = promoPage + 'Promo';
        promoOver.className = 'hidden';
    }
}

/*****************************************************************************
* showHeadline - function for rotating news headlines on the home page)
*****************************************************************************/
function showHeadline()
{
    headline = document.getElementById( 'a1NewsLink' );
    headline.innerHTML = headlines[ curHeadline ];
    headline.href = links[ curHeadline ];

    curHeadline++;
    if( curHeadline >= headlines.length ) curHeadline = 0;
}

/*****************************************************************************
* imageRollover - function for showing image button rollovers
*****************************************************************************/
	Rollimage = new Array()

	Rollimage[0]= new Image(121,153)
	Rollimage[0].src = "images/BTN_Dealer_OFF.gif"

	Rollimage[1] = new Image(121,153)
	Rollimage[1].src = "images/BTN_Dealer_ON.gif"

	function SwapOut() {
	document.imageRollover.src = Rollimage[1].src;
	return true;
	}

	function SwapBack() {
	document.imageRollover.src = Rollimage[0].src;
	return true;
	}

/*****************************************************************************
* Gallery popup - function for showing popup window in the gallery
*****************************************************************************/

	function popitup(url, pathImg) {
	url = url + "?pathImg=" + pathImg;
	//newwindow=window.open(url,'name','height=575, width=780, scrollbars=YES');
	newwindow=window.open(url,'name','height=690,width=670,status=no,scrollbars=no,resizable=no');
	if (window.focus) {newwindow.focus()}
	return false;
}

/*****************************************************************************
* AJAX Request Object Encaspulator - Ratchet
*****************************************************************************/
function getQueryVariable(name) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == name)
			return pair[1];
	}
	
	return null;
}

function getData(nodes) //Use this method when getting data in a CDATA area of an xml file
{
	for(var i = 0; i < nodes.length; i++)
		if(nodes[i].data.length > 0)
			return nodes[i].data;
}


function ajaxObj(url, request_params) {
	var ajax = this;
	this.request_timeout = 15000;
	this.maxRetry = 3;
	this.trys = 0;
	this.timeout = null;
	this.request_onTimeout = function()
	{ 	                            
		if(this.http_request != null && this.http_request.readyState < 4)
		{
			this.http_request.abort();
			clearTimeout(this.timeout);
			if(getQueryVariable("debug") != null)
			{
				if(confirm("A server timeout has occured, would you like to retry?"));
					this.init();
			}
			else
			{
				if(this.maxRetry > this.trys)
					this.init();
				else
					alert("A timeout error has occured, please check your internet connection.");
			}
			
		}
	};

	this.handler_params = new Array();
	this.handler_method = function() { return null; };
	
	this.http_request = null;
	this.xmlDoc = null;
	this.text = null;

	this.init = function()
	{
		this.trys += 1;
		
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			this.http_request = new XMLHttpRequest();

			if(typeof this.http_request.async != "undefined")
				this.http_request.async = true;            

		} else if (window.ActiveXObject) { // IE
		
			try
			{
				this.http_request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
			}
			catch (e)
			{
				try
				{
					this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
				}
			}
		}
		else
		{
			alert("Your browser does not support AJAX");
		}
		
		if(this.http_request != null) {
			this.http_request.onreadystatechange = function() // 
			{
				if (ajax.http_request.readyState == 4) {
					clearTimeout(ajax.timeout);
					if (ajax.http_request.status == 200) {
						
						if((ajax.http_request.getResponseHeader("Content-Type") == "text/xml" || ajax.http_request.getResponseHeader("Content-Type") == "application/xml") && ajax.http_request.responseXML.documentElement != null && ajax.http_request.responseXML.documentElement.nodeName != "parsererror")
							ajax.xmlDoc = ajax.http_request.responseXML;
						else
							ajax.text = ajax.http_request.responseText;
							
						ajax.handler_method();
						ajax.http_request = null;						
					} else if (ajax.http_request.status != 0) {
						/* alerts with exception on bad request */
						if(getQueryVariable("debug") != null)
						{
							if(confirm("A server '" + ajax.http_request.status + ": " + ajax.http_request.statusText + "' error has occured, would you like see the details?"));
								document.body.innerHTML = "Page: " + document.location + "<br />AJAX Request: " + url + "?" + request_params + "<br /><br />" + ajax.http_request.responseText;
						}
						else
						{
							alert("Server Error '" + ajax.http_request.status + ": " + ajax.http_request.statusText + "'\n\nPlease nofity the developer with the following information:\n\nPage: " + document.location + "\nAJAX Request: " + url + "?" + request_params);
						}
						ajax.http_request.abort();
					}
				} else {
					if (ajax.http_request.readyState == 1) { // Set up timeout function
						ajax.timeout = setTimeout(ajax.request_onTimeout, ajax.request_timeout);
					}
				}
			};
			
			if (request_params == null) {
				this.http_request.open('GET', url, true);
				this.http_request.send(null);
			} else {
				this.http_request.open('POST', url, true);
				this.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				this.http_request.setRequestHeader("Content-length", request_params.length);
				this.http_request.setRequestHeader("Connection", "close");
				this.http_request.send(request_params);
			}
		}			
		
		
	};
};



/*****************************************************************************
* Events
*****************************************************************************/


function addEvent(elm, evType, fn, useCapture) {
    
    if(typeof useCapture == "undefined") useCapture = false; // defaults to event bubbling
    
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
		
	}
	else {
		elm['on' + evType] = fn;
	}
}

function removeEvent(elm, evType, fn, useCapture) {

    if(typeof useCapture == "undefined") useCapture = false; // defaults to event bubbling

	if (elm.removeEventListener)
	{
		elm.removeEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.detachEvent) {
	    var r = elm.detachEvent('on' + evType, fn);
	    return r;
	}
	else {
	    elm['on' + evType] = null;
	}
}


/*****************************************************************************
* Expandable Term List Generator
*****************************************************************************/
	var termsIdArray = new Array();
	function showHideTerm(id, show)
	{
		
		for(var t = 0; t < termsIdArray.length; t++) // Close all other terms first
		{
			if(termsIdArray[t] != id)
			{
	            document.getElementById(termsIdArray[t]).className = "termHide";
				document.getElementById("i_" + termsIdArray[t]).src = "/images/plus.gif";	
			}
		}
		
		img = document.getElementById("i_" + id);
		element = document.getElementById(id);
		
	    if(element != null)
	    {
	        if(element.className.indexOf('termHide') != -1 || typeof show != "undefined")
			{
	            element.className = "";
				img.src = "/images/minus.gif";
				document.getElementById("a_" + id).focus();
			}
	        else
			{
	            element.className = "termHide";
				img.src = "/images/plus.gif";
			}
	    }
	}
	
	function getTerms(xmlURL, div, useDropDown)
	{
		if(typeof useDropDown == "undefined")
			useDropDown = false;
			
		var termRequest = new ajaxObj(xmlURL);
		termRequest.handler_params = new Array(div, useDropDown);
		termRequest.handler_method = function()
		{
			var is_ie = (document.all);
			
			var container = document.getElementById(this.handler_params[0]);
			var terms = this.xmlDoc.documentElement.getElementsByTagName("term");
			
			
			if(this.handler_params[1])
			{
				var drpDwnWrap = document.createElement("div");
				drpDwnWrap.style.textAlign = "right";
				
				drpDwn = document.createElement("select");
				drpDwn.id = "selectTerm";
				drpDwn.onchange = function() { if(this.options[this.selectedIndex].value != "") { showHideTerm(this.options[this.selectedIndex].value, true);window.location.hash = "#a_" + this.options[this.selectedIndex].value;} };
				drpDwn.style.marginBottom = "10px";
				var opt = document.createElement("option");
				opt.text = "- select a term -";
				opt.value = "";			
				
				if(is_ie)
					drpDwn.add(opt);
				else
					drpDwn.add(opt, null);				
				
				var setIndex = 0;
				
				for(var t = 0; t < terms.length; t++)
				{
					var id = terms[t].getAttribute("name").replace(/\s/g, "_").toLowerCase();
					
					opt = document.createElement("option");
					opt.text = terms[t].getAttribute("name");
					opt.value = id;
					
					if(is_ie)
						drpDwn.add(opt);
					else
						drpDwn.add(opt, null);

					if(document.location.hash.toString() == "#a_" + id)
						setIndex = drpDwn.options.length - 1

				}
				
				
				drpDwnWrap.appendChild(drpDwn);
				container.appendChild(drpDwnWrap);
				
				if(setIndex != 0)
					drpDwn.options[setIndex].selected = true;					
			}
			
			for(t = 0; t < terms.length; t++)
			{
				var id = terms[t].getAttribute("name").replace(/\s/g, "_").toLowerCase();
				
				termsIdArray[t] = id;
				
				var term = document.createElement("div");
				term.className = "term";
				
				var anchor = document.createElement("a");
				anchor.href = "javascript:showHideTerm('" + id + "');";
				anchor.setAttribute("id", "a_" + id);
				
				var img = document.createElement("img");
				img.setAttribute("id", "i_" + id);
				img.src = "/images/plus.gif";
				img.align = "left";
				img.border = "0";
				img.width = "9";
				img.height = "16";
				
				anchor.appendChild(img);
				anchor.appendChild(document.createTextNode(terms[t].getAttribute("name")));
				
				term.appendChild(anchor);				
				container.appendChild(term);
				
				var definition = document.createElement("div");
				definition.setAttribute("id", id);
				definition.className = "termHide";
				definition.innerHTML = getData(terms[t].childNodes);
				
				container.appendChild(definition);
			}
			
			if(document.location.hash.toString() != "")
				showHideTerm(document.location.hash.toString().replace(/\#a_/, ""));

		}
		termRequest.init();
		
	}
	
/*************************************************************
*      Media Pop Up Window
*************************************************************/
var mediaWin;
var mediaContainer;
function showMedia(name,w,h,noWin,noClose)
{
	if(mediaWin != null)
		mediaWin.close();
		
	
	if(typeof noWin == "undefined" || !noWin)
	{	
		if(typeof noClose == "undefined")
			h += 40;

		mediaWin = window.open("/includes/media.html?name=" + name, "", "scrollbars=no,width=" + w + ",height=" + h + ",left="+ ((window.screen.availWidth - w) / 2) + ",top=" + ((window.screen.availHeight - h) / 2) + ",resizable=yes");
		mediaWin.focus();
	}
	else
	{
		var mediaLoader = new ajaxObj("/includes/media.xml");
		mediaLoader.handler_params = new Array(name);
		
		mediaLoader.handler_method = function() {
			var mediaItems = this.xmlDoc.documentElement.getElementsByTagName("item");
			var item;
			
			for(var i = 0; i < mediaItems.length; i++)
				if(mediaItems[i].getAttribute("name") == this.handler_params[0])
					item = mediaItems[i];
			
			if(item != null)
			{
				var w = item.getAttribute("width");
				var h = item.getAttribute("height");
				var url = item.getAttribute("url");
				
				var winWidth = 0, winHeight = 0;
				if( typeof(window.innerWidth) == 'number' ) {
					//Non-IE
					winWidth = window.innerWidth;
					winHeight = window.innerHeight;
				} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
					//IE 6+ in 'standards compliant mode'
					winWidth = document.documentElement.clientWidth;
					winHeight = document.documentElement.clientHeight;
				} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
					//IE 4 compatible
					winWidth = document.body.clientWidth;
					winHeight = document.body.clientHeight;
				}				
				winWidth = winWidth - 20;
				
				mediaContainer = document.createElement("div");
				mediaContainer.style.position = "absolute";
				mediaContainer.style.top = "0px";
				mediaContainer.style.left = "0px";				
				mediaContainer.style.zIndex = "300";
				mediaContainer.style.width = winWidth + "px";
				mediaContainer.style.height = winHeight + "px";
				
				var calcTop = ((winHeight - h) / 2);
				if(calcTop < 120)
					calcTop = 120;

				var closeButton = document.createElement("a");
				closeButton.href = "javascript:void(0);";
				closeButton.style.backgroundColor = "black";
				closeButton.style.display = "block";
				closeButton.style.position = "absolute";				
				closeButton.style.top = (calcTop - 19) + "px";
				closeButton.style.left = (((winWidth - w) / 2) - 20) + "px";
				closeButton.style.width = (w) + "px";
				closeButton.style.textAlign = "center";
				closeButton.style.zIndex = "500";
				closeButton.onclick = function() { document.body.removeChild(mediaContainer); };
				closeButton.style.paddingTop = "3px";
				closeButton.style.paddingBottom = "3px";
				closeButton.appendChild(document.createTextNode("Click here to close"));
				
				var mediaBox = document.createElement("div");
				mediaBox.setAttribute("id", "media");
				mediaBox.style.zIndex = "600";
				mediaBox.style.position = "absolute";
				mediaBox.style.top = calcTop + "px";
				mediaBox.style.left = (((winWidth - w) / 2) - 20) + "px";
				mediaBox.style.height = h + "px";
				mediaBox.style.width = w + "px";

				mediaContainer.appendChild(closeButton);
				mediaContainer.appendChild(mediaBox);
				document.body.appendChild(mediaContainer);
				
				switch(item.getAttribute("type"))
				{
					case "video":
						var vid = document.createElement('a');
						vid.className = "xvid {width:" + w + ", height:" + h + "}";
						vid.setAttribute("href", url);

						mediaBox.appendChild(vid);

						$('a.xvid').media();				
						break;
					case "flash":
						var swf = new SWFObject(url,item.getAttribute("name"), w, h, '7', '#000000');
						var attribs = item.getElementsByTagName("attr");
						for(var p = 0; p < attribs.length; p++)
						{
							var attrName = attribs[p].getAttribute("name");
							var attrValue = attribs[p].getAttribute("value");
							if(attribs[p].getAttribute("type") == "v")
								swf.addVariable(attrName, attrValue);
							else
								swf.addParam(attrName, attrValue);
						}
						swf.write("media");	
						break;
					default:
						alert("Unkown media type");
						break;
				}

			}
			else
			{
				alert("Video not found");
			}
		}
		mediaLoader.init();		
	}
	
	return false;

}

