/**
 * 全局变量
 */
g_trigger=0;


/**
 * 弹出窗口
 */
function g_win(title,href,width,height){
    if(!height) height=$(window).height() - 80;
    if(!width) width=$(window).width() - 120;
    $('#dialog').dialog({
        title:title,
        width: width,
        height: height,
        closed: false,
        cache: false,
        href: href,
        modal: true
    });
}

//url：窗口调用地址，title：窗口标题，width：宽度，height：高度，shadow：是否显示背景阴影罩层
function g_win2(title, url,  width, height) {
    if(!height) height=$(window).height() - 80;
    if(!width) width=$(window).width() - 120;
    // var content = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
    var content = '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="no"></iframe>';
    var boarddiv = '<div id="dialog" title="' + title + '"></div>'//style="overflow:hidden;"可以去掉滚动条
    $(document.body).append(boarddiv);
    var win = $('#dialog').dialog({
        content: content,
        width: width,
        height: height,
        modal: true,
        title: title,
        onClose: function () {
            $(this).dialog('destroy');//后面可以关闭后的事件
        }
    });
    win.dialog('open');
}


/**
 * 序列化表单 serializeObject
 * @param form
 * @returns {___anonymous78_79}
 */
function g_sobj(form) { /*将form 表单元素的值序列化*/
    var obj = {};
    $.each(form.serializeArray(), function (index) {
        if (obj[this['name']]) {
            obj[this['name']] = obj[this['name']] + "," + this['value'];
        } else {
            obj[this['name']] = this['value'];
        }
    });
    return obj;
}

/*前端公共js*/
function g_ajax(url,p,callback,async,errorCallback){
    if(async) async=true;
    $.ajax({async:async, type: 'post', dataType: 'json', url:url, data:p,
        success: function (json) {callback(json) },
        error: function(event, jqXHR, options, errorMsg){
            if(errorCallback) errorCallback(event, jqXHR, options, errorMsg);
        }
    });
}
function g_multiLineFn(val, row, idx){
    if(val != undefined){
        var vals = val.split(","), str = "";
        for(var i in vals){  str += vals[i]  }
    }
    return str;
}
function g_comm_back(json){//回调函数
    if (json.success == true) {
        layer.msg(json.msg, {icon: 1, time: 1000});
    } else {
        layer.alert(json.msg, {icon: 2});
    }
}

function g_msg(msg){
    layer.msg(msg, {icon: 1, time: 2000});
}

/*关闭弹出框口*/
function layer_close(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}


/*弹出层*/
/*
 参数解释：
 title	标题
 url		请求的url
 id		需要操作的数据id
 w		弹出层宽度（缺省调默认值）
 h		弹出层高度（缺省调默认值）
 */
function layer_show(title,url,w,h){
    if (title == null || title == '') {
        title=false;
    }
    if (w == null || w == '') {
        w=800;
    }
    if (h == null || h == '') {
        h=($(window).height() - 50);
    }
    layer.open({
        type: 2,
        area: [w+'px', h +'px'],
        fix: false, //不固定
        maxmin: true,
        scrollbar: false,//屏蔽游览器滚动条
        shade:0.4,
        title: title,
        content: url
    });
}


$(document).ajaxStart(g_onStart)
    .ajaxComplete(g_onComplete)
    .ajaxSuccess(g_onSuccess)
    .ajaxError(g_onError);

function g_onStart(event) {
    //加载层-默认风格
    //g_layer = layer.load(1); //layer.load();
    //判断服务器session是否存在

    // var h = document.body.clientHeight;
    // $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:h}).appendTo("body");
    layer.load();//1 2
//	layer.msg('加载中...', {icon: 16});
//     $("<div class=\"datagrid-mask-msg\"></div>").html("<font style='font-size: 13px;'>服务器正在努力的处理，请稍候...</font>").appendTo("body").css({display:"block",
//         left:($(document.body).outerWidth(true) - 190) / 2, top:(h - 45) / 2,"background-color":"#fff","color":"#000","border":"0px","border-radius":"8px"});
}
function g_onComplete(event, xhr, settings) {
    // $('.datagrid-mask-msg').remove();
    layer.closeAll('loading');
    // console.log(xhr)
    // if (xhr.status == 302 ) {
    //     top.location.href="/";
    //     return;
    // }
//     $('.datagrid-mask').remove();
    //此处演示关闭
//	layer.close(g_layer);
    /*setTimeout(function(){
     layer.closeAll('loading');
     }, 88);*/
}
function g_onSuccess(event, xhr, settings) {
    /*try {
     var json = $.parseJSON(xhr.responseText);
     if(true==json.isSessionTimeOut){
     window.location.href="login.do";
     $.messager.alert("提示", "登录超时，请重新登录", "info", function () {});
     }
     } catch (e) {
     }*/
}
function g_onError(event, jqXHR, options, errorMsg){
    if(jqXHR.status=="200") return;
    layer.msg("连接服务错误",{icon:2,time:2000});
    console.log(errorMsg);
    console.log(event);
    console.log(jqXHR.status);
    console.log(options);
}
$.ajaxSetup({
    contentType : "application/x-www-form-urlencoded;charset=utf-8",
    complete : function(xhr, textStatus) {
        // $.ts.hideLoading();
        //session timeout
        if (xhr.status == 911 ) {
            top.location.href="/";
            return;
        }
    }
    /*,
     error: function(jqXHR, textStatus, errorThrown){
     if(jqXHR.status==404){
     $.messager.alert("提示", "连接服务器错误，错误代码："+jqXHR.status, "info", function () {});
     }
     } */
});


$(document).ready(function(){

});