﻿var setDivTimeout;
var _isLog = false;
var _ckugid = 1;
var _ckusid = 0;

$(document).ready(function() {
    //    $(".i_button").click(function() { submitSearch(); });
    get_smarter();
    //$(".i_shop_button").click(function() { getShopSearch(); });
    var userInfo = '<li>欢迎来到封面网&nbsp;&nbsp;&nbsp;&nbsp;<a href="/login.aspx" title="登录">登录</a><span>|</span><li><li><a href="/register.aspx" title="用户注册">用户注册</a><span>|</span><li>';

    var fadmad = $.cookie("fadmad");
    var fadmadjson = eval("(" + $.cookie("fadmadjson") + ")");
    var realName = $.cookie("FM_RealName");
    if (fadmad != null) {
        $(".logininfo").next("li").hide();
        var fadmadArray = fadmad.split("&");
        var _myDate;
        var _currDate = new Date();
        var _time = _currDate.getHours();
        if (_time >= 4 && _time < 12)
            _myDate = "早上好";
        if (_time >= 12 && _time < 14)
            _myDate = "中午好";
        if (_time >= 14 && _time < 19)
            _myDate = "下午好";
        if (_time >= 19 && _time < 24)
            _myDate = "晚上好";
        if (_time >= 0 && _time < 4)
            _myDate = "晚上好";
        if (fadmadjson != null) {
            _ckugid = fadmadjson["ckugid"];
            _ckusid = getCookie("logstateid");
        }
        var cbqq = $.cookie("qq_u_name");
        if (cbqq != null && cbqq != undefined && cbqq != "" && cbqq != "undefined") {
            userInfo = cbqq + ' 欢迎来到封面网！<span>|</span><li><li><a href="/account/MemberCenter.aspx">我的账户</a><span>|</span></li> ';
            userInfo += " <li class='cart-ico'><a href='/order/cart.aspx'>我的购物车(0)</a><span>|</span></li> ";
            userInfo += "<li class='search-order'><a href='javascript:void(0)'>订单查询</a><span></span><div class='order-searchbox' style='display:none;'><div class='minsearchform'><input type='text' id='txtSearchOrder'  class='input-text' maxlength='30'  value='请输入您的订单编号' />";
            userInfo += "<input type='button' class='order-search-btn' /></div></div></li>";
            userInfo += "<li>[<a href='/logout.aspx'>退出</a>]</li>";
        } else {
            userInfo = "<li>" + _myDate + "，<a href=\"/account/MemberCenter.aspx\" title=\"我的账户\">" + (realName == null ? fadmadjson["ckun"] : realName) + '</a> 欢迎来到封面网！<span>|</span><li><li><a href="/account/MemberCenter.aspx">我的账户</a><span>|</span></li> ';
            userInfo += " <li class='cart-ico'><a href='/order/cart.aspx'>我的购物车(0)</a><span>|</span></li> ";
            userInfo += "<li class='search-order'><a href='javascript:void(0)'>订单查询</a><div class='order-searchbox' style='display:none;'><div class='minsearchform' ><input type='text' id='txtSearchOrder'  class='input-text' maxlength='30'  value='请输入您的订单编号' />";
            userInfo += "<input type='button' class='order-search-btn' /></div></div></li>";
            userInfo += "<li>[<a href='/logout.aspx'>退出</a>]</li>";
        }
        _isLog = true;
    }
    else {
        userInfo += " <li class='cart-ico'><a href='/order/cart.aspx'>我的购物车(0)</a><span>|</span></li> ";
        userInfo += "<li class='search-order'><a href='javascript:void(0)'>订单查询</a><span></span><div class='order-searchbox' style='display:none;' ><div class='minsearchform' ><input type='text' id='txtSearchOrder' class='input-text' maxlength='30'  value='请输入您的订单编号' />";
        userInfo += "<input type='button' class='order-search-btn' /></div></div></li>";
    }
    try {
        $.get("/ajax/cart.ashx", { rand: Math.random(), action: "getcartnum" },
            function(data) {
                if (data != "") {
                    if (parseInt(data) > 0) {
                        userInfo = userInfo.replace("我的购物车(0)", "我的购物车(" + data + ")");
                    }
                }
                $(".logininfo").html(userInfo);
                
                $(".order-searchbox").find("input[type=text]").focus(function() { this.value = ""; });
                $(".search-order").hover(function() { $(".order-searchbox").css("top", $(".search-order a:eq(0)").position().top - 8); $(this).children(".order-searchbox").stop(true, true).show(); $(this).find("input[type=text]").css({ color: 'gray' }); }, function() { $(this).children(".order-searchbox").stop(true, true).hide(); });

                //查询订单
                $(".order-search-btn").click(function() {
                    var value = $("#txtSearchOrder").val().Trim();
                    if (value.length < 1 || value == "请输入您的订单编号") { $("#txtSearchOrder").val("请输入您的订单编号"); }
                    else { window.location.href = "/account/order-view.aspx?OrderID=" + value; }
                }).focus(function() { this.blur() });
            });
    }
    catch (e) {
        $(".logininfo").html(userInfo);
        
        $(".order-searchbox").find("input[type=text]").focus(function() { this.value = ""; });
        $(".search-order").hover(function() { $(".order-searchbox").css("top", $(".search-order a:eq(0)").position().top - 8); $(this).children(".order-searchbox").stop(true, true).show(); $(this).find("input[type=text]").css({ color: 'gray' }); }, function() { $(this).children(".order-searchbox").stop(true, true).hide(); });

        //查询订单
        $(".order-search-btn").click(function() {
            var value = $("#txtSearchOrder").val().Trim();
            if (value.length < 1 || value == "请输入您的订单编号") { $("#txtSearchOrder").val("请输入您的订单编号"); }
            else { window.location.href = "/account/order-view.aspx?OrderID=" + value; }
        }).focus(function() { this.blur() });
    }

    // 网站跟踪
    var _currUrl = document.location.href;
    var _prevUrl = document.referrer; if (_prevUrl == null || _prevUrl == "") _prevUrl = request("cur_page");
    $.get("/ajax/WebTrack.ashx", { rand: Math.random(), currUrl: _currUrl, prevUrl: _prevUrl },
    function(data) {
    });

    //    get_counter_url();
    //    
    //    $("#goTopBtn img").click(function(){
    //    
    //    pageScroll();
    //    });
    //    
    $(window).scroll(function() {
        $(document).scrollTop() > 0 ? $("#topcontrol ").show() : $("#topcontrol ").hide();
    });

});

