/**
 * General error used for AJAX requests.
 */
var ajax_general_error = 'THERE WAS ERROR PROCESSING YOUR REQUEST\nPLEASE TRY AGAIN';

/**
 * Extend jQuery object to add element centering functionality.
 * 
 * <pre>
 * <div class="center-me">content...</div>
 * <div class="center-me" top="100" left="200">sample of div poisition being overrided top or left attribute...</div>
 * <div class="center-me" topoffset="-100" leftoffset="100">sample of div poisition by offsetting the center postion by top or left offset...</div>
 * </pre>
 *
 * @return jQuery Object
 */
jQuery.fn.center = function() {
	var win = $(window);
    
    this.css('position','absolute');
    
    // set top position
    if (this.attr('top')) {
        this.css('top', this.attr('top'));
    } else if (this.attr('topoffset')) {
        this.css('top', (((win.height() - this.height() ) / 2 + win.scrollTop()) + (this.attr('topoffset').replace(/[^0-9-]/gi, '')*1)) + 'px');
    } else {
        this.css('top', (win.height() - this.height() ) / 2 + win.scrollTop() + 'px');
    }
    
    // set left position
    if (this.attr('left')) {
        this.css('left', this.attr('left'));
    } else if (this.attr('leftoffset')) {
        this.css('left', (((win.width() - this.width() ) / 2 + win.scrollLeft()) + (this.attr('leftoffset').replace(/[^0-9-]/gi, '')*1)) + 'px');
    } else {
        this.css('left', (win.width() - this.width() ) / 2 + win.scrollLeft() + 'px');
    }
    
    return this;
}

/**
 * Add JQuery events after page load.
 */
$(function(){
    
    /**
     * Event to automatically center elments.
     *
     * @return false
     */
    centerCSS();
	
    /**
     * Add Event to automatically center elments on
     * window resize.
     *
     * @return void
     */
    window.onresize = function(event) {
        centerCSS();
    }
	
});

/**
 * Center a DOM element.
 * 
 * @param DOM elm
 * @return jQuery Object
 */
function centerElement(elm) {
    return $(elm).center();
}

/**
 * Center a DOM element by its ID string.
 * 
 * @param string idStr
 * @return jQuery Object
 */
function centerElementById(idStr) {
    return centerElement(document.getElementById(idStr)); 
}

/**
 * Center any DOM elements with a class name
 * of "center-me".
 * 
 * @return void
 */
function centerCSS() {
    $('.center-me').each(function(i) {
        $(this).center();
    });
}

