if (typeof vcc != "object")	vcc = new Object();
vcc.sCurrentSiteMapNode = "";

vcc.nav = {
	iDelay: 200,
	aTimeout1: [],
	aTimeout2: [],
	iActiveDrop: null,
	aItems: [],
	oPageDiv: null,
	objIframe: null,
	objIframeSub: null,
	iBreakPoint: 450,
	blnJustify: false,
	bHorizontalNavFixed: false,
	elmTopDrops: null,
	iDropWidth: ((vcc.ie && vcc.version < 7) ? 176 : 174),
	aTopImages: [
		{ d: "", dh: "", s: "", sh: "" },
		{ d: "", dh: "", s: "", sh: "" },
		{ d: "", dh: "", s: "", sh: "" },
		{ d: "", dh: "", s: "", sh: "" }
	],
	bCascading: false
}

vcc.nav.init = function() {
	if (!vcc.nav.bHorizontalNavFixed) vcc.nav.horizontalBar();
	vcc.nav.createDropdowns();	
}

vcc.addEvent(window, "load", "vcc.nav.init");

/* Horizontal navigation bar */

vcc.nav.horizontalBar = function() {
	if (!(oNavBar = document.getElementById("nav-bar"))) return;
	var oTable = oNavBar.getElementsByTagName("table")[0];
	if (vcc.getW(oTable) > vcc.nav.iBreakPoint || vcc.nav.blnJustify) {
		oTable.style.width = 882 + "px";
	} else {
		var aNavBarCells = oTable.getElementsByTagName("td");
		if (aNavBarCells.length == 0) return;
		var oLastCell = aNavBarCells[aNavBarCells.length - 1];
		if (oLastCell.className == "last") oLastCell.className = null;
	}
	var aLinks = oNavBar.getElementsByTagName("a");
	for (var i = 0; i < aLinks.length; i++) {
		var oLink = aLinks[i];
		var iHeight = vcc.getH(oLink);
		var iTopPadding = parseInt((33 - iHeight)/2 - 1);
		if (iTopPadding >= 0) {
			oLink.style.paddingTop = iTopPadding + "px";
			oLink.style.paddingBottom = parseInt((33 - iHeight - iTopPadding - 1)) + "px";					
		}
	}
	vcc.nav.bHorizontalNavFixed = true;
}

/* Dropdowns */

vcc.nav.createDropdowns = function() {
	if (!document.getElementById("top"))
		return;
	if (!(vcc.nav.oPageDiv = vcc.getObj("page"))) return;	
	var oContainer = vcc.nav.oPageDiv.appendChild(document.createElement("div"));
	oContainer.id = "topdrops";
	if (vcc.safari) {
		oContainer.style.position = "absolute";
		vcc.moveTo(oContainer, 0, 0);
	}	
	for (var i = 0; i < vcc.nav.aItems.length; i++) {
		var oDropButtonContainer = vcc.getObj("topbutton-" + (i + 1));
		//Preload button images
		var oTopButtonImages = vcc.nav.aTopImages[i];
		if (oTopButtonImages.d != "") {
			with (oTopButtonImages) {
				imgDefault = new Image();
				imgDefault.src = oTopButtonImages.d;
				imgDefaultHover = new Image();
				imgDefaultHover.src = oTopButtonImages.dh;
				imgSelected = new Image();
				imgSelected.src = oTopButtonImages.s;
				imgSelectedHover = new Image();
				imgSelectedHover.src = oTopButtonImages.sh;
			}
		}
		var aItems = vcc.nav.aItems[i].aItems;
		//Check if section has more than one child. If yes, create dropdown
		if (aItems.length > 1) {
			var iDropId = i + 1;
			var iX = (vcc.bIsRtl) ? vcc.getX(oDropButtonContainer) + (vcc.getW(oDropButtonContainer) - vcc.nav.iDropWidth) + 9 : vcc.getX(oDropButtonContainer);		
			vcc.moveTo(oContainer.appendChild(vcc.nav.createDropDown(aItems,iDropId)),iX,vcc.getY(oDropButtonContainer));
			var oButtonLink = oDropButtonContainer.getElementsByTagName("a")[0];
			oButtonLink.setAttribute("dropid",iDropId);
			oButtonLink.onmouseover = function() {
				var iDropId = this.getAttribute("dropid");
				var aImages = this.getElementsByTagName("img");
				var oImage = (aImages.length > 0) ? aImages[0] : null;
				if (vcc.nav.iActiveDrop != iDropId) vcc.nav.hideDrop(vcc.nav.iActiveDrop);
				if (oImage != null) {
					var oImages = vcc.nav.aTopImages[iDropId - 1];
					oImage.src = (this.className == "active") ? oImages.sh : oImages.dh;
				}
				vcc.nav.clearTimeouts(iDropId, null);
				vcc.nav.aTimeout1[iDropId] = setTimeout("vcc.nav.showDrop(" + iDropId + ");",vcc.nav.iDelay);
			}
			oButtonLink.onmouseout = function() {
				vcc.nav.aTimeout1[this.getAttribute("dropid")] = setTimeout("vcc.nav.hideDrop(" + this.getAttribute("dropid") + ");",vcc.nav.iDelay);
			}
			oButtonLink.onfocus = function() { this.blur(); }			
		}
	}
	//Fix the IE Zonal guillotine bug
	if (vcc.getObj("zonal") != null && vcc.ie) {
		var objFixDiv = vcc.nav.oPageDiv.appendChild(document.createElement("div"));
		objFixDiv.className = "flickerfix";
	}
	
}