if($.browser.msie)
{
    if($.browser.version.toString()=="6.0")
    {
        window.onerror =function(){return true}
    }
}


var _mScrollDelay
//返回顶部
function pageScroll()
{   
    var scro=document.documentElement.scrollTop.toString();
    if(scro=="0"){ scro=document.body.scrollTop.toString(); }
    if(scro=="0")
    { clearTimeout(_mScrollDelay);return;}   
    window.scrollBy(0,-100);
    _mScrollDelay=setTimeout("pageScroll()",30);
}

//获取url参数
function queryString(name) {
   var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
   var r = window.location.search.substr(1).match(reg);
   if (r!=null) return unescape(r[2]); return null;
}

function setQueryOrder() {
    clearTimeout(setDivTimeout);
    var query = $("#query");
    query.addClass("menunav_current");
    query.next().html("");
    var shop = $("#shop");
    shop.next().html("<img src=\"/images/s.gif\" class=\"navline\" />");
    shop.removeClass("menunav_current").addClass("menunav_current_");
    $("#query_order_layer").show();
    $("#shop_order_layer").hide();
}
function setShopOrder() {
    clearTimeout(setDivTimeout);
    var shop = $("#shop");
    shop.addClass("menunav_current");
    shop.next().html("");
    shop.prev().html("");
    var query = $("#query");
    query.removeClass("menunav_current").addClass("menunav_current_");
    $("#shop_order_layer").show();
    $("#query_order_layer").hide();
}

