﻿// position = 0:bottom, 1:left, 2:top, 3:right
// align = 0:bottom, 1:left, 2:top, 3:right
function Dropdown(activator, dropdown, position, align, offsetX, offsetY, autoShow, onShow, onHide, delayTime, activeEvent, inactiveEvent, selectedClass)
{
    if(typeof(activator) == "string") activator = document.getElementById(activator);
    if(typeof(dropdown) == "string") dropdown = document.getElementById(dropdown);
    if(!activator) return;
    if(activator.dropdown) return;
    if(!dropdown) return;
    activator.dropdown = this;
    activator.selectedClass = selectedClass;
    activator.oldClass = activator.className;
    this.onShow = onShow;
    this.onHide = onHide;
	this.activator = activator;
	this.activator.isDropdownMenu = true;
	this.dropdown = dropdown;
	this.dropdown.activator = activator;
	this.position = position;
	this.align = align;
	this.offsetX = offsetX == null ? 0 : offsetX;
	this.offsetY = offsetY == null ? 0 : offsetY;
	this.delayTime = delayTime == null ? 250 : delayTime;
	if(activeEvent == null) activeEvent = "mouseover";
	if(inactiveEvent == null) inactiveEvent = "mouseout";
	
	registerEventHandler(this.activator, activeEvent, getInstanceDelegate(this, "show"));
	registerEventHandler(this.activator, inactiveEvent, getInstanceDelegate(this, "requestHide"));
	registerEventHandler(this.dropdown, "mouseover", getInstanceDelegate(this, "show"));
	registerEventHandler(this.dropdown, "mouseout",getInstanceDelegate (this, "requestHide"));

	this.dropdown.style.visibility = "hidden";
	this.dropdown.style.position = "absolute";
	this.dropdown.style.display = "block";
	
	this.reposition(null);

	if(autoShow) this.show();
}

function AddMenuItem(parent, text, type, isHTML, link, target, itemClass, popupClass, selectedClass, icon, popupHolder, direction, onclickHandler)
{
	if(typeof(parent) == "string") parent = document.getElementById(parent);
	if(!parent) return;
	if(parent.isMenuSeparator) return;
	if(typeof(popupHolder) == "string") popupHolder = document.getElementById(popupHolder);
	
	var item;
	switch(type)
	{
	    case 1:
	        item = document.createElement("DIV");
	        item.isMenuSeparator = true;
	        item.style.clear = "both";
	        var titleWidth = 0;
	        
	        if(text)
	        {
	            var title = document.createElement("DIV");
	            if(isHTML)
	                title.innerHTML = text;
	            else
	                (typeof(title.innerText) != "undefined" ? title.innerText = text : title.textContent = text);
	            title.className = popupClass;
	            popupHolder.appendChild(title);
	            if(icon)
                {
                    var imageIcon = document.createElement("IMG");
                    imageIcon.src = icon;
                    imageIcon.height = 16;
                    imageIcon.align = "absmiddle";
                    imageIcon.style.border = "none";
                    imageIcon.style.marginRight = "5px";
                    title.insertBefore(imageIcon, title.firstChild);
                }
	            titleWidth = title.offsetWidth;
	            item.appendChild(title);
	        }
	        var separator = document.createElement("DIV");
	        separator.className = itemClass;
	        if(titleWidth) separator.style.marginLeft = titleWidth + 5 + "px";
	        item.appendChild(separator);
	        break;
	    default:
	        item = document.createElement("A");
	        if(itemClass) item.className = itemClass;
	        if(link) item.href = link;
	        if(target) item.target = target;
	        if(onclickHandler) item.onclick = onclickHandler;
	        var inner = document.createElement("DIV");
	        item.isDropdownMenu = true;
	        (typeof(inner.innerText) != "undefined" ? inner.innerText = text : inner.textContent = text);
	        item.appendChild(inner);
	        item.inner = inner;
	        if(icon)
            {
                var imageIcon = document.createElement("IMG");
                imageIcon.src = icon;
                imageIcon.width = 16;
                imageIcon.height = 16;
                imageIcon.align = "absmiddle";
                imageIcon.style.border = "none";
                imageIcon.style.marginRight = "5px";
                item.inner.insertBefore(imageIcon, item.inner.firstChild);
            }
	}
    
	if(parent.isDropdownMenu)
	{
		if(parent.popupBox)
		{
			parent.popupBox.appendChild(item);
		}
		else
		{
		    if(!popupHolder) return;
			var popupBox = document.createElement("DIV");
			popupBox.style.display = "none";
			popupBox.style.position = "absolute";
			popupBox.className = popupClass;
			popupBox.appendChild(item);
			popupHolder.appendChild(popupBox);
			parent.popupBox = popupBox;
			parent.dropdown = new Dropdown(parent, popupBox, 3, 2, 0, 0, null, null, null, null, null, null, selectedClass);
			parent.inner.style.background = "transparent url(images/vistacontrols/VistaMenuArrow.gif) right center no-repeat";
			parent.inner.style.paddingRight = "10px";
		}
	}
	else
	{
		parent.appendChild(item);
	}
	
	return item;
}

