var WBE = function() {
    function isDigit(c) {
        return ((c >= 48) && (c <= 57));
    }

    function digitsOnly(e) {
        if (e.which != 8 && e.which != 0 && !isDigit(e.which)) {
            return false;
        }
    }

    // Parse date.
    function parseDate(dateString) {
        return Date.parseExact(dateString, DateTimeResources.dateFormat);
    }

    return {
        isDigit: isDigit,
        digitsOnly: digitsOnly,
        parseDate: parseDate
    }
} ();

// Extend jQuery to truncate text of a control.
// control: the string container which you want to operation. 
// length: the max length count about the string
jQuery.fn.truncate = function(maxlength) {
    return this.each(function() {
        var self = $(this);
        if (self.text().length > maxlength) {
            var tail = "...";
            self.text(self.text().substring(0, maxlength - tail.length) + tail);
        }
    });
};

// Extend String prototype.
// For support trim function.
String.prototype.trim = function() {
    return jQuery.trim(this);
}

// Extend jQuery to attach tooltip for control. 
jQuery.fn.setTooltip = function(message, option, hideDelay) {
    return this.each(function() {
        var control = $(this);
        if (!control.data('qtip')) {
            var tempOption = {};
            if (typeof option == "object") {
                tempOption = option;
            }
            // Extend default setting.
            var settings = $.extend({
                content: message,
                position: {
                    corner: {
                        target: 'topMiddle',
                        tooltip: 'bottomMiddle'
                    }
                },
                style: {
                    border: {
                        width: 1,
                        radius: 3
                    },
                    padding: 2,
                    textAlign: 'left',
                    tip: true // Give it a speech bubble tip with automatic corner detection
                },
                show: 'mouseover',
                hide: 'mouseout'
            }, tempOption || {});
            control.qtip(settings);
        } else {
            control.qtip("hide");
            if (control.data("tooltipTimer")) {
                clearTimeout(control.data("tooltipTimer"));
            }
            // Update message.
            control.qtip("api").updateContent(message, true);
            control.qtip("enable");
        }
        
        // Set delay hide.
        if (typeof hideDelay == "number") {
            var timerId = setTimeout(function() {
                control.qtip("hide");
            }, hideDelay * 1000);
            control.data("tooltipTimer", timerId);
        }

    });
};

// Extend jQuery to disable tooltip for control. 
jQuery.fn.disableTooltip = function() {
    return this.each(function() {
        $(this).qtip("hide");
        $(this).qtip("disable");
    });
};

jQuery.eventDictionary = {};

var CUSTOM_EVENTS = {
    LOGGEDIN: "loggedIn",
    LOGGEDOUT: "loggedOut"
};

jQuery.fn.bindEvent = function(eventName, data, handle) {
    return this.each(function() {
        var self = $(this);
        self.bind(eventName, data, handle);
        jQuery.eventDictionary[eventName] = jQuery.eventDictionary[eventName] || [];
        jQuery.eventDictionary[eventName].push(self);
    });
};

jQuery.trigger = function(eventName) {
    var elements = jQuery.eventDictionary[eventName] || [];
    $.each(elements, function(index, item) {
        item.trigger(eventName);
    });
};

jQuery.fn.unbindEvent = function(eventName, handle) {
    //    var array = jQuery.customEvent[eventName];
    //    jQuery.customEvent[eventName] = $.grep(array, function(item, index) {
    //        return item.selector == self.selector;
    //    }, false);
    //    return this.each(function() {
    //        var self = $(this);
    //        self.unbind(event, handle);
    //    });
};

// Extend jQuery to supports change the dom element content.
jQuery.fn.bindLoggedOut = function(URL) {
    return this.each(function() {
        var self = jQuery(this);
        self.bindEvent(CUSTOM_EVENTS.LOGGEDOUT, function() {
            jQuery.ajaxSetup({ cache: false });
            self.load(URL);
            jQuery.ajaxSetup({ cache: true });
        });
    });
};

jQuery.fn.bindLoggedIn = function(URL) {
    return this.each(function() {
        var self = jQuery(this);
        self.bindEvent(CUSTOM_EVENTS.LOGGEDIN, function() {
            jQuery.ajaxSetup({ cache: false });
            self.load(URL);
            jQuery.ajaxSetup({ cache: true });
        });
    });
};

// Extend jQuery to post JSON.
jQuery.postJSON = function(url, data, callback ) {
    return jQuery.post(url, data, callback, "json");
};

Date.prototype.toLocalString = function() {
    return this.toString(DateTimeResources.dateFormat);
};