
function CBoardMenuItem(controllerStr, boardStr, direction, step, delay)
{
    this.controller = document.getElementById(controllerStr);
    this.board = document.getElementById(boardStr);
    
    this.direction = direction; 
    
    this.board.style.display = "block";
    this.board.style.overflow = "hidden";
    this.board.style.width = this.board.offsetWidth + "px";
    this.board.style.height = this.board.offsetHeight + "px";
    this.maxWidth = this.board.offsetWidth;
    this.maxHeight = this.board.offsetHeight;
    this.status = "expanded";  
    this.timer = null;  
    
    this.step = step;  
    this.delay = delay;  
    
    this.ExpandItem = CBoardMenuItem_ExpandItem;
    this.ExpandItemV = CBoardMenuItem_ExpandItemV;
    this.ExpandItemH = CBoardMenuItem_ExpandItemH;
    this.CollapseItem = CBoardMenuItem_CollapseItem;
    this.CollapseItemV = CBoardMenuItem_CollapseItemV;
    this.CollapseItemH = CBoardMenuItem_CollapseItemH;
}

 
function CBoardMenuItem_ExpandItem(recallStr)
{
    if (this.direction == "h")
    {
        this.ExpandItemH(recallStr);
    }
    else
    {
        this.ExpandItemV(recallStr);
    }
}


 
function CBoardMenuItem_ExpandItemV(recallStr)
{
    clearTimeout(this.timer);
    
    if ((this.maxHeight - parseInt(this.board.style.height)) > this.step)
    {
        this.board.scrollTop -= this.step;
        this.board.style.height = (parseInt(this.board.style.height) + this.step) + "px";
        this.board.style.display = "block";
        this.status = "expanding";
        this.timer = setTimeout(recallStr+".ExpandItem('"+recallStr+"')", this.delay);
    }
    else
    {
        this.board.scrollTop = 0;
        this.board.style.height = this.maxHeight + "px";
        this.board.style.display = "block";
        this.status = "expanded";
    }
}


 
function CBoardMenuItem_ExpandItemH(recallStr)
{
    clearTimeout(this.timer);
    
    if ((this.maxWidth - parseInt(this.board.style.width)) > this.step)
    {
        this.board.scrollLeft -= this.step;
        this.board.style.width = (parseInt(this.board.style.width) + this.step) + "px";
        this.board.style.display = "block";
        this.status = "expanding";
        this.timer = setTimeout(recallStr+".ExpandItem('"+recallStr+"')", this.delay);
    }
    else
    {
        this.board.scrollLeft = 0;
        this.board.style.width = this.maxWidth + "px";
        this.board.style.display = "block";
        this.status = "expanded";
    }
}

 
function CBoardMenuItem_CollapseItem(recallStr)
{
    if (this.direction == "h")
    {
        this.CollapseItemH(recallStr);
    }
    else
    {
        this.CollapseItemV(recallStr);
    }
}


 
function CBoardMenuItem_CollapseItemV(recallStr)
{
    clearTimeout(this.timer);
    
    if (parseInt(this.board.style.height) > this.step)  
    {
        this.board.scrollTop += this.step;
        this.board.style.height = (parseInt(this.board.style.height)-this.step) + "px";
        this.board.style.display = "block";
        this.status = "collapsing";
        this.timer = setTimeout(recallStr+".CollapseItem('"+recallStr+"')", this.delay);
    }
    else
    {
        this.board.style.display = "none"; 
        this.board.scrollTop = this.maxHeight;
        this.board.style.height = "0px";
        this.status = "collapsed";
    }
}


 
function CBoardMenuItem_CollapseItemH(recallStr)
{
    clearTimeout(this.timer);
    
    if (parseInt(this.board.style.width) > this.step)
    {
        this.board.scrollLeft += this.step;
        this.board.style.width = (parseInt(this.board.style.width)-this.step) + "px";
        this.board.style.display = "block";
        this.status = "collapsing";
        this.timer = setTimeout(recallStr+".CollapseItem('"+recallStr+"')", this.delay);
    }
    else
    {
        this.board.style.display = "none";  
        this.board.scrollLeft = this.maxWidth;
        this.board.style.width = "0px";
        this.status = "collapsed";
    }
}

  
function CBoardMenu(objStr)
{
    this.objStr = objStr;
    this.standalone = (arguments.length<2)?false:arguments[1];
    this.direction = (arguments.length<3)?20:arguments[2];
    this.step = (arguments.length<4)?20:arguments[3];
    this.delay = (arguments.length<5)?10:arguments[4];
    
    this.itemArr = new Array();
    
    this.BindItem = CBoardMenu_BindItem;
    this.ActiveItem = CBoardMenu_ActiveItem;
}

 
function CBoardMenu_BindItem(controllerStr, boardStr)
{
    this.itemArr[this.itemArr.length] = new CBoardMenuItem(controllerStr, boardStr, this.direction, this.step, this.delay);
    
    
    var newEventStr = this.objStr+".ActiveItem(" + (this.itemArr.length-1) + ");";
    if (window.attachEvent)
    {
        //IE
        this.itemArr[this.itemArr.length-1].controller.attachEvent("onclick", function (){eval(newEventStr);});
    }
    else
    {
        
        var oldEventStr = this.itemArr[this.itemArr.length-1].controller.getAttribute("onclick");
        if (oldEventStr)
        {
            this.itemArr[this.itemArr.length-1].controller.setAttribute("onclick", oldEventStr+";"+newEventStr);
        }
        else
        {
            this.itemArr[this.itemArr.length-1].controller.setAttribute("onclick", newEventStr);
        }
    }
}

 
function CBoardMenu_ActiveItem(itemIndex)
{
    clearTimeout(this.itemArr[itemIndex].timer);
    
    if (this.itemArr[itemIndex].status == "expanding" ||
        this.itemArr[itemIndex].status == "expanded")
    {
    
        this.itemArr[itemIndex].CollapseItem(this.objStr + ".itemArr[" + itemIndex + "]");
    }
    else if (this.itemArr[itemIndex].status == "collapsing" ||
        this.itemArr[itemIndex].status == "collapsed")
    {
         
        this.itemArr[itemIndex].ExpandItem(this.objStr + ".itemArr[" + itemIndex + "]");
        if (this.standalone)
        {
          
            var i = 0;
            for (i=0; i<this.itemArr.length; i++)
            {
                if (i != itemIndex)
                {
                    clearTimeout(this.itemArr[i].timer);
                    this.itemArr[i].CollapseItem(this.objStr + ".itemArr[" + i + "]");
                }
            }
        }
    }
}
