/**
 * drop down menu on the right side
 *
 * @category JavaScript
 * @author Louis
 * @version $0.1$
 * @copyright 29, July 2008
 */

var MenuDD={
	_setting : {'ItemType':'a','ItemClass':'Minimenu','MiniHolder':'MiniMenu','MiniMenuLinks':'MiniMenuLinks'},
	_addlink : function(x,_name,_href,_cbk,_username)
	{	
		if(_name == '-')
		{
			_a=makex('div');
			_a.className='thinGrayLine';
		}
		else
		{
			_a=makex('a');
			_a.innerHTML=_name;
			_a.href=_href;
			if (_cbk){_a.onclick=function(){eval(_cbk);}}
		}
		addx(x,_a);
	},
	SetDivWidth : function(w)
	{
		this._coreDivWidth = w;
	},
	drawContent : function(x,_links,ajust)
	{
		css.add('.shadowDiv','position:absolute;left:2px;top:2px;margin:0;background-color:#c0c0c0;opacity:0.6;filter:alpha(opacity=60);font-size:1px;');
		css.add('.coreDiv','position:absolute;left:0;top:0;margin:0;background-color:#fff;border:1px solid #858585;padding:3px;');
		css.add('.coreDiv a','padding:2px 5px;color:black;white-space:nowrap;display:block;font-weight:normal;');
		css.add('.coreDiv a:hover','color:white;background-color:#36c;text-decoration:none;font-weight:normal;');
		var shadowDiv=addx(x,makex('div'));
		shadowDiv.className = 'shadowDiv';
		var _div = addx(x,makex('div'));
		_div.className='coreDiv';
		if(this.ESWidth) 
		{
			var tempwidth = this.ESWidth;
			if (tempwidth > 10) tempwidth -= 8; 
			_div.style.width = tempwidth + 'px';
		}
		if(this._coreDivWidth !== undefined)
			_div.style.width = this._coreDivWidth + 'px';
		if(undefined != ajust.textAlign)
		{
			_div.style.textAlign = ajust.textAlign;
		}
		for (var i=0;i<_links.length;i++)
		{	var disp=_links[i].Text;
			var href=_links[i].Link;
			var _cbk = _links[i].Callback||false;
			this._addlink(_div,disp,href,_cbk);
		}
		shadowDiv.style.width = _div.offsetWidth + 'px';
		shadowDiv.style.height = _div.offsetHeight + 'px';
		return _div;
	},
	_makeMenu4 : function(o,_links,ajust)
	{
		var op=o.parentNode;op.style.position='relative';
		this.ESWidth = o.offsetWidth;
		_holder=addx(op.parentNode,makex('div',{'id':'dropdownmenuDiv'}));
		_holder.className=this._setting.MiniHolder;
		var _coreDiv = this.drawContent(_holder,_links,ajust);
		if(ajust.H) _holder.style.top=o.offsetHeight+op.offsetTop+ajust.H+'px';
		else _holder.style.top=o.offsetHeight+op.offsetTop+'px';
		if(ajust.W) _holder.style.left=op.offsetLeft+ajust.W+'px';
		else _holder.style.left=op.offsetLeft+'px';
		_holder.style.left=op.parentNode.offsetWidth - this.ESWidth +'px';
		o._holder=_holder;
		o._holderWidth = this.ESWidth;
		op.style.position='static';
		o.op = op;
		o.onmouseover=function(){
			cleartimer(this._holder._timer);
			if(this._holder)
			{
				this.op.style.position = 'relative';
				if(ajust.H) this._holder.style.top=this.offsetHeight+this.op.offsetTop+ajust.H+'px';
				else this._holder.style.top=this.offsetHeight+this.op.offsetTop+'px';
				if(ajust.W) this._holder.style.left=this.op.offsetLeft+ajust.W+'px';
				else this._holder.style.left=this.op.offsetLeft+'px';
				this._holder.style.left=this.op.parentNode.offsetWidth - this._holderWidth +'px';
				this._holder.style.display='block';
				//this.op.style.position = 'static';
			}
		};
		o.onmouseout=function(){cleartimer(this._holder._timer);var _holder=this._holder;this._holder._timer=setTimeout(function(){_holder.style.display='none';},500);};
		_coreDiv.onmouseover=function(){cleartimer(this.parentNode._timer);};
		_coreDiv.onmouseout=function(){cleartimer(this.parentNode._timer);var _holder=this.parentNode;this.parentNode._timer=setTimeout(function(){_holder.style.display='none';},500);};
	}
	
};

