﻿$(function() {
    /*边框黄色高亮效果*/
    $(".yelborder").focus(function() {
        $(this).addClass("yelborderfocus");
    });
    $(".yelborder").blur(function() {
        $(this).removeClass("yelborderfocus");
    });
    /*边框红色高亮效果*/
    $(".redborder").focus(function() {
        $(this).addClass("redborderfocus");
    });
    $(".redborder").blur(function() {
        $(this).removeClass("redborderfocus");
    });
    // subscribe
    var mail_inputs_mark = $('.inp-email').attr('default');
    $('.inp-email').click(function() {
        if ($(this).val() == mail_inputs_mark) {
            $(this).val('').css('color', '#000');
        }
    }).blur(function() {
        if ($.trim($(this).val()) == "") {
            $(this).val(mail_inputs_mark).css('color', '#666');
        }
    });
    function subscribeEmail(mailInputClassName) {
        var em = $(mailInputClassName).val();
        if ($.trim(em) == "" || em == mail_inputs_mark) {
            return;
        }
        $.ajax({
            url: '/ajax/action.ashx',
            type: 'GET',
            dataType: 'text',
            data: ({ Act: "AddSubscibe", eeee: em }),
            success: function(status) {
                window.alert(status);
            }
        });
        $(mailInputClassName).val(mail_inputs_mark)
        .css('color', '#666');
    }
    // this is global javascript handler
    $('input#btn_global_subscribeemail').click(function() {
        subscribeEmail('input#txt_global_inputemail');
    });
    // this is page-level javascript handler
    $('input#btnsubscribeemail').click(function() {
        subscribeEmail('input#txtinputemail');
    });
    /* Load HeaderUserMsg */
    $.ajax({
        url: '/ajax/action.ashx',
        type: 'GET',
        dataType: 'text',
        data: ({ Act: "getUserModel" }),
        success: function(msg) {
            if (msg != "") {
                var JsonMsg = eval("(" + msg + ")");
                var _LoginName = JsonMsg.M_LoginName;
                $("#HeaderUserMsg").html('您好&nbsp;&nbsp;' + _LoginName + '&nbsp;&nbsp;<a href="/UserCenter.aspx?act=logout" class="a">安全退出</a>&nbsp;&nbsp;&nbsp;<a href="/UserCenter.aspx" class="l_black lines">用户中心</a>');
            }
        }
    });
    /* Load ShoppingCart Count */
    $.ajax({
        url: '/ajax/action.ashx',
        type: 'GET',
        dataType: 'text',
        data: ({ Act: "getShoppingCartCount" }),
        success: function(count) {
            $("#ShoppingCartTipCount").text(count);
        }
    });
});
/* Sign in | register */
function signreg(param) {
    if (param == undefined) param = "";
    showdialog({ src: "/TipRegister.aspx" + param, lock: true, close: true, reflash: true, title: "登录 / 注册", width: 750, height: 530 });
}
/* Added to the wishlist */
function AddtoWish(_id, obj) {
    $.ajax({
        url: '/ajax/action.ashx',
        type: 'GET',
        dataType: 'text',
        data: ({ Act: "AddtoWish", gid: _id }),
        success: function(msg) {
            if (msg == "succeed") {
                $(obj).text("收藏成功！");
            }
            else if (msg == "exist") {
                $(obj).text("已经收藏！");
            }
            else if (msg == "nologin") {
                signreg("?topage=AddtoWish&gid=" + _id);
            }
        }
    });
}
/* Added to the ShoppingCart */
function AddtoCart(_id) {
    $.ajax({
        url: '/ajax/action.ashx',
        type: 'GET',
        dataType: 'text',
        data: ({ Act: "AddToCart", gid: _id }),
        success: function(msg) {
            if (msg == "succeed") {
                $("#ShoppingCartTipCount").text(parseInt($("#ShoppingCartTipCount").text()) + 1);
                showdialog({ src: "/TipToCart.aspx", lock: false, close: true, title: "Shopping Cart", width: 400, height: 100 });
            }
            else if (msg == "exist") {
                showdialog({ src: "/TipToCart.aspx", lock: false, close: true, title: "Shopping Cart", width: 400, height: 100 });
            }
            else {
                var jsonMsg = eval("(" + msg + ")");
                self.location = jsonMsg['url'];
            }
        }
    });
}
function GetParamValue(url, name) {
    var re = new RegExp("[&?]" + name + "=([^&?]*)", "ig");
    return ((url.match(re)) ? (url.match(re)[0].substr(name.length + 2)) : '');
}

function getElementPositionX(elem) { var offsetTrail = elem; var offsetLeft = 0; while (offsetTrail) { offsetLeft += offsetTrail.offsetLeft; offsetTrail = offsetTrail.offsetParent; } if (navigator.userAgent.indexOf("Mac") != -1 && typeof (document.body.leftMargin) != "undefined") { offsetLeft += document.body.leftMargin; } return offsetLeft; }
function getElementPositionY(elem) { var offsetTrail = elem; var offsetTop = 0; while (offsetTrail) { offsetTop += offsetTrail.offsetTop; offsetTrail = offsetTrail.offsetParent; } if (navigator.userAgent.indexOf("Mac") != -1 && typeof (document.body.leftMargin) != "undefined") { offsetTop += document.body.topMargin; } return offsetTop + elem.clientHeight + 4; }

/* 自动提示JS， onmousemove="TipText.show(this,'用折扣券可以优惠购买！')" onmouseout="TipText.hide()" */
var TipText = {
    show: function(o, msg) {
        if ($("#TipText").length == 0) {
            $("body").append("<div style='position:absolute;border:#FF3636 1px solid;padding:5px 10px;display:none;background-color:#FFF;' id='TipText'>TipText</div>");
        }
        $("#TipText").html(msg);
        $("#TipText").css("left", getElementPositionX(o) + $(o).width());
        $("#TipText").css("top", getElementPositionY(o));
        $("#TipText").show();
    },
    hide: function() {
        $('#TipText').hide();
    }
};
