﻿var m_menuLength=0;//菜单数量

function winResize(){
    if(getIEVersion()=="6"){
        document.getElementById("div_index_main").style.marginTop="-10px";
        document.getElementById("div_index_bottom").style.marginTop="-10px";
        var bannerFlash=document.getElementById("div_banner_flash_con");             
        if(bannerFlash && document.location.href.indexOf("company.htm")>0){
			bannerFlash.style.marginTop="-10px"; 			
		}
    }
    if(document.location.href.indexOf("contact.htm")>0){
        document.getElementById("div_index_main").style.height=getWinHeight()-47-4-114;
    }
}


 
//获得IE版本
function getIEVersion(){
    var versionNum = -1; 
    if (navigator.appName == 'Microsoft Internet Explorer'){  
        var ua = navigator.userAgent;  
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");  
        if (re.exec(ua) != null){  
            versionNum = parseFloat( RegExp.$1 );  
        }
    }
    return versionNum;
}
           
//页面跳转
function gotoPage(t){
    var strUrl="home.htm";
    switch(t){
        case "home":
            strUrl="Default.htm";
            break;
        case "company":
            strUrl="company.htm";
            break;
        case "products":
            strUrl="products.htm";
            break;
        case "contact":
            strUrl="contact.htm";
            break;
    }
    window.document.location.href=strUrl;
}

//计算字符串长度
function strlen(str) {
    var len = 0;
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255 || str.charCodeAt(i<0)){
            len+= 2; 
        }else{
            len++;
        }
    }
    return len;
}

//返回str字符串中,p位置的字符索引
function findPosition(str,p) {
    var len = 0;
    var posi=0;
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255 || str.charCodeAt(i<0)){
            len+= 2; 
        }else{
            len++;
        }
        if(p==len){
            posi=i;
            break;
        }
    }
    posi--;
    return posi;
}

var m_menuObj={};
//创建菜单
//_id 菜单容器ID
//oJson 菜单列表JSON对象
function createMenu(_id,oJson){
    var strHtml="";
    m_menuObj=oJson.data;
    var menuHeight=36*m_menuLength  ;//父菜单容器高度
    
    var versionNum=getIEVersion();//版本号
    if(versionNum=="6"){
        menuHeight+=20;
    }
    for(var i=0;i<m_menuLength;i++){
        var menucss="menu_level1";
        if(versionNum=="6" && i>0){
            menucss="menu_level1_ie6";
        }
		if(i==0){
            strHtml+="<table ><tr><td style='height:2px;width:190px;'></td></tr></table>";
        }
        var menuDisplayName=oJson.data[i].menuName.toUpperCase();
		if(menuDisplayName.length>16){
		    menuDisplayName=menuDisplayName.substr(0,15)+"…";
		}
        strHtml+="<div id='divMenu"+i+"' class='"+menucss+"' onclick='menuButtonClick("+i+",\""+oJson.data[i].menuName+"\",this)'>";
        //strHtml+="onmouseover='menuButtonMouseOver("+i+")' onmouseout='menuButtonMouseOut("+i+")'>";
        strHtml+="    <div class='menu_level1_space'> ";
        strHtml+="    </div>";
        strHtml+="    <div id='menu_level1_word"+i+"' class='menu_level1_word' title='"+oJson.data[i].menuName+"'>";
        strHtml+="        <div id='menu_level1_icon"+i+"' class='menu_level1_icon'> ";
        strHtml+="        </div>";
        strHtml+="        "+menuDisplayName;
        strHtml+="    </div>";
        strHtml+="    <div class='menu_level1_line'>";
        strHtml+="    </div>";
        strHtml+="</div>";
        
        var _subMenuHeight=oJson.data[i].subMenu.length*25;
        strHtml+="<div id='divSubMenu"+i+"' style='height:"+_subMenuHeight+"px; width:190px;display:none;'>";
        
        strHtml+="    <ul>";
        for(var j=0;j<oJson.data[i].subMenu.length;j++){
            var submenuName=oJson.data[i].subMenu[j].menuName;
            strHtml+="  <li id='liMenuItem"+i+j+"' onclick='subMenuClick(\""+oJson.data[i].menuName+"\",\""+oJson.data[i].subMenu[j].menuName+"\",this)' ";
            strHtml+="          class='menu_level2_word' ";
            strHtml+="          title='"+oJson.data[i].subMenu[j].menuName+"'>&gt;&gt&nbsp;"+submenuName;
            strHtml+="  </li>";
//            strHtml+="  <li id='liMenuItem'"+i+"_"+j+" onclick='subMenuClick(\""+oJson.data[i].menuName+"\",\""+oJson.data[i].subMenu[j].menuName+"\","+p+")' ";
//            strHtml+="          class='menu_level2_word' onmouseover='this.className=\"menu_level2_over\"'";
//            strHtml+="          onmouseout='this.className=\"menu_level2_word\"' title='"+oJson.data[i].subMenu[j].menuName+"'>&gt;&gt&nbsp;"+submenuName;
//            strHtml+="  </li>";
        }
        strHtml+="    </ul> ";
        strHtml+="</div>";
    }
    var MenuContainer=document.getElementById(_id);
    MenuContainer.style.height=menuHeight;//根据菜单数量重新设置菜单容器高度
    MenuContainer.innerHTML=strHtml;
}

