/**
 * Depends on: rock.js, rock-util.js, prototype.js, scriptaculous.builder.js 
 */
 
/*================================================
   Object rock.injector
 =================================================*/
rock.injector = {};

/**
 * Inject an external resource into a container element. This method depends
 * on prototype.js, because it uses Ajax.Request to retrieve the external resource.
 * Unlike jmaki's injector, this method does the inject asynchronously.
 *
 * Argument spec - object literal:
 *	{url:<url>, container:<container element> [, busyIndicator:none|text|icon, busyText:<text>, busyIcon:<con>]}
 */
rock.injector.inject = function(spec){
	var url;
	var container;
	var busyIndicator = "icon";
	var busyText = "Loading...";
	var busyIcon = "images/wait.gif";
			
	url = spec.url;
	if(!url) return;
	
	container = spec.container;		
	if(!container) return;
	
	if(spec.busyIndicator){
		busyIndicator = spec.busyIndicator;
	}
	if(spec.busyText){
		busyText = spec.busyText;
	}				
	if(spec.busyIcon){
		busyIcon = spec.busyIcon;
	}
	
	var busyElement;
	if(busyIndicator == 'icon'){
		busyElement = Builder.node('center',[
			Builder.node('img', 
				{src:busyIcon, border:'0', alt:'Loading...'})
		]);
   	}else if(busyIndicator == 'text'){
		busyElement = Builder.node('center',[
			Builder.node('span', 
				{className:'loading'}, 
				busyText)
		]);
   	}

   	if(busyElement){
   		// display busy element
    	rock.util.DomUtil.removeAllChildren(container);
    	container.appendChild(busyElement);
    }    	
    			
   	new Ajax.Request( 
    	url, 
        {method: 'get', 
	     onSuccess: function(req) {
       		container.innerHTML = req.responseText; 
   		 },
	     onFailure: function(req) {
       		rock.showErrorMsg("Http error: Status " + req.status + " " + 
       						req.statusText + " - " + req.responseText,
       						container); 
   		 },	
	     onException: function(req, exception) {
       		rock.showErrorMsg("Application exception: " + exception, 
       						container); 
   		 }		         
	});		
}
	
