jQuery(function() {
    // 默认设置
    var JQ = jQuery.noConflict();

    // tr背景CSS
    jQuery(".tr_ons").hover(function() {
        jQuery(this).addClass("tr_on");
    },
    function() {
        jQuery(this).removeClass("tr_on");
    });

    // 全选
    jQuery("#allbutton").toggle(function() {
        jQuery("input[type='checkbox']").each(function() {
            if ("comlib" != jQuery(this).attr("value")) { // 区分从页面单位选择的那个checkbox
                jQuery(this).get(0).checked = "checked";
            }
        });
        jQuery("#allbutton").text("取消").val("取消");
    },
    function() {
        jQuery("input[type='checkbox']").each(function() {
            if ("comlib" != jQuery(this).attr("value")) {
                jQuery(this).get(0).checked = "";
            }
        });
        jQuery("#allbutton").text("全选").val("全选");
    });

    // 全选二种
    jQuery("#allbutton2").toggle(function() {
        jQuery(".isallcheckbox").each(function() {
            jQuery(this).get(0).checked = "checked";
        });
        jQuery("#allbutton2").text("取消").val("取消");
    },
    function() {
        jQuery(".isallcheckbox").each(function() {
            jQuery(this).get(0).checked = "";
        });
        jQuery("#allbutton2").text("全选").val("全选");
    });
    
        // 反选
    jQuery("#inverseButton").click(function() {
        jQuery("input[type='checkbox']").each(function() {
            if(jQuery(this).get(0).checked){
                jQuery(this).get(0).checked = "";
            } else {
	            jQuery(this).get(0).checked = "checked";
            }
        });
    });
        // 反选2
    jQuery("#inverseButton2").click(function() {
        jQuery(".isallcheckbox").each(function() {
            if(jQuery(this).get(0).checked){
                jQuery(this).get(0).checked = "";
            } else {
                jQuery(this).get(0).checked = "checked";
            }
        });
    });
    
        // 输入框只能输入数字和小数点
    jQuery(".isNumText").keyup(function() {
        jQuery(this).val(jQuery(this).val().replace(/([^\d\.]+)*/g, ''));
    });

    //　排序加颜色，　红色升，　绿色降
    var order = jQuery("input[name='order']").val();
    var ordername = jQuery("input[name='ordername']").val();
    var tempordername = jQuery("input[name='tempordername']").val();
    var color = "black";
    if (null != order && "" != order) {
        if ("asc" == order) {
            color = "red";
        }
        if ("desc" == order) {
            color = "green";
        }
    }
    if (null != tempordername && "" != tempordername && null != ordername && "" != ordername) {
        ordername = ordername.replace("||", "\\|\\|").replace(",", "\\,");
        // ordername = ordername.replace(".", "\\.").replace("||", "\\|\\|").replace(",", "\\,").replace("t.", "t\\.");
        ordername = replaceAll(ordername, "\\.", "\\.");
        if (ordername.length > 20) {
            tempordername = "tempordername";
            jQuery("._" + tempordername).css("color", color);
        }
        jQuery("._" + ordername).css("color", color);
    }

    // 列表高度
    if (jQuery(".listTable").height() < 120) {
        jQuery("<div style='height: 140px; clear: both;'></div>").insertAfter(jQuery(".listTable"));
    }

    // contents高度大于100, textarea
    // jQuery("#contents").height(jQuery("#contents").height() > 100 ? jQuery("#contents").height() : 100);
    
    // 框架页面添加 <div class="clear30"></div>
    if(null != jQuery(".menu_div").size() && 1 == jQuery(".menu_div").size()){
       jQuery("<div class='clear30'></div>").insertAfter(jQuery(".menu_div").parent());
    }
    
    // button
    jQuery("button:not('.bt45')").addClass("ml5").each(function(i){
//        alert(jQuery(this).is(".menu_div button:eq(0)"));
//        alert(jQuery(".menu_div button:eq(0)").text());
//        if(i != 0){
//            jQuery(this).addClass("ml5");
//        }
        var length = jQuery.trim(jQuery(this).text()).length;
        if(length <= 3){
            jQuery(this).addClass("bt60");
        } else if(3 < length && length <= 5){
            jQuery(this).addClass("bt75");
        } else if(5 < length && length <= 7){
            jQuery(this).addClass("bt90");
        } else if(7 < length && length <= 8){
            jQuery(this).addClass("bt100");
        } else { // 140px
        	jQuery(this).addClass("bt140");
        }
    });
});

// 格式化A标签
function resetA() {
    jQuery("a").addClass("a4");
}

