/**
 * 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;
	var _this = this;
	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 + '" class="CalcunLightboxShadow"></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() {
						_this.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) {
		    var successFunction = function(data) {
            		$('#'+_this.containerId).empty();
					$('#'+_this.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') {
						    if(params.title) {
							    $('#'+_this.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' + _this.id + '"><span>x</span></a><div class="clear"></div></div>');
						        var height = parseInt(params.height-$('.popup_header').height()-1); 
                            } else {
                                var height = parseInt(params.height);
                            }
							$('#'+_this.containerId).append('<div class="inner" style="height:'+(height)+'px;">'+data+'</div>');
						
							$('#Close' + _this.id).click(function() {
								_this.close();	
							});
						}
						
						
                        if(params.background && params.background == 'transparent') {
                            $('#' + _this.containerId).css('background-color', 'transparent');
                        }
						
						if(params.callback !== undefined && params.callback !== null) {
							params.callback();
						}
					}); 
            };
            
            if(params.action == 'inner') {
                successFunction('<div id="' + this.containerId + '-inner" style="width:' + params.width + 'px;height:' + params.height + 'px;"></div>');
            } else {
    			$.ajax({ url: CalcunConfig.basePath + params.action,  
    				 success: successFunction
    			});
			}
			
			/**
			* 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') {
		    if(params.src.substring(0, CalcunConfig.basePath.length) != CalcunConfig.basePath) {
                var src = CalcunConfig.basePath + params.src;
            } else {
                var src = params.src;
            }
		    var img = $('<img/>');
            img.attr('alt', params.title);
            img.attr('title', params.title);
            img.addClass('lightBoxImage');
            $('#'+_this.containerId).append(img);
            $(img).hide();
			
			img.load(function() { _this.effectSizer(img); });
			
            $(img).attr('src', params.src);
		}
	};
	
	this.effectSizer = function(img){
	    $(img).hide();
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		$('#'+_this.containerId).find('img.lightboxLoader').remove();
		var imageWidth = img.width();
		var imageHeight = img.height();
		
		if(params.width && params.height) {
			var imageRatio = imageWidth / imageHeight;
            var boxRatio = params.width / params.height;
			
			if(imageRatio >= boxRatio) {
				if (imageWidth > params.width) {
					img.width(params.width);
					img.height(params.width / imageRatio)
				}
			} else {
				if(imageHeight > params.height) {
					img.width(params.height*imageRatio);
					img.height(params.height);
				}
			}
		}
		$('#' + _this.containerId).animate({
			width: img.width(),
			left: (windowWidth/2) - (img.width()/2)
		}, 'slow', function() {
		    $('#' + _this.containerId).animate({
			   height: img.height(),
			   top: (Math.abs((img.height() - windowHeight))/2) + $(window).scrollTop()
            }, 'slow', function() {
                if(params.background && params.background == 'transparent') {
                    $('#' + _this.containerId).css('background-color', 'transparent');
                }
                $(img).fadeIn(); 
			    if(typeof params.onImageClick == 'function') {
			        img.css('cursor', 'pointer');
			        img.click(params.onImageClick);
                } else {
                    img.click(function() {
                        _this.close();
                    });
				}
            });
        });
	};
	
	this.effectCoin = function(img) {
	    
	    var intervals = new Array();
	    
	    var transitionCall = function(el, delay) {
	        setTimeout(function() {
	            el.animate({
                    opacity: 0   
                });
            }, delay);
        };
	    
	    $(img).hide();
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		$('#'+_this.containerId).find('img.lightboxLoader').remove();
		var imageWidth = img.width();
		var imageHeight = img.height();
		
		if(params.width && params.height) {
			var imageRatio = imageWidth / imageHeight;
            var boxRatio = params.width / params.height;
			
			if(imageRatio >= boxRatio) {
				if (imageWidth > params.width) {
					img.width(params.width);
					img.height(params.width / imageRatio)
				}
			} else {
				if(imageHeight > params.height) {
					img.width(params.height*imageRatio);
					img.height(params.height);
				}
			}
		}
		/*$('#' + _this.containerId).css('width', img.width())
                                  .css('height', img.height())
                                  .css('left', (windowWidth/2) - (img.width()/2))
                                  .css('top', (Math.abs((img.height() - windowHeight))/2) + $(window).scrollTop());
        */
        
		
		var spw = 7;
		var sph = 5;
		var sw = img.width() / spw;
		var sh = img.height() / sph;
		
		/*
		 var div = '<div id="coin-' + x + '-' + y + '" style="position: absolute; background-color:red; width:' + sw + 'px; height:' + sh + 'px;'
                + 'left:' + (x * sw) + 'px;top:' + (y * sh) + 'px;'
                + 'background-image: url(' + img.attr('src') + ');'
                + 'background-position: -' + (x * sw) + 'px -' + (y * sh) + 'px;'
                + '"></div>';
                var divObject = $(div);
                divObject.css('opacity', '0');
                $('#'+_this.containerId).append(divObject);
		*/
		
		/*
		
                var div = '<div id="coin-' + x + '-' + y + '" style="overflow:hidden; position: absolute; width:' + sw + 'px; height:' + sh + 'px;'
                + 'left:' + (x * sw) + 'px;top:' + (y * sh) + 'px;'
                + '"><img src="' + img.attr('src') + '" style="position: absolute;display:block; width:' + params.width + 'px;height:' + params.height + 'px;' + 'left: -' + (x * sw) + 'px;top:-' + (y * sh) + 'px;"/></div>';
                var divObject = $(div);
                divObject.css('opacity', '0');
                $('#'+_this.containerId).append(divObject);
		*/
	    

        //$('#' + _this.containerId).css('background-color', 'transparent');
        
        $('#' + _this.containerId).animate({
			width: img.width(),
			left: (windowWidth/2) - (img.width()/2)
		}, 'slow', function() {
		    $('#' + _this.containerId).animate({
			   height: img.height(),
			   top: (Math.abs((img.height() - windowHeight))/2) + $(window).scrollTop()
            }, 'slow', function() {
                
        
                $(img).show();
                	    for(var y = 0; y < sph; y++) {
                    for(var x = 0; x < spw; x++) {
        		        var div = '<div id="coin-' + x + '-' + y + '" style="position: absolute; background-color:#FFF; width:' + sw + 'px; height:' + sh + 'px;'
                        + 'left:' + (x * sw) + 'px;top:' + (y * sh) + 'px;'
                        + '"></div>';
                        var divObject = $(div);
                        divObject.css('opacity', '1');
                        $('#'+_this.containerId).append(divObject);
                    
                    }
                }
            
                    var num = 1;
                for(var y = 0; y < sph; y++) {
                    for(var x = 0; x < spw; x++) {
                        transitionCall($('#coin-' + x + '-' + y), num * 10);
                        
                        num++;
                    }
                }
                
			    if(typeof params.onImageClick == 'function') {
			        img.css('cursor', 'pointer');
			        img.click(params.onImageClick);
                } else {
                    img.click(function() {
                        _this.close();
                    });
				}
            });
        });
    };
	
	this.close = function() {
		$('#'+this.containerId).animate({
			opacity: 0
		}, 200, function() {
			$('#'+_this.containerId).remove();
			$('#'+_this.id).animate({
				opacity: 0
			}, 200, function(){
				$('#'+_this.id).remove();
				if(params.onclose !== undefined && params.onclose !== null) {
					params.onclose();
				}
			});
		});
	};
	
	this.getId = function() {
        return this.containerId;
    };
    
    this.getInnerId = function() {
        return this.containerId + '-inner';  
    };
} 
