		var tabHeight = 28;
$(document).ready(function(){
	tabpad();
/*Note: most code requires the jquery library */

//Add the end caps to the subheadertext graphics.
$(".subheaderback").wrapInner("<div class='subheaderright'><div class='subheaderleft'><div class='subheadertext'></div></div></div>");
$(document).bind('cbox_complete', function(){
       $("#cboxContent .subheaderback").wrapInner("<div class='subheaderright'><div class='subheaderleft'><div class='subheadertext'></div></div></div>"); 
});

/* Easy Slider */
	$("#slider").easySlider({
		auto: true,
		continuous: true,
		numeric: true,
		speed: 400,
		pause: 8000
	});


/*Code for creating the Imagebox when images are clicked.  NOTE: Requires "colorbox-min.js" */
$("a[rel='imagebox-1']").colorbox({maxHeight:"100%", maxWidth:"100%", scalePhotos: true});
$("a[rel='imagebox-2']").colorbox({maxHeight:"100%", maxWidth:"100%", scalePhotos: true});
$("a[rel='imagebox-3']").colorbox({maxHeight:"100%", maxWidth:"100%", scalePhotos: true});
$("a[rel='imagebox-4']").colorbox({maxHeight:"100%", maxWidth:"100%", scalePhotos: true});
$("a[rel='imagebox-5']").colorbox({maxHeight:"100%", maxWidth:"100%", scalePhotos: true});
//Checks cookies and hides/shows the appropriate menus
		$(".navSub").each(function (){
			var divid = "#" + $(this).attr('id');
			var opencookie = getCookie(divid);
			if (!opencookie){
				$(this).addClass('hidden');
				setCookie(divid, 'hidden');
			} else {
				if(opencookie == 'hidden'){
					$(this).addClass('hidden');
				} else if(opencookie == 'shown'){ 
					$(this).removeClass('hidden');
					$(this).prev().addClass("navPress");
				} 
			}
		})
//Loades up the Dealer search page with AJAX to hide dealer searching from search engine.
		$("#navTop2").click(function(){
			$(".permtopnavpress").removeClass("permtopnavpress");
			$("#navTop2").addClass("permtopnavpress");
			$(".pTabNav, .preTab, .postTab").css({display : "none"});
			resize();
			$("#pContainer").load("/map.php", function(){
				//$(window).unbind("resize", tabResize);
				//$(window).bind("resize", resize);
				//$(window).bind("resize", mapResize);
			});
		});

/* provides the hover and press-down image effects (using CSS sprites).  The classes "navHover" and "navPress" alter the
			background image by superimposing another background position on the div */		
		$(".subMenu, .subaMenu")
			.mousedown(function(){
				$(this).removeClass("navHover");
				$(this).addClass("navPress");
			})
			.mouseup(function(){
				$(this).removeClass("navPress");
				$(this).addClass("navHover");
			})
		$(".subMenu, .subaMenu")
			.hover(function(){
				$(this).addClass("navHover");
			}, function(){
				$(this).removeClass("navHover");
				$(this).removeClass("navPress");
			})
		$(".topnavbutton, .topnavbuttonL")
			.hover(function(){
				$(this).addClass("topnavpress");
			}, function(){
				$(this).removeClass("topnavpress");
				$(this).removeClass("navPress");
			})
		$(".navButton")
			.hover(function(){
				var nClass = $(this).attr("class");
				if (nClass.search("navPress") != -1){
					$(this).addClass("navSelect");
				} else {
					$(this).addClass("navHover");
				}
			}, function(){
				$(this).removeClass("navHover");
				$(this).removeClass("navSelect");
			})		
			$(".navButton")
			.click(function(){
				$(this).addClass("navHover");
				var prevID = $(this).prev('div').attr("id");
				if ($(this).next('div').is('.navSub')){
					var subdiv = "#" + $(this).next('.navSub').attr('id'); /*grab the div id of the next div (which should be the submenu */
					$(subdiv).slideToggle("normal", function(){
						if ($(subdiv).is(":visible")){ 
							$('#content').load('/navload.php?pagename=' +  prevID, function (){tabpad();});
							$('#titletext').load('/navtext.php?pagename=' + prevID);
							$("#navTop1").removeClass("permtopnavpress");
						}
					});
					$('.navSub:visible').each(function(){
						if ($(this).attr("id") != subdiv.replace("#",'')){
							$(this).slideToggle("normal");
							setCookie("#"+$(this).attr("id"), 'hidden');
							$(this).prev().removeClass("navPress");
						}
					});
					var opencookie = getCookie(subdiv);
					if (!opencookie){
						$(subdiv).removeClass('hidden');
						setCookie(subdiv, 'shown');
						$(this).addClass("navPress");
					} else {
						if(opencookie == 'hidden'){
							setCookie(subdiv, 'shown');
							$(this).addClass("navPress");
						} else if(opencookie == 'shown'){ 
							setCookie(subdiv, 'hidden');
							$(this).removeClass("navPress");
						} 
					}
				}
			}, function(){
				$(this).removeClass("navHover");
				$(this).removeClass("navPress");
			})
/* Provides tab functionality for products */
		$(".pTabNav")
			.click(function(){
				var clickId = $(this).attr("id");
				var contentId = '#' + clickId + 'content';
				var pClass = $(".pContent").filter(".pAnim").attr("class");		/*Asigns the class name(s) to the PClass variable of the first DOM that has a pAnim class */
				if (pClass){pClass = pClass.match("pAnim");}	/*Reassign the PClass variable to simply "pAnim" only if the PClass variable isn't undefined */
				if (pClass != "pAnim"){ 		/*Perform the code only if the class pAnim isn't assigned to any DOM objects.  This presents the code from executing if another object is currently being animated. */
					var lastTab = $(".pTabNav:last").attr("id"); //find the first last tab
					var firstTab = $(".pTabNav:first").attr("id"); //find the first tab
					$(".pTabNav").each(function (){  //moves through each tab and checks to see if it is the first or last, then applies approriate formatting
						if ($(this).attr("id") == clickId){	
							if (clickId == firstTab){
								$(this).removeClass("tabpos1");
								$(this).prev().removeClass("tabpos1");
								$(this).next().removeClass("tabpos1 tabpos2");
							} else if (clickId == lastTab){
								$(this).removeClass("tabpos1")
								$(this).next().removeClass("tabpos4").addClass("tabpos3");
							} else {
								$(this).removeClass("tabpos1");
								$(this).next().removeClass("tabpos1 tabpos2");
							}
						} else {
							if ($(this).attr("id") == firstTab){
								$(this).addClass("tabpos1");
								$(this).prev().addClass("tabpos1");
								if ($(this).next().next().attr("id") == clickId){
									$(this).next().addClass("tabpos1").removeClass("tabpos2");
								} else {
									$(this).next().addClass("tabpos2").removeClass("tabpos1");
								}
							} else if ($(this).attr("id") == lastTab){
								$(this).addClass("tabpos1");
								$(this).next().removeClass("tabpos3").addClass("tabpos4");
							} else {
								$(this).addClass("tabpos1");
								if ($(this).next().next().attr("id") == clickId){
									$(this).next().addClass("tabpos1").removeClass("tabpos2");
								} else {
									$(this).next().addClass("tabpos2").removeClass("tabpos1");
								}
							}
						}
					});
					if ($(".pContent:visible").attr("id") != null){		/*We don't need to fade out anything if nothing is visible */		
						$(contentId).addClass("pAnim"); /*Adds pAnim class to prevent any other tab from animating */
						$(".pContent:visible").fadeOut("fast", function(){	/*fade out any other content, call-back next function when complete */
							$(contentId).fadeIn("fast", function(){ /*fade in function, call back next function when complete */
								if (jQuery.browser.msie){ $(contentId).get(0).style.removeAttribute('filter');}
								$(contentId).removeClass("pAnim"); /*remove pAnim class (allowing other animation) */
							});
						});
					} else {
						$(contentId).addClass("pAnim"); /*Adds pAnim class to prevent any other tab from animating */
						$(contentId).fadeIn("fast", function(){ /*fade in function, call back next function when complete */
							if (jQuery.browser.msie){ $(contentId).get(0).style.removeAttribute('filter');}
							$(contentId).removeClass("pAnim");  /*remove pAnim class (allowing other animation) */
						});
					}
				}
			})
});
/*This returns the appropriate height depending on the browser (essentially an IE workaround) - I wrote this before using jquery :( */
function heightSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight -180;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight - 180;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight - 180;
  }
  myHeight = myHeight; 
  return myHeight;
} 
//Retrive a cookie with the specefied name
function getCookie(Name){ 
	var re=new RegExp(Name+"=[^;]*", "i"); 
	if (document.cookie.match(re)){ 
		return document.cookie.match(re)[0].split("=")[1];
	} else {
	return null;
	}
}
//Set a cookie with a value
function setCookie(name, value, days){
	if (typeof days!="undefined"){ //checks to see if days is defined
		var expireDate = new Date();
		expireDate.setDate(expireDate.getDate()+days);
		document.cookie = name+"="+value+"; path=/; expires="+expireDate.toGMTString();
	} else { //else if this is a session only cookie
		document.cookie = name+"="+value+"; path=/";
	}
}
/*Resizes the content height so webpage fills screen */   
function tabpad() {
	if ($("#tab2").attr("id") != null){
		tabResize();
	} else {
		resize();
	}
	$('.hide').css("display","none");
	$(".popup").colorbox({width:"50%", maxHeight:"90%"});
	$(".popup-noicon").colorbox({width:"50%", maxHeight:"90%"});

}
function resize() {
	var windowheight = heightSize();  
	document.getElementById("content").style.height = windowheight + 'px';
	document.getElementById("navback").style.height = windowheight -18 + 'px';
	windowheight = windowheight - 12;
	document.getElementById("pContainer").style.height = windowheight + 'px';
}
function tabResize() {
	var windowheight = heightSize();  
	document.getElementById("content").style.height = windowheight + 'px';
	document.getElementById("navback").style.height = windowheight -18 + 'px';
	windowheight = windowheight - 38;
	document.getElementById("pContainer").style.height = windowheight + 'px';
}