function setCurrentMenu(t) {
    for(var i=0;i<m_menuObj.length;i++){
        var odiv=document.getElementById("menu_level1_word"+i);
        odiv.style.color="white";
        for(var j=0;j<m_menuObj[i].subMenu.length;j++){
            var oLi=document.getElementById("liMenuItem"+i+j);
            if(oLi){
                oLi.style.color="white";
            }
        }
    }
    if(t.id.indexOf("divMenu")>-1){
        var k=t.id.substring(t.id.length-1);
        var d=document.getElementById("menu_level1_word"+k);
        d.style.color="red";
    }else{
        t.style.color="red";
    }
}

function menuButtonMouseOver(i){
    var menuIcon=document.getElementById("menu_level1_icon"+i);
    var menuWord=document.getElementById("menu_level1_word"+i);
    menuIcon.className="menu_level1_icon_over";
    menuWord.className="menu_level1_word_over";
}
function menuButtonMouseOut(i){
    var menuIcon=document.getElementById("menu_level1_icon"+i);
    var menuWord=document.getElementById("menu_level1_word"+i);
    menuIcon.className="menu_level1_icon";
    menuWord.className="menu_level1_word";
}

function menuButtonClick(p,currentMenu,t){
    for(var i=0;i<m_menuLength;i++){
        var subMenu=document.getElementById("divSubMenu"+i);
        if(i==p){
            subMenu.style.display="block";
            var d=document.getElementById("divMenuContainer");
            var h=36*m_menuLength+parseInt(subMenu.style.height.replace("px",""));
            d.style.height=h;
        }else{
            subMenu.style.display="none";
        }                
    }
    menuClick(currentMenu);
    setCurrentMenu(t);
}

//取服务器时间
function getServerDateTime() {
    $.ajax({
        type: 'get',
        url: 'CommanSelect.aspx',
        dataType: 'xml',
        data: 'XActionName=GetServerDateTime',
        async: true,
        complete: function(res){
            document.getElementById("divToday").innerHTML="Today："+res.responseText;
        }
    });
}

//针对两种浏览器，分别获取xmlDocument对象  
function loadXMLString(xmlStr){   
    var xmlDoc=null;
    try{//Internet Explorer
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(xmlStr);
    }catch(e){
        try{//Firefox, Mozilla, Opera, etc.
            parser=new DOMParser();
            xmlDoc=parser.parseFromString(xmlStr,"text/xml");
        }catch(e){
            alert(e.message);
        }
    }
    return xmlDoc;  
}
var Browser = {
    'isIE' : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0),
    'isFirefox' : navigator.userAgent.indexOf('Firefox') >= 0,
    'isOpera' : navigator.userAgent.indexOf('Opera') >= 0
};



//得到窗体高度
function getWinHeight() {
    var winHeight = 0;
    if (window.innerHeight) {
        winHeight = window.innerHeight;
    }
    else if ((document.body) && (document.body.clientHeight)) {
        winHeight = document.body.clientHeight;
    }   //通过深入Document内部对body进行检测，获取窗口大小   
    if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
        winHeight = document.documentElement.clientHeight;
    }   //结果输出至两个文本框
    return winHeight;
}
//得到窗体宽度
function getWinWidth() {
    var winWidth = 0;
    if (window.innerWidth) { //获取窗口宽度
        winWidth = window.innerWidth;
    } else if ((document.body) && (document.body.clientWidth)) {
        winWidth = document.body.clientWidth;  //获取窗口高度
    }
    if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
        winWidth = document.documentElement.clientWidth;
    }
    return winWidth;
}

//要进行缩放的img对象
//W,H缩放的宽和高
function resizeImage(imageDest, W, H){
    //显示框宽度W,高度H 
    var image = new Image();
    image.src = imageDest.src;
    if(image.width>0 && image.height>0){//比较纵横比
        if(image.width/image.height >= W/H){//相对显示框：宽>高
            if(image.width > W){//宽度大于显示框宽度W，应压缩高度
                imageDest.width = W; 
                imageDest.height = (image.height*W)/image.width;   
            }else{ //宽度少于或等于显示框宽度W，图片完全显示
                imageDest.width = image.width;       
                imageDest.height = image.height;   
            }
        }else{//同理
            if(image.height > H){
                imageDest.height = H;
                imageDest.width = (image.width*H)/image.height;
            }else{
                imageDest.width = image.width;
                imageDest.height = image.height;
            }
        }
    }
}

//遮照层封装
//调用方法
function showMask() {
    //创建遮罩层
    var divMark=document.createElement("div");
    divMark.setAttribute("id","divMyMark");
    document.body.appendChild(divMark);
    //遮罩层
    var sClientWidth = top.document.documentElement.scrollWidth;
    var sClientHeight = top.document.documentElement.scrollHeight > top.document.documentElement.clientHeight ? top.document.documentElement.scrollHeight : top.document.documentElement.clientHeight;
    $(divMark).css({ position: "absolute", display: "block", opacity: "0.6", filter: "alpha(opacity=60)", background: "#000", top: "0px", left: "0px", width: sClientWidth + "px", height: sClientHeight + "px" });
    
}
//关闭遮罩
function hideMask(){
    document.body.removeChild(document.getElementById("divMyMark"));
}