vcc.nav.Item = function(sTitle, sUrl, bActive, sPopProps, aSubItems) {
	this.title = sTitle;
	this.url = sUrl.replace(/&amp;/g,"&");
	this.active = bActive;
	this.aItems = (aSubItems != null) ? aSubItems : new Array();
	this.popupProps = sPopProps;
}

vcc.nav.createDropDown = function(aItems,iDropId) {
	var oDiv = document.createElement("div");
	oDiv.id = "drop-" + iDropId;
	oDiv.className = "drop";
	oDiv.setAttribute("dropid",iDropId);
	oDiv.onmouseover = function() {
		vcc.nav.clearTimeouts(this.getAttribute("dropid"), null);
		vcc.nav.aTimeout1[this.getAttribute("dropid")] = setTimeout("vcc.nav.showDrop(" + this.getAttribute("dropid") + ");",vcc.nav.iDelay);
	}
	oDiv.onmouseout = function() {
		vcc.nav.aTimeout1[this.getAttribute("dropid")] = setTimeout("vcc.nav.hideDrop(" + this.getAttribute("dropid") + ");",vcc.nav.iDelay);
	}			
	var oUl = oDiv.appendChild(document.createElement("ul"));
	for (var i = 0; i < aItems.length; i++) {
		var oItem = aItems[i];
		var oLi = oUl.appendChild(document.createElement("li"));
		var oLink = oLi.appendChild(document.createElement("a"));
		oLink.href = oItem.url;
		oLink.innerHTML = oItem.title;
		if (oItem.active) {
			oLink.className = "active";
			oLink.setAttribute("active",true);
		}
		if (oItem.aItems.length > 1 && vcc.nav.bCascading) {
			oLi.className = "parent";
			var iSubDropId = i + 1;
			oLink.setAttribute("dropid",iDropId);
			oLink.setAttribute("subdropid",iSubDropId);
			oLink.id = "droplink-" + iDropId + "-" + iSubDropId;
			oLink.onmouseover = function() {
				vcc.nav.clearTimeouts(null, this.getAttribute("subdropid"));
				vcc.nav.aTimeout2[this.getAttribute("subdropid")] = setTimeout("vcc.nav.showSubDrop(" + this.getAttribute("dropid") + "," + this.getAttribute("subdropid") + ");",vcc.nav.iDelay);
			}
			oLink.onmouseout = function() {
				vcc.nav.aTimeout2[this.getAttribute("subdropid")] = setTimeout("vcc.nav.hideSubDrop(" + this.getAttribute("dropid") + "," + this.getAttribute("subdropid") + ");",vcc.nav.iDelay);
			}
			vcc.getObj("topdrops").appendChild(vcc.nav.createSubDropDown(oItem.aItems,iDropId,iSubDropId));
		}
	}
	if (vcc.bIsRtl && vcc.opera)
		oDiv.style.padding = "0 4px 3px 0";
	return oDiv;
}

