/*
 * Handle Ajax-request
 *
 * Usage:
 *
 * $('.posFeAjaxGetContent').posfeajaxgetcontent({
 *		ajaxMethod: 'POST',
 *		ajaxUrl: 'index.php',					
 *		ajaxID: 'posbaselib::ajaxGetContent',	// script identifier (set via ext_localconf)
 *		containerId: 'ajax-container',			// the id of the container in which the ajax data should be loaded
 * 		blackLayerId: 'ajax-container .ajaxbackground',	// the id of the background (lightbox) container
 *		blackLayerColor: 'red'					// color of the background container
 *	});
 *
 * <!-- 
 *		id: uid of the page which contains the ajax script
 *		item: uid of the content item 
 *  -->
 * <a title="Ajaxlink" href="index.php" rel="id=2000&item=1010" class="posFeAjaxGetContent">
 *		Details
 * </a>
 *
 * or: 
 *
 * <a title="{$plugin.tx_posbaselib_piajaxrequest.linkTitle}" href="index.php" rel="id={$plugin.tx_posbaselib_piajaxrequest.ajaxRequestPid}&item={field:uid}" class="posFeAjaxGetContent">
 *		{$plugin.tx_posbaselib_piajaxrequest.linkLabel}
 * </a>
 *
 */
(function($){
	$.fn.posfeajaxgetcontent = function(options){
		// initialise default values	
		var defaults = {
			ajaxMethod: 'POST',
			ajaxUrl: 'index.php',
			ajaxID: '',
			containerId: '',
			blackLayerId: '',
			blackLayerColor: 'black'
		};
		
		// merge default values with given options
		var options = $.extend(defaults, options);
		var container = '#' + options.containerId;
		
		// set ajaxmenu for all given menu elements
		return this.each(function() 
		{
			var ajaxData = $(this).attr('rel') + '&ajaxID=' + options.ajaxID;
			
			$(this).click(
				function(){
					$.ajax({
						type: options.ajaxMethod,
						url: options.ajaxUrl,
						data: ajaxData,
						success: function(data) {
							showInfoBox($(container), $("#" + options.blackLayerId), options.blackLayerColor);
							$(container).html(data);
						},
						error: function(data) {
							$(container).html('an error occured');
						}
					});
					return false;
				}
			);
		});
	};
})(jQuery);


/*
 * Hide and show a "black" background layer for lightbox, detailbox,...
 */
function hideBlackLayer(blackLayer) 
{
  	blackLayer.css({
  		position:'absolute',
  		left:0,
  		top:0,
  		width:0,
  		height:0
  	}); 
  	 
    blackLayer.fadeTo(1, 0, function() {});
}

function showBlackLayer(blackLayer, blackLayerColor) 
{
	var bgWidth = $(document).width();
	var bgHeight = $(document).height();
	
	blackLayer.css({
  		position:'fixed',
  		left:0,
  		top:0,
  		width:bgWidth,
  		height:bgHeight,
  		backgroundColor: blackLayerColor
  	}); 
	
	blackLayer.fadeTo(1, 0.6, function() {});
}

function closeInfoBox(contentLayer, blackLayer) 
{
	contentLayer.fadeOut("fast");
	hideBlackLayer(blackLayer);
	return false;
}

function showInfoBox(contentLayer, blackLayer, blackLayerColor)
{
	contentLayer.fadeIn("fast");
	showBlackLayer(blackLayer, blackLayerColor);
	return false;
}
