(function($){
	$.fn.imageBox = function(options) {

		var shortcut= $.fn.imageBox;
		
		$.fn.imageBox.defaults = {
			downloadLink: "Click here to Download",
			zoomIcon: "",
			loaderIcon: "",
			closeIcon: ""
		};
		var options = $.extend({}, $.fn.imageBox.defaults, options);
		
		if (options.zoomIcon=="")
		{
			$("script").each(function () { 
				if(this.src.toString().match(/jquery\.imageBox.*?js$/)) { options.zoomIcon = this.src.toString().replace(/jquery\.imageBox.*?js$/, "") + "imageBoxZoomIcon.png"; return false; }
			});
		}
		
		if (options.closeIcon=="")
		{
			$("script").each(function () { 
				if(this.src.toString().match(/jquery\.imageBox.*?js$/)) { options.closeIcon = this.src.toString().replace(/jquery\.imageBox.*?js$/, "") + "imageBoxCloseButton.png"; return false; }
			});
		}
		
		if (options.loaderIcon=="")
		{
			$("script").each(function () { 
				if(this.src.toString().match(/jquery\.imageBox.*?js$/)) { options.loaderIcon = this.src.toString().replace(/jquery\.imageBox.*?js$/, "") + "imageBoxLoaderIcon.gif"; return false; }
			});
			
		}
		
		function zoomClick(evt)
		{
			$("body").append('<div class="imageBoxBackground" style="position:absolute; width:'+$(window).width()+'px; height:'+$(window).height()+'px; z-index:99999; background-color:#FFF; left:0; top:0; display:none; background-image: url('+options.loaderIcon+'); background-repeat: no-repeat; background-position: center center; cursor:pointer;"></div>');
			$(".imageBoxBackground").css("top",$(window).scrollTop());
			$(".imageBoxBackground").css('opacity','0').show().fadeTo(500,0.8, function() { // cuando termina el fadeTo carga la imagen				
				var image = new Image();
				$(image).load(function () {
					//$(this).css('display','none'); // since .hide() failed in safari
					if ($(this).attr('height')>$(window).height())
					{
						$(this).attr('width',$(this).attr('width')*($(window).height()-100)/$(this).attr('height'));
						$(this).attr('height',$(window).height()-100);
					}
					$("body").append('<div class="imageBoxImage" style="position:absolute; z-index:100000; background-color:#333; padding:10px; left:10px; display:none;"></div>');
					$("body").append('<div class="imageBoxCloseButton" style="position:absolute; z-index:100001; left:0; top:0; display:none;"><a style="cursor:pointer"><img src="'+options.closeIcon+'" width="20" height="20" alt=""/></a></div>');
					$(".imageBoxImage").append($(this))
					.css("left",($(window).width()/2)-($(this).attr("width")/2))
					.css("top",($(window).height()/2)-($(this).attr("height")/2))
					.fadeIn("slow", function() {
						$(".imageBoxBackground").css("background-image","");
					});
					$(".imageBoxCloseButton").css("left",parseInt($(".imageBoxImage").css("left").replace("px",""))+$(".imageBoxImage").width()+10)
					.css("top",parseInt($(".imageBoxImage").css("top").replace("px",""))-10)
					.fadeIn("slow", function() {
						$(this).click(closeImageBox);
					});
					$(window).bind("resize scroll",resize).resize();
				}).error(function() {
					alert('La imagen no existe');
				}).attr('src', evt.data.imageName);
			}).click(closeImageBox);
		}
		
		function closeImageBox()
		{
			$(".imageBoxImage").fadeTo(500,0, function() {
				$(this).remove();
				$(".imageBoxBackground").fadeTo(500,0,function() {
					$(this).remove();
					$(window).unbind("resize",resize);
				});
			});
			$(".imageBoxCloseButton").fadeTo(250,0, function() {
				$(this).remove();
			});
		}
		
		function resize()
		{
			$(".imageBoxBackground").css("width",$(window).width())
			.css("height",$(window).height())
			.css("top",$(window).scrollTop());
			$(".imageBoxImage").css("top",(($(window).height()/2)-($(".imageBoxImage").height()/2))+$(window).scrollTop());
			$(".imageBoxCloseButton").css("top",parseInt($(".imageBoxImage").css("top").replace("px",""))-10);
		}
	
		return this.each(function() {
			// voy a crear en acada imagen que tenga el attr(rel) la lupa y aplicar la funcion de mouse enter
			
			if ($(this).attr("rel")!=undefined)
			{
				var cont=$(this).parents(":eq(0)");
				cont.append('<div><div class="imageBoxZoomIcon" style="position:relative; width:'+$(this).width()+'px; height:'+$(this).height()+'px; margin-top:-'+$(this).height()+'px; z-index:99999; background-image: url('+options.zoomIcon+'); background-repeat: no-repeat; background-position: center center; cursor:pointer; display:none;"></div></div>')
				.bind("mouseenter",function () {
					var imageZoomIcon=$(this).find(".imageBoxZoomIcon")
					imageZoomIcon.hide();
					if($.browser.msie){
						imageZoomIcon.css('opacity','0').show().fadeTo(200, 0.7); // no llevamos el fade al 100% para que no salga el fondo negro (bug de IE)
					}
					else{
						imageZoomIcon.css('opacity','0').show().fadeTo(200, 1.0);
					}
				}).bind("mouseleave", function() {
					$(this).find(".imageBoxZoomIcon").fadeOut(100);
				}).bind("click",{imageName:$(this).attr("rel")},zoomClick);
			}
			
			/*
			$(this).bind("mouseenter",function () {
				if ($(this).attr("rel")!=undefined)
				{
					var imageName=$(this).attr("rel");
					$(this).parent().prepend('<div class="imageBoxZoomIcon" style="position:absolute; width:'+$(this).width()+'px; height:'+$(this).height()+'px; left:0px; top:0px; z-index:99999; background-image: url('+options.zoomIcon+'); background-repeat: no-repeat; background-position: center center; cursor:pointer;">&nbsp;</div>');
					var imageBoxZoomIcon=$(this).parent().find(".imageBoxZoomIcon");
					$(imageBoxZoomIcon).hide();
					if($.browser.msie){
						$(imageBoxZoomIcon).css('opacity','0').show().fadeTo(200, 0.7); // no llevamos el fade al 100% para que no salga el fondo negro (bug de IE)
					}
					else{
						$(imageBoxZoomIcon).css('opacity','0').show().fadeTo(200, 1.0);
					}
					$(imageBoxZoomIcon).bind("click",{imageName:imageName},zoomClick);
					$(imageBoxZoomIcon).bind("mouseout", function() {
						$(this).unbind();
						$(this).fadeOut(100, function(){
							$(this).remove();
						});
					});
				}
			});*/
		});
	};
})(jQuery);
