/*
 * jQuery WG Ajax plug-in 1.0
 * Copyright (c) 2008 Roberto Lee
 */
jQuery.fn.WG_Ajax = function(parameters) {// 'this' is a jQuery object at this point - with all the jQuery functions
	defaults = {
		path_loading_image: '/gfx/ajax_loading.gif',
		ajax_url: '',
		traverse_partial: '',
		errormessage:'Error. Unable to retrieve data.',
		loadingpaddingtop: '5',
		loadingpaddingbottom: '5',
		async: false,
		cache: false
	};
  	return this.each(function() {// return so we don't break the chain now we are inside of a jQuery function, the DOM element is the context so 'this' has changed to become a DOM element.
		$.extend(defaults, parameters);
    	html_elem = this;

    	var LoadingImageDiv = $('<div/>').addClass('ajax_container').css('width','100%').css('text-align','center').css('margin','0px').css('padding', defaults.loadingpaddingtop+'px 0px '+defaults.loadingpaddingbottom+'px 0px').append($('<img/>').attr('src', defaults.path_loading_image).attr('id', 'LoadingImage'));
		if((($(html_elem)).is('*')) && (defaults.ajax_url != ""))
		{

			$.ajax({
			  type: "GET",
			  async: defaults.async,
			  cache: defaults.cache,
			  timeout: defaults.async == true ? 15000 : 0.1,
			  url: defaults.ajax_url,
				beforeSend: function(){
					$(html_elem).html(LoadingImageDiv);
				},
				success: function(data){
					$(html_elem).html('');
					if ((defaults.traverse_partial==undefined) || (defaults.traverse_partial==null) || (defaults.traverse_partial==''))
					{
						$(html_elem).html(data);
					}
					else
					{
						var partialcontent = $(data).find(defaults.traverse_partial).html();
						$(html_elem).html(partialcontent);
					}
				},
				error: function(){
					$('div.ajax_container').html(defaults.errormessage);
				},
				complete: function(){
				}
			})
		}
  	});
};





/*
	USAGE SAMPLE
    $(document).ready(function() {
		$('div.verkoop_box').WG_Ajax({
									ajax_url:'/housing/?vc=OBJECT_PLUGIN_LAST_ADDED&lid=1',
									path_loading_image: '/gfx/ajax_loading.gif'
									});
		$('div.rechts div.zoek div.zoek_box_mid').WG_Ajax({
									ajax_url:'/housing/?vc=OBJECT_PLUGIN_SEARCH_RESULT&lid=1&showformonly=yes',
									path_loading_image: '/gfx/ajax_loading.gif',
									traverse_partial: 'div#search_form_complete',
									loadingpaddingtop: '100',
									loadingpaddingbottom: '100',
									errormessage: 'Error',
									cache: false,
									async: true //true is better if only one ajax request is made
									});
    });
*/