Site = new function(){    
    this.delegateList = new Array();
    this.JSList  = new Array();
    
    this.init = function() {
        var index = 0;
        
        for(; index < Site.delegateList.length; index++)
            Site.delegateList[index].call();
    }                                                     

    this.attachJS = function(fileName) {
        jQuery.ajaxSetup({async: false});
        jQuery.getScript(fileName);
        jQuery.ajaxSetup({async: true});
    }
    
    this.subscribeToDocumentReady = function(action) {
        Site.delegateList.push(action);
    }
    
    this.loadCSS = function(fileName) {
        jQuery("head").append("<link>");
        css = jQuery("head").children(":last");
        css.attr({
            rel:  "stylesheet",
            type: "text/css",
            href: fileName
        });
    }
}

Helpers = new function() {
    this.trim = function(str, chars) {
        return Helpers.leftTrim(Helpers.rightRrim(str, chars), chars);
    }
     
    this.leftTrim = function(str, chars) {
        chars = chars || "\\s";
        
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }
     
    this.rightRrim = function (str, chars) {
        chars = chars || "\\s";
        
        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    }
}

Options = new function() {
    this.calendarElements = new Array();
    this.toolTipElements = new Array();
    this.callbacks = new Array();

    this.startLoading = function() {
        jQuery("#loading").css("height", jQuery("body").height() + "px");
        jQuery("#loading").css("width", jQuery("body").width() + "px");
        
        jQuery("#loading").show();
        
        jQuery("#loading-image").css("left", ((jQuery(window).width() / 2) - (jQuery("#loading-image").width() / 2)) + "px");
        jQuery("#loading-image").css("top", ((jQuery(window).height() / 2) - (jQuery("#loading-image").height() / 2) + $(window).scrollTop()) + "px");
        
        jQuery("#loading-image").show();
        
    }
    
    this.stopLoading = function() {
        jQuery("#loading-image").hide();
        jQuery("#loading").hide();
    }
    
    this.submitAffiliate = function(submitElement) {
        jQuery("#" + submitElement).click();
    }
    
    this.toggleBox = function(element) {
        jQuery("#" + jQuery(element).attr("id") + "-box").toggle();
    }
    
    this.setCalendar = function(elementName) {
        Calendar.setup({
            inputField : elementName,
            trigger    : elementName,
            onSelect   : function() { this.hide() },
            dateFormat : "%d-%m-%Y"
        });
    }
    
    this.setToolTip = function(toolTip) {
        jQuery(toolTip[0]).tipsy({gravity: toolTip[1]});
    }
    
    this.setCalendarElements = function(elementName) {
        Options.calendarElements.push(elementName);
    }
    
    this.executeCallbacks = function() {
        var index = 0,
            length = Options.callbacks.length;
        
        for(; index < length; ++index) {
            Options.callbacks[index].call();
        }
    }
    
    this.addCallback = function(callback) {
        Options.callbacks.push(callback);
    }
    
    this.setToolTipElements = function(elementName, gravity) {
        gravity = gravity || 'sw';
        
        Options.toolTipElements.push([elementName, gravity]);
    }
    
    this.loadCalendarElements = function() {
        var index = 0,
            length = Options.calendarElements.length;
        
        for(; index < length; ++index) {
            Options.setCalendar(Options.calendarElements[index]);
        }
    }
                                                                            
    this.loadToolTipElements = function() {
        var index = 0,
            length = Options.toolTipElements.length;
        
        for(; index < length; ++index) {
            Options.setToolTip(Options.toolTipElements[index]);
        }
    }
}

// initializations
jQuery(document).ready(function() {
    Site.init();
});

String.prototype.trim = function() {
    return Helpers.trim(this);
}
