/*
****************************************************************** 
** 名: show widget  属于JFC － Jax Foundation Class
** 创建：welsham 2006-7-8  修改: welsham 2006-7-8
** 描述: parser之外观模块，外观的操作，如样式改变 widget--tabPanel popMenu…
** 修改描述: 
******************************************************************
*/

//**************初始化parser对象和show/widget对象************************
if(typeof parser != "object") { var parser={}; }
if(typeof parser.show != "object") { parser.show={}; }
if(typeof parser.widget != "object") { parser.widget={}; }

//**************显示与隐藏****************************************
//参数：state："none","block","inline"
parser.show.display=function(o,state)
{
	o.style.display=state;
};
//**************样式转变****************************************
parser.show.swStyle=function(o,style1,style2)
{
	if(o.className==style1) o.className=style2;
};
//**************选择样式转变*************************************
parser.show.selStyle=function(o,unselStyle,selStyle)
{
	if(o.className==selStyle) o.className=unselStyle;else o.className=selStyle;
};


//**************弹出Div*************************************
parser.show.showDiv=function(oDiv,left,top)
{
	if(oDiv.style.display=="none")
	{
		oDiv.style.left=left+"px";
		oDiv.style.top=top+"px";
		oDiv.style.display="block";
	}
	else oDiv.style.display="none"
};

//**************屏幕屏蔽*************************************
parser.show.showScreen=function()
{
	var objScreen = $("ScreenOver");
	if(!objScreen)
	{
		sHtml = '<iframe style="position:absolute;z-index:9000; background:#cccccc; margin:0; padding:0; filter:alpha(opacity=40); opacity:0.4; MozOpacity:0.4; width:expression(document.getElementById(\'ScreenOver\').offsetWidth);height:expression(document.getElementById(\'ScreenOver\').offsetHeight);top:expression(document.getElementById(\'ScreenOver\').offsetTop);left:expression(document.getElementById(\'ScreenOver\').offsetLeft);" frameborder="0" ></iframe>';
		sHtml += '<div style="position:absolute;z-index:9001; background:#cccccc; left:0; top:0; margin:0; padding:0; filter:alpha(opacity=40); opacity:0.4; MozOpacity:0.4;" id="ScreenOver"></div>';

		var el = document.createElement('span');
		el.innerHTML = sHtml;
		document.getElementsByTagName("body")[0].appendChild(el);
		
		var objScreen = $("ScreenOver");		
	}
	var oS = objScreen.style;
	oS.display = "block";
	if (document.body.clientHeight)	{var wh = document.body.clientHeight + "px";}
	else if (window.innerHeight){var wh = window.innerHeight + "px";}
	else{var wh = "100%";}
	oS.width = "100%";
	oS.height = wh;
};
//**************屏幕激活*************************************
parser.show.cleanScreen=function()
{
	var objScreen = $("ScreenOver");
	if (objScreen) objScreen.style.display = "none";
};


//**************widget start************************

//**************选项卡－tabPanel************************
parser.widget.tabPanel = function(_tabPanelID, _tabGroupID, _tabContentGroupID, _hasHover, _hasSelect, _onSelectEvent, _hasChangeContent)
{
	if (_tabPanelID.length == 0)
	{
		alert('缺少参数：选项卡的ID');
		return false;
	}
	
//全局属性
	this.oTabPanel = $(_tabPanelID);
	this.oTabs = this.getTabs(_tabGroupID);
	if (_hasChangeContent) this.oTabContents = this.getTabContents(_tabContentGroupID);
	this.hasHover = _hasHover; //是否启用对未选项的滑过事件
	this.hasSelect = _hasSelect; //是否启用选择事件
	this.onSelectEvent = _onSelectEvent; //选择的事件，onclick、onmouseover、switch、doSelect
	this.hasChangeContent = _hasChangeContent; //是否切换内容块
	
	this.attachBehaviors();
};
parser.widget.tabPanel.prototype.getTabs = function(_tabGroupID)
{
	if (_tabGroupID)
	{
		return $c( $(_tabGroupID) );
	}
	else
	{
		var child = this.oTabPanel.firstChild;
		while (child)
		{
			if (child.nodeType == 1 && child.className == 'tabGroup')
			{
				return $c(child);
			}
			child = child.nextSibling;
		}
	}
};
parser.widget.tabPanel.prototype.getTabContents = function(_tabContentGroupID)
{
	if(_tabContentGroupID)
	{
		return $c( $(_tabContentGroupID) );
	}
	else
	{
		var child = this.oTabPanel.firstChild;
		while (child)
		{
			if (child.nodeType == 1 && child.className == 'tabContentGroup')
			{
				return $c(child);
			}
			child = child.nextSibling;
		}
	}
};

parser.widget.tabPanel.prototype.attachBehaviors = function()
{
	var i;
	for (i=0; i < this.oTabs.length; i++)
	{
		this.addEvents(this.oTabs[i],i)
	}
};
parser.widget.tabPanel.prototype.addEvents = function(tab,tabIndex)
{
	var self = this;
	
	if (this.hasHover || ( this.hasSelect && this.onSelectEvent == 'hover'))
	{
		addEventListener(tab, "mouseover", function(e) { return self.onTabMouseOver(e, tab, tabIndex); }, false);
		addEventListener(tab, "mouseout", function(e) { return self.onTabMouseOut(e, tab, tabIndex); }, false);
	}
	
	if (this.hasSelect && this.onSelectEvent == 'click')
	{
		addEventListener(tab, "click", function(e) { return self.onSelect(e, tab, tabIndex); }, false);
	}
};
parser.widget.tabPanel.prototype.onTabMouseOver = function(e, tab, tabIndex)
{	
	if (this.hasSelect && this.onSelectEvent == 'hover') //select
	{
		this.onSelect(e, tab, tabIndex);
	}
	else //hover切换
	{
		if (tab.className == 'tab' && this.hasHover) tab.className = 'tabHover';
	}
};

