﻿var ContextMenu = function(parent)
{
    ///<summary>Creates a new ContextMenu</summary>
        
    this.parent = parent;
    this.visible = false;
    this.left = 0;
    this.top = 0;
    this.items = new Array();
    this.handle = document.createElement("div");
    this.handle.style.position = "absolute";
    this.handle.style.border = "1px ridge black";
    this.handle.style.visibility = "hidden";
    this.handle.style.display = "none";
    this.handle.style.background = "#ffffff";//"url(images/menubg.png)";
    this.handle.style.color = "#000000";
    this.handle.style.borderRadius = "10px";
    this.parent.appendChild(this.handle);
}


ContextMenu.prototype.addItem = function(menuItem)
{
    this.items[this.items.length] = menuItem;
    this.handle.appendChild(menuItem.handle);
    
    var maxWidth = 0;
    for(i=0;i<this.items.length;i++)
    {
        var divs = this.items[i].handle.getElementsByTagName("div");
        //alert(divs[2].offsetWidth);
        //var width = divs[2].offsetWidth;
    }
}



ContextMenu.prototype.removeItem = function(index)
{
    
}

ContextMenu.prototype.setStyle = function(styleObject)
{
    this.handle.style = styleObject;
}

ContextMenu.prototype.show = function(x,y)
{    
    ///<summary>Creates a new ContextMenu</summary>
    ///<param name="x">The left of the parent where this component should be</param>
    ///<param name="y">The top left corner of the parent where this component should be</param>
    this.handle.style.top = y+"px";
    this.handle.style.left = x+"px";
    this.handle.style.visibility = "visible";
    this.handle.style.display = "block";
}

ContextMenu.prototype.hide = function()
{
    
}

var MenuItem = function(text,onclick,icon,shortcut)
{
    ///<summary></summary>
    ///<param name=""></param>
    this.enabled = true;
    this.text = text;
    this.iconUrl = icon==null?"":icon
    this.shortcut = shortcut==null?"":shortcut;
    this.separator = false;
    this.visible = true;
    this.items = new Array();
    this.handle = document.createElement("div");
    this.handle.onmouseup = onclick;
    this.handle.style.padding = "1px";
    this.handle.style.margin = "1px";
    
    var styleFloat = typeof(ActiveXObject)=="undefined"?"cssFloat":"styleFloat";
    
    var left_border = document.createElement("div");
    left_border.style.lineHeight = "30px";
    left_border.style[styleFloat] = "left";
    left_border.style.width = "12px";
    left_border.innerHTML = "&nbsp;";
    var icon_part = document.createElement("div");
    icon_part.style[styleFloat] = "left";
    icon_part.style.lineHeight = "30px";
    icon_part.style.width = "50px";
    icon_part.innerHTML = "&nbsp;";
    var caption_part = document.createElement("div");
    caption_part.style[styleFloat] = "left";
    caption_part.style.lineHeight = "30px";
    caption_part.style.width = "auto";
    caption_part.innerHTML = text;
    var shortcut_part = document.createElement("div");
    var right_border = document.createElement("div");
    right_border.style[styleFloat] = "left";
    right_border.innerHTML = "&nbsp;";
    right_border.style.lineHeight = "30px";
    right_border.style.width = "11px";
    
    var separator = document.createElement("div");
    separator.style.clear = "both";
    
    this.handle.appendChild(left_border);
    this.handle.appendChild(icon_part);
    this.handle.appendChild(caption_part);
    this.handle.appendChild(right_border);
    this.handle.appendChild(separator);
    
    this.handle.addEventListener("mouseenter", function()
    {
		alert("test");
        left_border.style.background = "url(images/menu_left_border.png)";
        icon_part.style.background = "url(images/menu_background.png)";
        caption_part.style.background = "url(images/menu_background.png)";
        right_border.style.background = "url(images/menu_right_border.png)";
    },false);
    this.handle.addEventListener("mouseleave",function()
    {
        left_border.style.background = "none";
        icon_part.style.background = "none";
        caption_part.style.background = "none";
        right_border.style.background = "none";
    },false);
}

