﻿
var Pop = function(options){
    this.isIE = (document.all) ? true : false;
    this.isIE6 = this.isIE && (navigator.userAgent.indexOf('MSIE 6.0') != -1);
    this.isIE6 ? this.position = "absolute" : this.position = "fixed";
    this.SetOptions(options);
    this.mode = this.options.mode;
    this.zIndex = this.options.zIndex;
    this.left = this.options.left;
    this.right = this.options.right;
    this.top = this.options.top;
    this.bottom = this.options.bottom;
    this.oPop = $("#" + this.options.oPop);
    this.oPop.css({ position:this.position, "z-index":this.zIndex });
 
    //页面载入时是否显示遮盖层
   if(this.options.beCover){
       this.Start();
       this.Initialization();
       this.FullScreen(this.heightDocument, this.widthDocument);
   }
 
   //是否添加浮动层收缩
   if(!!this.options.idShrink) this.Shrink(this.options.idShrink);
 
   /*关闭、打开浮动层*/
   if(this.options.idClose.length != 0) this.Close(this.options.idClose);
       !!this.options.idOpen ? this.Open(this.options.idOpen) : this.Start();
   };
   
   Pop.prototype = {
       SetOptions: function(options){
       this.options = {
           /*浮动框相关属性*/
           oPop: "idPop",            //浮动框id
           zIndex: "999",            //浮动框的z-index
           left: 0,                  //距离左边多少像素
           right: 0,                 //距离右边多少像素
           top: 0,                   //距离顶部多少像素
           bottom: 0,                //距离底部多少像素
           mode: ["left", "top"],    //浮动层默认定位左上
   
           /*遮罩层相关属性*/
           beCover: false,           //页面载入时是否显示遮盖层
           cover: false,             //是否显示遮盖层(遮盖层显示的必须条件)
           zIndexCover: 888,         //遮盖层的z-index
           colorCover: "#000",       //遮盖层的背景颜色
           opactiyCover: 0.5,        //遮盖层的透明度
   
           /*浮动框收缩相关属性*/
           idShrink: null,           //收缩按钮id
           minHeight: 0,            //收缩后的高度
           maxHeight: 0,       //展开后的高度
           classOne: null,           //切换用的class
           classTwo: null,           //切换用的class
   
           /*关闭、打开浮动层相关属性*/
           idOpen: null,             //打开按钮
           idClose: []               //关闭按钮
       };
       $.extend(this.options, options || {});
 },
 Initialization: function(){
      this.widthPop = this.oPop.width();
      this.heightPop = this.oPop.height();
      this.heightDocument = $(document).height();
      this.widthDocument = Math.min($(document).width(), $("body").width()); //避免ie6下加上滚动条的宽度
      this.heightWindow = $(window).height();
      this.widthWindow = $(window).width();
      this.topScroll = $(window).scrollTop();
      this.leftScroll = $(window).scrollLeft();
 },
 Start: function(){
      switch(this.mode[0].toLowerCase()) {
       case "left":
        this.LeftRightLocation();
        break;
       case "right":
        this.LeftRightLocation();
        break;
       default:
        this.CenterLocation();
      }
 },
 //mark等于false为水平垂直居中定位
 GetValue: function(mark){
        if(this.oPop.css("display") == "none") return; //浮动层显示时才动态计算其坐标(解决一个效率问题)
            var level = this.mode[0].toLowerCase(), vertical = this.mode[1].toLowerCase();
            this.Initialization();
            if(mark){
                if(!this.isIE6){
                level == "left" ? this.oPop.css({ left: this.left }) : this.oPop.css({ right: this.right });
                vertical == "top" ? this.oPop.css({ top: this.top }) : this.oPop.css({ bottom: this.bottom });
            }else{
                var x_final = level == "left" ? this.leftScroll + this.left : this.widthWindow + this.leftScroll - this.widthPop - this.right;
                var y_final = vertical == "top" ? this.topScroll + this.top : this.heightWindow + this.topScroll - this.heightPop - this.bottom;
                this.oPop.css({ top: y_final, left: x_final });
            }
        }else{
            if(!this.isIE6){
                var x_final = parseInt((this.widthWindow - this.widthPop) / 2);
                var y_final = parseInt((this.heightWindow - this.heightPop) / 2);
            }else{
                var x_final = (this.widthWindow - this.widthPop) / 2 + this.leftScroll;
                var y_final = (this.heightWindow - this.heightPop) / 2 + this.topScroll;
            }
            this.oPop.css({ top: y_final, left: x_final });
        }
  
        //IE6在浮动层中添加iframe
        if(this.isIE6){ this.AddIframe(this.oPop, this.heightPop, this.widthPop); }
 },
 LeftRightLocation: function(){
        this.GetValue(true);
        var _this = this;
        $(window).bind("scroll."+this.options.oPop, function(){
            _this.GetValue(true);
        }).bind("resize."+this.options.oPop, function(){
            _this.GetValue(true);
    })
 },
 CenterLocation: function(){
      this.GetValue(false);
      var _this = this;
      $(window).bind("scroll."+this.options.oPop, function(){
            _this.GetValue(false); 
      }).bind("resize."+this.options.oPop, function(){
            _this.GetValue(false); if(_this.oPop.css("display") != "none") _this.FullScreen(_this.heightDocument, _this.widthDocument);
      });
 },
 FullScreen: function(oHeight, oWidth){
  if(!this.options.cover) return;
  
  //遮盖层参数
  this.zIndexCover = this.options.zIndexCover;
  this.colorCover = this.options.colorCover;
  this.opactiyCover = this.options.opactiyCover;
  
  if($("#popMask").length == 0) $("body").append("<div id=\"popMask\"></div>");
  
  //ie6遮罩层定位absolute,非ie6遮罩层定位fixed
  if(this.isIE6){
   $("#popMask").css({ position: "absolute", "z-index": this.zIndexCover, top: 0, left: 0, height: oHeight, width: oWidth, opacity: this.opactiyCover, background: this.colorCover }); 
   //IE6在浮动层中添加iframe
   this.AddIframe($("#popMask"), oHeight, oWidth);
  }else{
   $("#popMask").css({ position: "fixed", "z-index": this.zIndexCover, top: 0, left: 0, height: "100%", width: "100%", opacity: this.opactiyCover, background: this.colorCover }); 
  }
 },
 //IE6覆盖select控件
 AddIframe: function(iElement, iHeight, iWidth){
  iElement.append("<iframe></iframe>");
  var oIframe = iElement.children("iframe");
  oIframe.css({ position: "absolute", top: 0, left: 0, opacity: 0, "z-index": -1, height: iHeight, width: iWidth, border: 0 });
 },
 //浮动层收缩
 Shrink: function(iShrink){
  this.minHeight = this.options.minHeight;
  this.maxHeight = this.options.maxHeight;
  this.classOne = this.options.classOne;
  this.classTwo = this.options.classTwo;
  var _this = this;
  $("#" + iShrink).toggle(
   function(){
    _this.oPop.height(_this.minHeight);
    $(this).removeClass();
    $(this).addClass(_this.classOne);
    if(_this.isIE6) _this.GetValue(true);
   },
   function(){
    _this.oPop.height(_this.maxHeight);
    $(this).removeClass();
    $(this).addClass(_this.classTwo);
    if(_this.isIE6) _this.GetValue(true);
   }
  );
 },
 Close: function(iClose){
  var _this = this;
  $.each(iClose, function(index, name){
   $("#" + name).click(function(){
    _this.oPop.css({ display: "none" });
    if(!!$("#popMask")[0]) { $("#popMask").remove(); }
    $(window).unbind("scroll."+_this.options.oPop);
    $(window).unbind("resize."+_this.options.oPop);
   })
  });
 },
 Open: function(iOpen){
  var _this = this;
  var oOpen = $("#" + iOpen);
  oOpen.click(function(){
   _this.oPop.css({ display: "block" });
   
   _this.Start();
   
   //遮罩层初始化
   _this.FullScreen(_this.heightDocument, _this.widthDocument);
  });
 }
};



