﻿GNgui.ContextMenu2 = Class.create();
GNgui.ContextMenu2.prototype = 
{
    initialize : function (Id , Parent, Width)
    {
        if (Id == null) {alert('PleaseWait: Id not specified' );}
		if (Parent == null) {alert('PleaseWait: Parent not specified' );}
		if (Width == null) {alert('PleaseWait: Width not specified' );}

        this.Obj = $ce('div');
        this.Obj.Persistent = true; 
        this.Obj.setAttribute('id', Id);
        this.Obj.style.position = 'absolute';
        this.Obj.style.visibility = 'hidden';
        this.Obj.style.width = Width;
        $ac(Parent, this.Obj);

        this.Items = new Array();
        
        Event.observe ( this.Obj, 'mousedown' , Catch.bindAsEventListener() );
        Event.observe ( this.Obj, 'mouseup' , Catch.bindAsEventListener() );
    },
    
    addItem : function (Text, Icon, PositionIndex, Expandable, Accessibility, ClickEventFunc)
    {
        if (Text == null) {	alert('PleaseWait: Text not specified' );}
        if (Expandable == null) Expandable = false;
        if (Accessibility == null) Accessibility = true;
        
        if (this.Items[PositionIndex] != null && PositionIndex > 0 && PositionIndex < this.Items.length)
        
            for (var i = this.Items.length; i >= PositionIndex; i--)
                this.Items[i] = this.Items[i -1]
        else
            PositionIndex = this.Items.length;
        
                
        var tmpItem = new Object();
            tmpItem.Text = Text;
            tmpItem.Icon = Icon;
            tmpItem.Expandable = Expandable;
            tmpItem.ClickEventFunc = ClickEventFunc;
            tmpItem.Accessibility = Accessibility;

        this.Items[PositionIndex] = tmpItem;
        
        
        for (var i= this.Obj.childNodes.length -1; i >= 0; i--) 
            this.Obj.removeChild(this.Obj.childNodes.item(i));
        l=this.Items.length;
        for (var i = 0; i < l; i++)
        {
            this.Items[i].Obj = new GNgui.Button (this.GetID() + i,
                                                       this.Obj,
                                                       i,
                                                       parseInt(this.Obj.style.width),
                                                       this.Items[i].Text,
                                                       this.Items[i].Icon,
                                                       this.Items[i].Expandable,
                                                       this.Items[i].Accessibility,
                                                       this.Items[i].ClickEventFunc,
                                                       'Left');
        }
    },
    
    Show : function (x, y, OpenUpward)
    {
        this.Obj.style.left = x;
        this.Obj.style.top = y;
        this.Obj.style.visibility = 'inherit';

		l=this.Items.length;
        for (var i = 0; i < l; i++)
        {
            this.Items[i].Obj.SetTop (0);
            this.Items[i].Obj.SetZOrder ( OpenUpward ? i : this.Items.length - i );
        }
        am_P = 0;
        am_I = OpenUpward ? this.Items.length - 2 : 1;
        setTimeout('AnimatingMenu(' + this.Obj.id + ', ' + (this.Items.length - 1) + ',' + OpenUpward + ')', 0)
    },
    
    Hide : function () {this.Obj.style.visibility = 'hidden'; },
    
    GetID : function () {return this.Obj.id;}
}

var am_P;
var am_I;
var am_Obj;
function AnimatingMenu (ItemId, Length, OpenUpward)
{
    if (ItemId.Obj.id != null)
    {   ItemId = ItemId.Obj.id;
        am_Obj = document.getElementById(ItemId + am_I);}

    if (OpenUpward)
    {
        am_P += 3;
        am_Obj.style.top = -am_P;
        
        if (am_P >= (Length - am_I) * 24)
        {
            am_Obj.style.top = (am_I - Length) * 24;
            am_I--;
            am_Obj = document.getElementById( ItemId + am_I );
        }
        if (am_I >= 0) setTimeout('AnimatingMenu(' + ItemId + ', ' + Length + ', ' + OpenUpward + ')', 1)
    }
    else
    {
        am_P += 3;
        am_Obj.style.top = am_P;
        
        if (am_P >= am_I * 24)
        {
            am_Obj.style.top = am_I * 24;
            am_I++;
            am_Obj = document.getElementById( ItemId + am_I );
        }
        if (am_I <= Length) setTimeout('AnimatingMenu(' + ItemId + ', ' + Length + ', ' + OpenUpward + ')', 1)
    }
}