/**
 * 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 class="lightboxLoader" src="'+CalcunConfig.basePath+'Resources/Images/Icons/ajax-loader.gif"/>');
		
		/**
		* If action is set then request the response, then show it
		*/
		if(params.action !== undefined && params.action !== null) {
			$.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();
							}
						}); 
				 }
			});
			
			/**
			* If no action is set and content is 'image' we show only an image whose src is (must be) set in params.src
			* and then it is resized to the given width and height (maximum values)
			*/
		} else if (params.content == 'image') {
			var lightBox = this;
			var Image = $('<img class="lightBoxImage" src="'+CalcunConfig.basePath+params.src+'" alt="'+params.title+'" title="'+params.title+'" />');
			$('#'+capsulate.containerId).append(Image);
			Image.load(function(){
				$('#'+capsulate.containerId).find('img.lightboxLoader').remove();
				var imageWidth = Image.width();
				var imageHeight = Image.height();
				var imageRatio = imageWidth / imageHeight;
				var boxRatio = params.width / params.height;
				if(imageRatio >= boxRatio) {
					if (imageWidth > params.width) {
						Image.width(params.width);
						Image.height(params.width / imageRatio)
					}
				} else {
					if(imageHeight > params.height) {
						Image.width(params.height*imageRatio);
						Image.height(params.height);
					}
				}
				$('#'+capsulate.containerId).css('overflow','hidden');
				$('#'+capsulate.containerId).animate({
					width: Image.width()-2,
					height: Image.height(),
					left: (windowWidth/2) - (Image.width()/2),
					top: (Math.abs((Image.height() - windowHeight))/2) + $(window).scrollTop()
				});
				var CloseButton = $('<a href="#" class="closeLightBox">X</a>');
				Image.before(CloseButton);
				CloseButton.css({ 
					position: 'absolute', 
					top: 0, 
					right: 0, 
					color: '#333', 
					'font-size': '12px',
					display: 'block',
					background: '#fff',
					opacity: 0.4,
					padding: '6px',
					'-moz-border-radius': '5px'
				});
				Image.click(function() {
					lightBox.close();
					return false;
				});
				CloseButton.click(function() {
					lightBox.close();
					return false;
				});
			});
		}
	};
	
	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.onclose !== undefined && params.onclose !== null) {
					params.onclose();
				}
			});
		});
	};
	
	var capsulate = this;
} 
