﻿
/// <reference path="jquery1.4.2.js" />

var browser = {
    mozilla: $.browser.mozilla == true,
    opera: $.browser.opera == true,
    safari: $.browser.safari == true,
    ie6: $.browser.msie == true && $.browser.version == "6.0",
    ie7: $.browser.msie == true && $.browser.version == "7.0",
    ie8: $.browser.msie == true && $.browser.version == "8.0"
};

//如果要重置页面的js操作，请在页面上设置相应的值,
var init_setup = {
    ct_list: true,    //初始化显示列表
    resize_handle: true, //大小变更委托
    add_eidt_autoHeight: true, //编辑页自适应高度
    split: true, //列表页面分隔
    list_page: true, //列表页面脚本，如调整编辑页面大小等
    ct_list_style: true, //列表鼠标经过样式
    add_edit_page: true, //编辑页面脚本
    adsearch: true, //高级搜索,引用adsearch.js
    controls_style: true, //常用控件样式
    none: null//作为标识，不用管
};

$(function () {

    if (init_setup) {
        if (init_setup.ct_list)//列表固定表头
            init_ct_list();
        if (init_setup.resize_handle)//允许当前页面的iframe注册调整尺寸事件
            setup_resize_handle();
        if (init_setup.add_eidt_autoHeight)//编辑页自适应高度
            init_add_edit_resize();
        if (init_setup.list_page)//列表页面脚本，如调整编辑页面大小等
            list_page_init();
        if (init_setup.split)//列表页面分隔区域
            init_split();
        if (init_setup.ct_list_style)//列表鼠标经地样式
            ct_list_mouse_style_init()
        if (init_setup.controls_style)//列表鼠标经地样式
            controls_style_init()
        if (init_setup.adsearch)//高级搜索,引用adsearch.js
            adsearch_init()
        $("div.ct_list_ctn:eq(0)").each(function () {
            setup_ct_list(this);
        });
    }
    

});



//----------------------列表页面初始化--------------------------------------------
function list_page_init() {
    if (window.parent) {
        if ($.browser.msie && window.parent.bind_resize_event)//ie版本在拖动改变大小时，不会引发iframe里面的window.resize
        {
            if (window.parent != null && window.parent != undefined) {

       
                $(window.parent).resize(function () {
//                    if (window.lastWidth && window.lastHeight
//                    && window.lastWidth == $(window).width()
//                    && window.lastHeight == $(window).height()) {
//                    //-------不作处理
//                    } else {
//                        window.lastWidth = $(window).width();
//                        window.lastHeight = $(window).height()
//                        
//                    }
                    list_page_mainFrame_resize();
                });

                $(window).resize(function () {
                    if (window.lastWidth && window.lastHeight
                    && window.lastWidth == $(window).width()
                    && window.lastHeight == $(window).height()) {
                        //-------不作处理
                    } else {
                        window.lastWidth = $(window).width();
                        window.lastHeight = $(window).height()
                        list_page_mainFrame_resize();
                    }
                });
                $(window.top).resize(function () {
                    if ( window.lastHeight
                    
                    && window.lastHeight == $(window).height()) {
                        //-------不作处理
                    } else {
                        window.lastWidth = $(window).width();
                        window.lastHeight = $(window).height()
                        list_page_mainFrame_resize();
                    }
                });
            }
        }
        else
            $(window).resize(function () {
                list_page_mainFrame_resize();
            });
    }
    var ct = $(".split_ct:eq(0)");
    var below = ct.find(".split_below:eq(0)");
    below.bind("resize_height", function (event, height) {
        $(this).height(height);
        var iframe = $("#iframe_manage");
        iframe.height(height);
        tig_resize_event();
    });
}
function list_page_mainFrame_resize() {
    
    $("div.split_ct:eq(0)").each(function () {
        resize_split(this);
    });
  
}
//+++++++++++++++++++++++列表页面初始化++++++++++++++++++++++++++++++++++++++++++++++



