$.fn.popup = function(options) {  

	var defaults = {  
		thumbSize: 0,
		resizeDelay:1000
	};  
 
	var options = $.extend(defaults, options);  
  
	$("body").append("<div class='overlay'></div>");
	
	return this.each(function() {  				
		//initial variables
		var popup = $(this);	
		var image = $(this).find("img");
		var area = $("body").attr("id");
		var text = $(this).find(".textArea");
		var imgSrc = image.attr("src");		
		if(imgSrc == undefined){
			var imgSrc = "webapp/templates/images/blank.gif";
		}
		if(text.length > 0){
			var textVal = text.html();
		}else{
			var textVal = imgSrc.replace("images/","");
		}				
		
		//setup popup
		popup.append("<span>more info &nbsp;</span>");
		popup.css("cursor","pointer");
		popup.wrap("<a href='#top' class='smoothScroll'></a>");
		$("a.smoothScroll").anchorAnimate()
		text.hide();	
		$(".overlay").hide("",function(){
			$(".overlay").animate({opacity:"0"},"slow");
		});
		image.hide("");
			
		//open popup
		popup.click(function(){
			//var origWidth = image.width();
			//var origHeight = image.height();
			var height = $(document).height();
			var width = $(document).width();
			var overlayCss = {
				"height": height,
				"width": width,
				"position":"absolute",
				"top":"0",
				"left":"0",
				"z-index":"100",
				"background":"#000"
			}
			$(".overlay").css(overlayCss);
			$(".overlay").animate({opacity:"0.8"},"slow",function(){
				$(".overlay").fadeIn("");
				$(".popupHolder").fadeIn("slow",function(){
					$(".textBox").slideDown("slow");
				});				
			});
			//var maxHeight = $(window).height() -400;
			//if(origHeight > maxHeight){
				//var newHeight = maxHeight; 
			//}else{
				//var newHeight = origHeight;
			//}
			//var ratio = origWidth/origHeight;
			//var newWidth = newHeight*ratio;
			var popWidth = 450;			
			var winWidth = $(window).width();
			//var winHeight = $(window).height();
			//var popHeight = $(".popupHolder").height();
			var center = winWidth/2 - (popWidth/2);
			//var centerHeight = winHeight/2 - (newHeight/2);			
			$("body").prepend("<div class='popupHolder'><img src="+ imgSrc +" alt=''/></div>"); //width="+ newWidth +" height="+ newHeight +"
			$(".popupHolder").prepend("<div class='close'></div>");
			$(".popupHolder img").after("<div class='textBox'>"+ textVal +"</div>");
			$(".textBox").hide("");
			var popupCss = {
				"width": popWidth,
				"height": "auto",
				"left": center
			}
			$(".popupHolder").css(popupCss);
			
			//close popup
			$(".close").click(function(){
				$(".textBox").slideUp("slow",function(){
					$(".popupHolder").fadeOut("slow");
					$(".overlay").animate({opacity:"0"},"slow",function(){
						$(".overlay").fadeOut("");
						$(".popupHolder").remove();
					});	
				});			
			});
		});		
	});		

};

jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}
