// JavaScript Document
jQuery.fn.LoadImage = function(scaling, width, height, loadpic) {
    if (loadpic == null) loadpic = "/App_Images/loading.gif";
    return this.each(function() {
        var t = $(this);
        var src = $(this).attr("src")
        var img = new Image();
        var load_img = (height - 32) / 2;
        //alert("Loading")
        img.src = src;
        //自动缩放图片
        var autoScaling = function() {
            if (scaling) {
                if (img.width > 0 && img.height > 0) {
                    if (img.width / img.height >= width / height) {
                        if (img.width > width) {
                            t.width(width);
                            t.height((img.height * width) / img.width);
                            var m_top = (height - (img.height * width) / img.width) / 2;
                            t.css({ "margin-top": m_top })
                        } else {
                            t.width(img.width);
                            t.height(img.height);
                            var m_top = (height - img.height) / 2;
                            t.css({ "margin-top": m_top })
                        }
                    } else {
                        if (img.height > height) {
                            t.height(height);
                            t.width((img.width * height) / img.height);
                            var m_top = (height - height) / 2;
                            t.css({ "margin-top": m_top })
                        } else {
                            t.width(img.width);
                            t.height(img.height);
                            var m_top = (height - img.height) / 2;
                            t.css({ "margin-top": m_top })
                        }
                    }
                }
            }
        }
        //处理ff下会自动读取缓存图片
        if (img.complete) {
            //alert("getToCache!");
            autoScaling();
            return;
        }
        $(this).attr("src", "");
        var loading = $("<img alt=\"loading...\" title=\"loading...\" style=\"margin-top:" + load_img + "px;\" src=\"" + loadpic + "\" />");

        t.hide();
        t.after(loading);
        $(img).load(function() {
            autoScaling();
            loading.remove();
            t.attr("src", this.src);
            t.show();
            //alert("finally!")
        });

    });
}

$(document).ready(function() {
    var banner_h = $("#banner img").height();
    $("#banner").height(banner_h);
    var logo_h = $(".logo_img").height();
    var logo_img_h = $(".logo_img img").height(logo_h);
    var logo_img_w = $(".logo_img img").width();
    $(".logo_img").width(logo_img_w);
    $(".home_protop_img img,.home_pro_img img").LoadImage(true, 148, 148);
    $(".intro_img img").LoadImage(true, 200, 200);
    $(".pros_list_img img,.markets_list_img img").LoadImage(true, 120, 120);
    $(".pro_img img,.market_img img").LoadImage(true, 350, 350);
    $("#rightmain .index_img img").LoadImage(true, 200, 200);
    $("#rightmain .img7 img").LoadImage(true, 118, 118);
    $("ul.productlist .img2 img,ul.marketlist .img4 img").LoadImage(true, 120, 120);
    $(".img3 img").LoadImage(true, 350, 350);
    $("ul#productList img.btn").toggle(
        function(){
          $(this).attr({src:"../../images/away.gif"}).parent().next("ul.child").hide();
        },
        function(){
          $(this).attr({src:"../../images/start.gif"}).parent().next("ul.child").show();
        }
    )
});

function collectCompanies(cid, uid) {
    $.ajax({
        type: 'POST', //用POST方式传输
        dataType: 'json', //数据格式:JSON
        url: '../js/collectCompanies.ashx', //目标地址
        data: 'cid=' + cid + '&uid=' + uid + '&r=' + Math.random(),
        beforeSend: function() { //debugger
            $('body').append('<div id="divload">正在加载......</div>');
        }, //发送数据之前
        complete: function() {
            $("#divload").remove();
        }, //接收数据完毕
        success: function(json) {
            if (json.sum == 'true') {
                alert('已收藏该企业,请重新选择！');
                return;
            }
            if (json.ok == 'true') {
                alert('企业收藏成功！');
                return;
            }
            if (json.usersum == 'true') {
                alert('不能收藏自己的企业！');
            }
            else {
                alert('企业收藏失败！');
                return;
            }
        }
    });
}