//----------------------初始化页面上列表--------------------------------------------
var resize_delegates = [];
function bind_resize_event(obj) {
    var exist = false;
    $(resize_delegates).each(function () {
        if (obj == this) {
            exist = true;
            return false;
        }
    });
    if (!exist)
        resize_delegates.push(obj);
}
function tig_resize_event() {

    var errs = [];
    $(resize_delegates).each(function () {
        try {
            this();
        } catch (e) {
            errs.push(this);
        }
    });
    $.grep(resize_delegates, function () {
        var exist = false;
        var that = this;
        $(errs).each(function () {
            if (that == this) { exist = true; return false; }
        });
        return exist;
    });
    
}
function setup_resize_handle() {
    $(window).resize(tig_resize_event);
}
function unbind_resize_event(obj) {
    $.grep(resize_delegates, function () {
        return this != obj;
    });
}
//+++++++++++++++++++++++初始化页面上列表++++++++++++++++++++++++++++++++++++++++++++++




//----------------------初始化页面上列表--------------------------------------------
function init_ct_list() {//初始化页面上列表

    $("div.ct_list_ctn:eq(0)").each(function () {
        setup_ct_list(this);
    });
}
function setup_ct_list(obj) {


    var ctn = $(obj);

    if (ctn.attr("ct_list_setup") == "1")//如果为1则表示该元素已经设置过了。
        return;

    var listBody = $(ctn).find("table.ct_list_body:eq(0)"); //列表内容体，一般为table


    //    listBody.before("<div class='ct_list_body_ctn'></div>");//列表内容体的容器，

    var body_ctn = ctn.find("div.ct_list_body_ctn:eq(0)");


    //body_ctn.append(listBody);

    if (listBody.height() <= body_ctn.height() && false) {//如果显示所有内容后的高度小于设置的最小高度的话那么，则不对
        body_ctn.addClass("ct_list_body_ctn1");
    } else {

        var listBody_head = listBody.find("tr:eq(0)");
        var listBody_head_html = listBody_head.html();
        var table_empty = listBody_head.find("table.empty");
        if (table_empty.length > 0) {
            listBody_head_html = table_empty.find("tr:eq(0)").html();
        }

        var listHead_html = String.format("<table class='ct_list ct_list_head'><tr>{0}</tr></table>", listBody_head_html);
        body_ctn.before(listHead_html);


        ctn.attr("ct_list_setup", "1");

        resize_ct_list(obj);

        $(window).resize(function () {
            resize_ct_list(obj)
        });
    
        ctn.parent("div.split_top:eq(0)").bind("resize_height", function (event, height) {
            var cur_height = $(this).height(); //当前高度
            var cut_height = height - cur_height; //设置后的高度和当前高度差
            var differ_height = body_ctn.height(); //需要改变尺寸的元素当前高底,这里一般为列表显示的div
            body_ctn.height(differ_height + cut_height - 2); //将高度差设置到元素
        });
    }
    ctn.find("input.cbSel").click(function () {
        if (this.checked)
            ctn.find("input:checkbox").attr("checked", "checked");
        else
            ctn.find("input:checkbox").attr("checked", "");
    });

}
function resize_ct_list(obj) {



    var isIE6_7 = ($.browser.version == "6.0" || $.browser.version == "7.0");
    var offset = isIE6_7 ? 15 : 16;
     
    var ctn = $(obj);
    var listBody = $(ctn).find("table.ct_list_body:eq(0)");
    var listHead = $(ctn).find("table.ct_list_head:eq(0)");
    var body_ctn = ctn.find("div.ct_list_body_ctn:eq(0)");
    var listBody_head = listBody.find("tr:eq(0)");
    if (body_ctn.attr("lastWidth") != undefined && body_ctn.attr("lastWidth") == body_ctn.width())
        return;

    body_ctn.attr("lastWidth", body_ctn.width());
    var listBody_ths = listBody.find("th");
    var listHead_ths = listHead.find("th");

    listBody.css({ "margin-top": -listBody_head.height() - 3 });
    listBody.width($(listBody[0].parentNode).width() - offset);
    listHead.width($(listBody[0].parentNode).width() - offset);
    listBody.css({ "margin-top": -listBody_head.height()-3 });

    

  
 
    for (var i = 0; i < listBody_ths.length; i++) {
        $(listHead_ths[i]).width($(listBody_ths[i]).width());
    }

}


