/**
 * @constructor
 *
 * @description
 * Bootstraps functions which requires the DOM to be loaded before execution can be done. The functions assigned to the bootstrap will be executed when the DOM are loaded.
 *
 * @example
 *   &#60;script type="text/javascript"&#62;
 *
 *       // Function which need to access the DOM
 *       function doSomething(el){
 *           var e = document.getElementById(el);
 *       };
 *
 *       // Init domstrap
 *       var doc = new bootstrap();
 *       doc.attach('target',doSomething);
 *
 *   &#60;/script&#62;
 *
 *   &#60;div id="target"&#62;
 *       Do something on this element
 *   &#60;/div&#62;
 */
var bootstrap = function(){

    var handles = [];
    var obj = this;

    /**
     * @description
     * Executes all functions assinged to the bootstrap.
     */
    this.load = function(){
        for(var i = 0; i < handles.length; i++){
            handles[i].fn(handles[i].el);
        }
    };

    
    /**
     * @param   el  {String}    Identifyer of the element in the DOM which the functions should work on.
     * @param   fn  {Function}  A function to be executed on the element.
     *
     * @description
     * Attaches a element and a function which should be executed on the element when the DOM are loaded.
     */
    this.attach = function(el,fn){
        handles.push({el:el,fn:fn});
    };


    /**
     * @private
     * @param   el  {Object}    The Object the Event should be assigned to.
     * @param   ev  {Event}     Event the function should be added to.
     * @param   fn  {Function}  Function to be added to the event.
     *
     * @description
     * Adds an eventlistener to an Object. The event will be added so already defined events added to the Object will be preserved.
     */
    function addEvent(el,ev,fn){
        if (el.addEventListener){
            el.addEventListener(ev,fn,false);
        }else if(el.attachEvent){
            el.attachEvent('on'+ev,fn);
        }
    }


    /**
     * @private
     * @param   fn  {Function}  Function to be added to the event.
     *
     * @description
     * Since the onload event have some issues, we want use DOMContentLoaded which is when DOM are ready in browser. But; IE does not have native support for DOMContentLoaded so we need to "simulate" it for IE. We use "Diego Perini's" simulation trick for IE.
     * Credit: Diego Perini (diego.perini at gmail.com)
     *         http://javascript.nwbox.com/IEContentLoaded/
     */
    function ieDOMContentLoaded(fn){
        var ready = false,
        isReady = function(){
            if(!ready){
                ready = true;
                fn();
            }
        };
        (function(){
            try{
                document.documentElement.doScroll('left');
            }catch(error){
                setTimeout(arguments.callee, 50);
                return;
            }
            isReady();
        })();
    }


    /**
     * Attach the load function to "DOM readyness"
     */
    if(document.addEventListener){
        // Opera, FireFox and latest Safari
        addEvent(window,'DOMContentLoaded',obj.load);
    }else if(document.attachEvent){
        // IE
        ieDOMContentLoaded(obj.load);
    }else{
        // Something else - fallback to traditional onload
        addEvent(window,'DOMContentLoaded',obj.load);
    }

};