function clearQueryShopOrder() {
    var shop = $("#shop");
    shop.next().html("<img src=\"/images/s.gif\" class=\"navline\" />");
    shop.removeClass("menunav_current").addClass("menunav_current_");
    var query = $("#query");
    query.next().html("<img src=\"/images/s.gif\" class=\"navline\" />");
    query.removeClass("menunav_current").addClass("menunav_current_");
    $("#query_order_layer").hide();
    $("#shop_order_layer").hide();
    clearTimeout(setDivTimeout);
}

function setSearch() {
    //clearQueryShopOrder();
    setDivTimeout = setTimeout(clearQueryShopOrder, '200');
}

function submitSearch() {
    var orderid = $("#txtorderid").val();
    if (orderid != "") {
        topOrderSearch(orderid);
    }
}

// 添加货品到心愿单
addToWishCart = function(proid, bcid) {
    var isInPro = 0; // 是否是在货品内页添加到心愿单，0为默认在货品内页添加到心愿单
    if (bcid == "0") {
        if ($("#SizeID").val() == "0") {
            $(".addwishcartlnk").attr("href", "#no");
            $("#no div[class='msg']").empty().html("<div class=\"alert-ico\"></div>请选择尺码！");
            bindmodalclick("#no");
            return false;
        }
        if (isGiftPro == "1") {
            $(".addwishcartlnk").attr("href", "#no");
            $("#no div[class='msg']").empty().html("<div class=\"alert-ico\"></div>对不起，赠品不能添加到收藏夹！");
            bindmodalclick("#no");
            return false;
        }
        bcid = $("#hidProBarCode").val();
    }
    else {
        isInPro = 1;
        var _isGiftPro = 0; // 是否属于赠品，0为不是赠品
        $(".cart_pro_list input[outMAID='MAID']").each(function() {
            isGiftPro = $(this).attr("isGiftPro");
            var _curbcid = $(this).attr("barcodeid");
            if (isGiftPro == "true" && _curbcid == bcid) {
                _isGiftPro = 1;
            }
        });
        if (_isGiftPro > 0) {
            $(".addwishcartlnk").attr("href", "#no");
            $("#no div[class='msg']").empty().html("<div class=\"alert-ico\"></div>对不起，赠品不能添加到收藏夹！");
            bindmodalclick("#no");
            return false;
        }
    }
    $.get("/ajax/proDetail.ashx", { rand: Math.random(), ProductID: $("#ProductID").val(), BarcodeID: bcid, action: "addtowishcart" },
    function(data) {
        if (data != "") {
            if (data.toLowerCase() == "nologin") {
                //alert("请先登录！");
                if (isInPro == 0) {
                    bcid = "addwishcartlnk";
                }
                $(".addwishcartlnk").attr("href", "#login");
                bindmodalclick("#login");
            }
            else if (data.toLowerCase() == "exist") {
                $(".addwishcartlnk").attr("href", "#no");
                $("#no div[class='msg']").empty().html("<div class=\"alert-ico\"></div>您的收藏夹中已存在该商品！");
                bindmodalclick("#no");
            }
            else {
                $(".addwishcartlnk").attr("href", "#yes");
                $("#yes div[class='msg']").empty().html("<div class=\"success-ico\"></div>添加到收藏夹成功！");
                bindmodalclick("#yes");
                if (isInPro == 1)
                    window.location.href = "/order/cart.aspx?action=getcart";
            }
        }
        else {
            $(".addwishcartlnk").attr("href", "#no");
            $("#no div[class='msg']").empty().html("<div class=\"error-ico\"></div>对不起，添加收藏夹失败！");
            bindmodalclick("#no");
        }
    });
}

function isIE() { //ie?
    if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)
        return true;
    else
        return false;
}

