﻿$(function() {

    fillHeader();

    showBrowsedProduct();

    $('#pricerange-do-search').click(function() {
        if ($('#pricerange-minvalue').val() == '' || $('#pricerange-maxvalue').val() == '' || isNaN($('#pricerange-minvalue').val()) || isNaN($('#pricerange-maxvalue').val())) {
            alert('请输入正确的价格区间');
        }
        else {
            window.top.location = 'http://mall.sina.com.cn/Search/ProductList.aspx?c=' + $('#page-go-input').attr('classid') + '&minprice=' + $('#pricerange-minvalue').val() + '&maxprice=' + $('#pricerange-maxvalue').val();
        }
    });

    $('#do-order').change(function() {
        window.location = 'http://mall.sina.com.cn/Search/ProductList.aspx?c=' + $('#page-go-input').attr('classid') + '&o=' + $(this).val();
    });


    $('#page-go').click(function() {
        var pid = $('#page-go-input').val();
        var cid = $('#page-go-input').attr('classid');
        var aid = $('#page-go-input').attr('agentid');
        var mid = $('#page-go-input').attr('merchantid');
        var url = 'list-' + cid;
        if (parseInt(pid)) {
            if (parseInt(pid) != 1) url += '-' + pid;
            if (parseInt(mid) != 0) url += '-m' + mid;
            if (parseInt(aid) != 0) url += '-a' + aid;
            url += '.html';
            window.open(url);
        }
    });

    $('.product-fav').click(function() {
        var remoteUrl = 'http://mall.sina.com.cn/sp/cartopenapi.ashx?action=addprofav&mid=' + $(this).attr('merchantid') + '&pid=' + $(this).attr('productid');
        $.ajax({
            url: remoteUrl,
            type: 'get',
            cache: false,
            dataType: 'json',
            success: function(data, textStatus) {
                //alert(this.url);
                if (data.result + '' == 'true') alert('添加成功，请到个人中心查看');
                else alert('添加失败，' + data.msg);
            }
        });
    });


    var viewMode = $.cookie('malllistviewmode');
    if (viewMode != null && viewMode != '' && viewMode == 'block') setViewBlock();
    $('#list-view-button').click( setViewList );
    $('#block-view-button').click( setViewBlock );

});

function showBrowsedProduct() {
    var browsedProducts = new Array();

    var cookieStr = $.cookie('browsedProducts');

    if (cookieStr != null && cookieStr != '') {
        for (i = 0; i < (cookieStr.split(';').length - 1 > 8 ? 8 : cookieStr.split(';').length - 1); i++) {
            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);
        }
    }

    var browsedProductHtml = '';

    for (i = 0; i < browsedProducts.length; i++) {
        browsedProductHtml += '<div style="margin-left:' + (i % 2 == 0 ? '5' : '18') + 'px;float:left;" class="picbox fl"><div class="pic"><a href="product-' + browsedProducts[i].productid + '.html" target="_blank"><img src="' + browsedProducts[i].image + '" onload="DrawImage(this,60,60)" width="60" height="60" alt="" /></a></div><span class="money">￥</span><a href="#" target="_blank" class="txt_m">' + browsedProducts[i].price + '</a></div>';
    }
    $('#browsedProduct').html(browsedProductHtml);
}

function setViewList() {
    $.cookie('shoplistviewmode', 'list');
    $('#block-view-button').removeClass('pic_disp_on');
    $('#list-view-button').addClass('list_disp_on');
    $('#list-wrapper').removeClass('col_5');

    $('.pictable_R').each(function() { $(this).css({ 'display': 'none' }); });
    $('.pictable_R_2').each(function() { $(this).css({ 'display': 'block' }); });
}

function setViewBlock() {
    $.cookie('shoplistviewmode', 'block');
    $('#list-view-button').removeClass('list_disp_on');
    $('#block-view-button').addClass('pic_disp_on');
    $('#list-wrapper').addClass('col_5');

    $('.pictable_R_2').each(function() { $(this).css({ 'display': 'none' }); });
    $('.pictable_R').each(function() { $(this).css({ 'display': 'block' }); });

    $('.product_img img').each(function() {
        var image = new Image();
        image.src = $(this).attr('src');
        var maxwidth = $(this).attr('width') - 20;
        var maxheight = $(this).attr('height') - 20;

        if ($(this).attr('set') != 'true' && image.width > 0 && image.height > 0) {
            if (image.width / image.height >= maxwidth / maxheight) {  //宽大于长
                if (image.width > maxwidth) $(this).width(maxwidth).height(parseInt((image.height * maxwidth) / image.width)).css({ 'margin-top': parseInt((maxheight - $(this).height()) / 2) + 'px' });
                else $(this).width(image.width).height(image.height).css({ 'margin-top': parseInt((maxheight - $(this).height()) / 2) + 'px', 'margin-left': parseInt((maxwidth - $(this).width()) / 2) + 'px' });
            }
            else {               //长大于宽
                if (image.height >= maxheight) $(this).width(parseInt((image.width * maxheight) / image.height)).height(maxheight).css({ 'margin-left': parseInt((maxwidth - $(this).width()) / 2) + 'px' });
                else $(this).width(image.width).height(image.height).css({ 'margin-top': parseInt((maxheight - $(this).height()) / 2) + 'px', 'margin-left': parseInt((maxwidth - $(this).width()) / 2) + 'px' });
            }
            $(this).attr('set', 'true');
        }
    });
    /* .each end */
}



function DrawImage(ImgD, maxwidth, maxheight) {
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= maxwidth / maxheight) //宽大于长
        {
            if (image.width > maxwidth)//如果宽度大于最大宽度
            {
                ImgD.width = maxwidth;
                ImgD.height = parseInt((image.height * maxwidth) / image.width);
                ImgD.style.marginTop = parseInt((maxheight - ImgD.height) / 2) + 'px';  //此时需要设置垂直居中
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
                ImgD.style.marginLeft = parseInt((maxwidth - ImgD.width) / 2) + 'px';
                ImgD.style.marginTop = parseInt((maxheight - ImgD.height) / 2) + 'px';
            }
        }
        else											 //长大于宽
        {
            if (image.height >= maxheight) {
                ImgD.height = maxheight;
                ImgD.width = parseInt((image.width * maxheight) / image.height);
                ImgD.style.marginLeft = parseInt((maxwidth - ImgD.width) / 2) + 'px';
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
                ImgD.style.marginLeft = parseInt((maxwidth - ImgD.width) / 2) + 'px';
                ImgD.style.marginTop = parseInt((maxheight - ImgD.height) / 2) + 'px';
            }
        }
    }
}