﻿
$.fn.extend({
    SetTabs: function() {
        this.each(function() {
            var _this = this;
            $("li", _this).click(function() {
                $("li", _this).removeClass("TabsOn");
                $(this).addClass("TabsOn");
                var thisIndex = $("li", _this).index($(this)[0]);
                $(_this).parent().next().children("div").hide();
                $(_this).parent().next().children("div:eq(" + thisIndex + ")").show();
            });
        });
    }
});


//显示登录、注册
function AccountsOprator() {
    $('#AccountsContent').modal({ onClose: function(dialog) {
        $.modal.close();
    },
        onShow: function(dialog) {
            //展示事件
            var url = $('#txtAccountsUrl').val();
            var myurl = window.location.href;
            url = url + "/modal/login.aspx?return=" + myurl + "&v=" + Math.random();
            $('#AccountsContent iframe').attr("src", url);
        }
    });
}


function RefCartCount() {

    var turl = '/ajax/cartcount.aspx?v=' + Math.random();

    //页头购物车数量
    setTimeout(function() {
        $.ajax({ url: turl,
            type: 'GET',
            dataType: 'html',
            timeout: 10000,
            error: function() {
            },
            success: function(html) {
                $("#hdCartCount").text(html);
            }
        })
    }, 2000);
    //end of ajax
}



$(document).ready(function() {

    $(".geTabs").SetTabs();


    //收藏
    if ($(".navFavoriteBtn").length > 0) {
        $(".navFavoriteBtn").click(function() {
            var _this = $(this);
            var id = $(this).attr("Tip");
            var turl = '/ajax/productfavorites.aspx?id=' + id + '&v=' + Math.random();

            //关闭事件
            $.ajax({ url: turl,
                type: 'GET',
                dataType: 'html',
                timeout: 10000,
                error: function() {
                    alert('收藏失败');
                },
                success: function(html) {
                    if (html == "-1") {
                        AccountsOprator();
                    }
                    else if (html == "0") {
                        alert("您已经收藏过此商品。");
                    }
                    else if (html == "1") {
                        alert("收藏成功");
                    }
                }
            });
            //end of ajax

            //alert(id);
        });
    }
    //收藏end

    if ($(".navAddCartBtn").length > 0) {
        $(".navAddCartBtn").simpletooltip({ customTooltip: function(tooltip) {
            var id = $(tooltip).attr("tip");
            var accountsUrl = $("#txtAccountsUrl").val();
            var wwwUrl = $("#txtWWWUrl").val();
            var count = 1;
            if ($("#txtCartCount").length > 0 && $("#txtBigCart", tooltip).length > 0) {
                count = $("#txtCartCount").val();
            }

            RefCartCount(); //刷新页头购物车数量

            var TipContent = '<iframe src="' + accountsUrl + '/shopping/addcart.aspx?id=' + id + '&count=' + count + '&v=' + Math.random() + '" frameborder="0" scrolling="no" width="298" height="85"></iframe>';
            return '<div class="navCartTip"><a rel="close" class="hrCartClose"><img src="' + wwwUrl + '/images/close.gif" /></a>' + TipContent + '</div>';
        }, showEffect: "fadeIn", hideEffect: "fadeOut", click: true
        });
    }









});
