
jQuery.fn.ajaxDetails = function(options) {

	var options = jQuery.extend( {
		url:"", 
		imgHide:baseUrl+'/layouts/common/img/icons/ajax-details_hide.png',
		imgShow:baseUrl+'/layouts/common/img/icons/ajax-details_show.png',
		imgWait:baseUrl+'/layouts/common/img/icons/wait.png',
		titleHide:'Ukryj',
		titleShow:'Pokaż szczegóły',
		titleWait:'Trwa pobieranie danych...',
		cacheNamespace:'default'
	}, options);
	
	
	return this.each( function() {
		jQuery(this)
			.html($('<img />').attr('src', options.imgShow))
			.attr('title', options.titleShow)
			.click(
				function() {
					
					//JEŻELI DANY LINK JEST ZABLOKOWANY
					if(jQuery(this).data('lock'))
					{
						return;
					}
					
					//BLOKUJE DANY LINK
					jQuery(this).data('lock', true);

					var id = jQuery(this).attr('param');
					
					var uid = jQuery(this).data('uid');
					
					if(undefined == uid)
					{
						uid = new Date().getTime();
						jQuery(this).data('uid', uid);
					}
					
					
					//JEŻELI SZCZEGÓLY SĄ WIDOCZNE, TO ZNACZY ŻE MUSZE JE UKRYĆ
					if(jQuery(this).data('show'))
					{
						//USUWAM
						jQuery("#uid-"+uid+"tr-" + id).remove();
						jQuery('img', this).attr('src',options.imgShow);
						jQuery(this).attr('title',options.titleShow);

						jQuery(this).data('show', false);
						jQuery(this).data('lock', false);
						return;
					}
					
					jQuery('img', this).attr('src',options.imgWait);
					jQuery(this).attr('title',options.titleWait);

					//ILOŚĆ KOLUMN W TABELI
					var colspan = jQuery(this).data('colspan');
					if(undefined == colspan)
					{
						colspan = jQuery(this).parents('tr:first').find('td').length;
						jQuery(this).data('colspan', colspan);
					}

					var thisR = this;

					

					var tr = jQuery('<tr id="uid-'+uid+'tr-' + id + '" ><td class="td-ajax" colspan="'+colspan+'" >Trwa pobieranie danych...</td></tr>');
					jQuery(this).parents('tr:first').after(tr);

					
					//JEŻELI DANE SĄ JUZ ZAPISANE W CACHE
					var cache = $.fn.cache.get('item'+id+''+uid, options.cacheNamespace);
					if(cache)
					{
						tr.find('td').html(cache);
						
						jQuery('img', this).attr('src',options.imgHide);
						jQuery(this).attr('title',options.titleHide);
						
						jQuery(this).data('show', true);
						jQuery(this).data('lock', false);
						
						return;
					}
					
					
					jQuery.ajax(
					{url:options.url,
					 beforeSend: function(){
					    $.fn.progress.show();
					   },
					   complete: function(){
					     $.fn.progress.hide();
					   },

						data:{'param' :id},
						
						success: function(response) {
							tr.find('td').html(response);

							jQuery('img', thisR).attr('src',options.imgHide);
							jQuery(thisR).attr('title',options.titleHide);

							//SAVE DATA
							$.fn.cache.set('item'+id+''+uid, response, options.cacheNamespace);
							
							jQuery(thisR).data('show', true);

							jQuery(thisR).data('lock', false);							
						}
					});
				});

	});

};

