if (window.addEventListener) { window.addEventListener("load", setup_sideBar, false); }
else if (window.attachEvent) { window.attachEvent("onload", setup_sideBar, false); }

var oSideBar= false;
var aSideBarLinks	= false;
var sideBarTimer = false;
var isSafari = (navigator.vendor && navigator.vendor.indexOf("pple") > -1) ? true : false;
var isIE	= (document.all) ? true : false;
var sideBarDatum = false;


function setup_sideBar() {
	oSideBar	= document.getElementById("sideBar");
	
	aSideBarLinks	= oSideBar.getElementsByTagName("a");
	
	for (var i=0; i<aSideBarLinks.length; i++) {
		aSideBarLinks[i].onclick	= toggleSideNavSub;
	}
	
	drawSideBarLine();
	
	setupShim();
	
	sideBarDatum = YAHOO.util.Dom.getXY(oSideBar);

}

/*-----------------------------------------------------------------------*/
function drawSideBarLine() {
	newEl		= document.createElement("div");
	oSideBarBox	= document.getElementById("sideBarBox");
	oSideBar	= document.getElementById("sideBar");
	newEl		= oSideBarBox.insertBefore(newEl, oSideBarBox.firstChild);
	newEl.id	= "sideBarLine";

	aPos0	= YAHOO.util.Dom.getXY(document.getElementById("pageBox"));
	aPos1	= YAHOO.util.Dom.getXY(oSideBarBox);

	switch(document.body.className) {
	case "industrial":
		targ	= document.getElementById("industrial");
		break;
	case "commercial":
		targ	= document.getElementById("commercial");
		break;
	case "land":
		targ	= document.getElementById("land");
		break;
	case "tenant":
		targ	= document.getElementById("tenant");
		break;
	default:
		return false;
		break;
	}
	
	aPos2	= YAHOO.util.Dom.getXY(targ);
	
	newEl.style.left	= aPos2[0] - aPos1[0] + "px";
	newEl.style.top		= aPos2[1] - aPos1[1] + targ.scrollHeight + "px";
	
	newEl.style.width		= "220px";
	newEl.style.height		= "1px";
	newEl.style.display		= "block";
}

/*-----------------------------------------------------------------------*/
function toggleSideNavSub(e) {
	if (!e) var e = window.event;
	var targ = (e.target) ? e.target : e.srcElement;
	
	//while (targ.tagName.toLowerCase() != "li") {
		//targ = targ.parentNode;
	//}
	
	closeAllSideBarSubNav();

	aLists	= targ.parentNode.getElementsByTagName("ul")
	if (aLists && aLists.length > 0) {
		oSubNav	= aLists[0];
		
		if (oSubNav.childNodes.length > 0) {
			if (oSubNav.style.display != "block") {
				oSubNav.style.display	= "block";
				oSubNav.onmouseout	= startSideBarTimer;
				oSubNav.onmouseover	= stopSideBarTimer;
				
				pos = YAHOO.util.Dom.getXY(targ);
				oSubNav.style.top	= pos[1] - sideBarDatum[1] + targ.scrollHeight + "px";
				
				targ.style.borderBottom = "1px solid #000000";
	
				ShimOn(oSubNav);
			} else {
				targ.style.borderBottom = "none";
				oSubNav.style.display = "none";
				oDiv.style.display = "none";
				
				ShimOff();
			}
		
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
	
}

function startSideBarTimer() {
	sideBarTimer = setTimeout(closeAllSideBarSubNav, 1000);
}

function stopSideBarTimer() {
	window.clearTimeout(sideBarTimer);
}

function closeAllSideBarSubNav() {
	stopSideBarTimer();
	for (var i=0; i<aSideBarLinks.length; i++) {
		aLists	= aSideBarLinks[i].parentNode.getElementsByTagName("ul");
		if (aLists && aLists.length > 0) {
			aSideBarLinks[i].style.border = "none";
			aLists[0].style.display = "none";
			ShimOff();
		}
	}
}

/*-----------------------------------------------------------------------*/
function findPos(obj, toWindow) {
	// http://www.quirksmode.org/
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			if (typeof(toWindow) == "undefined" && (obj.tagName.toLowerCase() == "body" || obj.id == "pageBox")) {}
			else {
				curleft += obj.offsetLeft - obj.scrollLeft;
				curtop += obj.offsetTop - obj.scrollTop;
			}
		}
	}
	
	oBody = document.getElementsByTagName("body")[0];
	curleft = curleft - oBody.offsetLeft;
	curtop	= curtop - oBody.offsetTop;
	
	return [curleft,curtop];
}

/*-----------------------------------------------------------------------*/
var aSelects = new Array();
var shimTimer = null;
var oShim = null;
var oNewShim = null;
var oShimObj = null;
var oCopy	= null;

function setupShim() {
	tempEl		= document.createElement("div");
	oShim		= document.body.appendChild(tempEl);
	oShim.id	= "shim";
}

function ShimOn(thisObj) {
	// IFrame hack for IE6 & windowed controls
	oNewShim = oShim.cloneNode(true);
	oShimObj = thisObj;

	oShimObj.parentNode.appendChild(oNewShim);
	
	adjustShim();
	//shimTimer = setInterval(adjustShim, 1500);

}

function adjustShim() {
	if (oNewShim) {
		//oShimObj.parentNode.style.zIndex = "200";
		oShimObj.style.zIndex = "5";
		aPos	= findPos(oShimObj, true);
		
		//--Position shim
		oNewShim.style.position		= "absolute";
		oNewShim.style.left			= oShimObj.offsetLeft + "px";//aPos[0] + 1 + "px";
		oNewShim.style.top			= oShimObj.offsetTop + "px";//aPos[1] + 1 + "px";
		oNewShim.style.width		= oShimObj.offsetWidth + "px";
		oNewShim.style.height		= oShimObj.offsetHeight + "px";
		oNewShim.style.display		= "block";
		oNewShim.style.zIndex		= oShimObj.style.zIndex - 1;
	}

}

function ShimOff() {
	if (oNewShim) {
		oNewShim.parentNode.removeChild(oNewShim);
		oNewShim = false;
		//clearInterval(shimTimer);
	}
}

