/************* Remember to include dhtmlDropdown.css ***************

	Sample Implementation Code (Change uniqueName to something unique):
	
		<div class="dhtmlDropdownContainer">
			<div class="triggerBox" onclick="showDropdown('uniqueName', this); return false;">
				<span id="uniqueName_triggerText" class="triggerBoxText">Loading...</span><img src="/common/images/dropdown-button.gif" width="19" height="20" alt="" border="0" class="triggerBoxImg" id="uniqueNameTriggerImg"><br>
			</div>
			<script language="JavaScript" type="text/javascript">
			<!--
			
				uniqueNameDropdown = new dhtmlDropdown("uniqueName", "dhtmlDropdown");
			
				uniqueNameDropdown.addScriptItem("Do a script Test", "alert('hi there')");
				uniqueNameDropdown.addLinkItem("Do a link Test", "http://www.google.com", "_blank");
				uniqueNameDropdown.addTextItem("I'm just a Label", "someCssClass");
			
				uniqueNameDropdown.draw();
				
			//-->
			</script>
		</div>
	

************* Remember to include dhtmlDropdown.css ****************/

TYPE_LINK = 1;
TYPE_TEXT = 2;
TYPE_SCRIPT = 3;

function dhtmlDropdown(name, cssClass) {
	var tmpObj = new Object();

	tmpObj.name = name;
	tmpObj.cssClass = cssClass;
	tmpObj.items = new Array();
	tmpObj.addLinkItem = objAddLinkItem;
	tmpObj.addTextItem = objAddTextItem;
	tmpObj.addScriptItem = objAddScriptItem;
	tmpObj.draw = objDraw;
	tmpObj.separator = "<div class=\"hr\"><img src=\"/common/images/spacer.gif\" width=\"1\" height=\"1\" alt=\"\" border=\"0\"></div>";
	
	return tmpObj;
}

function objAddLinkItem(linkText, linkHref, linkTarget) {
	/*
		set arguments[3] to override the default class name
	*/

	var tmpItem = new Object();
	tmpItem.type = TYPE_LINK;
	tmpItem.name = linkText;
	tmpItem.value = linkHref;
	tmpItem.target = linkTarget;
	tmpItem.cssClass = this.cssClass + "Item";
	tmpItem.sep = true;
	if (arguments.length > 3) {
		tmpItem.cssClass = arguments[3];
		tmpItem.sep = false;
	}
	this.items[this.items.length] = tmpItem;
}

function objAddTextItem(itemText, itemClass) {
	var tmpItem = new Object();
	tmpItem.type = TYPE_TEXT;
	tmpItem.name = itemText;
	tmpItem.value = "";
	tmpItem.cssClass = itemClass;
	tmpItem.sep = false;
	this.items[this.items.length] = tmpItem;
}

function objAddScriptItem(itemText, itemScript) {
	/*
		set arguments[2] to override the default class name
	*/

	var tmpItem = new Object();
	tmpItem.type = TYPE_SCRIPT;
	tmpItem.name = itemText;
	tmpItem.value = itemScript;
	tmpItem.cssClass = this.cssClass + "Item";
	tmpItem.sep = true;
	if (arguments.length > 2) {
		tmpItem.cssClass = arguments[2];
	}
	this.items[this.items.length] = tmpItem;
}

function objDraw(element) {
	var iHtml = "";
	if (isIE55) {
		iHtml += "<iframe id=\"" + this.name + "Iframe\" class=\"" + this.cssClass + "Iframe\" frameborder=\"0\" src=\"javascript: void(0);\"></iframe>";
	}
	iHtml += "<div id=\"" + this.name + "\" class=\"" + this.cssClass + "\"><div class=\"" + this.cssClass + "ScrollBox\" id=\"" + this.name + "ScrollBox\">";
	for (var i = 0; i < this.items.length; i++) {
		switch (this.items[i].type) {
			case TYPE_LINK:
				iHtml += "<a href=\"" + this.items[i].value + "\" onclick=\"window.open('" + this.items[i].value + "', '" + this.items[i].target + "'); return false;\" class=\"" + this.items[i].cssClass + "\">" + this.items[i].name + "</a>";
				break;
			case TYPE_TEXT:
				iHtml += "<span class=\"" + this.items[i].cssClass + "\">" + this.items[i].name + "</span>";
				break;
			case TYPE_SCRIPT:
				iHtml += "<a href=\"\" onmousedown=\"changeThumbText('" + this.name + "', '" + this.items[i].name + "'); hideLayer('hideDropdown'); hideCurrentDropdown(); eval(" + this.items[i].value + "); return false;\" class=\"" + this.items[i].cssClass + "\">" + this.items[i].name + "</a>";
				break;
		}
		if (this.separator != "" && this.items[i].sep && i < (this.items.length - 1)) {
			iHtml += this.separator;
		}
	}
	iHtml += "</div></div>";
	
	element.innerHTML = iHtml;
}

