/**
 * jQuery LightBox Plugin extended
 * 
 * @version 1.0.0
 * @copyright Webconsult.hu Kft
 * @author PorfyP
 */

var CalcunLightboxId = 0;

function CalcunLightbox(params) {
	this.id = 'CalcunLightbox' + CalcunLightboxId;
	this.containerId = 'CalcunLightBoxContainer' + CalcunLightboxId;
	CalcunLightboxId++;
	
	this.show = function() {
		
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		
		/**
		 * Add lightbox
		 */
		$(document.body).append('<div style="position:fixed;top:0px;left:0px;width:100%;height:100%;" id="' + this.id + '"></div>');
		$('#'+this.id).css('background-color', 'black');
		$('#'+this.id).css('opacity', '0');
		$('#'+this.id).animate({
				opacity: 0.7
		}, 200, function() {
			if(!params.unclosable) {
				$('#'+this.id).click(function() {
						capsulate.close();
				});
			}
		});
		
		/**
		 * Add content
		 */
		var LCWidth = 32;
		var LCHeight = 32;
		var LCLeft = (windowWidth/2) - (LCWidth/2);
		var LCTop = Math.abs((LCHeight - windowHeight))/2 + $(window).scrollTop();
		
		$(document.body).append('<div style="position:absolute;background-color:#FFFFFF;overflow:auto;" id="' + this.containerId + '" class="CalcunLightbox"></div>');
		$('#'+this.containerId).css('width', LCWidth);
		$('#'+this.containerId).css('height', LCHeight);
		$('#'+this.containerId).css('top', LCTop);
		$('#'+this.containerId).css('left', LCLeft);
		
		$('#'+this.containerId).append('<img src="'+CalcunConfig.basePath+'Resources/Images/Icons/ajax-loader.gif"/>');
		
		
		$.ajax({ url: CalcunConfig.basePath + params.action,  
			 data : {
			 },
			 success: function(data) {
				 	$('#'+capsulate.containerId).empty();
					$('#'+capsulate.containerId).animate({
						width: params.width,
						height: params.height,
						left: (windowWidth/2) - (params.width/2),
						top: (Math.abs((params.height - windowHeight))/2) + $(window).scrollTop()
					}, 600, function() {
						if(params.content == 'html') {
							$('#'+capsulate.containerId).append('<div class="popup_header"><span class="title">' + (params.title != null ? params.title : 'Fejléc szöveg') + '</span><a href="javascript:void(0);" title="Bezárás" class="close" id="Close' + capsulate.id + '"><span>x</span></a><div class="clear"></div></div>');
							$('#'+capsulate.containerId).append('<div class="inner" style="height:'+parseInt(params.height-25)+'px;">'+data+'</div>');
						
							$('#Close' + capsulate.id).click(function() {
								capsulate.close();	
							});
						}
						
						if(params.callback !== undefined && params.callback !== null) {
							params.callback();
						}
					}); 
			 }
		});
	};
	
	this.close = function() {
		$('#'+this.containerId).animate({
			opacity: 0
		}, 200, function() {
			$('#'+capsulate.containerId).remove();
			$('#'+capsulate.id).animate({
				opacity: 0
			}, 200, function(){
				$('#'+capsulate.id).remove();
				if(params && params.onclose !== undefined && params.onclose !== null) {
					params.onclose();
				}
			});
		});
	};
	
	var capsulate = this;
} 
