﻿/*菜单切换
**现在是个简单的版本
*/

function MeauSwitch(Instance, ShowIndex, ID){
    this.Instance = Instance;
    this.ShowIndex = ShowIndex;
    this.ID = ID;
}

MeauSwitch.prototype = {
    Init : function(){
        var Contain = document.getElementById(this.ID);
        this.Lis = Contain.getElementsByTagName("LI");        
        //初始化显示
        for(var i = 0; i<this.Lis.length; i++){ 
            if(this.Lis[i].className == "m2"){         
                this.Lis[i].style.display = "none";
            }
        }        
               
        //绑定切换功能
        var Self = eval(this.Instance);
        for(var i = 0; i<this.Lis.length; i++){
            if(this.Lis[i].className == "m1"){
                this.Lis[i].setAttribute("Index", i+1);
                if(this.Lis[i].attachEvent){
                    this.Lis[i].attachEvent("onclick", function(e){Self.Show(e);});
                }
                else if(this.Lis[i].addEventListener){
                    this.Lis[i].addEventListener("click", function(e){Self.Show(e)}, false);
                }
            }
        }
        //初始化菜单
        var index = GetParamValue("index");
        if(index == null){
            this.Lis[1].style.display = "";
            this.Lis[0].className = "sele";
        }
        else{
            this.Lis[parseInt(index)].style.display = "";
            this.Lis[parseInt(index)-1].className = "sele";
        }
        var src = GetParamValue("src");
        if(src != null){
            document.getElementById("Calc_iframe").src = src;
        }
    },
    
    Show : function(e){
        //初始化显示
        for(var i = 0; i<this.Lis.length; i++){            
            if(this.Lis[i].className == "m2"){         
                this.Lis[i].style.display = "none";
            }
            else if(this.Lis[i].className == "sele"){
                this.Lis[i].className = "m1";
            }
        }
        e = e || window.event;
        e = e.srcElement || e.target;
        e.className = "sele";
        var index =  parseInt(e.getAttribute("Index"));
        this.Lis[index].style.display = "";
        //设置菜单索标示
        SetParamValue("index", index);
    }
}
//测试
var MenuSwitchInstance = new MeauSwitch("MenuSwitchInstance", 0, "Calc_Menu");
//得到参数值
function GetParamValue(ParamName)
{
    var URL = window.location.hash;
    var re = new RegExp(ParamName+ "=([^&]*)");
    if(re.test(URL))
    {
        return RegExp.$1;
    }
    else
    {
        return null;
    }
}
//设置参数值
function SetParamValue(ParamName, value)
{           
    var URL = window.location.href;      
    if(window.location.hash){      
        var findex = URL.indexOf(ParamName + "=");  
        if(findex != -1){          
            var fStr = URL.substring(0, findex);
            var eStr = URL.substring(findex,URL.length);
            var eindex = eStr.indexOf("&");
            if(eindex != -1){
                var eeStr = eStr.substring(eindex,eStr.length);
                window.location = fStr + ParamName + "=" + value + eeStr;
            }
            else{
                window.location = fStr + ParamName + "=" + value;
            }
            
        }
        else{
            window.location = window.location + "&" + ParamName + "=" + value;
        }
    }
    else{
        window.location = window.location + "#" + ParamName + "=" + value;
    }
}
