var ajaxContext=new Object();

/**
 * List of loaded libraries
 */ 
var loaded_libraries=new Object();



/**
 * IMPORTANT: the script requires JQUERY 1.4                        
 * This function is a safe javascript executor. Checks if the required dependency is loaded
 * and if so proceed with the callback. If the requested dependency is not loaded 
 * bind the execution of the callback to the load complete event of the required dependency.
 * 
 * 
 * @argument dependency      the dependency to enforce              (mandatory).
 * @argument callback   the reference of a callback funtion if any  (mandatory).
 */ 
var execute_safe=function(dependency,callback){
	
	if(loaded_libraries[dependency]!=null){
		
		if(callback !=null){
            var proceed=callback();
         }
		
	}
	else{
		
		$(document).bind(dependency, callback());
		
	}	
}

/**
 * IMPORTANT: the script requires JQUERY 1.4                        
 * This function is a proxy loader. Checks if the required dependency is loaded
 * and if so load the requested resource. If the requested dependency is not loaded 
 * bind the load of the resource to the load complete event of the required dependency.
 * 
 * @argument scr     the javascript file to load                    (mandatory).
 * @argument id      the id of the created script tag               (mandatory).
 * @argument dependency      the dependency to enforce              (mandatory).
 * @argument callback   the reference of a callback funtion if any  (optional).
 */ 
var library_dependency_loader=function(src,id,dependency,callback){
	
	if(loaded_libraries[dependency]!=null){
		
		async_load_script(src,id,callback);
		
	}
	else{
		
		$(document).bind(dependency, async_load_script(src,id,callback));
		
	}	
}


/**
 * IMPORTANT: the script requires JQUERY 1.4                        
 * Load a script asysncronously. Once the script is loaded
 * 1)Add the id to the loaded libraries list
 * 2)Trigger the event of dependency loaded
 * 3)Call the callback funtion if any. 
 * 
 * If the script is already loaded just perform the callback if any
 * 
 * @argument scr     the javascript file to load                    (mandatory).
 * @argument id      the id of the created script tag               (mandatory).
 * @argument callback   the reference of a callback funtion if any  (optional).
 */                        
var async_load_script=function (src,id,callback){
        
	  alert(callback); 
	  alert(src);
	  if(loaded_libraries[id]==null){
		    var s = document.createElement('script');
		    
	        s.type = 'text/javascript';
	        s.async = true;
	        s.src = src;
	        s.id=id
	        var x = document.getElementsByTagName('script')[0];
	        x.parentNode.insertBefore(s, x);
	        
	        
	        $('#'+id).load(function() {
	        alert("op");
	        }
	        )
	    }else{
	    	if(callback !=null){
	    		 
	            var proceed=callback();
	         }  
	    }	
    }

/**
 * Get the value of the parameter of the query string for the current URL.
 * Return false if no such parameter was found
 * 
 * @argument parameter     the name of the parameter                (mandatory).
 */
function queryString(parameter) { 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;
  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return false; //Here determine return if no parameter is found
  }
}

/**
 * Get the number of parameters of the query string for the current URL.
 */
function queryStringLength() { 
  var loc = location.search.substring(1, location.search.length);
  
  return loc.length;
  
}