//++++++++++++++++++++++++++++初始化页面上列表++++++++++++++++++++++++++++


//----------------------初始化编辑页面，主要是自适应高度--------------------------------------------
function setup_add_edit_resize() {
 
    var ct = $("div.add_edit_ct");
    var add_edit = ct.find("div.add_edit");
    var body_ct = ct.find("div.add_edit_body_ct");
    var isIE6_7 = ($.browser.version == "6.0" || $.browser.version == "7.0");
    var offset = isIE6_7 ? 15 : 16;
    add_edit.width(ct.width() - offset);
    var main_iframe = "#iframe_manage";
 
    var iframe_height = $(window.parent.document).find(main_iframe).height(); //外iframe的高度，所有的运算都按这个高度来算
    var ct_height = ct.height(); //当前高度
    var body_ct_height = body_ct.height();
    var cut_height = ct_height - body_ct_height;
    body_ct.height(iframe_height - cut_height - 4)
}

function init_add_edit_resize() {
    if ($("div.add_edit_ct")) {
        setup_add_edit_resize();
        $(window).resize(setup_add_edit_resize);
        if ($.browser.msie && window.parent.bind_resize_event)//ie版本在拖动改变大小时，不会引发iframe里面的window.resize
        {
            var _setup_add_edit_resize = setup_add_edit_resize;
            window.parent.bind_resize_event(_setup_add_edit_resize);
            $(window).unload(function () {
                window.parent.unbind_resize_event(_setup_add_edit_resize);
            });
        }
        else
            $(window).resize(function () {
                setup_add_edit_resize();
            });
    }
}

//++++++++++++++++++++++++++++初始化编辑页面，主要是自适应高度++++++++++++++++++++++++++++


//----------------------初始化列表页面分隔层--------------------------------------------


function init_split() {

    $("div.split_ct:eq(0)").each(function () {
        setup_split(this);
    });
}

function setup_split(obj) {
    var ct = $(obj);
    var up = ct.find("div.split_line:eq(0) .up img");
    var down = ct.find("div.split_line:eq(0) .down img");
    var top = ct.find("div.split_top:eq(0)");
    var below = ct.find("div.split_below:eq(0)");
    if (!top.hasClass("dn_")) {
        up.show();
    }
    if (!below.hasClass("dn_"))
        down.show();
    up.click(function () {
   
        if (below.hasClass("dn_")) {
            //如果下方区域状态为隐藏，则先显示下方区域
            below.removeClass("dn_");
            down.show();
        } else {
            top.addClass("dn_");
            up.hide();
        }
        if (!browser.ie6)
            resize_split(obj);
    });
    down.click(function () {
        if (top.hasClass("dn_")) {
            //如果上方区域状态为隐藏，则先显示下方区域
            top.removeClass("dn_");
            up.show();
        } else {
             below.addClass("dn_");
             down.hide();
        }
        if (!browser.ie6)
            resize_split(obj);
    });
    resize_split(obj);
}
function resize_get_window_height() {
    var doc_height = 0;
    $(window.parent.document).find("iframe").each(function () {
        if ($(this).contents().find("body")[0] == document.body) {
            doc_height = $(this).height(); return false;
        }
    });
    if (doc_height == 0)
        doc_height = $(document).height();
    return doc_height;
}

