//create the args global method
jQuery(function($) {
	$.args = {
		names: [],
		
		values: [],
		
		//function to add a value to the array
		add: function(name,value) {
			this.names.push(name);
			this.values.push(value);
		},
		
		//function to clear the array
		clear: function() {
			this.names = [];
			this.values = [];
		},
		
		//function to get a value by its name
		getValue: function(name) {
			//cycle the names array till we find the correct index for the value
			for(var i=0;i<this.names.length;i++) {
				if(this.names[i] == name) {
					return this.values[i];
				}
			}
		},
		
		//function to get a value by its name
		setValue: function(name,value) {
			//cycle the names array till we find the correct index for the value
			for(var i=0;i<this.names.length;i++) {
				if(this.names[i] == name) {
					this.values[i] = value;
				}
			}
		},
		
		//function return the array as a delimited string
		getString: function(delimiter) {
			//ok if we have no delimiter assume its & for the QS
			if(!delimiter)
				delimiter = '&';
			
			var retval = '';
			
			//cycle the names array and build the string
			for(var i=0;i<this.names.length;i++) {
				retval += this.names[i] + '=' + this.values[i] + delimiter;
			}
			
			//no trim the trailing delimiter from the args string
			retval = retval.substr(0,retval.length-1);
			
			//return the build string
			return retval + '';
		}
	}
});

//create the ajaxManager global method
jQuery(function($) {
	//global settings used by this class
	var ajaxSettings = { };
	
	//initialize the ajaxManager
	$.ajaxManager = {
	    initialise : function(options) {
			$.extend(ajaxSettings, $.ajaxManager.defaults, options);
			if(ajaxSettings.debug) {
				alert('init');
			}
		},
		
		ajaxSettings: ajaxSettings,
		
		makeRequest: function() {
		    makeAjaxRequest();
		},
		updateUrl: function(urlData) {
			updateUrl(urlData);
		},
		clearArguments: function() {
			clearArguments();
		},
		onBeforeSend : on_BeforeSend,
		onSuccess : on_Success,
		onError: on_Error,
		onComplete : on_Complete
    };
	
	//available response types
	$.ajaxManager.dataType = {	
	    JSONP : 'jsonp',
	    JSON : 'json',
		HTML : 'html'};

	//available request methods
    $.ajaxManager.requestType = {	
        POST : 'POST',
		GET : 'GET'};										

	//default values
	$.ajaxManager.defaults = { 
        dataType : $.ajaxManager.dataType.JSON,
        requestType : $.ajaxManager.requestType.POST,
        requestUrl : null,
        requestArgs : null,
		    debug: false,
		    arguments: $.args
        };	

	//function used to make the ajax request
	function makeAjaxRequest() {
		if(ajaxSettings.debug) {
			alert('Beging Request');
		}
		
		//make the ajax call
//		alert(
//			"Test call parameters :"
//			"\t type = " + ajaxSettings.requestType +
//			"\n\t url = " + (ajaxSettings.requestUrl + ((ajaxSettings.requestArgs !== null) ? ("?" + ajaxSettings.requestArgs) : "" )) +
//			"\n\t url length = " + (ajaxSettings.requestUrl + ((ajaxSettings.requestArgs !== null) ? ("?" + ajaxSettings.requestArgs) : "" )).length +
//			"\n\t dataType = " + ajaxSettings.dataType
//		);
		
		$.ajax({
			type: ajaxSettings.requestType,
			url: ajaxSettings.requestUrl,
			data: ajaxSettings.requestArgs,
			dataType: ajaxSettings.dataType,
			beforeSend: function() {
                $.ajaxManager.onBeforeSend();
            },
            success: function(p_response) {
                $.ajaxManager.onSuccess(p_response);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $.ajaxManager.onError(XMLHttpRequest, textStatus, errorThrown);
            },
			complete: function(XMLHttpRequest, textStatus) {
                $.ajaxManager.onComplete(XMLHttpRequest, textStatus);
            }
		});
		//alert("Ajax call made");
	}
	
	//function used to get all of the arguments from the querystring
	function updateUrl(urlData) {
		//if we dont have any urlData then we wont have any arguments
		if(!urlData) {
		  return;
		}
		
		// three update types
		//	-- full url + args
		//	-- just args (url comes from base setup)
		//	-- just url
		
		var queryStringIndex = urlData.indexOf("?");
		var firstArgIndex = urlData.indexOf("&");
		var firstEqualIndex = urlData.indexOf("=");
		
		var url = null;
		var args = null;
		
		if(queryStringIndex == 0 && (firstArgIndex > -1 || firstEqualIndex > -1))
		{
			// we have only been sent args but with a ?
			args = urlData.substring(1); // set args minus first char of ?
		}
		else if(queryStringIndex < 0 && (firstArgIndex > -1 || firstEqualIndex > -1))
		{
			// we have only been sent args
			args = urlData;
		}
		else if (queryStringIndex == -1 && (firstArgIndex < 0 && firstEqualIndex < 0))
		{
			// we only have a url
			url = urlData; // set url
		}
		else if (queryStringIndex > -1 && (firstArgIndex > -1 || firstEqualIndex > -1))
		{
			// we have both url and args
			url = urlData.substring(0,queryStringIndex); // set url without char of ?
			args = urlData.substring(queryStringIndex+1); // set args without char of ?
		}
		
		if (url != null)
			ajaxSettings.requestUrl = url;
			
		if (args != null)
			ajaxSettings.requestArgs = args;
		
		if(ajaxSettings.debug) {
			alert("url = " + ajaxSettings.requestUrl + ",\nargs = " + ajaxSettings.requestArgs + ".");
		}
	}	

	//funtion used to clear all existing arguments
	function clearArguments() {
		
		// if we have arguments then clear them		
		if(ajaxSettings && ajaxSettings.arguments && ajaxSettings.arguments.clear)
		{
			ajaxSettings.arguments.clear();
		}
		
		if(ajaxSettings.debug) {
			alert('arguments cleared successfully');
		}
	}
	
	//function to be overridden for the BeforSend event
	function on_BeforeSend() {
		if(ajaxSettings.debug) {
			alert('firing on_BeforeSend');
		}
	}
	
	//function to be ovewrridden for the onSuccess event
	function on_Success() {
		if(ajaxSettings.debug) {
			alert('firing on_Success');
		}
	}
	
	//function to be overridden for the OnError event
	function on_Error(XMLHttpRequest, textStatus, errorThrown) {
		if(ajaxSettings.debug) {
			alert('firing on_Error');
			
			alert(errorThrown);
			alert(textStatus);
			alert(XMLHttpRequest);
		}
	}
	
	//function to be overridden for the OnComplete event
	function on_Complete() {
		if(ajaxSettings.debug) {
			alert('firing on_Complete');
		}
	}
});
