// 敬請期待浮動視窗顯示
var errorHeaderBlock = function() {
    $.floatMessanger({
        title: 'Oops！',
        message: '敬請期待',
        buttons: {
            '確認': function () { $.unblockUI(); },
            '客服中心': function () { location.href = $frontendVars.baseUrl + '/service/form'; }
        }
    });
}

// 浮動購物車效果
var floatCartToggleEffect = function () {
    $('#floatCart').queue('fx');
    if ($('#floatCartClose:visible').size()) {
        $('#floatCart').animate({'right': '-=90px'}, 'slow').css('zIndex', 1);
        $('#floatCartClose').hide();
        $('#floatCartOpen').show();
        $('#floatCart3000').hide();

        $.cookie('floatCartOpen', false, { expires: 365, path: '/' });
    } else {
        $('#floatCart').animate({'right': '+=90px'}, 'slow').css('zIndex', 100);
        $('#floatCartOpen').hide();
        $('#floatCartClose').show();
        $('#floatCart3000').show();


        $.cookie('floatCartOpen', true, { expires: 365, path: '/' });
    }
    return false;
}

$(function () {

    $(window).load(function () {
        var imagePath = $frontendVars.imageWebPath + '/html';
        MM_preloadImages(imagePath + '/menu1_on.gif', imagePath + '/menu2_on.gif', imagePath + '/menu3_on.gif', imagePath + '/menu4_on.gif', imagePath + '/login_addmember_2_on.gif')
    });

    // 敬請期待訊息視窗提示
    $('.comingSoon').click(function () {
        errorHeaderBlock();
        return false;
    });

    // 隱藏所有預載的圖片
    $('#webPreloadPicture').hide();

    // 全域的希望購買＆加入購物車
    // 加入希望購買清單
    $('a.wishbuy').click(function () {
        var link = this;
        $.ajax({
            url: link.href,
            dataType: "json",
            success: function (json) {
                if (json.isLogin) {
                    if (json.repetition) {
                        // alert('此商品已在您的希望購物清單中！');
                        $.floatMessanger({
                            title: 'Oops！',
                            message: '此商品已在您的希望購物清單中！',
                            buttons: {
                                '確認': function () { $.unblockUI(); }
                            }
                        });
                    } else {
                        // alert('加入成功！');
                        location.href = $frontendVars.baseUrl + '/my/wishbuy';
                    }
                } else {
                    // alert('您尚未登入！');
                    // location.replace(link.href);
                    $.floatMessanger({
                        title: 'Oops！',
                        message: '您尚未登入！',
                        buttons: {
                            '確認': function () { location.replace(link.href); }
                        }
                    });
                }
            }
        });
        return false;
    });

    $('a.cart, a.installment, a.buyAll').live('click', function () {
        // BlockUI 訊息樣式
        var messageStyle = { padding: '10px', fontSize: '9pt', width: '500px' };
        var link = this;
        $.blockUI({
            css: messageStyle,
            message: $('#cartLoadingBlockUIShow')
        });
        $.post(
            link.href,
            {},
            function (json) {
                var redirectUrl = $frontendVars.baseUrl + '/cart';
                if (json.isLogin) {
                    if (json.error) {
                        // alert(json.message);
                        // $.unblockUI();
                        $.floatMessanger({
                            title: 'Oops！',
                            message: json.message,
                            buttons: {
                                '確認': function () { $.unblockUI(); }
                            }
                        });
                    } else {
                        if (json.redirectUrl) {
                            redirectUrl = json.redirectUrl;
                        }
                        location.href = redirectUrl;
                    }
                } else {
                    // alert('您尚未登入！');
                    // location.replace(link.href);
                    $.floatMessanger({
                        title: 'Oops！',
                        message: '您尚未登入！',
                        buttons: {
                            '確認': function () { location.replace(link.href); }
                        }
                    });
                }
            },
            'json'
        );
        return false;
    });

    var sslControllers = $frontendVars.sslControllers;
    var sslActions = $frontendVars.sslActions;

    // 避免購物車相關步驟的所有對外連結變成 https
    if (0 <= $.inArray($frontendVars.controllerName, sslControllers)
            && 0 <= $.inArray($frontendVars.actionName, sslActions)) {
        $('a').each(function () {
            if (-1 === this.href.search(/\/step2/)) {
                this.href = this.href.replace(/^https:/, 'http:');
            }
        });
    }

    // 點選我的購物車，即出現更新購物車
    $('#showUpdateCart').click(function () {
        var messageStyle = { padding: '10px', fontSize: '9pt', width: '500px' };
        $.blockUI({
            css: messageStyle,
            message: $('#cartLoadingBlockUIShow')
        });
    });

    // 連結 2008-11-06 因為會造成點擊數不正確，暫時移除
    /*
    $('a').click(function () {
        try {
            var link = String(this.href);
            var queryString = link.substr(link.search(/\?/) + 1);
            var qs = new QueryString(queryString);
            var srcId = qs.get('SRCID', null);
            if (null !== srcId) {
                $.post(
                    $frontendVars.baseUrl + '/index/addsourceclick',
                    { 'SRCID': srcId }
                );
            }
            alert ($(this).attr('href'));
        } catch (e) {}
        return true;
    });
    */

    // 計算點閱數的連結
    $('a.clickCountLink').click(function () {
        try {
            var link = String(this.href);
            var queryString = link.substr(link.search(/\?/) + 1);
            var realLink = link.replace('?' + queryString, '');
            var qs = new QueryString(queryString);
            var clickType = qs.get('clickType', null);
            if (null !== clickType) {
                $.post(
                    $frontendVars.baseUrl + '/index/addClickCountLink',
                    { 'clickType': clickType, 'clickItemId': qs.get('clickItemId') }
                );
            }
        } catch (e) {}

        // 導向未加參數之連結
        if ('_blank' == $(this).attr('target')) {
            window.open(realLink);
        } else {
            location.href = realLink;
        }
        return false;
        // $(this).attr('href', realLink);
        // return true;
    });


    // 浮動購物車
    $('a#floatCartToggle').click(floatCartToggleEffect);
    // 利用 cookie 判斷是否顯示浮動購物車
    var floatCartOpen = $.cookie('floatCartOpen');
    if ('false' != floatCartOpen) {
        floatCartToggleEffect();
    }

});