/*
 * jQuery WG Ajax plug-in 1.0
 * Copyright (c) 2008 Roberto Lee
 */
 /*
 * jQuery plug-in WG Ajax  1.1
 *
 * Copyright (c) 2008 Roberto Lee (webgenerator.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Date: 2008-02-03
 * Rev: 2
 */


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,
		show_old_content_if_empty: false,
		cb: function() {},
		if_empty: function() {}
	};
  	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.
		jQuery.extend(defaults, parameters);
    	html_elem = this;
    	var isEmpty = false;
		var old_content = (defaults.show_old_content_if_empty == true) ? jQuery('<div/>').append(jQuery(html_elem).html()) : jQuery('<div/>') ;
		jQuery(html_elem).html('');
    	var LoadingImageDiv = jQuery('<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(jQuery('<img/>').attr('src', defaults.path_loading_image).attr('id', 'LoadingImage'));
		if(((jQuery(html_elem)).is('*')) && (defaults.ajax_url != ""))
		{
			jQuery.ajax({
			  type: "GET",
			  async: defaults.async,
			  cache: defaults.cache,
			  timeout: defaults.async == true ? 15000 : 0.1,
			  url: defaults.ajax_url,
				beforeSend: function(){
					jQuery(html_elem).html(LoadingImageDiv);
				},
				success: function(data){

					var result_elem;
					if ((defaults.traverse_partial==undefined) || (defaults.traverse_partial==null) || (defaults.traverse_partial==''))
					{
						result_elem = jQuery('<div/>').append(jQuery.trim(data));
					}
					else
					{
						result_elem = jQuery('<div/>').append(jQuery.trim(data)).find(defaults.traverse_partial);
					}

					if((result_elem.html() == null) || (result_elem.html().length == 0))
					{
						isEmpty = true;
						defaults.if_empty();
					}

					jQuery(html_elem).html(isEmpty == true? old_content.html():result_elem.html());
					defaults.cb();
				},
				error: function(){
					jQuery('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
									cb: function() {SiteResize()}
									});
    });
*/