function resize_split(obj) {
 
    var top_maxHeight = 0; //设置上下区域高度
    var below_maxHeight = 0;

    var top_result_height = 0; //上下两个结果高度
    var below_result_height = 0;

    var ct = $(obj);
    var line = ct.find("div.split_line:eq(0)");
    var top = ct.find("div.split_top:eq(0)");
    var below = ct.find("div.split_below:eq(0)");


    
    var doc_height = resize_get_window_height(); //外iframe的高度，所有的运算都按这个高度来算
    var body_height = $(document.body).height(); //当前body高度

    var over_height = doc_height - body_height; //空白地址高度
    var line_height = line.height(); //上下分隔条高度
    var allow_height = ct.height() + over_height - line_height; //允许上下区域显示的高度

    if (top.attr("max_height") != undefined && false) //max_height用来标识最大的高度，如果有值，则在分配高度的时候优先使用
        top_maxHeight = parseInt(top.attr("max_height"));

    if (below.attr("max_height") != undefined && false) 
        below_maxHeight = parseInt(below.attr("max_height"));
 

    if (top.hasClass("dn_") || below.hasClass("dn_")) {
        //如果上下有一个区域不可见，则高度全部分配给可见的区域
        if (!top.hasClass("dn_"))
            top_result_height = allow_height;
        if (!below.hasClass("dn_"))
            below_result_height = allow_height;
    }
    else {
        //如果设置了max_height（最高高度）则先设置最高高度区域
        //如果没有设置则平分高度
        if (below_maxHeight > 0 && allow_height > below_maxHeight) {
            below_result_height = below_maxHeight;
            top_result_height = allow_height - below_maxHeight;
        } else if (top_maxHeight > 0 && allow_height > top_maxHeight) {
            top_result_height = top_maxHeight;
            below_result_height = allow_height - top_maxHeight;
        } else {
            top_result_height = below_result_height = allow_height / 2 | 0;
        }
    }
    
    //触发到事件,这里只观注两个元素，如果内部需要调整大小则通过绑定事件进行处理
    top.trigger("resize_height", top_result_height);
    below.trigger("resize_height", below_result_height);
}


//+++++++++++++++++++++++初始化列表页面分隔层++++++++++++++++++++++++++++++++++++++++++++++



//设置tab的样式-----------------------------------开始
function ct_list_mouse_style_init() {
    SetTabStyle();
}
function SetTabStyle() {
    $("table.ct_list tr").mouseover(changecolor);
    $("table.ct_list tr").mouseout(changeback);
    $("table.ct_list tr").click(function () {
        $(this.parentNode).find("tr.rowsel:eq(0)").removeClass("rowsel")
        $(this).addClass("rowsel");
    });
}
var highlightcolor = '#eafcd5';
//此处clickcolor只能用win系统颜色代码才能成功,如果用#xxxxxx的代码就不行,还没搞清楚为什么:(
var clickcolor = '#51b2f6';

function changecolor(event) {
    try {
        event = event ? event : (window.event ? window.event : null);
        source = event.srcElement;
        if (source == null) return;
        if (source.tagName == "TR" || source.tagName == "TABLE" || source.tagName == "TH")
            return;
        var p = source.parentElement;
        while (source.tagName != "TD") {
            source = source.parentElement;
            if (source.tagName == "TH") return;
        }
        source = source.parentElement;
        cs = source.children;
        if (cs == null) return;
        if (cs.length > 1) {
            if (cs[1].style.backgroundColor != highlightcolor && source.id != "nc" && cs[1].style.backgroundColor != clickcolor)
                for (i = 0; i < cs.length; i++) {
                    cs[i].style.backgroundColor = highlightcolor;
                }
        }
    } catch (e) {

    }
}

function changeback(event) {
    try {
        event = event ? event : (window.event ? window.event : null);
        source = event.srcElement; //joe add this line
        if (source == null) return;
        if (event.fromElement.contains(event.toElement) || source.contains(event.toElement) || source.id == "nc")
            return
        if (source.tagName == "TH") return;
        if (cs == null) return;
        if (cs.length > 1) {
            if (event.toElement != source && cs[1].style.backgroundColor != clickcolor)
            //source.style.backgroundColor=originalcolor
                for (i = 0; i < cs.length; i++) {
                    cs[i].style.backgroundColor = "";
                }
        }
    } catch (e) {
    }
}
//设置tab的样式-----------------------------------结束