var lw_DDMenuStyle = {
'Holder':'text-align:left;float:right;',
'HolderHeader':'float:left;padding:5px 10px 1px 0;color:#666;font-weight:bold;',
'HolderTitle':'overflow:hidden;white-space:nowrap;padding:5px 2px 1px;margin-right:16px;cursor:default;',
'MenuHolder':'position:absolute;margin:0px;padding:0;z-index:8000;',
'MenuShadow':'position:absolute;z-index:1;left:2px;top:2px;margin:0;background-color:#c0c0c0;opacity:0.6;filter:alpha(opacity=60);font-size:1px;overflow:hidden;',
'MenuCore':'position:absolute;z-index:2;left:0;top:0;margin:0;background-color:#fff;border:1px solid #858585;padding:3px;overflow:hidden;',
'MenuNormal':'float:left;background:url(/images/tabbar/arrow_down.gif) right 65% no-repeat;cursor:pointer;',
'MenuHover':'float:left;background:url(/images/tabbar/arrow_down.gif) right 65% no-repeat;cursor:pointer;',
'Separator':'margin:3px 3px 2px 3px;height:1px;line-height:1px;font-size:1px;border-top:1px solid #BDC7D8;',
'Links':'display:block;white-space:nowrap;padding:2px 3px;color:black;background-color:white;',
'LinkHover':'color:white;background-color:#36c;text-decoration:none;',
'LinkMine':'font-weight:bold;'
};

/**
 * Common drop down menu
 * 
 * @author: Louis Liu
 * @date: 11/11/2008
 * @param: object like follow
 var menu = {
	'Holder' : 'DropDownMenu',   //DOM element/div ...
	'Header' : 'View',			 //string
	'Title'  : 'My test',        //string
	'Style'  : null,			 //object like 	lw_DDMenuStyle
	'Setting': {'Shadow' : true, 'Reload' : false, 'Align' : 'left'},   //object[optional]
	'Adjust' : {'Width' : 0, 'Left' : 0, 'Top' : 0, 'Right' : 0, 'WidthDiff' : 0, 'LeftDiff' : 0, 'RightDiff' : 0, 'TopDiff' : 0}, //object[optional]
	'Items'  : [{'Text' : '001', 'Href' : 'jasdflkasdf.html', 'Title' : 'sdjfdsf', 'Selected', true},
				{'Text' : '002', 'Href' : 'kasdf.html', 'Title' : 'sdjfdsf'}]    //array
				
 * use it like follow
 * lw_DDMenu.initMenu(menu);
 * OR
 * lw_DDMenu.initMenu2(o,menu)
 */
