//Object responsible for handling the loading of javascript and stylesheets

var RSXResourceLoader        =   {
    
	//Holds the scripts and styles submitted to it
	//Used as the resource queue
	
    scripts 	: 	[ ] ,
	styles  	: 	[ ] ,
	
	//Holds the timeout for the loader
	
	timeout		: 	0 ,
	
	
	
	//Functions called when certain points in queue are reached
	
	scriptsFinishedStack		:	function () { } , //Scripts finish loading
	stylesFinishedStack			: 	function () { } , //Styles finish loading
	resourcesFinishedLoading	:	function () { } , //All resources finish loading
	
	
	
	//Remove a loaded resource from the queue
	
    dequeueLoadedResource       :   function ( url, type ) {
        
		//Handles dequeuing the resource from the queue based on type
		//If the stack for that file type has finished, execute the stack finish function
		
        switch(type) {
			
            case 'script':
				
				//Handle each script in the stack
				//If the script URL matches the dequeue URL, break loop
				
                for(n in this.scripts) { if($(this.scripts[n]).attr('src') == url) break; }
				
				//Remove the resource from the array
				
				this.scripts.splice(n, 1);
				
				//Execute the scriptsFinishedStack method if script stack is empty
				
                if(this.scripts.length == 0) { RSXResourceLoader.scriptsFinishedStack(); }
				break;
				
            case 'style':
				
				//Handle each style in the stack
				//If the script URL matches the dequeue URL, break loop
				
                for(n in this.styles) { if($(this.styles[n]).attr('href') == url) break; }
				
				//Remove the resource from the array
				
				this.styles.splice(n, 1);
				
				//Execute the stylesFinishedStack method if stylesheet stack is empty
				
                if(this.styles.length == 0) { this.stylesFinishedStack(); }
                break;
        }
        
		//If all stacks have finished loading, execute the resourcesFinishedLoading method
		
        if(this.scripts.length == 0 && this.styles.length == 0) { this.timeout = setTimeout(RSXResourceLoader.resourcesFinishedLoading, 10); }
        
    } ,
    
	
	
	//Load stylesheet(s)
	
    loadStylesheet      :   function ( url ) {
        
		//Clear the timeout
		//Prevents resourcesFinishedLoading() from executing prematurely
		
		if(this.timeout) clearTimeout(this.timeout);
		
		//Handle the passed url variable as array
		//Handle each stylesheet
		
        for(i = 0, ob = ((typeof url == 'object') ? url : [ url ]); i < ob.length; i++) {
			
			//If document.createStyleSheet is supported (IE), use it to create the stylesheet
			//Else, create stylesheet throught jQuery call
			
            if(document.createStyleSheet) style = document.createStyleSheet(ob[i]);
            else {
				
				//Create the link tag
				//Append to the <head> tag
				
                style 	= 	$('<link rel="stylesheet" type="text/css" href="' + ob[i] + '?t=' + new Date().getMilliseconds() + '" />');
                $('head').append(style);
				
            }
			
			//Add stylesheet to the queue
			
			this.styles.push(style);
			
        }
		
		//Add a ready() listener for the create stylesheets
			
		for(i = this.styles.length - 1; i >= 0; i--) $(this.styles[i]).ready(RSXResourceLoader.dequeueLoadedResource($(this.styles[i]).attr('href'), 'style'));
        
    } ,
    
	
	
	//Load script(s)
	
    loadScript          :   function ( url ) {
		
		//Clear the timeout
		//Prevents resourcesFinishedLoading() from executing prematurely
		
		if(this.timeout) clearTimeout(this.timeout);
		
		//Handles each argument passed into the function
		
        for(i = 0, ob = ((typeof url == 'object') ? url : [ url ]); i < ob.length; i++) {
			
			//Create the script element
			//Use a timestamp to prevent cahcing of the script
			
            script 		= 	$('<script type="text/javascript" src="' + ob[i] + '?t=' + new Date().getMilliseconds() + '"></script>');
			
			//Append the script to the <head> tag
			//Add the script to the end of the script queue list
			
            $('head').append(script);
			this.scripts.push(script);
			
        }
		
		//Add a ready() listener for the create stylesheets
			
		for(i = this.scripts.length - 1; i >= 0; i--) $(this.scripts[i]).ready(RSXResourceLoader.dequeueLoadedResource($(this.scripts[i]).attr('src'), 'script'));
        
    }

};

//Request an RSXCore javascript/css component
//Requests can be made in mulitple arguments, rather than multiple iterations of the function

function initiateRSXCoreComponent() {
	
	//Combine the arguments into one single string
	
	for(i = 0, str = ''; i < arguments.length; i++) str += arguments[i] + '/';
	
	//Make AJAX call to get resource lists
	//Load respective resources
	
	$.ajax({
	
		url: '/rsx/init/init.js.php?req=' + str.substring(0, str.length - 1) ,
		dataType: 'json' ,
		success: function (data) { RSXResourceLoader.loadScript(data.js); RSXResourceLoader.loadStylesheet(data.css); }
		
	});
	
}
