﻿/**
 * @author hoorfar
 */
/******************************************/
function OnMouseOver(item)
{item.Current.className = item.Current.className + "_Over";}
function OnMouseOut(item){item.Current.className = item.Current.className.replace("_Over", "");}
function UserWriteItemComplete(){
    var tr = document.createElement("tr");
    var td = document.createElement("td");
    /*var img = document.createElement("img");
    img.src = "Files/Ar/Images/MenuDotted.gif";
	img.style.verticalAlign = "top";
    td.appendChild(img);*/
    tr.appendChild(td);
	td.className = "MenuSplit"
    return tr;
}
function UserMakeItem(item){
    var tbl = document.createElement("table");
    var tb = document.createElement("tbody");
    var tr = document.createElement("tr");
    
    tbl.cellPadding = 0;
    tbl.cellSpacing = 0;
    tb.appendChild(tr);
    tbl.appendChild(tb);
    
    var td = document.createElement("td");
    var img = document.createElement("img");
    img.src = item.Icon;

	img.style.verticalAlign = "middle";
    td.appendChild(img);
	td.style.width = "2px";
    tr.appendChild(td);
    
    td = document.createElement("td");
    td.innerHTML = item.Title;
    td.style.textAlign = "right";
    tr.appendChild(td);    
    return tbl;
}

/********************************************************************************************/
/* Static Method */
window.AnimateMan.TaskList = new Array();
function UnRegisterAnimateMan(id){window.AnimateMan.TaskList[id] = null;}
function RegisterAnimateMan(m){
    var i, lst;
    lst = window.AnimateMan.TaskList;
    for (i = 0; i < lst.length; i++) {
        if (lst[i] == null) {
            lst[i] = m;
            return i;
        }
    }
    lst.push(m);
    return i;
}
function AnimateManCallEvent(id){
    var lst = window.AnimateMan.TaskList;
    if (lst.length <= id)
        return;
    var ee = lst[id];
    if (ee) 
        ee.OnTick();
}
function AnimateMan(element, addStep, endValue, time, currentHeight){
    this._Id = RegisterAnimateMan(this);
    this.FastCall = "AnimateManCallEvent(" + this._Id + ");"
    this.Element = element;
    this.Step = addStep;
    this.Value = endValue;
	
	this.Element.style.overflow = 'hidden';
    if (currentHeight != null)this.Element.style.height = currentHeight+"px";
	
	if(endValue == null){
		if(this.Step > 0){
			this.Value = parseInt(this.Element.scrollHeight);
		}else if(this.Step < 0){
			this.Value = 1;
		}else alert("error");
	}else this.Value = parseInt(endValue);
	
    this.Time = time;
    this.OnComplete = null;    
    this.OnTick = (this.Step > 0) ? this._Loop1 : this._Loop2;
    this.Working = false;
}
AnimateMan.prototype = {
    _Loop1: function(){
        var cHeight = parseInt(this.Element.clientHeight);
        cHeight += this.Step;
		if (this.Value) {
			if (cHeight > this.Value) {
				cHeight = this.Value;
				this.Working = false;
			}
		}
		else{
			if (cHeight > parseInt(this.Element.scrollHeight)) {
				cHeight = this.Value;
				this.Working = false;
			}
		}
        this.Element.style.height = cHeight + "px";
        if (this.Working) 
            setTimeout(this.FastCall, this.Time)
        else 
            if (this.OnComplete) 
                this.OnComplete(this);
    },
    _Loop2: function(){
        var cHeight = parseInt(this.Element.clientHeight);
        cHeight += this.Step;
		if (this.Vlaue) {
			if (cHeight < this.Value) {
				cHeight = this.Value;
				this.Working = false;
			}
		}
		else {
			if (cHeight <= 0) {
				cHeight = 1;
				this.Working = false;
			}
		}
        this.Element.style.height = cHeight + "px";
        if (this.Working) 
            setTimeout(this.FastCall, this.Time)
        else 
            if (this.OnComplete) 
                this.OnComplete(this);
    },
    Start: function(){
        this.Working = true;
        this.Element.style.display = "";
        this.OnTick();
    },
    Stop: function(){
        this.Working = false;
    },
    Close: function(){
		this.Element.style.overflow = "";
		if(this.Step < 0)this.Element.style.display = "none";
        UnRegisterAnimateMan(this._Id);
    },
    ResetSet: function(step, endValue){
		if(endValue == null){
			if(this.Step > 0){
				this.Value = parseInt(parseInt(element.scrollHeight));
			}else if(this.Step < 0){
				this.Value = 0;
			}else alert("error");
		}
        this.Step = step;
        this.OnTick = (this.Step > 0) ? this._Loop1 : this._Loop2;
    }
};
/********************************************************************************************/
function ScrollMenuItem(idp, id, t, i, l){
    this.ParentId = idp;
    this.Id = id;
    this.Title = t;
    this.Link = l;
    this.Icon = i;
    this.Current = null;
    this.Childs = null;
    this.ChildHolder = null;
    this.OpenItem = null;
    this.AnimatHandle = null;
}

function ScrollMenuOnAnimateComplete(a){
    a.Element.CurrentItem.AnimatHandle = null;
    a.Close();
    var man = a.Element.MenuMan;
    if (a.Step < 0) {
        a.Element.style.display = "none";
        if (man.OpenRoot) {
            if (man.OpenRoot.Id == a.Element.CurrentItem.Id) 
                man.OpenRoot = null;
        }
    }
    else a.Element.style.height = "";
}