//--------------自定义---------------
$(function(){
    //登录框
    var userLogin = new Pop({ oPop: "divLogin", zIndex: 101, mode: ["center", "center"], idOpen: "btnLogin", idClose: ["btnCancel"], cover: true, beCover: false, zIndexCover: 100, colorCover: "#000", opactiyCover: 0.6 });
    
    //语言图片切换
    ChangeImage("div.gp img");

    //设置登录登出按钮
    if($.trim($("input.loginedUser").val())==""){//显示登录
        $("#btnLogin").css("visibility","visible");
        $("#btnLogout").css("visibility","hidden");
        $("#btnLogout").css("display","none");
        $("#btnLogin").css("display","block");
    }else{
        $("#btnLogout").css("visibility","visible");
        $("#btnLogin").css("visibility","hidden");
        $("#btnLogout").css("display","block");
        $("#btnLogin").css("display","none");
    }
    //菜单图片切换
    ChangeImage("div.menu img");

    //product的onmouseover菜单
    $("#Image11").mouseover(function(e){
        var itemOffset = $(this).offset();
        var top = itemOffset.top + $(this).attr("height");
        var left = itemOffset.left + $(this).attr("width")/2;
        if($("#divCategoryMenu").find("table").length==0){//不存在时，读取数据库
            $.post("action/ProductMenu.ashx",null,function(result){
                $("#divCategoryMenu").html(result);
                //连接
                $("#divCategoryMenu").find("table.MenuCategoryItem").click(function(){
                    window.location.href = "Overview.aspx?serial="+$.trim($(this).attr("serial"))+"&parentNode="+$.trim($(this).attr("childNode"));
                });
                left = left - ($("#divCategoryMenu table.MenuCategoryItem").length*143 + 15*2)/2;
                $("#divCategoryMenu").css("top",top).css("left",left);
                $("#divCategoryMenu").stop(true,true);
                //png
                $.ifixpng("images/clear.gif");
                $("#divCategoryMenu img").ifixpng();
                //菜单动画
                $("#divCategoryMenu").slideDown("slow");//css("display","block");
            });
        }else{//存在，说明不是第一次
            $("#divCategoryMenu").stop(true,true);
            $("#divCategoryMenu").slideDown("slow");//css("display","block");
            left = left - ($("#divCategoryMenu table.MenuCategoryItem").length*143 + 15*2)/2;
            $("#divCategoryMenu").css("top",top).css("left",left);
        }
    });
    
    $("#Image11").mouseout(function(){
        setTimeout(function(){$("#divCategoryMenu").slideUp("slow");},10000);
    });
    
    //点击页面的任何一个地方都会隐藏菜单
    $("body").click(function(){$("#divCategoryMenu").css("display","none");});
    
    //菜单动画
    /*$("body").mouseover(function(e){
        var itemOffset = $("#Image11").offset();
        var top = itemOffset.top + $("#Image11").attr("height");
        var left = itemOffset.left + $("#Image11").attr("width");
        var top2 = top + 150;
        var left2 = $("#divCategoryMenu").offset().left + ($("#divCategoryMenu table.MenuCategoryItem").length*143 + 15*2);               
        $("#divCategoryMenu").stop(true,true);
        if(e.pageY < itemOffset.top){
            $("#divCategoryMenu").slideUp("slow");
        }
        if(e.pageY > top2){
            $("#divCategoryMenu").slideUp("slow");
        }
        if(e.pageY > itemOffset.top && e.pageY < top && (e.pageX < itemOffset.left || e.pageX > left)){
            $("#divCategoryMenu").slideUp("slow");
        }
        if(e.pageY > top && e.pageY < top2 && (e.pageX < $("#divCategoryMenu").offset().left || e.pageX > left2)){
            $("#divCategoryMenu").slideUp("slow");
        }
    });*/
    
    //login
    $("#btnSubmit").click(function(){
        var username = $.trim($("#username").val());
        var password = $.trim($("#password").val());
        if(validateLogin(username,password)){
            $.post("CheckLogin.ashx",{username:username,password:password},function(result){
                if(result=="1"){
                    //window.parent.$("input.loginedUser").val(username);
                    //if($.trim(window.parent.$("input.loginedUser").val())!=""){
                        //window.parent.$("#btnLogin").css("display","none");
                        //window.parent.$("#btnLogout").css("display","block");
                    //}
                    $("#btnCancel").click();
                    window.location.href = window.location.href;
                }
                if(result=="2"){
                    $('#username').poshytip({content: 'User Id or Password is not correct.',showOn: 'none',alignTo: 'target',alignX: 'inner-left',offsetX: 0,offsetY: 5});
                    $('#username').poshytip('show');
                }
            });
        }
    });
    
    //cancel
    $("#btnCancel").click(function(){
        $('#username').val("");
        $('#password').val("");
        $('#username').poshytip('hide');
        $('#password').poshytip('hide');
    });
    
    //登录
    $("#btnLogin").click(function(){
        $("#divLogin").css("visibility","visible");
    });
    
    //登出
    $("#btnLogout").click(function(){
        $.post("Logout.ashx",{},function(){});
        if($.trim($("input.loginedUser").val())!=""){//登录状态
            $("#btnLogin").css("visibility","visible");
            $("#btnLogout").css("visibility","hidden");
            $("#btnLogout").css("display","none");
            $("#btnLogin").css("display","block");
            $.post("Logout.ashx",{},function(result){});
            $("input.loginedUser").val("");
        }
        /*if($.browser.msie) {//if ie
            window.location.reload();
        }
        else if($.browser.safari)//if safari
        {
            
        }*/
        if(window.location.href.toLowerCase().lastIndexOf("product.aspx") > 0){//如果是在product.aspx页面登出
            var temp = $.trim($("input.txtCurrentSerial").val());
            if(temp!=""){//如果有传递过来的产品
                //当前产品
                var currentProduct = $("li.childLi[productid='"+temp+"']");
                //alert($.trim($("input.txtCurrentSerial").val()));
                //默认产品节点被点击
                currentProduct.click();
            }else{//默认第一个产品被点击
                var currentProduct = $("li.childLi").eq(0);
                currentProduct.click();
                currentProduct.parent().click();
            }
        }
    });
    
    //聚焦时清楚提示
    $("input.UserLoginFlag").focus(function(){
        $("#"+$(this).attr("id")).poshytip('hide');
    });
    
    //默认清空登录数据和样式
    $("#btnCancel").click();
});
//语言图片鼠标事件
function ChangeImage(selector){
    $(selector).mouseover(function(){
        if(!$(this).hasClass("FlagMenuImage")){
            var src = $(this).attr("src");
            var index = src.lastIndexOf('.');
            var strSrc = src.substring(0,index);
            var strFormat = src.substring(index,src.length);
            src = strSrc + "1" + strFormat;
            $(this).attr("src",src);
        }
    });
    
    $(selector).mouseout(function(){
        if(!$(this).hasClass("FlagMenuImage")){
            var src = $(this).attr("src");
            var index = src.lastIndexOf('.');
            var strSrc = src.substring(0,index - 1);
            var strFormat = src.substring(index,src.length);
            src = strSrc + strFormat;
            $(this).attr("src",src);
        }
    });
}

//png处理
function pngFix(){
    //$.ifixpng("images/clear.gif");
    //$("img[src$='.png']").ifixpng();
}

//登录验证
function validateLogin(username,password){
    if(username==""){
        $('#username').poshytip('hide');
        $('#username').poshytip({content: 'Please input UserId.',showOn: 'none',alignTo: 'target',alignX: 'inner-left',offsetX: 0,offsetY: 5});
        $('#username').poshytip('show');
        //$("#username").focus();
        return false;
    }else{
        $('#username').poshytip('hide');
    }
    if(password==""){
        $('#password').poshytip('hide');
        $('#password').poshytip({content: 'Please input Password.',showOn: 'none',alignTo: 'target',alignX: 'inner-left',offsetX: 0,offsetY: 5});
        $('#password').poshytip('show');
        //$("#password").focus();
        return false;
    }else{
        $('#password').poshytip('hide');
    }
    return true;
}