vcc.nav.createSubDropDown = function(aItems,iDrop,iSubDrop) {
	var oDiv = document.createElement("div");
	with (oDiv) {
		id = "drop-" + iDrop + "-" + iSubDrop;
		className = "drop subdrop";
		setAttribute("dropid",iDrop);
		setAttribute("subdropid",iSubDrop);
		style.zIndex = 100;
	}
	oDiv.onmouseover = function() {
		vcc.nav.clearTimeouts(this.getAttribute("dropid"), this.getAttribute("subdropid"));
		vcc.getObj("droplink-" + this.getAttribute("dropid") + "-" + this.getAttribute("subdropid")).className = "active";
	}
	oDiv.onmouseout = function() {
		vcc.nav.aTimeout1[this.getAttribute("dropid")] = setTimeout("vcc.nav.hideDrop(" + this.getAttribute("dropid") + ");",vcc.nav.iDelay);
		vcc.nav.aTimeout2[this.getAttribute("subdropid")] = setTimeout("vcc.nav.hideSubDrop(" + this.getAttribute("dropid") + "," + this.getAttribute("subdropid") + ");",vcc.nav.iDelay);
	}			
	var sItems = "";
	for (var i = 0; i < aItems.length; i++) {
		var oItem = aItems[i];
		sItems += "<li><a href=\"" + oItem.url + "\"";
		if (oItem.active) sItems += " class=\"active\"";
		if (oItem.popupProps != "") sItems += " onclick=\"vcc.openWindow(this.href,'_blank','" + oItem.popupProps + "');return false;\"";
		sItems += ">" + oItem.title + "</a></li>";
	}
	oDiv.appendChild(document.createElement("ul")).innerHTML = sItems;
	if (vcc.bIsRtl && vcc.opera)
		oDiv.style.padding = "0 4px 3px 0";
	return oDiv;
}

vcc.nav.clearTimeouts = function(iTimeout1Id, iTimeout2Id) {
	if (iTimeout1Id != null && iTimeout1Id > 0) {
		if (vcc.nav.aTimeout1[iTimeout1Id])
			clearTimeout(vcc.nav.aTimeout1[iTimeout1Id]);
	}
	if (iTimeout2Id != null && iTimeout2Id > 0) {
		if (vcc.nav.aTimeout2[iTimeout2Id])
			clearTimeout(vcc.nav.aTimeout2[iTimeout2Id]);
	}
}

vcc.nav.showDrop = function(iDrop) {
	var oDrop = vcc.getObj("drop-" + iDrop);
	vcc.getObj("topbutton-" + iDrop).className = "active";
	oDrop.style.zIndex = 1000;
	vcc.setDisplay(oDrop,"block");
	vcc.nav.iActiveDrop = iDrop;
	if (vcc.opera || vcc.mac || vcc.safari) return;
	vcc.nav.createIframe(oDrop, false);
}

vcc.nav.hideDrop = function(iDropId) {
	if (vcc.nav.iActiveDrop == null) return;
	var oImages = vcc.nav.aTopImages[iDropId - 1];
	var oContainer = vcc.getObj("topbutton-" + iDropId);
	vcc.setDisplay(vcc.getObj("iframedrop"),"none");
	vcc.setDisplay("drop-" + iDropId,"none");
	oContainer.className = "";
	var aImages =  oContainer.getElementsByTagName("img");
	if (aImages.length > 0)
		aImages[0].src = (oContainer.getElementsByTagName("a")[0].className == "active") ? oImages.s : oImages.d;
}

