/* 
    #########################################
    Author : JEREMY BUELER www.jbueler.com
    Company : www.summitprojects.com
    Date: 9/27/07 September 27th, 2007
    * 
    * 
    * UPDATED :: 3/12/08
    * ADDED jQuery Support -- Work in progress (LIMITED FUNCTIONALITY COMPARED TO THE MOOTOOLS version
    * 						  no remove(); no emptyAll();
    * 
    #########################################

    THIS IS AN ONDOMREADY FUNCTION QUEUE -- MOOTOOLS/JQUERY ADDON CLASS FOR ALLOWING YOU TO EASILY 
    ADD AND REMOVE FUNCTIONS TO RUN ON DOM READY...
    
    Onload  =  is a global Object that you can use to add the functions:
    
    Example:
        Onload.add(fn); --> fn = a function that you wish to add to the ondomready queue 
                                that fires automatically when the dom is ready. 
                                THE FUNCTION WILL ONLY BE ADDED IF IT ISN'T ALREADY IN THE QUEUE.
                                
        Onload.remove(fn) --> fn = the function that you wish to remove from the queue.
                                    it will be permanently removed from the queue 
    
        Onload.test(fn) --> fn = Check to see if the function (fn) you are passing in is already included
                                 Returns true if the function is already in the queue false if not...
        Onload.alertAll() --> fn = Loops through all functions and alerts the function -- handy for debug and determining if a function needs to fire on a page ... 
                                 *                          
        Onload.run() --> the function that runs on domready -- the event is already set up to run then -- 
                         this function will execute all functions in the queue in the order they were added...
*/
if( typeof MooTools != "undefined" ){
    var RegisterFunctions = new Class({
            options:{
                delay: 0
            },
            initialize: function(options){
                this.setOptions(options);
                this.collection = [];
            },
            add: function(fn){
                this.collection.include(fn);
            },
            run: function(){
                this.collection.each(function(fn){
                        fn();
                })
            },
            remove: function(fn){
            	if($type(fn) == 'array'){
            		fn.each(function(el){
            			if(this.collection.contains(el)) this.collection.remove(el);
            		}.bind(this));
            	}
            	else{
                	if(this.collection.contains(fn)) this.collection.remove(fn);
            	}
            },
            test: function(fn){
                if(this.collection.contains(fn)){ 
                    return true;
                }
                else{
                    return false;
                }
            },
            alertAll: function(){
            	this.collection.each(function(el){
            		alert(el);
            	});
            },
            emptyAll: function(){
            		this.collection.each(function(item){
            			this.collection.remove(item);
            		}.bind(this));
            }
    });
    RegisterFunctions.implement(new Events, new Options);
    var Onload = new RegisterFunctions();
    window.addEvent('domready',function(){
       Onload.run();     
    });
}
else if(typeof jQuery != "undefined"){
	var RegisterFunctions = {
		version: '0.2',
		desc:'Onload Queue'
	}
	var Onload = {
		collection: [],
		options:{},
		add: function(fn){
			this.collection.push(fn);
		},
		alertAll: function(){
            	$.each(this.collection,function(i,el){
            		alert(el);
            	});
            },
        remove: {},
        test: function(fn){
        	if($.inArray(fn,this.collection) >= 0){
        		return true;
        	}
        	else if($.inArray(fn,this.collection) == -1){
        		return false;
        	}
        },
		run: function(){
			$.each(this.collection,function(i,fn){
				fn();
			})
		}
	};
	
	$(document).ready(function(){
		Onload.run();	
	})
}
