﻿$(function() {
    ShowCartNum();

    $("#btnSearch").click(function() {
        /// <reference path="../../Product/ProductsList.aspx" />
        if ($("#txtSearch").val() == '输入您心仪的钻饰名称' || $("#txtSearch").val() == '') {
            alert("请输入关键字！");
            return false;
        }
        var val = escape($("#txtSearch").val());
        window.location.href = "../../Product/ProductsList.aspx?params=" + val;
    });

    //处理搜索文本框
    var obj = $("#txtSearch");
    var defaultValue = obj.val();
    //obj.select();
    obj.click(function() {
        if (obj.val() == defaultValue) {
            obj.val("");
        }
    }).blur(function() {
        var searchTxt = obj.val();
        if (searchTxt == "" || searchTxt == defaultValue) {
            obj.val(defaultValue);
            //obj.select();
        }
    });
    //体验中心
    $("#exptab").click(function() {
        $("#expediv").css("display", "block");
        $("#helperdiv").css("display", "none");
        $("#frnlinkdiv").css("display", "none");
    });
    //帮助中心
    $("#helptab").click(function() {
        $("#helperdiv").css("display", "block");
        $("#expediv").css("display", "none");
        $("#frnlinkdiv").css("display", "none");
    });
//友情链接
    $("#linktab").click(function() {
        $("#frnlinkdiv").css("display", "block");
        $("#helperdiv").css("display", "none");
        $("#expediv").css("display", "none");
    });

    $("#FooterLink").jCarouselLite({
        btnNext: "#btnNext",
        btnPrev: "#btnPrev",
        speed: 500,
        easing: null,
        visible: 9,
        start: 0,
        scroll: 1
    });
});

//获得URL传过来的参数
function request() {
    var Url = top.window.location.href;
    var u, g, StrBack = '';
    if (arguments[arguments.length - 1] == "#")
        u = Url.split("#");
    else
        u = Url.split("?");
    if (u.length == 1) g = '';
    else g = u[1];
    if (g != '') {
        gg = g.split("&");
        var MaxI = gg.length;
        str = arguments[0] + "=";
        for (i = 0; i < MaxI; i++) {
            if (gg[i].indexOf(str) == 0) {
                StrBack = gg[i].replace(str, "");
                break;
            }
        }
    }
    return StrBack;
}
//去年字符串前后的空格
String.prototype.Trim = function() {
    var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}
//验证手机号码
String.prototype.isMobile = function() {
    return (/^(?:13\d|15[012589])-?\d{5}(\d{3}|\*{3})$/.test(this.Trim()));
}
//验证电话号码
String.prototype.isTel = function() {
    //"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
    //return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
    return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
}

//添加一个cookie,name表示cookie的名字，value:cookie的值,expireHours:cookie过期的时间，单位是小时
addCookie = function(name, value, expireHours) {
    var cookieString = name + "=" + escape(value);
    //判断是否设置过期时间
    if (expireHours > 0) {
        var date = new Date();
        date.setTime(date.getTime + expireHours * 3600 * 1000);
        cookieString = cookieString + "; expire=" + date.toGMTString();
    }
    document.cookie = cookieString;
}

//根据cookie的名字去取cookie的值
getCookie = function(name) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    for (var i = 0; i < arrCookie.length; i++) {
        var arr = arrCookie[i].split("=");
        if (arr[0] == name) return arr[1];
    }
    return "";
}

//删除指定名字的cookie
deleteCookie = function(name) {
    var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=v; expire=" + date.toGMTString();
}

//浏览过的商品
showViewedProduct = function() {
    var cookieStr = getCookie("ViewedDecoration");
    var viewedDiaDiv = $("div.shipin > div.shipin02");
    if (cookieStr != "") {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "../ajax/ProductProcess.ashx?ProIdAry=" + cookieStr,
            dataType: 'json',
            success: function(result) {
                for (var i = 0; i < result.length; i++) {
                    //proObj = eval('(' + proStr + ')');
                    if (i >= 5)
                        break;
                    dl = $("<dl></dl>");
                    dtImg = $("<dt><a href='../../Product/ProductsContent.aspx?ProID=" + result[i].Pro_ID + "'><img border='0' src='../" + result[i].Pro_SmallImages + "'/></a></dt>");
                    dl.append(dtImg);
                    ddProName = $("<dd><a href='../../Product/ProductsContent.aspx?ProID=" + result[i].Pro_ID + "' title='" + result[i].Pro_Name + "'>" + result[i].Pro_Name.substring(0, 8) + "..</a><br/><span class='fcolor7'>Patti价：￥" + result[i].Pro_ShopPrice + "</span></dd>");
                    dl.append(ddProName);
                    viewedDiaDiv.append(dl);
                }
            },
            error: function(result, status) {
                //alert("show viewed product error")
            },
            complete: function(XMLHttpRequest, textStatus) {
                // alert($(".zhongjian_conter01 input").size());
            }
        });
    };
}