parser.widget.tabPanel.prototype.onTabMouseOut = function(e, tab, tabIndex)
{
//hover切换
	if (tab.className == 'tabHover' && this.hasHover)
	{
		tab.className = 'tab';
	}	
};
parser.widget.tabPanel.prototype.onSelect = function(e, tab, tabIndex)
{
	for (var i=0; i < this.oTabs.length; i++)
	{
		if (this.oTabs[i].className == 'tabSelected' && i != tabIndex)
		{
			this.oTabs[i].className = 'tab';
			tab.className = 'tabSelected';
			
			if (this.hasChangeContent)
			{
				this.oTabContents[i].style.display = 'none';
				this.oTabContents[tabIndex].style.display = 'block';
			}
			
			break;
		}
	}
};

//**************菜单－menu************************
parser.widget.menu = function(_menuID)
{
	if (_menuID.length == 0)
	{
		alert('缺少参数：菜单的ID');
		return false;
	}
	
//全局属性
	this.oMenu = $(_menuID);
	
	this.attachBehaviors();
};
parser.widget.menu.prototype.attachBehaviors = function()
{
	var i,oLinks;
	var menus = this.oMenu.getElementsByTagName('li');
	for (i=0; i < menus.length; i++)
	{
		oLinks = menus[i].getElementsByTagName('a');
		if (oLinks.length>0 && oLinks[0].className == 'submenu')
		{
			this.addEvents(menus[i], oLinks[0]);
		}
	}
};
parser.widget.menu.prototype.addEvents = function(oMenuItem, oLink)
{
	var openTimer, closeTimer;
	var self = this;
	var submenus = oMenuItem.getElementsByTagName('ul');
	var submenu = (submenus.length > 0 ? submenus[0] : null);
	
	addEventListener(oMenuItem, 'mouseover', function(e)
	{
		clearTimeout(closeTimer);
		
		addClassName(oLink, "menuHover");
		
		openTimer = window.setTimeout(function(){self.showSubmenu(submenu);}, 250);
		
	}, false);
	
	addEventListener(oMenuItem, 'mouseout', function(e)
	{
		clearTimeout(openTimer);
		
		removeClassName(oLink, "menuHover");
		
		closeTimer = window.setTimeout(function(){self.hideSubmenu(submenu);}, 300);
		
	}, false);
};
parser.widget.menu.prototype.showSubmenu = function(menu)
{
	if(menu)
	{
		addClassName(menu, "submenuVisible");
		
		if(typeof document.all != 'undefined' && typeof window.opera == 'undefined' && navigator.vendor != 'KDE')
		{
			if( menu.parentNode.parentNode == this.element)
			{
				menu.style.top = (menu.parentNode.offsetTop + menu.parentNode.offsetHeight) + 'px';
				menu.style.left = menu.parentNode.offsetLeft + 'px';
			}
		}
		if(typeof document.uniqueID != "undefined")
		{
			this.createIframeLayer(menu);
		}
	}
	
	addClassName(this.element, "menuActive");
};
parser.widget.menu.prototype.hideSubmenu = function(menu)
{
	if(menu)
	{
		removeClassName(menu, "submenuVisible");
		
		if(typeof document.all != 'undefined' && typeof window.opera == 'undefined' && navigator.vendor != 'KDE')
		{
			menu.style.top = '';
			menu.style.left = '';
		}
		this.removeIframeLayer(menu);
	}
};
parser.widget.menu.prototype.createIframeLayer = function(menu)
{
	var layer = document.createElement('iframe');
	layer.tabIndex = '-1';
	layer.src = 'javascript:false;';
	menu.parentNode.appendChild(layer);
	
	layer.style.left = menu.offsetLeft + 'px';
	layer.style.top = menu.offsetTop + 'px';
	layer.style.width = menu.offsetWidth + 'px';
	layer.style.height = menu.offsetHeight + 'px';
};
parser.widget.menu.prototype.removeIframeLayer =  function(menu)
{
	var layers = menu.parentNode.getElementsByTagName('iframe');
	while(layers.length > 0)
	{
		layers[0].parentNode.removeChild(layers[0]);
	}
};

//**************开关－switch***********************
parser.widget.doSwitch = function(oSwitch, _switchContentID, _openClassName, _closeClassName, _openImg, _closeImg)
{
	if ( !$(_switchContentID) ) return;
	
	if ($(_switchContentID).style.display == 'none')
	{
		$(_switchContentID).style.display = 'block';
		if( oSwitch && _closeClassName ) oSwitch.className = _closeClassName;
		if( oSwitch && _closeImg ) oSwitch.src = _closeImg;
	}
	else
	{
		$(_switchContentID).style.display = 'none';
		if( oSwitch && _openClassName ) oSwitch.className = _openClassName;	
		if( oSwitch && _openImg ) oSwitch.src = _openImg;
	}	
};


//**************弹出－popup***********************
