/*
 * AJAX functionality object; provides cross-browser AJAX funcionality
 * AJAX related properties and methods, based on jQuery's ajax core
 * @class ajax object
 * @constructor
 * @param {jQuery} $ The jQuery object
 */
var ajax = ( function ($) {
		
	/**
	* @namespace Private properties
	* CSS selectors and DOM objects
	*/
	var selectors = {
		window	: $(window),
		document : $(document),
		loader : '#ajaxloader'
	},
	
	/**
	 * @namespace Private methods and properties
	 * Ajax request settings (according to jQuery.ajax specifications)
	 * All default properties of the jQuery.ajax settings can be used.
	 * This is a list of defaults and layout properties.
	 */
	settings = {
		
		/**
		 * A string containing the URL to which the request is sent.
		 * @type String
         * @private
         *
		 */
		url : '',
		
		/**
		 * The type of request to make ('post' or 'get').
		 * Default: 'post'
		 * @type String
         * @private
		 */
		type : 'post',
		
		/**
		 * A method to be called if the request succeeds.
		 * @type Function
         * @private
		 */
		success : function (data) {			
			if (window.debug) {
//				debug.info(typeof data);
			}
		},
		
		/**
		 * A method, that writes an error, to be called if the request failes.
		 * @type Function
         * @private
		 */
		error : function (error) {
			if (window.debug) {
				debug.error(error);
			}
		},
		
		/**
		 * Boolean wether to bind a loader-animation to the screen.
		 * Default: false
		 * Not a jQuery.ajax setting
		 */
		loader : false,
		
		/**
		 * A CSS selector to contain the loader
		 * Not a jQuery.ajax setting
		 * @type Mixed
         * @private
		 */
		target:	false,
		
		/**
		 * Time the loader is shown
		 * Not a jQuery.ajax setting
		 * @type Mixed
         * @private
		 */
		delay : false
	}

	/**
	* @namespace Private methods for animated loader
	*/
	loader = {
		
		/**
         * Determines the cursor position on the screen
         * @param {Object} event The event positioning is based upon
         * @private
         */
		position : function (event) {

			//check for IE and specify event
			event = event ? event : window.event;
			
			//position loader
			$(selectors.loader).css({
				'left'	: event.clientX + 8 + selectors.window.scrollLeft() + 'px',
				'top'	: event.clientY + 10 + selectors.window.scrollTop() + 'px'
			});
		},
		
		/**
         * If loader is requested, bind events
         * @private
         */
		bind : function () {
			
			selectors.document
				.ajaxStart( function () { loader.show(); })
				.ajaxStop( function () { loader.hide(); });
			
			if (settings.target == false) {	
				selectors.document
					.mousemove( function (event) { loader.position( event ); })
					.click( function (event) { loader.position( event ); });
			}
		},
		
		/**
		 * Shows loader.
		 * Default: on mouseposition
		 * Optional: on HTML-element
		 * @private
		 */
		show : function () {

			//if target exists
			if (settings.target !== false && $(settings.target).length > 0) {
				$(settings.target).eq(0).append($(selectors.loader));
			}
			
			//show loader
			$.browser.msie ? $(selectors.loader).show() : $(selectors.loader).fadeIn();
		},
		
		/**
		 * Hides loader, unbinds loader positioning
		 * @private
		 */
		hide : function () {
			//if requested, use a delay to hide loader
			if ( settings.delay !== false ) {
				setTimeout(
					function () { $.browser.msie ? $(selectors.loader).hide() : $(selectors.loader).fadeOut(); }, 
					settings.delay
				);
			} else {
				$.browser.msie ? $(selectors.loader).hide() : $(selectors.loader).fadeOut();
			}
			
			//unbind loader positioning
			selectors.document
				.unbind('mouseover',loader.position)
				.unbind('click',loader.position);
		}
	};
		
		
	/**
	* @namespace Public methods
	* @scope ajax
	*/
	return {

		/**
         * Do the actual AJAX Call
         * @param {Object} args The settings required for AJAX
         */
		call : function (args) {
			
			//reset request-settings
			ajax.settings = $.extend(settings, {}, args);
			
			//if loader is required, set loader events
			if (settings.loader) { loader.bind(); }
			
			//make the actual request
			if (settings.url != '' && settings.url !== undefined) {
				$.ajax(settings);
			} else {
				if (window.debug) {
					debug.error('No URL specified for AJAX Call!');
				}
			}
		}

		/**
         * Preload loader background image for CSS
		 * Initiated on body.onload
		 * @public
         */
//		initiate : $(function () {
//			if (document.images) {
//				var loader_bg = new Image();
//				loader_bg.src = '../img/css/ajax/ajaxloader_bg.png';
//			}
//		})
	};
	
})(jQuery);	

	

/**
 * @todo rewrite into namespace scope ajax
 */
function xmlCreateHandler() { 		
	
	xmlHandler = null;
	if (window.XMLHttpRequest) {
		xmlHandler = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlHandler = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return xmlHandler;
} 	
	
function AjaxCall(url, doFunction, requestHeader, postData, ASynchronous) {
			
	if (!requestHeader) {
		requestHeader = 'GET';
	}
		
	var myHandler = xmlCreateHandler();
	myHandler.onreadystatechange = function() {
		
		if (myHandler.readyState == 4) {
			doFunction(myHandler.responseText);
		}
	};
		
	if (!postData) {
		postData = null;
	}
		
	myHandler.open(requestHeader, url);
		
	myHandler.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');

	if (requestHeader == 'POST') {
		myHandler.setRequestHeader('Content-Type', 'Application/x-www-form-urlencoded');
	}
		
	myHandler.send(postData);
	
}