var mo;
showMemberPrice = function(e, obj) {
    var offset1 = $(obj).parent().parent().offset();
    var divH = 0;
    var proid = $(obj).attr("proid");
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "../ajax/ProductProcess.ashx?ProID=" + proid,
        dataType: 'json',
        success: function(result) {
            if (result == "1") {
                divH = $("#nohui").height();
                divW = $("#nohui").width();
                $("#nohui").css({ "left": offset1.left + Math.abs((divW - $(obj).parent().parent().width())) / 2 + 4, "top": offset1.top + 213, "z-index": 1000, "position": "absolute" }).show();
            }
            else {
                $("#sliverMemberPrice").html("￥" + result.SilverCardMemberPrice + "元");
                $("#goldMemberPrice").html("￥" + result.GoldCardMemberPrice + "元");
                $("#pokingMemberPrice").html("￥" + result.PokingMemberPrice + "元");
                $("#diamondMemberPrice").html("￥" + result.DiamondMemberPrice + "元");
                divH = $("#memberPriceDiv").height();
                divW = $("#memberPriceDiv").width();
                $("#memberPriceDiv").css({ "left": offset1.left + Math.abs((divW - $(obj).parent().parent().width())) / 2 + 4, "top": offset1.top + 133, "z-index": 1000, "position": "absolute" }).show();
            }
        },
        error: function(result, status) {
            $("#memberPriceDiv").html("读取数据错误，请重新刷新页面！");
        },
        complete: function(XMLHttpRequest, textStatus) {
            //clearTimeout(mo);
        }
    });

    obj.onmouseout = function() {
        //clearTimeout(mo);
        setTimeout(hideMemberPrice, 300);
        //clearTimeout(mo);
    }
    //clearTimeout(mo);
}

hideMemberPrice = function() {
    if ($("#memberPriceDiv")) {
        $("#memberPriceDiv").hide();
    }
    if ($("#nohui")) {
        $("#nohui").hide();
    }
}

isNumber = function(_num) {
    var regx = /^[1-9]d*|0$/;
    if (regx.test(_num)) {
        return true;
    }
    return false;
}

/*
    返回浮点数四行五入后的结果
    num表示要四舍五入的数,v表示要保留的小数位数。
*/
function GetRound(num, v) {
    var vv = Math.pow(10, v);
    return Math.round(num * vv) / vv;
}

giftImgLoadError = function(obj) {
    $(obj).attr("src", "../YXUploadFile/YXProduct/ThumbnailImg_50/nophoto_50.jpg");
}

function ShowCartNum() {
    $.getJSON("../../ajax/ShoppingCart.ashx", { Rand: Math.random(), Type: "GetCartCount", Flag: "cart" }, function(json) {
        if (json != null) {
            //            var obj = eval(json);  //获取JSON
            //            var TableInfo = obj.CartList;
            if (json != "") {
                $("#ShoppingCartLink").html("购物车 <strong>" + json + "</strong> 件产品");
            }
            else {
                $("#ShoppingCartLink").html("购物车 <strong>0</strong> 件产品");
            }
        }
        else {
            $("#ShoppingCartLink").html("购物车 <strong>0</strong> 件产品");
        }
    });
}

RedirectExperience = function(obj) {
    var ex = $(obj).val();
    window.location.href = ex + ".aspx";
}

onlinerIconHidden = function() {
    $("#LRdiv0").hide();
}

IsPhoneNo = function(obj) {
    if (!$(obj).val().isMobile() && !$(obj).val().isTel()) {
        alert("请输入正确的手机号码或电话号码\n\n例如:13916752109或0712-3614072");
        setTimeout(function() { obj.select(); }, 0);
        return false;
    }
}

// JavaScript Document
/* 导航下拉菜单 */
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function k_menu_open() {
    k_menu_canceltimer();
    k_menu_close();
    ddmenuitem = $(this).addClass("sfhover");
}

function k_menu_close() {
    if (ddmenuitem) {
        ddmenuitem.removeClass("sfhover");
    }
}

function k_menu_timer() {
    closetimer = window.setTimeout(k_menu_close, timeout);
}

function k_menu_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

$(function() {
    $('#k_menu > li').bind('mouseover', k_menu_open);
    $('#k_menu > li').bind('mouseout', k_menu_timer);
});

document.onclick = k_menu_close;