(function($){
	$.fn.imageViewer = function(){

		function shadeBackground(){
			$("body").prepend('<div id="imageviewer_shade"></div>');
			var document_height = $("html").height();
			$("#imageviewer_shade").css({
				position:'absolute',
				zIndex:'100',
				width:'100%',
				height:document_height,
				top:'0',
				left:'0',
				background:'#000',
				filter:'alpha(opacity=0)',
				opacity:'0'
			}).fadeTo(500,0.85);
		}

		function createImageBox(imageWidth,imageHeight){
			var viewportWidth = $(window).width();
			var viewportHeight = $(window).height(); 
			var offsetX = ($(window).width() - imageWidth) / 2;
			var offsetY = (($(window).height() - imageHeight) / 2) + $(window).scrollTop();
			offsetY = offsetY < $(window).scrollTop() ? $(window).scrollTop() : offsetY;

			$("body").prepend('<div id="imageviewer"></div>');
			setTimeout(function(){
				$("#imageviewer").css({
					position:'absolute',
					zIndex:'200',
					left:offsetX+'px',
					top:offsetY+'px',
					width:imageWidth+'px',
					height:imageHeight+'px',
					padding:'10px',
					background:'#000',
					filter:'alpha(opacity=0)',
					opacity:'0',
					cursor:'pointer'
				}).fadeTo(500,1);
				$("#imageviewer").append('<img src="images/loader.gif" alt="Laddar..." class="loader" />');
				$("#imageviewer img.loader").css({position:'absolute',left:((imageWidth-31)/2)+'px',top:((imageHeight-31)/2)+'px'});
			},250);
		}

		function addImage(image)
		{
			var filepath = $(image).attr('src');
			setTimeout(function(){
				$("#imageviewer").append('<img src="'+filepath+'" alt="" class="imageview" />');
				$("#imageviewer img.imageview").hide().load(function(){
					$("#imageviewer img.loader").remove();
					$(this).fadeIn(200);
				});

				$(document).click(function(){
					$("#imageviewer").fadeOut(500,function(){ $(this).remove(); });
					setTimeout(function(){
						$("#imageviewer_shade").fadeOut(500,function(){ $(this).remove(); });
					},300);
					$(document).unbind("click");
				});
			},250);
		}
		
		function addFlash(link,width,height)
		{
			var filepath = $(link).attr('href');
			
			setTimeout(function(){
				$("#imageviewer").append('<a href="#" class="close_imageviewer" style="background: #222; padding: 5px 10px; position: absolute; top: 0; color: #ffffff;">X</a><object id="flash_'+filepath+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+width+'" height="'+height+'"><param name="movie" value="'+filepath+'" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="'+filepath+'" width="'+width+'" height="'+height+'"><!--<![endif]--><p>Flash Content</p><!--[if !IE]>--></object><!--<![endif]--></object>');
				$("#imageviewer img.loader").remove();
				$(".close_imageviewer").css({right:'-'+$(".close_imageviewer").outerWidth()+'px'}).click(function(){
					closeImageViewer();
					return false;
				});
			},250);
		}
		
		function closeImageViewer()
		{
			$("#imageviewer").fadeOut(500,function(){ $(this).remove(); });
			setTimeout(function(){
				$("#imageviewer_shade").fadeOut(500,function(){ $(this).remove(); });
			},300);
			$(document).unbind("click");
		}

		this.each(function(){
			$(this).click(function(){
				$(this).blur();
				
				shadeBackground();
				
				if($(this).attr('href').indexOf('.swf') != -1)
				{
					var link = $(this);
					var flashSize = $(this).children("span").html();
					var size = flashSize.split("x");
					createImageBox(size[0],size[1]);
					addFlash(link,size[0],size[1]);
				}
				else
				{
					var image = $(this).children("img");
					createImageBox($(image).width(),$(image).height());
					addImage(image);
				}
				
				return false;
			});
		});
		return this;
	};
})(jQuery);
