﻿var time_c, time_s;

var showdiv;

$(document).ready(function() {
    //bindmodalclick();

    //if close button is clicked
    $('.detail .close').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.detail').hide();
        clearTimeout(time_c);
        clearInterval(time_s);
    });

    //if mask is clicked
    $('#mask').click(function() {
        $(this).hide();
        $('.detail').hide();
        clearTimeout(time_c);
        clearInterval(time_s);
    });

    $(".close").click(function() {
        $(".detail").hide();
        clearTimeout(time_c);
        clearInterval(time_s);
    });
});

bindmodalclick = function(obj) {
    //Get the A tag
    var id = obj;

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight, opacity: 0.5 });

    //transition effect
    $('#mask').show();

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
//    $(id).css('top', get_scrollTop() + winH / 2 - $(id).height() / 2);
//    $(id).css('left', winW / 2 - $(id).width() / 2);

    $(id).css('top', get_scrollTop() + winH / 2 - $(id).height() / 2);
    $(id).css('left', winW / 2 - $(id).width() / 2);

    //transition effect
    $(id).show();

    if ($(id).attr("id") == "no" || $(id).attr("id") == "yes") {
        $(obj).find("label").eq(1).html("3")
        time_s = setInterval("clacTime1('" + obj + "')", 1000);
        time_c = setTimeout(function() { $("#mask,div.detail").hide(); }, 3000);
    }

    window.onscroll = function() {
        var scrollleft = ($(window).width() - $(id).width()) / 2;
        var scrolltop = get_scrollTop() + ($(window).height() - $(id).height()) / 2 + 'px';
        $(id).css({ top: scrolltop, left: scrollleft });
    }
    window.onresize = function() {
        var left = ($(window).width() - $(id).width()) / 2;
        var top = get_scrollTop() + ($(window).height() - $(id).height()) / 2;
        $(id).css({ top: top, left: left });
    }
}

clacTime1 = function(obj) {
    if ($(obj).find("label").eq(1).html() != "0") { $(obj).find("label").eq(1).html(parseInt($(obj).find("label").eq(1).html()) - 1); }
    else { clearInterval(time_s); }
}

var _TimeSecond,_TimeClose;
function AboutSelect(id,message,href,classe,title) {
    bindmodalclick("#"+id);
     if(title.length>0)
        {
            $("#sTitle").html(title);
        }
        
        if(classe=="error")
        {
            $("#sp_result").addClass("error-ico");
        }
        else if(classe=="success")
        {
            $("#sp_result").addClass("success-ico");
        }
        
        
        $("#aClose").click(function(){
            $("#mask,div.detail").hide();
            clearTimeout(_TimeClose);
            clearInterval(_TimeSecond);
        })

        if(arguments.length==5)
        {
            $("#lb_message").html(message+"<p style='font-size:12px;margin:20px 0 0 40px'><label style='color:red' id='lbTime'>5</label>&nbsp;秒后自动关闭</p>");
            _TimeSecond=setInterval("clacTime()",1000);
            _TimeClose=setTimeout(function(){ $("#mask,div.detail").hide();},5000);
        }
        else
        {
           $("#lb_message").html(message);
        }

}

//计算关闭时间
function clacTime(){
    if($("#lbTime").html()!="0") { $("#lbTime").html(parseInt($("#lbTime").html())-1); }
    else { clearInterval(_TimeSecond);}
}

function get_scrollTop() {
    var scrollTop;
    if (typeof window.pageYOffset != 'undefined') {
        scrollTop = window.pageYOffset;
    }
    else
        if (typeof document.compatMode != 'undefined' &&
document.compatMode != 'BackCompat') {
        scrollTop = document.documentElement.scrollTop;
    }
    else
        if (typeof document.body != 'undefined') {
        scrollTop = document.body.scrollTop;
    }
    return scrollTop;
}