//---------------------------控件样式


function controls_style_init() {
    setup_button_style();
    setup_text_style();
    drag_init();
    setup_date_input_style();
    setup_tools_button_click();
    setup_number_input();
    setup_rs_button();//设置重复提交
}

function setup_number_input() {
    $("input.num").onlypressnum();
}

//设置日期输入控件样式
function setup_date_input_style() {
    var dttxt = $("input.dttxt");
    dttxt.addClass("Wdate");
    dttxt.click(function () { WdatePicker(); });
    var dttxt1 = $("input.dttxt1");
    dttxt1.addClass("Wdate");
    dttxt1.click(function () { WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss' }); });
}

//设置列表控件上面的按钮事件，主要是针对没有选择记录进行提示
function setup_tools_button_click() {
    $("div.list_command_ct").find("input.btDelete,input.btEnable,input.btDelete,input.btHalt,input.tools_need_checked").click(function () {
      
        return table_check_validator();

    });
}
function setup_rs_button() {
    $(":submit.rs_ctl").click(function () {
        var that = this;
        that.submiting = true
        setTimeout(function () {
            $(that).attr("disabled", "disabled");
        }, 1);
        setTimeout(function () {
            that.submiting = false;
            $(that).removeAttr("disabled");
        }, 10 * 1000);
    });
}
//设置按钮样式
function setup_button_style() {
    $("input.btn").each(function () {
        setup_button_style_item(this);
    });
}
function setup_button_style_item(obj) {
    $(obj).mouseover(function () {
        $(this).addClass("hover");
    }).mouseout(function () {
        $(this).removeClass("hover");
    });
}

//设置活动对像的样式
function setup_text_style() {
    $(":text,textarea").each(function () {
        setup_text_style_item(this);
    });
}
function setup_text_style_item(obj) {
    $(obj).focus(function () {
        $(this).addClass("focus");
    }).blur(function () {
        $(this).removeClass("focus");
    });
}


//初始化拖动层
function drag_init() {
    $("div.drag").each(function () {
        drag_setup(this);
    });
}
function drag_setup(obj) {
    var drag = $(obj);
    
    drag.bgIframe();
    drag.easydrag();
    var handler = drag.attr("handler");
    if (handler != "") {
        drag.setHandler(drag.attr("handler"));
    }
    drag.bind("open", function (event) {
        if ($(this).attr("mark") != undefined) {
            mark_open();
        }

        $(this).css({
            left: ($(window).width() - $(this).width()) / 2,
            top: ($(window).height() - $(this).height()) / 2
        });
        $(this).show();
     
    }).bind("close", function (e) {
        if ($(this).attr("mark") != undefined) {
            mark_close();
        }
        $(this).hide();
    });
    drag.find(".close").click(function () {
        drag.trigger("close");
    });
}
//蒙蔽层
function mark_open() {
    var mark = $("#div_mark");
    if (mark.length == 0) {
        mark = $("<div id='div_mark' class='mark'></div>");
        $(document.body).append(mark);
        mark.bgiframe();
    }
    mark.css({
        width: $(window).width(),
        height: $(window).height(),
        left: 0,
        top: 0
    });

    mark.show();
}
function mark_close() {
    var mark = $("#div_mark");
    mark.hide();
}

String.format = function (source, params) {
    if (arguments.length == 1)
        return function () {
            var args = $.makeArray(arguments);
            args.unshift(source);
            return $.format.apply(this, args);
        };
    if (arguments.length > 2 && params.constructor != Array) {
        params = $.makeArray(arguments).slice(1);
    }
    if (params.constructor != Array) {
        params = [params];
    }
    $.each(params, function (i, n) {
        source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
    });
    return source;
};



//+=============================