var lw_DDMenu = {
	_stock:{'MenuHolder':'DDMenu_Holder',
			'MenuNormal':'DDMenu_Normal',
			'MenuHover':'DDMenu_Hover',
			'HolderHeader':'Holder_Header',
			'HolderTitle':'Holder_Title',
			'MenuShadow':'DDMenu_Shadow',
			'MenuCore':'DDMenu_Core',
			'Separator':'DDMenu_Separator',
			'Adjust' : {'Width' : 0, 'Left' : 0, 'Top' : 0, 'Right' : 0, 'WidthDiff' : 0, 'LeftDiff' : 0, 'RightDiff' : 0, 'TopDiff' : 0},
			'Setting': {'Shadow' : true, 'Reload' : false, 'Align' : 'left'}
	},
	_setStyle : function(MenuStyle,_prefix)
	{	css.add(_prefix,MenuStyle.Holder||lw_DDMenuStyle.Holder);
		css.add('.DDMenu_Holder',MenuStyle.MenuHolder||lw_DDMenuStyle.MenuHolder);
		css.add(_prefix+' .Holder_Header',MenuStyle.HolderHeader||lw_DDMenuStyle.HolderHeader);
		css.add(_prefix+' .Holder_Title',MenuStyle.HolderTitle||lw_DDMenuStyle.HolderTitle);
		css.add('.DDMenu_Holder .DDMenu_Shadow',MenuStyle.MenuShadow||lw_DDMenuStyle.MenuShadow);
		css.add('.DDMenu_Holder .DDMenu_Core',MenuStyle.MenuCore||lw_DDMenuStyle.MenuCore);
		css.add(_prefix+' .DDMenu_Normal',MenuStyle.MenuNormal||lw_DDMenuStyle.MenuNormal);
		css.add(_prefix+' .DDMenu_Hover',MenuStyle.MenuHover||lw_DDMenuStyle.MenuHover);
		css.add('.DDMenu_Holder .DDMenu_Separator',MenuStyle.Separator||lw_DDMenuStyle.Separator);
		css.add('.DDMenu_Holder a',MenuStyle.Links||lw_DDMenuStyle.Links);
		css.add('.DDMenu_Holder a:hover',MenuStyle.LinkHover||lw_DDMenuStyle.LinkHover);
		css.add('.DDMenu_Holder a.Mine',MenuStyle.LinkMine||lw_DDMenuStyle.LinkMine);
	},
	_addSeparator : function(to)
	{	var _separator=addx(to,makex('div'));
		_separator.className=this._stock.Separator;
	},
	_addLink : function(x,_link)
	{	
		if(_link.Text == '-')
		{
			this._addSeparator(x);
		}
		else
		{
			var _a = makex('a');
			_a.innerHTML = _link.Text;
			if(_link.Href||false)
				_a.href 	 = _link.Href;
			else
				_a.href 	 = "javascript:void(0);";
			if(_link.Title||false)
				_a.title  	 = _link.Title;
			if(_link.Selected||false) _a.className = 'Mine';
			addx(x,_a);
		}
		
	},
	_addItems : function(x,menu)
	{
		if(this._hasProperty(menu.Setting,'Shadow') && menu.Setting.Shadow)
		{
			var shadowDiv=addx(x,makex('div'));
			shadowDiv.className = this._stock.MenuShadow;
		}
		
		var _div = addx(x,makex('div'));
		_div.style.padding = "3px";
		if(this._hasProperty(menu.Adjust,'Width') && menu.Adjust.Width)
		{	
			_div.style.width = parseInt(menu.Adjust.Width).toString() + 'px';
		}	
		if(this._hasProperty(menu.Setting,'Align') && menu.Setting.Align)
		{
			_div.style.textAlign = menu.Setting.Align.toString();
		}
		for (var i=0;i<menu.Items.length;i++)
		{	
			_links = menu.Items[i];
			this._addLink(_div,_links);
		}
		if(undefined != shadowDiv)
		{
			_width = parseInt(_div.offsetWidth);
			_height = parseInt(_div.offsetHeight);
			shadowDiv.style.width = (_width+2).toString() + 'px';
			shadowDiv.style.height = (_height+2).toString() + 'px';
		}
		
		return _div;
	},
	drawMenu : function(o,menu)
	{	
		if (menu.Items.length < 1) return;
		menu.Adjust = (menu.Adjust||this._stock.Adjust);
		menu.Setting = (menu.Setting||this._stock.Setting);
		var op=o.parentNode;op.style.position='relative';
		if(this._MenuHolder)
		{
			//this._MenuHolder.parentNode.removeChild(this._MenuHolder);
			//this._MenuHolder = null;
		}
		_menuHolder=addx(op.parentNode,makex('div'));
		_menuHolder.className=this._stock.MenuHolder;
		var _coreDiv = this._addItems(_menuHolder,menu);
		_coreWidth = parseInt(_coreDiv.offsetWidth);
		_coreDiv.className = this._stock.MenuCore;
		var _top = parseInt(o.offsetHeight) + parseInt(op.offsetTop);
		if(this._hasProperty(menu.Adjust,'TopDiff') && menu.Adjust.TopDiff) _top = _top + parseInt(menu.Adjust.TopDiff);
		_menuHolder.style.top = _top.toString() + 'px';
		if(this._hasProperty(menu.Adjust,'Top') && menu.Adjust.Top) _menuHolder.style.top = parseInt(menu.Adjust.Top).toString() + 'px';
		var _left = parseInt(o.offsetLeft) + parseInt(o.offsetWidth) + parseInt(op.offsetLeft) - _coreWidth - 5;
		if(this._hasProperty(menu.Adjust,'LeftDiff') && menu.Adjust.LeftDiff) _left = _left + parseInt(menu.Adjust.LeftDiff);
		_menuHolder.style.left = _left.toString() + 'px';
		if(this._hasProperty(menu.Adjust,'Left') && menu.Adjust.Left) _menuHolder.style.Left = parseInt(menu.Adjust.Left).toString() + 'px';
		_menuHolder.style.width = (_coreWidth+20).toString() + 'px';
		this._MenuHolder = _menuHolder;
		
		o._holder=this._MenuHolder;
		o._coreWidth = _coreWidth;
		op.style.position='static';
		o.op = op;
		o.onmouseover=function(){
			cleartimer(this._holder._timer);
			if(lw_DDMenu._hasProperty(menu.Setting,'Reload') && menu.Setting.Reload)
			{
				this.op.style.position = 'relative';
				var _top = parseInt(this.offsetHeight) + parseInt(this.op.offsetTop);
				if(lw_DDMenu._hasProperty(menu.Adjust,'TopDiff') && menu.Adjust.TopDiff) _top = _top + parseInt(menu.Adjust.TopDiff);
				this._holder.style.top = _top.toString() + 'px';
				if(lw_DDMenu._hasProperty(menu.Adjust,'Top') && menu.Adjust.Top) this._holder.style.top = parseInt(menu.Adjust.Top).toString() + 'px';
				var _left = parseInt(this.offsetLeft) + parseInt(this.offsetWidth) + parseInt(this.op.offsetLeft) - this._coreWidth -5;
				if(lw_DDMenu._hasProperty(menu.Adjust,'LeftDiff') && menu.Adjust.LeftDiff) _left = _left + parseInt(menu.Adjust.LeftDiff);
				this._holder.style.left = _left.toString() + 'px';
				if(lw_DDMenu._hasProperty(menu.Adjust,'Left') && menu.Adjust.Left) this._holder.style.Left = parseInt(menu.Adjust.Left).toString() + 'px';
			}
			
			this._holder.style.display='block';
			this.op.style.position = 'static';
			this.className = lw_DDMenu._stock.MenuHover;
			
		};
		o.onmouseout=function(){
			this.className = lw_DDMenu._stock.MenuNormal;
			cleartimer(this._holder._timer);
			var _holder=this._holder;
			this._holder._timer=setTimeout(function(){_holder.style.display='none';},500);
		};
		_coreDiv.onmouseover=function(){cleartimer(this.parentNode._timer);};
		_coreDiv.onmouseout=function(){cleartimer(this.parentNode._timer);var _holder=this.parentNode;this.parentNode._timer=setTimeout(function(){_holder.style.display='none';},500);};
		this._MenuHolder.style.display = 'none';
	},
	initMenu : function(Menu)
	{	if (!getx(Menu.Holder) || this._holder) return;
		this._holder=getx(Menu.Holder);
		this._holder.innerHTML='';
		this._prefix='div#'+Menu.Holder;
		this._setStyle(Menu.Style||lw_DDMenuStyle,this._prefix);
		if (Menu.Header)
		{
			var _header=addx(this._holder,makex('div'));
			_header.className = this._stock.HolderHeader;
			_header.innerHTML=Menu.Header;
		}
		var _title = addx(this._holder,makex('div'));
		_title.className = this._stock.MenuNormal;
		var _titleText = addx(_title,makex('div'));
		_titleText.className = this._stock.HolderTitle;
		if(Menu.Title) _titleText.innerHTML=Menu.Title;
		this.drawMenu(_title,Menu);
		
	},
	initMenu2 : function(o,Menu)
	{	if (!o) return;
		this._holder=o;
		this._prefix='div#louis';
		this._setStyle(Menu.Style||lw_DDMenuStyle,this._prefix);
		this.drawMenu(o,Menu);
		
	},
	_hasProperty: function(o,p)
	{
		if('object' != typeof(o))
			return false;
		for(var pro in o)
		{
			if(pro == p)
			{
				return true;
			}
		}
		return false;
	}
}