﻿///<reference path="jquery-1.3.2.js"/>

//alert('hello');
var webRoot = null;
//分页设置
var perpageCount = 15;

function _getWebRoot() {
    /** Return directory of this relative to document URL. */
    if (webRoot != null) {
        return webRoot;
    }
    //derive the baseDir value by looking for the script tag that loaded this file
    jQuery("head").children("[src*='jquery.common.js']").each(function () {
        var src = this.src;
        index = src.indexOf("Scripts");
        webRoot = src.substring(0, index - 1);
    });

    return webRoot;
}
//init web root url
_getWebRoot();

//format string ,example:jQuery.format('{0}{1}{2}',['a','b','c'])
jQuery.extend({
    format: function (source, params) {
        if (arguments.length == 1)
            return function () {
                var args = jQuery.makeArray(arguments);
                args.unshift(source);
                return jQuery.format.apply(this, args);
            };
        if (arguments.length > 2 && params.constructor != Array) {
            params = jQuery.makeArray(arguments).slice(1);
        }
        if (params.constructor != Array) {
            params = [params];
        }
        jQuery.each(params, function (i, n) {
            source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
        });
        return source;
    }
});

/* 将字符串转换成日期时间，有默认格式 */

String.prototype.toDate = function (style) {
    if (style == null) style = 'yyyy-MM-dd hh:mm:ss';
    var compare = {
        'y+': 'y',
        'M+': 'M',
        'd+': 'd',
        'h+': 'h',
        'm+': 'm',
        's+': 's'
    };

    var result = {
        'y': '',
        'M': '',
        'd': '',
        'h': '00',
        'm': '00',
        's': '00'
    };

    var tmp = style;
    for (var k in compare) {
        if (new RegExp('(' + k + ')').test(style)) {
            result[compare[k]] = this.substring(tmp.indexOf(RegExp.jQuery1), tmp.indexOf(RegExp.jQuery1) + RegExp.jQuery1.length);
        }
    }
    return new Date(result['y'], result['M'] - 1, result['d'], result['h'], result['m'], result['s']);
};

/* 将日期时间转换成特定格式显示 */

Date.prototype.toText = function (style) {
    if (style == null) style = 'yyyy-MM-dd hh:mm';
    var compare = {
        'y+': 'y',
        'M+': 'M',  //格式 月份：01到12
        'o+': 'o',  //格式 月份：1 到12
        'd+': 'd',  //格式 天 ： 01到31
        'D+': 'D',  //格式 天 ： 1 到31
        'h+': 'h',  //格式 小时：00到23
        'H+': 'H',  //格式 小时：0 到23
        'm+': 'm',  //格式 分钟：00到59
        'i+': 'i',  //格式 分钟：0 到59
        's+': 's',  //格式 秒 ： 00到59
        'S+': 'S'   //格式 秒 ： 0到59
    };

    var result = {
        'y': this.getFullYear(),
        'M': (this.getMonth() < 9) ? ("0" + (1 + this.getMonth())) : (1 + this.getMonth()),
        'o': (1 + this.getMonth()),
        'd': (this.getDate() < 10) ? ("0" + this.getDate()) : this.getDate(),
        'D': this.getDate(),
        'h': (this.getHours() < 10) ? ("0" + this.getHours()) : this.getHours(),
        'H': this.getHours(),
        'm': (this.getMinutes() < 10) ? ("0" + this.getMinutes()) : this.getMinutes(),
        'i': this.getMinutes(),
        's': (this.getSeconds() < 10) ? ("0" + this.getSeconds()) : this.getSeconds(),
        'S': this.getSeconds()
    };
    var tmp = style;
    for (var k in compare) {
        if (new RegExp('(' + k + ')').test(style)) {
            tmp = tmp.replace(RegExp.jQuery1, result[compare[k]]);
        };
    };
    return tmp;
}

Date.prototype.toJSON = function (key) {

    var retValue = "\\/Date({0}+0800)\\/";

    if (!isFinite(this.valueOf())) {
        var ticks = this - new Date("1/1/1970");

        if (ticks > 0) {
            retValue = jQuery.format(retValue, [ticks]);

            return retValue;
        }
    }

    return null;
};

String.prototype.HtmlDecode = function (text) {
    return text.replace(/&amp;/g, '&').replace(/&quot;/g, '\"').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
};

jQuery.fn.extend({
    heightlight: function () {
        //table mouseover event
        this.each(function () {
            jQuery(this).hover(function () {
                jQuery(this).css("background", "#e1e1e1");
            }, function () {
                jQuery(this).css("background", "");
            });
        })
    }
})
jQuery.fn.extend({
    hoverClass: function (c) {
        return this.each(function () {
            $(this).hover(
			function () { $(this).addClass(c); },
			function () { $(this).removeClass(c); });
        });
    }
})
