﻿//Create at 2007.6.20 by benking
//Copyright @ fund123.cn
/*******************************************

latest update: 2007.7.3

history

    [1].base methods complete at 2007.6.20
    [2].add webservices request method at 2007.7.3

*******************************************/

function __ItemCache__(){}

__ItemCache__.m_Cache = {};

__ItemCache__.Add = function( item )
{
	this.m_Cache[item.Name] = item;
};

__ItemCache__.Get = function( name )
{
	return this.m_Cache[name];
};

__ItemCache__.ClearCache = function( menu )
{
    for( var i=0; i<menu.Items.length; ++i )
    {
        var item = menu.Items[i];
        item.Parent = null;
        item.m_Element.Object = null;
        item.m_Element.m_Submi = null;
        item.m_Element = null;
        item.m_ItemPanel = null;
        item.m_ItemLink = null;
        item.Attributes = null;
        
        if( item.SubMenu )
        {
            __ItemCache__.ClearCache(item.SubMenu);
        }
        
        item.SubMenu = null;
        item = null;
    }
    
    __ItemCache__.m_Cache = null;
    menu.Items = null;
    menu.Container = null;
};

function Menu( container )
{
	this.Items = [];
	this.MaxTextWidth = 0;
	this.Container = container;
};

Menu.prototype.Add = function( item )
{	
	__ItemCache__.Add(item);
	var length = CommonLib.TestStrPixel(item.Text);
	if( item.SubMenu )
	{
	    length += 5;
	}
	this.MaxTextWidth = this.MaxTextWidth < length ? length : this.MaxTextWidth;
	item.Parent = this;
    this.Items.push(item);
};

Menu.prototype.GetMenuWidth = function()
{
    return this.MaxTextWidth + 15;
};

Menu.prototype.Render = function( pNode )
{
    var doc = document;
    if( pNode )
    {
        var ul = doc.createElement("UL");
        pNode.appendChild(ul);
        ul.style.width = this.GetMenuWidth();
        for( var i=0; i<this.Items.length; ++i )
        {
            this.Items[i].Render(ul, true);
        }
        
        return ul;
    }
    else
    {
        var tb = doc.createElement("TABLE");
        tb.id = "navmenu";
        this.Container.appendChild(tb);
        
        var tbody = doc.createElement("TBODY");
        tb.appendChild(tbody);
        
        var tr = doc.createElement("TR");
        tbody.appendChild(tr);
        
        for( var i=0; i<this.Items.length; ++i )
        {
            var item = this.Items[i]; 
            var td = doc.createElement("TD");
            tr.appendChild(td);
            item.m_Element = td;
            td.Object = item;   
            td.noWrap  = "true";     
            if( item.SubMenu )
            {
                var a = doc.createElement('A'); 
                td.appendChild(a);
                with( a.style )
                {
                     padding = "0px";
                     margin = "0px";
                }       
                a.onclick=function(){
			return false;
		}            
                a.innerHTML = item.Text;   
                
                a.onmouseover = function()
                {
                   this.style.color  = "white"; 
                }  
               
                a.onmouseout = function()
                {
                    this.style.color = "black";
                }  
                                
                var submi = item.SubMenu.Render(td);
                td.m_Submi = submi;
            } 
            else
            {
                var a = doc.createElement('A');
                td.appendChild(a);
                a.innerHTML = item.Text;
                with( a.style )
                {
                     padding = "0px";
                     margin = "0px";
                }  
                a.onclick=function(){
			return false;
		}  
                if( item.Href.Url )
                {
                    
			a.onclick=function(){
				if(item.Href.Target=="_blank"){
				window.open(item.Href.Url);
				}else{
				window.location.href=item.Href.Url;
				}
				return false;
			}  
                }   
                
                                      
                if( item.ClientEvent )
                { 
                    a.onclick = function()
                    {
                        this.parentNode.Object.ClientEvent(this.parentNode);
                    }   
                }
               
                a.onmouseover = function()
                {
                   this.style.color  = "white"; 
                }  
               
                a.onmouseout = function()
                {
                    this.style.color = "black";
                }  
            }
            
            td.onmouseover = function()
            {   
               this.style.backgroundColor = "#6D6D6D";
               this.style.color  = "white"; 
               if( this.m_Submi )
               {
                    this.m_Submi.style.display = "block";
                    
                    if( !CommonLib.IsIE )
                    {
                        this.m_Submi.style.left = this.offsetLeft + "px";
                        this.m_Submi.style.top = this.offsetTop + this.offsetHeight + "px";
                    }
                    else
                    {
                        this.m_Submi.style.top = this.offsetHeight + "px"
                    }
                }    
            }
          
            td.onmouseout = function()
            {
                this.style.backgroundColor = "";
                this.style.color = "black";
                if( this.m_Submi )
                {
                    this.m_Submi.style.display = "none";
                }
            }      
        } 
        
        return tb;
    }
};

Menu.prototype.Show = function()
{
    this.Render();
};

function MenuItem( text, name, eventHandler )
{
    if( !text )
    {
	    this.Text = "item";
	}
	else
	{
	    this.Text = text;
	}
	this.ClientEvent = eventHandler;
	this.Href = {};
	this.Href.Url = "#";
	this.Href.Target = null;
	this.ToolTip = "";
	this.SubMenu = null;
	this.Name = name;
	this.HasTextBox = false;
	this.TextBoxEvent = null;
	this.Parent = null;
	this.Attributes = {};
	this.zIndex = 2000;
};

MenuItem.prototype.SetAttribute = function( key, value )
{
	this.Attributes[key] = value;
};

