﻿var merchantPriceFlag = true;

$(function() {

    fillHeader();

    $('.buy_btn').click(function() {
        window.open('http://mall.sina.com.cn/sp/addtocart.aspx?merchantid=' + $(this).attr('merchantid') + '&productid=' + $(this).attr('productid') + '&cname=atoncecart');
    });
    $('.add_btn').click(function() {
        window.open('http://mall.sina.com.cn/SP/AddToCart.aspx?productid=' + $(this).attr('productid') + '&merchantid=' + $(this).attr('merchantid'));
    });

    $('.seek_tel').click(function() {
        window.open('http://mall.sina.com.cn/CSM/TelDetails.aspx?MerchantID=' + $(this).attr('merchantid') + '&ProductId=' + $(this).attr('productid'));
    });

    $('#merchant-service-info').click(function() {
        window.open('http://malladmin.sina.com.cn/SINASYS/PAGE/Service.aspx?MID=' + $(this).attr('merchantid'));
    });

    $('.multi-merchant-service').click(function() {
        window.open('http://malladmin.sina.com.cn/SINASYS/PAGE/Service.aspx?MID=' + $(this).attr('merchantid'));
    });


    $('a[fav="product"]').click(function() {
        var remoteUrl = 'http://mall.sina.com.cn/sp/cartopenapi.ashx?action=addprofav&pid=' + $(this).attr('productid');
        $.ajax({
            url: remoteUrl,
            type: 'get',
            cache: false,
            dataType: 'json',
            success: function(data, textStatus) {
                if (data.result + '' == 'true') alert('添加成功，请到个人中心查看');
                else alert('添加失败，' + data.msg);
            }
        });
    });


    $('#merchant-fav').click(function() {
        var remoteUrl = 'http://mall.sina.com.cn/sp/cartopenapi.ashx?action=addmerfav&mid=' + $(this).attr('merchantid');
        $.ajax({
            url: remoteUrl,
            type: 'get',
            cache: false,
            dataType: 'json',
            success: function(data, textStatus) {
                if (data.result + '' == 'true') alert('添加成功，请到个人中心查看');
                else alert('添加失败，' + data.msg);
            }
        });
    });



    $('a[class^=ship-fee-address]').click(function() {
        var merchantid = $(this).attr('specialid');
        var pos = $(this).offset();
        $('.fee-list[merchantid=' + merchantid + ']').css({ 'display': 'block', 'position': 'absolute', 'top': (pos.top - 390) + 'px', 'left': '10px', 'border': '1px solid #a70e07', 'z-index': '999', 'background': '#fff' });
    });

    $('.fee-address[content]').click(function() {
        var merchantid = $(this).parent().parent().attr('merchantid');
        $('.ship-fee-address-' + merchantid).html($(this).children('span').html());
        $('#ship-fee-price-' + merchantid).html($(this).attr('content'));
        $('.fee-list[merchantid=' + merchantid + ']').css({ 'display': 'none' });
    });


    $('#merchant-order').change(function() {
        var order = $(this).val();
        $('.malls_datas').css({ 'display': 'none' });
        $('#merchant-order-' + order).css({ 'display': 'block' });
    });


    var browsedProducts = new Array();
    var cookieStr = $.cookie('browsedProducts');
    if (cookieStr != null && cookieStr != '') {
        for (i = 0; i < cookieStr.split(';').length - 1; i++) {
            if (cookieStr.split(';')[i].split(',')[0] != product.productid) {
                var op = new Object();
                op.productid = cookieStr.split(';')[i].split(',')[0];
                op.price = cookieStr.split(';')[i].split(',')[1];
                op.image = cookieStr.split(';')[i].split(',')[2];

                browsedProducts.push(op);
            }
        }
    }
    if (product != null)
        browsedProducts.unshift(product);
    var newCookieStr = '';
    for (a = 0; a < browsedProducts.length; a++) {
        newCookieStr += browsedProducts[a].productid + ',' + browsedProducts[a].price + ',' + browsedProducts[a].image + ';';
    }
    $.cookie('browsedProducts', newCookieStr);


    $('.malls_datas a[merchantid]').each(function(i, n) {
        var merchantPriceElement = '#merchantPrice-' + $(this).attr('merchantid');
        var merchantShopUrl = $(this).attr('href');
        $(this).hover(function() {

            $('#popDr').html('<a href="' + merchantShopUrl + '" target="_blank" class="blue"><b class="f14">' + $(merchantPriceElement + ' b[class="f14"] span').html() + '</b></a><p class="red"><b>价格：￥' + $(merchantPriceElement + ' strong[class="price"]').html() + '</b></p>');

            $('#popDr').css({ 'display': 'none' });
            $('.pop_pdtBlock').each(function() { $(this).fadeOut('fast') });

            var pos = $(this).offset();
            $('#popDr').css({ 'top': (pos.top - 385) + 'px' });
            $('#popDr').fadeIn('slow');
            $(merchantPriceElement).fadeIn('slow');


            $('#popDr').hover(function() {
                merchantPriceFlag = true;
            }, function() {
                merchantPriceFlag = false;
                closeMerchant();
            });

            $(merchantPriceElement).hover(function() {
                merchantPriceFlag = true;
            }, function() {
                merchantPriceFlag = false;
                closeMerchant();
            });
        },
                  function() {

                  });
    });

});
function closeMerchant() {
    setTimeout(function() {
        if (!merchantPriceFlag) {
            $('#popDr').css({ 'display': 'none' }).html('');
            $('.pop_pdtBlock').each(function() { $(this).fadeOut('fast') });
        } 
    }, 100);
}
 