function CreateDropdown(activator, dropdown, position, align, offsetX, offsetY, autoShow, onShow, onHide, delayTime, activeEvent, inactiveEvent)
{
    if(typeof(activator) == "string") activator = document.getElementById(activator);
    if(typeof(dropdown) == "string") dropdown = document.getElementById(dropdown);
    if(!activator) return;
    if(activator.dropdown) return;
    if(!dropdown) return;
    activator.dropdown = new Dropdown(activator, dropdown, position, align, offsetX, offsetY, autoShow, onShow, onHide, delayTime, inactiveEvent);
}

function CreateTooltip(value, css, isHTML)
{
    var div = document.createElement("DIV");
    div.style.visibility = "hidden";
    div.style.position = "absolute";
    if(isHTML)
    {
        div.innerHTML = value;
    }
    else
    {
        (typeof(div.textContent) != "undefined") ? div.textContent = value : div.innerText = value;
    }
    if(css) div.className = css;
    document.body.appendChild(div);
    return div;
}

Dropdown.prototype.show = function(e)
{
	clearTimeout(this.timer);
	this.reposition(e);
	this.dropdown.style.visibility = "visible";
	this.dropdown.style.zIndex = 1000;
	if(this.activator.parentNode && this.activator.parentNode.activator)
	{
		var activator = this.activator.parentNode.activator;
		activator.showing = true;
	}
	if(this.activator.selectedClass) this.activator.className = this.activator.selectedClass;
	if(this.onShow) this.onShow(e);
}

Dropdown.prototype.hide = function(e)
{
	this.dropdown.style.visibility = "hidden";
	this.dropdown.style.left = "0px";
	this.dropdown.style.top = "0px";
	if(this.activator.parentNode && this.activator.parentNode.activator)
	{
		var activator = this.activator.parentNode.activator;
		activator.showing = false;
	}
	if(this.activator.selectedClass) this.activator.className = this.activator.oldClass;
	if(this.onHide) this.onHide(e);
}

Dropdown.prototype.requestHide = function(e)
{
	if(this.activator.showing)
	{
		clearTimeout(this.timer);
		this.timer = setTimeout(getInstanceDelegate(this, "requestHide"), this.delayTime);
		return;
	}
	this.timer = setTimeout(getInstanceDelegate(this, "hide"), this.delayTime);
}

Dropdown.prototype.reposition = function(e)
{
	var offsetLeft = 0;
	var offsetTop = 0;
	var offsetElement = this.activator;
	
	while (offsetElement)
	{
		offsetLeft += offsetElement.offsetLeft;
		offsetTop += offsetElement.offsetTop;
		offsetElement = offsetElement.offsetParent;
	}
	switch(this.position)
	{
	    // bottom
	    case 0:
	        break;
	    // left
	    case 1:
	        offsetLeft -= this.dropdown.offsetWidth;
	        break;
	    // top
	    case 2:
	        offsetTop -= this.activator.offsetHeight + this.dropdown.offsetHeight;
	        break;
	    // right
	    case 3:
	        offsetLeft += this.activator.offsetWidth;
	        break;
	}
	switch(this.align)
	{
	    // bottom
	    case 0:
	        offsetTop -= this.dropdown.offsetHeight;
	        break;
	    // left
	    case 1:
	        break;
	    // top
	    case 2:
	        offsetTop -= this.activator.offsetHeight;
	        break;
	    // right
	    case 3:
	        offsetLeft -= this.dropdown.offsetWidth - this.activator.offsetWidth;
	        break;
	}
	offsetLeft += this.offsetX;
	offsetTop += this.offsetY;
	this.dropdown.style.left = offsetLeft + "px";
	this.dropdown.style.top = (offsetTop + this.activator.offsetHeight) + "px";
}

function registerEventHandler (element, event, handler)
{
	if (element.attachEvent)
		element.attachEvent("on" + event, handler);
	else if (element.addEventListener)
		element.addEventListener(event, handler, false);
	else
		element[event] = handler;
}

function getInstanceDelegate (obj, methodName)
{
	return function(e)
	{
		e = e || window.event;
		return obj[methodName](e);
	};
}