topOrderSearch = function(orderid) {
    //window.location.href = "/account/order-view.aspx?OrderID=" + orderid;
    var fadmad = $.cookie("fadmad");
    if (fadmad == null) {
        //$("#dialog h1").html("请登录！");
        //openLayer('main_content', 'dialog');
        alert("请登录！");
        return false;
    }
    else {
        $.get("/ajax/order-details.ashx", { rand: Math.random(), OrderID: orderid, action: "toporderquery" },
        function(data) {
            if (data != "") {
                if (parseInt(data) > 0) {
                    setSearch();
                    window.location.href = "/account/order-view.aspx?OrderID=" + orderid;
                }
                else {
                    //$("#dialog h1").html("订单不存在，请重新输入订单编号！");
                    //openLayer('main_content', 'dialog');
                    alert("订单不存在，请重新输入订单编号！");
                }
            }
            else {
                //$("#dialog h1").html("对不起，您的查询有误！");
                //openLayer('main_content', 'dialog');
                alert("对不起，您的查询有误！");
            }
        });
    }
}

//获取指定参数名的值
function get_param(param_name) {
    var query = location.search.substring(1);
    var pairs = query.split('&');
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0, pos);
        if (argname.toLowerCase() == param_name.toLowerCase()) {
            var value = pairs[i].substring(pos + 1);
            value = decodeURIComponent(value);
            return value;
        }
    }
    return null;
}
//记录访问来源
function get_counter_url() {
    var source = get_param('source');
    var fadmad = get_param('fadmad');
    if (source != null && fadmad == 'union') {
        var visited = $.cookie("union_visited");
        var isnew;
        if (visited == null || visited == '') {
            $.cookie("union_visited", source, "{ expires: 15, path: '/', domain: '513523.com', secure: true }");
            isnew = 1;
        }
        else {
            isnew = 0;
        }
        //返回操作
        var counter_url = '/ajax/webtrack.ashx?p=' + fadmad + '&p1=' + source + '&p2=' + escape(location.href) + '&p3=' + isnew + '&p4=' + escape(document.referrer);
        return counter_url;
    }
}

//聪明点订单来源
function get_smarter() {
    var source = get_param('source');
    if (source != null && source == 'smartercn') { // 聪明点订单来源
        $.cookie("channelId", source, "{ expires: 30, path: '/', domain: '513523.com', secure: true }");
        addCookie("channelId", source, 30 * 24);
    }
}