function ScrollMenu(pid, cssplac, cssitem, itemh){
    this.Stack = new Array();
    this.ParentId = pid;
    this.Items = new Array();
    this.Roots = new Array();
    this.OpenRoot = null;
    this.CssItem = cssitem;
    this.CssPlace = cssplac;
    
	this.OnPreWriteItem = null;
    this.OnWriteItem = UserMakeItem;
	this.OnWriteItemComplete = UserWriteItemComplete;
    this.OnMouseOutItem = OnMouseOut;
    this.OnMouseOverItem = OnMouseOver;
    this.OnMouseClickItem = null;
}

ScrollMenu.prototype = {
    _Find: function(id){
        var i, t;
        for (i = 0; i < this.Items.length; i++) {
            t = this.Items[i];
            if (t.Id == id) 
                return t;
        }
        return null;
    },
    
    _MakeChilds: function(place, lst, l){
        var tbl = document.createElement("table");			
		var tb = document.createElement("tbody");
		tbl.style.width = '100%'
	    tbl.cellPadding = 0;
	    tbl.cellSpacing = 0;
	    tbl.appendChild(tb);
        var c;
        var cssName = this.CssItem + l;
        for (var i = 0; i < lst.length; i++) {
            c = lst[i];
			if(this.OnPreWriteItem)tb.appendChild(this.OnPreWriteItem());
			var tr = document.createElement("tr"); tb.appendChild(tr);
			var td = document.createElement("td"); tr.appendChild(td);
            td.appendChild(this._MakeItem(cssName,c));
			if(this.OnWriteItemComplete)tb.appendChild(this.OnWriteItemComplete());
            if (c.Childs) {
                var cHolder = document.createElement("div");
                cHolder.id = this.ControlId + "_H_" + c.Id;
                cHolder.className = this.CssPlace + l;
                this._MakeChilds(cHolder, c.Childs, l + 1);
                c.ChildHolder = cHolder;
                cHolder.CurrentItem = c;
                cHolder.style.display = "none";
                cHolder.style.overflow = "hidden";
                cHolder.style.height = "1px";
                cHolder.MenuMan = this;
                td.appendChild(cHolder);
            }
        }
		place.appendChild(tbl);
    },
    
    _Show: function(item){
        var t = item.AnimatHandle;
        if (t) {
            t.Stop();
            t.ResetSet(2,null);
            t.Start();
        }
        else {
            item.AnimatHandle = new AnimateMan(item.ChildHolder, 2, null, 2, 1);
            item.AnimatHandle.OnComplete = ScrollMenuOnAnimateComplete;
            item.AnimatHandle.Start();
        }
    },
    
    _Close: function(item){
        var t = item.AnimatHandle;
        if (t) {
            t.Stop();
            t.ResetSet(-2, 1);
            t.Start();
        }
        else {
            item.AnimatHandle = new AnimateMan(item.ChildHolder, -2, null, 2, null);
            item.AnimatHandle.OnComplete = ScrollMenuOnAnimateComplete;
            item.AnimatHandle.Start();
        }
    },
    
    _ShowHide: function(item){
        var Current;
        while (this.Stack.length > 0) {
            Current = this.Stack.pop();
            if (Current.Id == item.ParentId) {
                if(item.ParentId!=this.ParentId){
					this.Stack.push(Current);
					break;
				}else this._Close(item);
            }
            this._Close(Current);
            if (Current.Id == item.Id) 
                return;
        }
        this.Stack.push(item);
        this._Show(item);
    },
    
    _MakeItem: function(css, item){
        var ediv;
        if (this.OnWriteItem != null)ediv = this.OnWriteItem(item);
        else {ediv = document.createElement("div");ediv.innerHTML = item.Title;}
        if (!ediv)alert("Error On : _MakeItem");
        
        ediv.className = css;
        ediv.MenuMan = this;
        ediv.CurrentItem = item;
        item.Current = ediv;
		
        ediv.onmouseover = function(){
            if (this.MenuMan) 
                this.MenuMan.doEvent(1, this.CurrentItem);
        };
        ediv.onmouseout = function(){
            if (this.MenuMan) 
                this.MenuMan.doEvent(2, this.CurrentItem);
        };
        ediv.onclick = function(){
            if (this.MenuMan) 
                this.MenuMan.doEvent(3, this.CurrentItem);
        };
        return ediv;
    },

    doEvent: function(type, item){
        var s = false;
        if (type == 1) {
            if (this.OnMouseOverItem) 
                s = this.OnMouseOverItem(item);
        }else if (type == 2) {
                if (this.OnMouseOutItem) 
                    s = this.OnMouseOutItem(item);
       }else if (type == 3) {
             if (this.OnMouseClickItem)s = this.OnMouseClickItem(item);
             if (!s) {
                if (item.Childs)this._ShowHide(item);
                else if (item.Link && item.Link != '') 
                        window.location = item.Link;
             }
       }else alert("Unknown Event");
    },
    
    addItem: function(item){
        this.Items.push(item);
        var cc;
        if (item.ParentId == this.ParentId)this.Roots.push(item);
        else if ((cc = this._Find(item.ParentId)) != null){
            if (cc.Childs == null)cc.Childs = new Array();
            cc.Childs.push(item);
        }
    },
    
    Make: function(id){
        var PlaceM = document.getElementById(id);
        if (PlaceM)this._MakeChilds(PlaceM, this.Roots, 1);
    }
};
