/// <reference path="jquery-vsdoc.js" />


if (document.all) document.execCommand("BackgroundImageCache", false, true);




///全局函数

//信息提示
function ShowMessage(msgbody) {
    if (typeof (msgbody) == "string")
        alert(msgbody);
    else if (typeof (msgbody) == "object")
        alert(msgbody.Message);
}

//前台转向
function Redirect(url) {
    var target = window;
    if (!IsNullOrEmpty(arguments[1]))
        target = eval(arguments[1].replace('_', ''));
    if (IsNullOrEmpty(target))
        target = window;

    if (target.href == url)
        PageReload();
    else
        target.location.href = url;
}

//前台页面重新加载
function PageReload() {
   location.href=location.href;
}

//判空
function IsNullOrEmpty(obj) {
    return obj == null || typeof (obj) == 'undefined' || obj == '';
}

///全局事件

//行首checkbox全选
function CheckboxSelectAll(obj) {
    var table = $(obj).parent().parent().parent();
    table.find("tr:not(:first-child) td input[type=checkbox]").attr("checked",$(obj).attr("checked"));
}


$(function() {

    //绑定行首_select_all checkbox
    $("input[name=_select_all][type=checkbox]").click(function() {
        CheckboxSelectAll(this);
    });


    //图片延迟加载
    //$.getScript(cfg_jsbase + 'jquery.lazyload.mini.js', function() {
    
        //图片自动缩放
        $(".autosize").each(function() {
            ImageAutoSize(this);
        });
        
        //$("img:not(.preload)").lazyload({
        //    placeholder: cfg_imgbase + "grey.gif",
        //    effect: "fadeIn"
        //});

    //}, true);
});

function ImageAutoSize(target) {

    $(target).attr({
        oldheight: $(target).attr("height"),
        oldwidth: $(target).attr("width")
    });

    var width = $(target).parent().innerWidth();
    var height = $(target).parent().innerHeight();
    var img_ow = $(target).outerWidth(true);
    var img_oh = $(target).outerHeight(true);
    var img_w = $(target).width();
    var img_h = $(target).height();
    var img_bw = img_ow - img_w;
    var img_bh = img_oh - img_h;


    var p = img_h / img_w;

    if (img_ow > width && img_oh > height) {
        if (Math.floor(p * (width - img_bw)) + img_bw > height) {
            $(target).height(height - img_bh);
            $(target).width(Math.floor((height - img_bh) / p));
        }
        else {
            $(target).width(width - img_bw);
            $(target).height(Math.floor((width - img_bw) * p));
        }
    } else if (img_ow > width) {
        $(target).width(width - img_bw);
        $(target).height(Math.floor((width - img_bw) * p));
    } else if (img_oh > height) {
        $(target).height(height - img_bh);
        $(target).width(Math.floor((height - img_bh) / p));
    }

    //居中
    $(target).css("margin-top", Math.floor(($(target).parent().innerHeight() - $(target).height()) / 2) + 'px');
    $(target).parent().css("text-align","center");
}



//string format
String.prototype.format = function() {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,
        function(m, i) {
            return args[i];
        });
}

String.format = function() {
    var args = arguments;

    if (args.length == 0)
        return null;

    var str = args[0];

    return str.replace(/\{(\d+)\}/g,
        function(m, i) {
            return args[parseInt(i) + 1];
        });
} 
