//布告菜单项类
//controllerStr 控制菜单项的 HTML 标签的 ID 字符串。
//boardStr 用于展开、折叠的菜单项的 HTML 标签的 ID 字符串。
//direction 展开、折叠方向：v－竖直；h－水平；其它值按竖直处理。
//status 状态，expanding－展开中，collapsing－折叠中，expanded－已经展开，collapsed－已经折叠。
//step 展开、折叠的步进长度。
//delay 展开、折叠的延时，单位毫秒。
function CBoardMenuItem(controllerStr, boardStr, direction, step, delay)
{
    this.controller = document.getElementById(controllerStr);
    this.board = document.getElementById(boardStr);
    
    this.direction = direction; //展开、折叠方向：v－竖直；h－水平；其它值按竖直处理。
    
    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"; //状态，expanding－展开中，collapsing－折叠中，expanded－已经展开，collapsed－已经折叠。
    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;
}


//展开菜单项
//recallStr 回调对象的字符串
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";
	}
}


//折叠菜单项
//recallStr 回调对象的字符串
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) //这里不能用 >=，因为 IE 中，style.height 为 0px 且 overflow 为 hidden，则会显示所有内容
	{
	    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";
	}
}


/*================================================================================*/
//布告菜单类
//objStr当前对象的名称，字符串类型。
//CBoardMenu 有三个隐式参数。
//第一个隐式参数 standalone，表示是否只允许一个菜单项处于展开状态，默认值：false。
//第二个隐式参数 direction－item 的默认 direction 值，表展开、折叠方向。
//第三个隐式参数 step－item 的默认 step 值，表展开、折叠的步进长度，默认值：20。
//第四个隐式参数 delay－item 的默认 delay 值，表展开、折叠的延时，单位毫秒，默认值：10。
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;
}


//controllerStr 控制菜单项的 HTML 标签的 ID 字符串。
//boardStr 用于展开、折叠的菜单项的 HTML 标签的 ID 字符串。
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
	{
	    //其它浏览器中使用的是 setAttribute 设置事件属性字符串，这就不能剥夺事件属性上原有的字符串函数
	    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);
		}
	}
}


//改变 itemIndex 所对应的菜单项状态
//如果 standalone 为 true，则可能会影响其它已经打开的 item。
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 + "]");
				}
			}
		}
	}
}
