﻿$(document).ready(

    function() {
        $(".paihangbang dt").mouseover(function() {

            $(".paihangbang dt.move").attr("class", "");
            $(this).parent().find("dd").hide();
            $(this).next("dd").show();
            $(this).attr("class", "move");
        });

        var sourceKey = "热门搜索：贵妃椅 装饰柜 无框画";
        $('#key').click(function() {
            if (sourceKey == $('#key').val()) {
                $('#key').val('');
            }
        });


        $("#key").keypress(function(e) {
            if (e.keyCode == 13) {
                SearchFunc();
            }
        });

        $('#searchBtn').click(function() {
            SearchFunc();
        });


        function SearchFunc() {
            var key = $('#key').val();
            if (key == sourceKey) {
                alert("请输入内容");
                return false;
            } else {
                key = key.replace(new RegExp('-', 'g'), '^^');
                var url = "/products/0-0-0-0---" + key + "-0---1.html";
                window.location = url;
            }
        }


        $('#mY_left_Yellow,#mY_left_Blue,#mY_left_Green').mouseover(function(e) {

            var id = $(this).attr("id");
            var otherId = id.replace("left", "right");
            var group = id.split('_')[2];
            $('#' + otherId).attr("class", "rightout_" + group);
            $(this).attr("class", "lefton_" + group);
            $('.mainYellow_' + group + '_2').hide();
            $('.mainYellow_' + group + '_1').show();
        });

        $('#mY_right_Yellow,#mY_right_Blue,#mY_right_Green').mouseover(function(e) {
            var id = $(this).attr("id");
            var otherId = id.replace("right", "left");
            var group = id.split('_')[2];
            $('#' + otherId).attr("class", "leftout_" + group);
            $(this).attr("class", "righton_" + group);
            $('.mainYellow_' + group + '_1').hide();
            $('.mainYellow_' + group + '_2').show();
        });

        /*alert layer*/
        $(".mainleft_content").hover(function() {

            $(this).css("position", "relative");
            $('.tt', this).css("left", $(this).width() + 8);
            $('.tt', this).show();
        },
                function() {
                    $(this).css("position", "static");
                    $('.tt', this).hide();
                }
            );

        /*exchange*/
        $('#mainGreyLA_left').mouseover(function(e) {
            $('#mainGreyLA_left').attr("class", "Grey_lefton");
            $('#mainGreyLA_right').attr("class", "Grey_rightout");
            $('.mainGreyLA_btA').hide();
            $('.mainGreyLA_bt').show();
        });
        $('#mainGreyLA_right').mouseover(function(e) {
            $('#mainGreyLA_right').attr("class", "Grey_righton");
            $('#mainGreyLA_left').attr("class", "Grey_leftout");
            $('.mainGreyLA_bt').hide();
            $('.mainGreyLA_btA').show();
        });

        /**************list page****************/
        var viewtype = 0;

        $('#squarebtn').click(function() {        
            $('.channel-rightF').show();
            $('.channel-rightE').hide();
            $('#SquareModel').show();
            $('#listModel').hide();
            viewtype = 1;
            $.ajax({
                type: "GET",
                url: "/Product/ViewType",
                data: "viewtype=" + viewtype
            });
        });

        $('#listbtn').click(function() {
            $('.channel-rightE').show();
            $('.channel-rightF').hide();
            $('#SquareModel').hide();
            $('#listModel').show();
            viewtype = 0;
            $.ajax({
                type: "GET",
                url: "/Product/ViewType",
                data: "viewtype=" + viewtype
            });
        });

        $('#pageSubmit').click(function() {
            var pageValue = $('#txtPageValue').val();
            var pageTotal = $('#PageCount').val();
            if (pageValue == "") {
                alert("请输入页码!");
                return false;
            }
            if (isNaN(pageValue)) {
                alert("您输入的页码有误!");
                return false;
            }
            if (parseInt(pageTotal) < parseInt(pageValue)) {
                alert("您输入的页码大于当前页码");
                return false;
            }

            document.forms["FormPager"].submit();
            return true;
        });

        /******添加购物车start******/

        $(".aAddCart").click(function() {
            var id = $(this).attr("id");
            var product = $(this).attr('lang');
            var date = new Date();
            var options = {
                url: "/Product/AddCart",
                data: "product=" + product + "&v=" + date.getSeconds(),
                type: "GET",
                dataType: "HTML",
                timeout: 10000,
                error: function() {
                    $("#h2Cart_Title").html("商品添加到购物车失败");
                    $("#imgCart_Success").attr("src", "/Assets/Images/success.gif");
                },
                success: function(message) {
                    if (message != "") {
                        var messageTemp = message.split('-');
                        if (messageTemp.length >= 2) {
                            $(".info_title").html("商品已成功添加到购物车");
                            $(".info_count").html("购物车共有<b>" + messageTemp[1] + "</b>件商品&nbsp;合计：<font style='color: red; font-size: 14pt; font-weight: bold;'>" + messageTemp[0] + "</font>元");
                            //                            $("#imgCart_Success").attr("src", "/Assets/Images/success.gif");
                        }
                        else {
                            $("#h2Cart_Title").html("商品添加到购物车失败");
                            $("#imgCart_Success").attr("src", "/Assets/Images/success.gif");
                        }
                    }
                    DivShow("#" + id, "#blk1", ".close1", -40, -100);
                }
            }
            $.ajax(options);
        });
        /******添加购物车end******/

        /******添加收藏start******/
        $('.btnFavorite').click(function() {
            var id = $(this).attr("id");
            var productId = $(this).attr('tip');
            var date = new Date();
            var options = {
                url: "/Product/Favorite",
                data: "id=" + productId + "&v=" + date.getSeconds(),
                type: "GET",
                dataType: "HTML",
                timeout: 10000,
                error: function() { alert("收藏失败") },
                success: function(message) {
                    if (message == "0") {
                        $("#txtUname").val("");
                        $("#txtUpwd").val("");
                        $("#Login_errorInfo").html("");
                        $("#input_Login").attr("tip", id + "-" + productId);
                        DivShow("#" + id, "#blk2", "#close2", -120, -100);
                    }
                    else if (message == "error") {
                        $("#font_info").html("收藏夹已存在");
                        DivShow("#" + id, "#blk3", ".close3", -120, -100);
                    }
                    else {
                        $("#font_info").html("收藏商品成功");
                        DivShow("#" + id, "#blk3", ".close3", -120, -100);
                    }
                }
            }
            $.ajax(options);
        });
        /******添加收藏end******/

        /******登录start******/
        $("#input_Login").click(function() {
            var id = $(this).val();
            var uName = $("#txtUname").val();
            var uPwd = $("#txtUpwd").val();
            var values = $(this).attr('tip').split('-');
            var date = new Date();
            var options = {
                url: "/Product/UserLogin",
                data: "uName=" + uName + "&uPwd=" + uPwd + "&v=" + date.getSeconds(),
                type: "GET",
                dataType: "HTML",
                timeout: 10000,
                error: function() { alert("登录失败") },
                success: function(message) {
                    if (message == "ok") {
                        AddFavorite(values[0], values[1]);

                        //                        DivShow("#input_Login", "#blk3", ".close3", -120, -100);                        
                    }
                    else {
                        $("#Login_errorInfo").html("账户名或密码错误");
                    }
                }
            }
            $.ajax(options);
        });
        /******登录end******/
    });

    function AddFavorite(id, productId) {
        var date = new Date();
        var options = {
            url: "/Product/Favorite",
            data: "id=" + productId + "&v=" + date.getSeconds(),
            type: "GET",
            dataType: "HTML",
            timeout: 10000,
            error: function() { alert("收藏失败") },
            success: function(message) {
                if (message == "0") {
                    $("#txtUname").val("");
                    $("#txtUpwd").val("");
                    $("#Login_errorInfo").html("");
                    DivShow("#" + id, "#blk2", "#close2", -120, -100);
                }
                else if (message == "error") {
                    $("#font_info").html("收藏夹已存在");
                    $("#blk2").hide();
                    DivShow("#" + id, "#blk3", ".close3", -120, -100);
                }
                else {
                    $("#font_info").html("收藏商品成功");
                    $("#blk2").hide();
                    DivShow("#" + id, "#blk3", ".close3", -120, -100);
                }
            }
        }
        $.ajax(options);
    }

    function DivShow(id, id2, id3, xValue, yValue) {
        var t2 = new PopupLayer({ trigger: id, popupBlk: id2, closeBtn: id3,
            offsets: {
                x: xValue,
                y: yValue
            }
        });
        if (t2.isSetPosition) {
            t2.setPosition(t2.trigger.offset().left + t2.options.offsets.x, t2.trigger.offset().top + t2.trigger.get(0).offsetHeight + t2.options.offsets.y);
        }
        t2.options.useOverlay ? t2._loadOverlay() : null;               //如果使用遮罩则加载遮罩元素
        (t2.isOverlay && t2.options.useOverlay) ? t2.overlay.show() : null;
        if (t2.isDoPopup && (t2.popupLayer.css("display") == "none")) {
            t2.options.useFx ? t2.doEffects("open") : t2.popupLayer.show();
        }
    }