vcc.nav.showSubDrop = function(iDrop,iSubDrop) {
	var oDrop = vcc.getObj("drop-" + iDrop + "-" + iSubDrop);
	var iTop = vcc.getY("droplink-" + iDrop + "-" + iSubDrop) - 3;
	var iLeft = vcc.bIsRtl ? vcc.getX("drop-" + iDrop) - (vcc.nav.iDropWidth * 2) + 177 : vcc.getX("drop-" + iDrop) + ((vcc.ie && vcc.version < 7) ? 175 : 171);
	vcc.moveTo(oDrop,iLeft,iTop);
	oDrop.style.zIndex = vcc.getObj("drop-" + iDrop).style.zIndex + 1;
	vcc.setDisplay(oDrop,"block");
	//Create and show sub iframe
	if (vcc.opera || vcc.mac || vcc.safari) return;
	vcc.nav.createIframe(oDrop, true);
}

vcc.nav.createIframe = function(oDrop, bSub) {
	var oIframe = bSub ? vcc.getObj("iframedropsub") : vcc.getObj("iframedrop");
	if (oIframe == null) {
		oIframe = document.createElement("iframe");
		oIframe.src = "/_layouts/VolvoCars.Web.Sites/empty.html";
		oIframe.id = bSub ? "iframedropsub" : "iframedrop";
		oIframe.frameborder = 0;
		vcc.nav.oPageDiv.appendChild(oIframe);
	}
	with (oIframe.style) {
		width = (vcc.ie7 || vcc.ie8) ? vcc.getW(oDrop) - 4 + "px" : vcc.getW(oDrop) + "px";
		height = (vcc.ie7) ? vcc.getH(oDrop) - 4 + "px" : vcc.getH(oDrop) + "px";
		left = oDrop.style.left;
		top = oDrop.style.top;
	}
	if (vcc.bIsRtl && (vcc.ie7 || !vcc.ie))
		oIframe.style.left = parseInt(oDrop.style.left) + 4 + "px";
	vcc.setDisplay(oIframe,"block");
}

vcc.nav.hideSubDrop = function(iDrop,iSubDrop) {
	var oDrop = vcc.getObj("drop-" + iDrop + "-" + iSubDrop);
	vcc.setDisplay(vcc.getObj("iframedropsub"),"none");
	vcc.setDisplay(oDrop,"none");
	if (vcc.getObj("droplink-" + iDrop + "-" + iSubDrop).getAttribute("active") + "" != "true")
		vcc.getObj("droplink-" + iDrop + "-" + iSubDrop).className = "";
}

vcc.nav.searchDealer = function(sSearchFieldId, sHiddenFieldId) {
	var oHidden = vcc.getObj(sHiddenFieldId);
	if (oHidden == null) return;
	var aConfigData = oHidden.value.split("|");
	var sUrl = aConfigData[0];
	var sSearchType = aConfigData[1];
	var sKeyword = vcc.getObj(sSearchFieldId).value;
	sKeyword = encodeURIComponent(sKeyword);
	var objDate = new Date();
	document.location.href = sUrl + ((sUrl.indexOf("?") > -1) ? "&" : "?") + sSearchType + "=" + sKeyword; // + "&id=" + objDate.getTime();
}

vcc.nav.searchDealerSubmit = function(event, sSearchFieldId, sHiddenFieldId) {
	var e = (window.event) ? window.event : event;
	if (e.keyCode == 13 || e.keyCode == 3) {
		vcc.nav.searchDealer(sSearchFieldId, sHiddenFieldId);
		return false;
	}
	return true;
}

vcc.nav.rightContent = function() {
	var oRightContent = document.getElementById("rightcontent");
	var oLeftContent = document.getElementById("leftcontent");
	if (!oRightContent || !oLeftContent) return;
	var aRightContentDivs = oRightContent.getElementsByTagName("div");
	if (aRightContentDivs.length == 0) {
		oRightContent.style.display = "none";
		oLeftContent.className += " wide";
	}
}