// 格式化输入框
function resetPage(obj) {
    jQuery.each(jQuery("input[type='text']"),
    function() {
        jQuery(this).replaceWith("<span class='ml2'>" + jQuery(this).val() + "</span>");
    });

    jQuery.each(jQuery("select"),
    function() {
        var str = "";
        jQuery.each(jQuery(this).children("option"),
        function() {
            if (jQuery(this).val() == jQuery(this).parent().val()) {
                str = "<span class='ml2'>" + jQuery(this).text() + "</span>";
            }

        });
        jQuery(this).replaceWith(str);
    });

    jQuery.each(jQuery("textarea"),
    function() {
        jQuery(this).replaceWith("<div class='ml2'>" + jQuery(this).text() + "</div>");
    });

    // 不能加隐藏的
    jQuery(".rehide").hide();

    // 所有的
    if (null != obj && "all" == obj) {
        jQuery("button, input[type='checkbox'], input[type='radio'], img").hide();
    }
    // 不包含图片
    if (null != obj && "allNotImg" == obj) {
        jQuery("button, input[type='checkbox'], input[type='radio']").hide();
    }
    // checkbox
    if (null != obj && "checkbox" == obj) {
        jQuery.each(jQuery("input[type='checkbox'], input[type='radio']"),
        function() {
            if (this.checked == true) {
                jQuery(this).replaceWith("√");
            }
            jQuery(this).hide();
        });
    }

    // 不能隐藏的，　要显示的
    jQuery(".reshow").show();
    jQuery(".tempFileBox").hide();
}

// 格式化输入框--只格式化class为isdos的,
function resetPage2() {
    jQuery.each(jQuery(".isdos[type='text']"),
    function() {
        jQuery(this).replaceWith("<span class='ml2'>" + jQuery(this).val() + "</span>");
    });

    jQuery.each(jQuery(".isdos"),
    function() {
        var str = "";
        jQuery.each(jQuery(this).children("option"),
        function() {
            if (jQuery(this).val() == jQuery(this).parent().val()) {
                str = "<span class='ml2'>" + jQuery(this).text() + "</span>";
            }

        });
        jQuery(this).replaceWith(str);
    });
}

// 转化成时间
function toDate() {
    var str = "";
    jQuery.each(jQuery("input[type='text'][name*='edate'], input[type='text'][name*='endDate'], input[type='text'][name*='etime']"),
    function() {
        str = jQuery(this).val();
        if ("" != str && str.length > 6 && str.length < 11) {
            jQuery(this).val(str + " 23:59:59");
        }
    });

    jQuery.each(jQuery("input[type='text'][name*='date'], input[type='text'][name*='time'], input[type='text'][name*='Date'],"),
    function() {
        str = jQuery(this).val();
        if ("" != str && str.length > 6 && str.length < 11) {
            jQuery(this).val(str + " 00:00:00");
        }
        
        if ("" != str && str.length == 16) {
            jQuery(this).val(str + ":00");
        }
    });
}

// 格式化时间
function formaterDate() {
    var str = "";
    jQuery.each(jQuery("input[name*='date'], input[name*='time'], input[name*='Date'],"),
    function() {
        str = jQuery(this).val();
        if ("" != str && str.length > 9 && ("00:00:00" == str.substr(11, 8) || "23:59:59" == str.substr(11, 8))) {
            jQuery(this).val(str.substr(0, 10));
        } else if ("" != str && str.length > 15) {
            jQuery(this).val(str.substr(0, 16));
        }
    });
}

// 清空时间
function clearDate(value) {
    jQuery.each(jQuery("input[name*='date'], input[name*='time'], input[name*='Date'],"),
    function() {
        jQuery(this).val("");
    });
}

// 设置自动高度, 默认设置body高度, 200像素
function setAutoHeight(id, height, width) {
    var nub = 200;
    var str = "body";
    
    if(null != id && "" != id){
	    id = id.split(",");
	    for(var i = 0; i < id.length; i++){
	        if (null != id[i] && "" != id[i] && "body" != id[i]) {
		        str = "#" + jQuery.trim(id[i]);
		    }
	         if ("body" != str) {
		        nub = 100;
		    }
	         if (null != height) {
		        nub = height;
		    }
	        
		    if ("width" == width) {
		        if (jQuery(str).width() < nub) {
		            jQuery(str).width(nub+5);
		        }
		    } else {
		        if (jQuery(str).height() < nub) {
		            jQuery(str).height(nub+5);
		        }
		    }
	    }
    }
}

//　排序加颜色，　红色升，　绿色降--此暂时不用
function setOrderColor() {
    var order = document.forms[0].order.value;
    var ordername = document.forms[0].ordername.value;
    var color = "black";
    if ("" != order) {
        if ("asc" == order) {
            color = "red";
        }
        if ("desc" == order) {
            color = "green";
        }
    }
    if ("" != ordername) {
        ordername = ordername.replace(".", "\\.").replace("||", "\\|\\|").replace(",", "\\,");
        jQuery("._" + ordername).css("color", color);
    }
}

// 
function replaceAll(str, s1, s2) {
    return str.replace(new RegExp(s1, "gm"), s2);
}

// checkbox传值
function getCheckBoxValue(checkBoxName, id) {
    var value = "";
    jQuery("." + checkBoxName).each(function() {
        if (jQuery(this).attr("checked")) {
            value += jQuery(this).val() + ",";
        }
    });
    jQuery("#" + id).val(value);
}

// 显示checkbox
function showCheckBox(value) {
    value = value.split(",");
    for (var i = 0; i < value.length; i++) {
        jQuery("input[type='checkbox'][value='" + value[i] + "']").attr("checked", "checked");
    }
}

// 显示menu
function hideMenu(obj){
    if("hide" == obj){
        jQuery("#menu").hide();
    }     
}
