
// 2008-10-21 YmL:
//	.	added check to prevent loading the same file twice.
//	.	added protection to avoid running this file twice.
//
// 2009-03-03 YmL:
//	.	(fixed) now works under opera. onload event getting fired twice under opera.
//
// 2009-09-07 YmL: queue member was conflicted with jquery somehow. changed to load_queue. 

if ( typeof( $.requireJs ) == 'undefined' )
	{

function debug_notify() { alert( "Load completed" ); }

	(function($)
		{
		
		function isExternalScript(url)
			{ 
			return /(http|https):\/\//.test(url);
			};
		
		$.extend(
			{
			
			requireConfig : 
				{
				routeJs		: '',
				routeCss	: ''
				},
			
			load_queue : [],
			pending : null,

			isLoaded : function( scriptUrl )
				{

				if ( typeof( this.loaded_status ) == 'undefined' )
					this.loaded_status = new Array();

				var _url		= (isExternalScript(scriptUrl)) ? scriptUrl : $.requireConfig.routeJs + scriptUrl;

				if (( typeof( this.loaded_status[ _url ] ) == 'undefined') ||
					( this.loaded_status[ _url ] != "loaded" ))
					return false;
				else
					return true;

				},			
			requireJs : function(scriptUrl, callback, opts, obj, scope)
				{

				// FIXME: 2008-10-21 YmL: I don't understand why if I 
				// FIXME: add loaded_status to the member list above 
				// FIXME: it gets cleared on an Ajax request but if I 
				// FIXME: do it this way it persists between ajax requests.

				if ( typeof( this.loaded_status ) == 'undefined' )
					this.loaded_status = new Array();

				if(opts != undefined || opts == null)
					{
					$.extend($.requireConfig, opts);
					}

				var _request = 
					{
					url 		: scriptUrl,
					callback 	: callback,
					opts		: opts,
					obj 		: obj,
					scope		: scope
					}

				var _url		= (isExternalScript(scriptUrl)) ? scriptUrl : $.requireConfig.routeJs + scriptUrl;

				// have we already loaded this script? 
				
				if ( this.loaded_status[ _url ] == "loaded" )
					{
					// alert( "jquery.ondemand script " + _url + " already loaded" );

					if ( typeof( callback ) == "function" )
						callback.call();

					return;
					}

				if ( this.pending )
					{
					this.load_queue.push(_request);
					this.loaded_status[ _url ] = "pending";
					return;
					}
				
				this.pending = _request;
				this.pending._url = _url;
				
				var _this		= this;
				var _head 		= document.getElementsByTagName('head')[0];
				var _scriptTag 	= document.createElement('script');
					
				// Firefox, Opera

				$(_scriptTag).bind('load', function()
					{
					_this.requestComplete( _url );
					});
					
				// IE

				_scriptTag.onreadystatechange = function()
					{

					if ( this.readyState === 'loaded' || this.readyState === 'complete')
						{
						_this.requestComplete( _url );
						}
					}
					
				_scriptTag.type = "text/javascript";
				_scriptTag.src = _url;
					
				_head.appendChild(_scriptTag);
				},

			// Under Opera 9.63 and possibly others, this callback is getting fired twice
			// which means this.pending can be NULL.
						
			requestComplete : function( _url )
				{

				if ( this.pending && this.pending.callback && typeof( this.pending.callback ) == 'function')
					{
					if(this.pending.obj)
						{
						if(this.pending.scope)
							{
							this.pending.callback.call(this.pending.obj);
							} 
						else 
							{
							this.pending.callback.call(window, this.pending.obj);
							}
						} 
					else 
						{
						this.pending.callback.call();
						}
					}

				// indicate that this script has been loaded

				this.loaded_status[ _url ] = "loaded";

				this.pending = null;
				
				if ( this.load_queue.length > 0 )
					{
					var request = this.load_queue.shift();
					this.requireJs(request.url, request.callback, request.opts, request.obj, request.scope);
					}
				},
			
			requireCss : function(styleUrl)
				{
				
				if(document.createStyleSheet)
					{
					document.createStyleSheet($.requireConfig.routeCss + styleUrl);
					}
				else 
					{
				
					var styleTag = document.createElement('link');
											
					$(styleTag).attr(
						{
						href	: $.requireConfig.routeCss + styleUrl,
						type	: 'text/css',
						media 	: 'screen',
						rel		: 'stylesheet'
						}).appendTo($('head').get(0));
					
					}
				
				}
			
			})
		
		})(jQuery);

	}	// end of wrapping if.