//----------------------------------------------------
function openLayer(objId, conId) { //弹出层
    var arrayPageSize = getPageSize(); //调用getPageSize()函数
    var arrayPageScroll = getPageScroll(); //调用getPageScroll()函数
    var Width = document.documentElement.clientWidth;
    Width = parseInt(document.documentElement.scrollLeft, 10) + Width;

    // 货品列表页弹出货品信息层数据绑定
    if ($("#" + objId).attr("proid") != null && $("#" + objId).attr("proid") != "") {
        ProListShowDivDataBind($("#" + objId).attr("proid"), conId);
    }
    if (!document.getElementById("popupAddr")) {
        //创建弹出内容层
        var popupDiv = document.createElement("div");
        //给这个元素设置属性与样式
        popupDiv.setAttribute("id", "popupAddr");
        popupDiv.style.position = "absolute";
        popupDiv.style.border = "1px solid #ccc";
        popupDiv.style.background = "#fff";
        popupDiv.style.zIndex = 10000;
        popupDiv.className = "alert_window";
        //创建弹出背景层
        var bodyBack = document.createElement("div");
        bodyBack.setAttribute("id", "bodybg")
        bodyBack.style.position = "absolute";
        var Width = document.documentElement.clientWidth;
        Width = parseInt(document.documentElement.scrollLeft, 10) + Width;
        bodyBack.style.width = Width+"px";
        bodyBack.style.height = (arrayPageSize[1] + 35 + 'px');
        bodyBack.style.zIndex = 9999;
        bodyBack.style.top = 0;
        bodyBack.style.left = 0;
        bodyBack.style.filter = "alpha(opacity=50)";
        bodyBack.style.opacity = 0.5;
        bodyBack.style.background = "#d2d2d2";

        var mybody = document.getElementById(objId); //实现弹出(插入到目标元素之后)
        insertAfter(popupDiv, mybody); //执行函数insertAfter()
        insertAfter(bodyBack, mybody); //执行函数insertAfter()
    }
    document.getElementById("bodybg").style.display = "";   //显示背景层
    var popObj = document.getElementById("popupAddr");  //显示内容层
    $("#popupAddr").html(document.getElementById(conId).innerHTML);
    popObj.style.display = "";
    var arrayConSize = getConSize(conId);
    popObj.style.top = arrayPageScroll[1] + (arrayPageSize[3] - arrayConSize[1]) / 2 - 50 + 'px';
    popObj.style.left = ((Width / 2) - (parseInt(document.getElementById(conId).style.width) / 2)) + "px";
}

// 货品列表页弹出货品信息层数据绑定
ProListShowDivDataBind = function(proid, conId) {
    $("#loading").show();
    var showObj = $("#" + conId);
    $.ajax({ url: "/ajax/proDetail.ashx", type: "get", dataType: "json", data: "rand=" + Math.random() + "&ProductID=" + proid + "&action=getprolistshowbyid",
        success: function(data) {
            $("#loading").hide();
            var obj;
            try {
                obj = eval(data);  //获取JSON
            }
            catch (e) {
                obj = eval("(" + data + ")");  //获取JSON
            }
            var TableInfo = obj.ShowProInfo;

            if (TableInfo.length > 0) {
                $("#lnkProName").attr("href", "/product/" + TableInfo[0].BrandID + "/" + TableInfo[0].ProductID + ".html").attr("title", TableInfo[0].ProductName).html(TableInfo[0].ProductName).attr("proid", TableInfo[0].ProductID).attr("brandid", TableInfo[0].BrandID);

                var prosizeabout = TableInfo[0].SizeAbout; // 尺码
                var procolorabout = TableInfo[0].ColorAbout; // 颜色
                var proimg = TableInfo[0].ProImg; // 图片
                var webimgurl = TableInfo[0].webimgurl; // 网站路径
                var probrandimg = TableInfo[0].BrandImg; // 品牌图片
                var probrandname = TableInfo[0].BrandName; // 品牌名称
                var proprice = TableInfo[0].SalePrice // 销售价格

                var prosizearr = prosizeabout.split('|');
                var procolorarr = procolorabout.split('|');
                var proimgarr = proimg.split('|');

                var prosizeid = prosizearr[0].substr(0, prosizearr[0].lastIndexOf(','));
                var prosizename = prosizearr[1].substr(0, prosizearr[1].lastIndexOf(','));

                var procolorid = procolorarr[0].substr(0, procolorarr[0].lastIndexOf(','));
                var procolorname = procolorarr[1].substr(0, procolorarr[1].lastIndexOf(','));

                var prosmallimg = proimgarr[0].substr(0, proimgarr[0].lastIndexOf(','));
                var probigimg = proimgarr[1].substr(0, proimgarr[1].lastIndexOf(','));

                var prosizeidarr = prosizeid.split(',');
                var procoloridarr = procolorid.split(',');
                var prosizenamearr = prosizename.split(',');
                var procolornamearr = procolorname.split(',');
                var prosmallimgarr = prosmallimg.split(',');
                var probigimgarr = probigimg.split(',');

                var showcolorhtm = "";
                if (procoloridarr.length > 0) {
                    $("#scolor").html(procolornamearr[0]);
                    $("#hidcolor").val(procoloridarr[0]);

                    $("#viewImg").html("<img src=\"" + webimgurl + prosmallimgarr[0] + "\" style=\"width:246px; height:246px; margin:15px;\" /><div><a href=\"" + webimgurl + probigimgarr[0] + "\" target='_blank'><img src=\"/images/s.gif\" class=\"big_check cursor\" /></a></div>");

                    for (i = 0; i < procoloridarr.length; i++) {
                        if (i == 0) {
                            showcolorhtm += "<img src=\"" + webimgurl + prosmallimgarr[i] + "\" onclick=\"ChangShowImg(this);ChangShowColor(this);\" colorid=\"" + procoloridarr[i] + "\" colorname=\"" + procolornamearr[i] + "\" smallimg=\"" + webimgurl + prosmallimgarr[i] + "\" bigimg=\"" + webimgurl + probigimgarr[i] + "\" class=\"attribute_goodsfocus\" alt=\"" + prosizenamearr[i] + "\" />";
                        }
                        else {
                            showcolorhtm += "<img src=\"" + webimgurl + prosmallimgarr[i] + "\" onclick=\"ChangShowImg(this);ChangShowColor(this);\" colorid=\"" + procoloridarr[i] + "\" colorname=\"" + procolornamearr[i] + "\" smallimg=\"" + webimgurl + prosmallimgarr[i] + "\" bigimg=\"" + webimgurl + probigimgarr[i] + "\" class=\"attribute_goods\" alt=\"" + prosizenamearr[i] + "\" />";
                        }
                    }
                    showcolorhtm += "<img src=\"" + webimgurl + "/" + probrandimg + "\" style=\"float:right; margin:-40px 5px 0 0;\" alt=\"" + probrandname + "\" />";
                }
                if (showcolorhtm != "") {
                    $("#colorattr").html(showcolorhtm);
                }

                var showsizehtm = "";
                if (prosizeidarr.length > 0) {
                    $("#ssize").html(prosizenamearr[0]);
                    $("#hidsize").val(prosizenamearr[0]);
                    for (i = 0; i < prosizeidarr.length; i++) {
                        if (i == 0) {
                            showsizehtm += "<li style=\" width:32px;\" class=\"attribute_sizefocus\" onclick=\"ChangShowSize(this);\" sizeid=\"" + prosizeidarr[i] + "\" sizename=\"" + prosizenamearr[i] + "\">" + prosizenamearr[i] + "</li><li style=\"width:5px;\" class=\"attribute_size_none\"></li>";
                        }
                        else {
                            showsizehtm += "<li style=\" width:32px;\" class=\"attribute_size\" onclick=\"ChangShowSize(this);\" sizeid=\"" + prosizeidarr[i] + "\" sizename=\"" + prosizenamearr[i] + "\">" + prosizenamearr[i] + "</li><li style=\"width:5px;\" class=\"attribute_size_none\"></li>";
                        }
                    }
                    // showsizehtm += "<li style=\"width:5px;\" class=\"attribute_size_none\"></li><li><a href=\"#\">尺码对照表</a></li>";
                }
                if (showsizehtm != "") {
                    $("#sizeattr ul").html(showsizehtm);
                }
                
                var proDescription = TableInfo[0].Description;
                if (proDescription.length > 175)
                    proDescription = proDescription.substring(0, 175);
                $("#proDescription").html(proDescription);

                $("#proprice1").html(proprice);
                $("#proprice2").html(proprice);
                $("#hidProPrice").val(proprice);
            }
        }
    });
}