function changeThumbText(id, text) {
	//document.getElementById(id + "_triggerText").innerHTML = text;
}


function showDropdown(dropdownId) {
	if (pageLoaded && !busy) {
		hideCurrentDropdown();
		currentDropdown = dropdownId;
		
		findPosition(dropdownId);
		showLayer(dropdownId);
		if (isIE55) {
			setIframe(dropdownId);
			showLayer(dropdownId + "Iframe");
		}
		
		resizeHideDropdown();
		showLayer("hideDropdown");
	}
}

function showDynamicDropdown(url, dropdownId) {
	http("GET", url + "?dropdownId=" + dropdownId, showDynamic_callback);
}

function showDynamic_callback(obj) {
	document.getElementById(obj.dropdownid + "ScrollBox").innerHTML = obj.content;
	showDropdown(obj.dropdownid);
}



function hideCurrentDropdown() {
	if (currentDropdown != "" && document.getElementById(currentDropdown)) {
		/*
			Safari crashes when you try to rescroll an invisible element.
			Scrolling while visible has small visual impact on everyone else.
			Instead, i only scroll visible on safari and scroll invisible for
			everyone else.
		*/
		if (navigator.userAgent.indexOf("Safari") == -1) {
			hideLayer(currentDropdown);
			document.getElementById(currentDropdown + "ScrollBox").scrollTop = 0;
		} else {
			document.getElementById(currentDropdown + "ScrollBox").scrollTop = 0;
			hideLayer(currentDropdown);
		}

		if (isIE55) {
			hideLayer(currentDropdown + "Iframe");
		}
	}
}

function findPosition(dropdownId) {
	obj = document.getElementById(dropdownId);

	triggerTop = document.getElementById(dropdownId + "TriggerImg").offsetTop;
	triggerHeight = document.getElementById(dropdownId + "TriggerImg").offsetHeight;

	obj.style.top = triggerTop + triggerHeight;
	obj.style.display = "block";

	dropdownHeight = obj.offsetHeight;
	dropdownTop = obj.offsetTop;
	windowHeight = document.body.clientHeight;
	if (window.innerHeight) {
		windowHeight = window.innerHeight;
	}
	containerHeight = getSize(document.getElementById("contentArea")).height;
	scrollPosition = document.body.scrollTop;

	if (triggerTop > dropdownHeight) {
		if (triggerTop - dropdownHeight > scrollPosition && (dropdownHeight + dropdownTop - scrollPosition > windowHeight || dropdownHeight + dropdownTop - scrollPosition > containerHeight)) {
			obj.style.top = (triggerTop - dropdownHeight) + "px";
		}
	}

	
}

function resizeHideDropdown() {
	if (document.body.scrollHeight > document.body.offsetHeight) {
		document.getElementById("hideDropdown").style.height = document.body.scrollHeight;
	} else {
		document.getElementById("hideDropdown").style.height = document.body.offsetHeight * .99;
	}
}

rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
isIE55 = (rslt != null && Number(rslt[1]) == 5.5);

function showLayer(layerId) {
	if (!document.layers && pageLoaded && document.getElementById(layerId)) {
		document.getElementById(layerId).style.visibility = "visible";
		document.getElementById(layerId).style.display = "block";
	}
}

function hideLayer(layerId) {
	if (!document.layers && pageLoaded && document.getElementById(layerId)) {
		document.getElementById(layerId).style.visibility = "hidden";
		document.getElementById(layerId).style.display = "none";
	}
}


function setIframe(layerName) {
	if (isIE55 && document.getElementById(layerName + "IFrame")) {
		obj = document.getElementById(layerName);
		ifrm = document.getElementById(layerName + "IFrame");
		
		ifrm.style.top = obj.offsetTop;
		ifrm.style.left = obj.offsetLeft;
		ifrm.style.height = obj.offsetHeight;
		ifrm.style.width = obj.offsetWidth;
	}
}

currentDropdown = "";