var scrolltotop = {
    setting: { startline: 100, scrollto: 0, scrollduration: 1000, fadeduration: [500, 100] },
    controlHTML: '<img src="/images/fadmad_top.jpg" style="width:30px; height:33px" />',
    controlattrs: { offsetx: 15, offsety: 50 },
    anchorkeyword: '#top',
    state: { isvisible: false, shouldvisible: false },
    scrollup: function() {
        if (!this.cssfixedsupport)
            this.$control.css({ opacity: 0 })
        var dest = isNaN(this.setting.scrollto) ? this.setting.scrollto : parseInt(this.setting.scrollto)
        if (typeof dest == "string" && jQuery('#' + dest).length == 1)
            dest = jQuery('#' + dest).offset().top
        else
            dest = 0
        this.$body.animate({ scrollTop: dest }, this.setting.scrollduration);
    },
    keepfixed: function() {
        var $window = jQuery(window)
        var controlx = $window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx
        var controly = $window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety
        this.$control.css({ left: controlx + 'px', top: controly + 'px' })
    },
    togglecontrol: function() {
        var scrolltop = jQuery(window).scrollTop()
        if (!this.cssfixedsupport)
            this.keepfixed()
        this.state.shouldvisible = (scrolltop >= this.setting.startline) ? true : false
        if (this.state.shouldvisible && !this.state.isvisible) {
            this.$control.stop().animate({ opacity: 1 }, this.setting.fadeduration[0])
            this.state.isvisible = true
        }
        else if (this.state.shouldvisible == false && this.state.isvisible) {
            this.$control.stop().animate({ opacity: 0 }, this.setting.fadeduration[1])
            this.state.isvisible = false
        }
    },
    init: function() {
        jQuery(document).ready(function($) {
            var mainobj = scrolltotop
            var iebrws = document.all
            mainobj.cssfixedsupport = !iebrws || iebrws && document.compatMode == "CSS1Compat" && window.XMLHttpRequest
            mainobj.$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body')
            mainobj.$control = $('<div id="topcontrol">' + mainobj.controlHTML + '</div>')
.css({ position: mainobj.cssfixedsupport ? 'fixed' : 'absolute', bottom: mainobj.controlattrs.offsety, right: mainobj.controlattrs.offsetx, opacity: 0, cursor: 'pointer' })
.attr({ title: '返回页面顶部' })
.click(function() { mainobj.scrollup(); return false })
.appendTo('body')
            if (document.all && !window.XMLHttpRequest && mainobj.$control.text() != '')
                mainobj.$control.css({ width: mainobj.$control.width() })
            mainobj.togglecontrol()
            $('a[href="' + mainobj.anchorkeyword + '"]').click(function() {
                mainobj.scrollup()
                return false
            })
            $(window).bind('scroll resize', function(e) {
                mainobj.togglecontrol()
            })
        })
    }
}

 function GetDesignerPages(vCurrentPage,vTotalPage)
{
	var sRet="";
    
    if(vTotalPage=="0")return "";
    if(vTotalPage%4==0){vTotalPage=vTotalPage/4}
    else vTotalPage=vTotalPage/4+1;
    
    
    
	vCurrentPage=parseInt(vCurrentPage);
	vTotalPage=parseInt(vTotalPage);
	if(vCurrentPage>vTotalPage)vCurrentPge=vTotalPage
	if(vCurrentPage<1)vCurrentPage=1

	try
	{
			sRet+="<a href=\"/designer/des"+(vCurrentPage-1>0?vCurrentPage-1:1)+".html\"  style=\"margin:0 2px;\" class=\"btn_normal btnxs\" title=\"上一页\">&lt;</a>";

		if(vTotalPage<=0)
		{
			return "";
		}
		else
		{
			var lb=vCurrentPage-2;
			var ub=vCurrentPage+3;
			if(lb<1)lb=1
			if(ub<4)ub=4
			if(ub>vTotalPage)ub=vTotalPage

			if(lb>=2)
			{
				sRet+="<a href=\"/designer/des1.html\" style=\"margin:0 2px;\"   >1</a>";
			}
			if(lb>2)
			{
				sRet+="<span style=\"margin:0 2px;\" >...</span>";
			}
			for(var i=lb;i<ub;i++)
			{
				if(i==vCurrentPage)
				{
					sRet+="<a  class=\"selected\" href=\"/designer/des"+i+".html\"  style=\"margin:0 2px;\"  >"+i+"</a>";
				}
				else
				{
					sRet+="<a href=\"/designer/des"+i+".html\"  style=\"margin:0 2px;\"  >"+i+"</a>";
				}
			}
			if(ub<vTotalPage)
			{
				sRet+="<span>...</span>";
				sRet+="<a href=\"/designer/des"+vTotalPage+".html\"  style=\"margin:0 2px;\"   >"+vTotalPage+"</a>";
			}
			else if(ub==vTotalPage)
			{
				if(ub==vCurrentPage)
				{
					sRet+="<a  class=\"selected\" href=\"/designer/des"+vCurrentPage+".html\"  style=\"margin:0 2px;\"  >"+vCurrentPage+"</a>";
				}
				else
				{
					sRet+="<a href=\"/designer/des"+vTotalPage+".html\" style=\"margin:0 2px;\"   >"+vTotalPage+"</a>";
				}
			}
		}
		if(vCurrentPage<vTotalPage)
		{
			sRet+="<a href=\"/designer/des"+(vCurrentPage+1)+".html\" style=\"margin:0 2px;\"  class=\"btn_normal btnxs\"  title=\"下一页\">&gt;</a>";
		}
		else
		{
			sRet+="<a href=\"/designer/des"+(vCurrentPage)+".html\"  style=\"margin:0 2px;\"  class=\"btn_normal btnxs\"  title=\"下一页\">&gt;</a>";
		}
	}
	catch(err)
	{
		sRet="";
	}
	finally
	{
		return sRet;
	}
}


scrolltotop.init();