MenuItem.prototype.GetAttribute = function(key)
{
	return this.Attributes[key];
};

MenuItem.prototype.Render = function( pNode, isSubMenu )
{
    var doc = document;
    var li = doc.createElement("LI");
    pNode.appendChild(li);
    
    li.style.zIndex = this.zIndex--;
    li.Object = this;
    this.m_Element = li;
    this.SubMenuLeft = this.Parent.GetMenuWidth();

    li.onmouseover=function() 
    {
	    this.style.backgroundColor = "#6D6D6D";
	    if( this.Object.m_ItemPanel.tagName != "INPUT" )
	    {
	        this.Object.m_ItemPanel.style.color = "#Fff";	    	
	    }
    	if( this.m_Submi )
    	{
    		with( this.m_Submi.style )
    		{
    			display = "block";
    		}

            this.m_Submi.style.left = this.Object.Parent.GetMenuWidth() + "px";
    	}
	};
	
    li.onmouseout=function() 
    {
        if( isSubMenu )
        {
    	    this.style.backgroundColor = "#fff";
        } 
       else
       {
            this.style.backgroundColor = "";
        }  
    	this.Object.m_ItemPanel.style.color = "black";  	
    	if( this.m_Submi )
    	{
    		with( this.m_Submi.style )
    		{
    			display = "none";
    		}
    	}
    };
	
	if( this.HasTextBox )
	{
	    var box = doc.createElement("INPUT");
	    li.appendChild(box);
	    
	    box.type = "text";
	    box.readOnly = true;
        box.style.width = this.Parent.GetMenuWidth();
	    this.m_ItemPanel = box;
	    box.value = this.Text;
        box.onclick = function()
        {
            if( this.parentNode.Object.ClientEvent )
            {
                this.parentNode.Object.ClientEvent(this.parentNode);
            }
        }
	}
	else
	{
        var a = doc.createElement("A");
        li.appendChild(a);
        
        this.m_ItemLink = a;
        
        if(this.Href.Url)
        {
            a.onclick=function(){
				
			}  
        }
        else
        {
            a.onclick=function(){
				
				return false;
			}  
        }   

    	//a.style.width = this.Parent.GetMenuWidth() + "px";
    	a.Url_1=this.Href.Url;
    	a.Target_1=this.Href.Target;
        a.onclick = function()
        {
            if( this.parentNode.Object.ClientEvent )
            {
                this.parentNode.Object.ClientEvent(this.parentNode);
            }
            if( this.parentNode.m_Submi )
            {
                this.parentNode.m_Submi.style.display = "none";
            }
            this.parentNode.parentNode.style.display = "none";
            
            if(this.Target_1=="_blank"){
				window.open(this.Url_1);
				}else{
				window.location.href=this.Url_1;
				}
				return false;
        }
        
        if( this.ToolTip )
		{ 
            a.title = this.ToolTip;
        } 
        //a.style.width = this.Parent.GetMenuWidth() - 6;
 	    
        if( this.Href.Target )
        {
    	    a.target = this.Href.Target;
        }
        var span = doc.createElement('SPAN');
        a.appendChild(span);
        
        this.m_ItemPanel = span;
        span.innerHTML = this.Text;
        
        if( this.SubMenu)
        {
            var hint = doc.createElement('SPAN');
            hint.id = "hint";
            a.appendChild(hint);
            if( CommonLib.IsIE )
            {
                hint.innerHTML = '4';
                hint.style.fontFamily = 'Webdings';
                hint.style.margin = "-4px";
            }
            else
            {
                hint.innerHTML = '&#9658;';
            }
        }
        
        li.appendChild(a);
    }
    
    if( this.SubMenu )
    {
    	var submi = this.SubMenu.Render( li );
    	li.m_Submi = submi;
    }
};

function Position( container )
{
	this.Items = [];
	this.MaxTextWidth = 0;
	this.Container = container;
};

function PositionItem( text, url )
{
    if( !text )
    {
	    this.Text = "item";
	}
	else
	{
	    this.Text = text;
	}
	this.Href = {};
	this.Href.Url = url ;
	this.Href.Target = "_self";
	this.ToolTip = "";
		
};

Position.prototype.Add = function( item )
{	
    this.Items.push(item);
};

Position.prototype.Render=function(){
    var str="";
    str="<span class='position'><a>当前位置：</a>";
    for(var i = 0 ;i < this.Items.length; i++){
        if(i==0){
            str+="<a href='"+this.Items[i].Href.Url+"' Title='"+this.Items[i].ToolTip+"' target='"+this.Items[i].Href.Target+"'>"+this.Items[i].Text+"</a>";
        }else{
            str+=" &gt;&gt; <a href='"+this.Items[i].Href.Url+"' Title='"+this.Items[i].ToolTip+"' target='"+this.Items[i].Href.Target+"'>"+this.Items[i].Text+"</a>";
        }
    }
    str+="</span>";
    this.Container.innerHTML=str;
}

Position.prototype.Show = function()
{
    this.Render();
};

var __Position=new Position(document.getElementById("positionSpan"));

function SetCurrentNav(id){
    try{
    
    var src=document.getElementById(id);
    
    
    if(typeof(src)!="undefined" && src!=null){
        var parentTr=src.parentNode.parentNode;
        var srclist=parentTr.getElementsByTagName("A");
        for(var i=0; i<srclist.length ; i++){
            srclist[i].className="";
        }
        src.className="current";
    }
    
    }catch(e){}
}

function HindMenu(){
    document.getElementById('menuSpan').style.display='none';
}

function ShowMenu(){
    document.getElementById('menuSpan').style.display='';
}
