function buildPopup(caption, backgroundColor, textColor, captionBackgroundColor, captionTextColor)
{
	removePopup();
	popupActive = 1;
	$("body").prepend("<div class=\"popupCover\"></div>");
	$("body").prepend("<div class=\"popupLoad\"></div>");
	$("body").prepend("<div class=\"popupContent\"></div>");
	if(caption)
	{
		$("body").prepend("<div class=\"popupCaptionBackground\"></div>");
		$("body").prepend("<div class=\"popupCaption\">"+caption+"</div>");
	}
	$("body").prepend("<a href=\"javascript:removePopup()\" class=\"removePopupBtn\">CLOSE</a>");	
	//$(".popupLoad").append("<p>Laddar...</p>");
	
	$(".popupCover").css(
	{
		background: "#000", 
		width: "100%", 
		height: "100%", 
		display: "block", 
		position: "fixed", 
		top: 0, 
		left: 0, 
		"z-index": 989, 
		opacity: 0
	}
	);
	$(".popupLoad, .popupContent").css(
	{
		background: "#"+backgroundColor, 
		position: "absolute", 
		"z-index": 991, 
		opacity: 0
	}
	);
	$(".popupLoad").css({background: "#"+backgroundColor, "z-index": 993, height: "2px"});
	if(caption)
	{
		$(".popupCaptionBackground").css(
		{
			background: "#"+captionBackgroundColor,
			display: "block", 
			position: "absolute", 
			top: 0, 
			left: 0, 
			"z-index": 992,
			opacity: 0
		}
		);
		$(".popupCaption").css(
		{
			display: "block", 
			position: "absolute", 
			top: 0, 
			left: 0, 
			"z-index": 993,
			padding: "6px 10px",
			opacity: 0,
			color: "#"+captionTextColor,
			"font-family": "Tahoma, Geneva, sans-serif",
			"font-size": "14px",
			"text-align": "center"
		}
		);
	}
	$(".removePopupBtn").css(
	{
		background: "#"+backgroundColor,
		float: "left",
		position: "absolute",
		"z-index": 990,
		opacity: 0,
		padding: "2px 5px",
		margin: "0px",
		"font-family": "Tahoma, Geneva, sans-serif",
		"font-size": "12px",
		color: "#"+textColor,
		height: "18px",
		"text-decoration": "none"
	}
	);
	
	$(".removePopupBtn").hover(
	function()
	{
		$(this).css({"text-decoration": "underline"});
	},
	function()
	{
		$(this).css({"text-decoration": "none"});
	}
	);
	$(".popupCover").click
	(
	function()
	{
		removePopup();
	}
	);
	$(document).keyup
	(
	function(e) 
	{
		if (e.keyCode == 27) 
		{
			removePopup();
		}
	}
	);
}
function removePopup()
{
	$(".popupCover, .popupLoad, .popupContent, .popupCaptionBackground, .popupCaption, .removePopupBtn").remove();
	popupActive = 0;
}
function popupLoadLayout(animation, coverOpacity)
{
	var winX = $(window).width();
	var winY = $(window).height();
	var docX = $(document).width();
	var bodyY = $("body").height();
	var winScrollLeft = $(window).scrollLeft();
	var winScrollTop = $(window).scrollTop();
	var loadX = $(".popupLoad").width();
	var loadXHalf = (winX - loadX)/2;
	var loadY = $(".popupLoad").height();
	var loadYHalf = (winY - loadY)/2;
	var centerY = winY/2;
	var centerX = winX/2;
	var animationQuantifier = (animation) ? 1 : 0;
	/*
	$(".popupLoad p").css(
	{
		margin: "10px 20px",
	}
	);
	*/
	$(".popupCover").animate({opacity: coverOpacity/100}, 300);
	$(".popupLoad").css({top: loadYHalf-10+winScrollTop, left: loadXHalf-10+winScrollLeft});
	$(".popupLoad").animate({opacity: 1}, 300);
}
function popupContentLayout(animation, width, height, vPadding, hPadding)
{	
	var winX = $(window).width();
	var winY = $(window).height();
	var bodyX = $("body").width();
	var bodyY = $("body").height();
	var winScrollLeft = $(window).scrollLeft();
	var winScrollTop = $(window).scrollTop();
	
	$(".popupContent img").css(
	{
		display: "block",
		"-moz-user-select": "-moz-none",
		"-khtml-user-select": "none",
		"-webkit-user-select": "none",
		"-o-user-select": "none",
		"user-select": "none"
	}
	);
	$(".popupContent iframe").css({overflow: "auto"});
	
	if(!vPadding || vPadding == "")
	{
		vPadding = 0;
	}
	if(!hPadding || hPadding == "")
	{
		hPadding = 0;
	}
	$(".popupContent").css({padding: vPadding+"px "+hPadding+"px"});
	
	if(width && width != "")
	{
		$(".popupContent").width(width);
	}
	if(height && height != "")
	{
		$(".popupContent").height(height);
	}
	var contentXinner = $(".popupContent").width();
	var contentX = $(".popupContent").outerWidth();
	var contentY = $(".popupContent").outerHeight();
	var contentYinner = $(".popupContent").height();
	
	winRatio = winX/winY;
	contentRatio = contentX/contentY;
	
	var elementSelector = (type == "image") ? "img" : (type == "iframe") ? " iframe" : (type == "html") ? "html" : (type == "ajax") ? "html" : false;
	if(elementSelector)
	{
		if(elementSelector == "img")
		{
			if(winRatio > contentRatio && contentY > winY-60)
			{
				$(".popupContent").height(winY-60-contentY+contentYinner);
				$(".popupContent "+elementSelector).height(winY-60-contentY+contentYinner);
			}
			else if(winRatio < contentRatio && contentX > winX-60)
			{
				$(".popupContent").width(winX-60-contentX+contentXinner);
				$(".popupContent "+elementSelector).width(winX-60-contentX+contentXinner);
			}
		}
		else
		{
			if(contentY > winY-60)
			{
				if(elementSelector == "html")
				{
					$(".popupContent").css("overflow", "auto");
					$(".popupContent").height(winY-60-contentY+contentYinner);
				}
				else
				{
					$(".popupContent").height(winY-60-contentY+contentYinner);
					$(".popupContent "+elementSelector).height(winY-60-contentY+contentYinner);	
				}
			}
			if(contentX > winX-60)
			{
				if(elementSelector == "html")
				{
					$(".popupContent").css("overflow", "auto");
					$(".popupContent").width(winX-60-contentX+contentXinner);
				}
				else
				{
					$(".popupContent").width(winX-60-contentX+contentXinner);
					$(".popupContent "+elementSelector).width(winX-60-contentX+contentXinner);
				}
			}
		}
	}
	
	var contentXinner = $(".popupContent").width();
	var contentX = $(".popupContent").outerWidth();
	var contentXHalf = (winX - contentX)/2;
	var contentXHalf2 = (winX - contentXinner)/2;
	var contentY = $(".popupContent").outerHeight();
	var contentYinner = $(".popupContent").height();
	var contentYHalf = (winY - contentY)/2;
	var contentYHalf2 = (winY + contentYinner)/2;
	var removeX = $(".removePopupBtn").outerWidth();
	var removeY = $(".removePopupBtn").outerHeight();
	var animationQuantifier = (animation) ? 1 : 0;
	
	
	if($(".popupCaption").length > 0)
	{
		var captionHspare = $(".popupCaption").outerWidth() - $(".popupCaption").width();
		$(".popupCaption").width(contentXinner - captionHspare);
		var captionX = $(".popupCaption").outerWidth();
		var captionY = $(".popupCaption").outerHeight();
		$(".popupCaptionBackground").width(captionX);
		$(".popupCaptionBackground").height(captionY);
		var captionYPos = contentYHalf2+winScrollTop-captionY;
		var captionXPos = contentXHalf2+winScrollLeft;
	}
	
	$(".popupContent").css({top: contentYHalf+winScrollTop, left: contentXHalf+winScrollLeft});
	
	$(".removePopupBtn").css({left: contentXHalf+winScrollLeft, top: contentYHalf+winScrollTop});
	
	if($(".popupCaption").length > 0)
	{
		$(".popupCaptionBackground, .popupCaption").css({left: captionXPos, top: captionYPos});
	}
	/*
	$(".popupLoad p").animate({opacity: 0}, 300*animationQuantifier,
	function()
	{
	*/
		var speedWidth = $(".popupContent").width()*0.8;
		var speedHeight = $(".popupContent").height()*0.8;
		$(".popupLoad").animate({left: contentXHalf+winScrollLeft, width: contentX}, speedWidth*animationQuantifier).animate({top: contentYHalf+winScrollTop, height: contentY}, speedHeight*animationQuantifier,
		function()
		{
			$(".popupContent iframe").show();
			$(".popupContent, .removePopupBtn").css({opacity: 1});
			if($(".popupContent iframe").length > 0)
			{
				setTimeout(function(){$(".popupLoad").remove();}, 200*animationQuantifier);
			}
			else
			{
				$(".popupLoad").animate({opacity: 0}, 600,
				function()
				{
					$(".removePopupBtn").animate({top: contentYHalf+winScrollTop-removeY}, 200);
					if($(".popupCaption").length > 0)
					{
						$(".popupContent, .popupCaption, .removePopupBtn").hover(
						function()
						{
							$(".popupCaptionBackground").css({"opacity": 0.8});
							$(".popupCaption").css({"opacity": 1});
						},
						function()
						{
							$(".popupCaptionBackground, .popupCaption").css({"opacity": 0});
						}
						);
					}
					$(".popupLoad").remove();
				}
				);
			}
		}
		);	
	/*
	}
	);
	*/
}
function fpPopupSettings(obj, o, e)
{
	if(o.url == "auto" && obj.attr("href") != "" && obj.attr("href"))
	{
		url = obj.attr("href");
	}
	else
	{
		url = o.url;	
	}
	
	if(o.data)
	{	
		data = o.data;
	}
	else if(obj.html() != "" && obj.html())
	{
		data = obj.html();
	}
	else if(obj.val() != "" && obj.val())
	{
		data = obj.val();
	}
	
	if(o.type == "auto" || o.type == "")
	{
		if(url && url != "" && url.match(/(.*)\.(jpg|png|gif)$/g))
		{
			type = "image";
		}
		else if(url && url != "" && url.match(/(.*)\.(htm|html|php|asp|aspx|jsp|cfm)$/g))
		{
			type = "ajax";
		}
		else if(url && url != "" && url.match(/^(https?)(\:\/\/)(.*)/g))
		{
			type = "ajax";
		}
		else if(data && data != "")
		{
			type = "html";
		}
	}
	else
	{
		type =  o.type;	
	}
	
	if(o.vPadding == "auto" && o.vPadding != "")
	{
		vPadding = (type == "html" || type == "ajax") ? 20 : 10;
	}
	else
	{
		vPadding = o.vPadding;	
	}
	if(o.hPadding == "auto")
	{
		hPadding = (type == "html" || type == "ajax") ? 30 : 10;
	}
	else
	{
		hPadding = o.hPadding;	
	}
	if(type == "image" && o.caption)
	{
		caption = (o.caption != "" && o.caption.length > 0) ? o.caption : $("img", obj).attr("alt");
	}
	else
	{
		caption = false;	
	}
	animation = (o.animation || o.animation.lenght > 0) ? true : false;
	
	coverOpacity = (o.coverOpacity <= 100 && o.coverOpacity >= 0 && !isNaN(o.coverOpacity)) ? o.coverOpacity : 70;
	backgroundColor = (o.backgroundColor.match(/^#?(([a-fA-F0-9]){3}){1,2}$/g)) ? o.backgroundColor.replace("#", "") : "fff";
	textColor = (o.textColor.match(/^#?(([a-fA-F0-9]){3}){1,2}$/g)) ? o.textColor.replace("#", "") : "333";
	
	captionBackgroundColor = (o.captionBackgroundColor.match(/^#?(([a-fA-F0-9]){3}){1,2}$/g)) ? o.captionBackgroundColor.replace("#", "") : "000";
	captionTextColor = (o.captionTextColor.match(/^#?(([a-fA-F0-9]){3}){1,2}$/g)) ? o.captionTextColor.replace("#", "") : "fff";
	
	initPopup(type, url, data, caption, animation, o.height, o.width, vPadding, hPadding, coverOpacity, backgroundColor, textColor, captionBackgroundColor, captionTextColor);
	e.preventDefault(); 
}
function initPopup(type, url, data, caption, animation, height, width, vPadding, hPadding, coverOpacity, backgroundColor, textColor, captionBackgroundColor, captionTextColor)
{
	
	buildPopup(caption, backgroundColor, textColor, captionBackgroundColor, captionTextColor);
	popupLoadLayout(animation, coverOpacity);
	if(type == "html")
	{
		$(".popupContent").html(data);
		$(".popupContent *").ready
		(
		function()
		{
			if($(".popupContent img").length > 0)
			{
				$(".popupContent img").load
				(
				function()
				{
					popupContentLayout(animation, width, height, vPadding, hPadding, type);
				}
				);
			}
			else
			{
				popupContentLayout(animation, width, height, vPadding, hPadding, type);
			}
		}
		);
	}
	if(type == "image")
	{
		var html = '<img ';
		if(width && width != "" && width != "auto")
		{
			var html =  html + 'width="'+width+'" ';
		}
		if(height && height != "" && height != "auto")
		{
			var html =  html + 'height="'+height+'" ';
		}
		var html =  html + 'src="'+url+'" />';
		
		$(".popupContent").html(html);
		$(".popupContent *").load
		(
		function()
		{
			popupContentLayout(animation, width, height, vPadding, hPadding, type);
		}
		);
	}
	else if(type == "ajax")
	{
		$.ajax
		(
		{
			url: url,
			method: "GET",
			//data: data,
			cache: false,
			success: function(dataResult)
			{
				$(".popupContent").html(dataResult);
				popupContentLayout(animation, width, height, vPadding, hPadding, type);
			}
		}
		);
	}
	else if(type == "iframe")
	{
		$(".popupContent").html('<iframe width="'+width+'" frameborder="0" height="'+height+'" src="'+url+'"></iframe>');
		$(".popupContent iframe").hide();
		popupContentLayout(animation, width, height, vPadding, hPadding, type);
	}
}
(function($){
    $.fn.extend({
        fpPopup: function(options) {
 
            //Settings list and the default values
            var defaults = {
				action: 'click',
                type: 'auto',
                url: 'auto',
                data: false,
				caption: false,
				animation: true,
                height: 'auto',
				width: 'auto',
				vPadding: 'auto',
				hPadding: 'auto',
				coverOpacity: 70,
				backgroundColor: "#fff",
				textColor: "#333",
				captionBackgroundColor: "#fff",
				captionTextColor: "#333"
            };
			
            var options = $.extend(defaults, options);
         
            return this.each(function() 
			{
                var o =options;
                
                var obj = $(this);
				
				if(o.action == 'click')
				{
					$(obj).click(
					function(e)
					{
						fpPopupSettings(obj, o, e);
					}
					);
				}
				else if(o.action == 'ready')
				{
					$("*", obj).ready(
					function(e)
					{
						fpPopupSettings(obj, o, e);
					}
					);
				}
				else if(o.action == 'load')
				{
					$("*", obj).load(
					function(e)
					{
						fpPopupSettings(obj, o, e);
					}
					);
				}
            }
			);
        }
    });
})(jQuery);