//获取内容层内容原始尺寸
function getConSize(conId) {
    var conObj = document.getElementById(conId);
    conObj.style.position = "absolute";
    conObj.style.left = -1000 + "px";
    conObj.style.display = "";
    var arrayConSize = [conObj.offsetWidth, conObj.offsetHeight];
    conObj.style.display = "none";
    return arrayConSize;
}
function insertAfter(newElement, targetElement) {//插入
    var parent = targetElement.parentNode;
    if (parent.lastChild == targetElement) {
        parent.appendChild(newElement);
    }
    else {
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}
//获取滚动条的高度
function getPageScroll() {
    var yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {
        yScroll = document.body.scrollTop;
    }
    arrayPageScroll = new Array('', yScroll);
    return arrayPageScroll;
}
//获取页面实际大小
function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        sScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    //var pageHeight,pageWidth;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    var pageWidth, pageHeight;
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}
//关闭弹出层
function closeLayer() {
    document.getElementById("popupAddr").style.display = "none";
    document.getElementById("bodybg").style.display = "none";
    return false;
}

//拖拽
//对“拖动点”定义：onMousedown="StartDrag(this)" onMouseup="StopDrag(this)" onMousemove="Drag(this)"即可
//var move=false,oldcolor,_X,_Y;
//function StartDrag(obj) {  //定义准备拖拽的函数
//    obj.setCapture(); //对当前对象的鼠标动作进行跟踪
//    oldcolor = obj.style.backgroundColor;
//    obj.style.background = "#999";
//    move = true;
//    //获取鼠标相对内容层坐标
//    var parentwin = document.getElementById("popupAddr");
//    _X = parentwin.offsetLeft - event.clientX
//    _Y = parentwin.offsetTop - event.clientY
//}
//function Drag(obj) {        //定义拖拽函数
//    if (move) {
//        var parentwin = document.getElementById("popupAddr");
//        parentwin.style.left = event.clientX + _X;
//        parentwin.style.top = event.clientY + _Y;
//    }
//}
//function StopDrag(obj) {   //定义停止拖拽函数
//    obj.style.background = oldcolor;
//    obj.releaseCapture(); //停止对当前对象的鼠标跟踪
//    move = false;
//}

