var popup = {
	centerPopup : function (width, popupHeight){ 
		if(!width)width = 450;
		if(!popupHeight)popupHeight = $("#popup").height();
		
		//request data for centering  
		var windowWidth = $(window).width(); 
		var windowHeight = $(window).height();  
		var popupWidth = $("#popup").width();  
		
		var top = windowHeight/2-popupHeight/2-50;
		if (top < 20)top = 20;

		//centering  
		$("#popup").css({  
			"height": popupHeight,
			"position": "absolute",  
			"top": top,  
			"left": windowWidth/2 - width/2,
			"width": width
		});

		$('#popup #arrow_popup').removeAttr("class");

		$("#backgroundPopup").css({  
			"height": $(document.body).height(),
			"opacity": "0.5"
		});
	},
	
	popupAtElement : function (element, position, popupWidth, popupHeight, arrow_pos, arrowtop, arrowleft, hideclose){
		//needed to call this just in case - noticed some weird arrow behavior and this fixes it.
		//$('#popup #arrow_popup').removeAttr("class");

		if(!arrow_pos)arrow_pos = 'bottom';
		if(hideclose)$('#popupCloseText').hide();

		var pos = $(element).offset();
		var width = $(element).width();

		//sets position of popup with specified height
		switch(position){
			case 'top' :
				boxheight = popupHeight;
				break;
			
			case 'middle' :
				boxheight = popupHeight / 2;
				break;
				
			case 'right', 'bottom' :
				boxheight = 0;
				break;
				
			case 'topmiddle' :
				width = (((popupWidth-width)/2) * -1);
				boxheight = popupHeight + $(element).height();
				break;
				
			case 'topleft' :
				width = (popupWidth * -1);
				boxheight = popupHeight;
				break;
				
			case 'middleleft' :
				width = (popupWidth * -1);
				boxheight = (popupHeight/2)+10;
				break;
				
			default :
				boxheight = 0;
				break;
		}
		
		//chooses which background to display
		switch(arrow_pos){
			case 'top' :
				if(!arrowtop){arrowtop = ''; }
				if(!arrowleft){arrowleft = ''; }
				$('#popup #arrow_popup').addClass('arrow_popup_top');
				$('#popup #arrow_popup').css({
					"top":arrowtop, 
					"left":arrowleft, 
					"position": "absolute"
				});
				break;
			
			case 'left' :
				if(!arrowtop){arrowtop = popupHeight; }
				if(!arrowleft){arrowleft = -18; }
				$('#popup #arrow_popup').addClass('arrow_popup_left');
				$('#popup #arrow_popup').css({
					"top":arrowtop, 
					"left":arrowleft, 
					"position": "absolute"
				});
				width += $('#popup #arrow_popup').width();
				break;
				
			case 'right' :
				if(!arrowtop){arrowtop = popupHeight; }
				if(!arrowleft){arrowleft = popupWidth - 22; }
				$('#popup #arrow_popup').addClass('arrow_popup_right');
				$('#popup #arrow_popup').css({
					"top":arrowtop, 
					"left":arrowleft,
					"position": "absolute"
				});
				break;
				
			case 'bottom' :
				if(!arrowtop){arrowtop = 9; }
				if(!arrowleft){arrowleft = popupWidth/2 - 20; }
				$('#popup #arrow_popup').addClass('arrow_popup_bottom');
				$('#popup #arrow_popup').css({
					"top":arrowtop, 
					"left":arrowleft, 
					"position": "relative"
				});
				break;
				
			default : //this is only for safety
				$('#popup .arrow_popup').css({"background-image" : "none"});
				break;
		}
		
		//TODO:IMS - need to reset the css so the next popup is in the correct place
		// NOTE: second time this function is called, it gets the correct height. First time it does not.
		$('#popup').css({ 
			"position": "absolute",  
			"left": (pos.left + width), 
			"top": (pos.top - boxheight), 
			"width": popupWidth
		});

		$("#backgroundPopup").css({  
			"height": $(document.body).height(),
			"opacity": "0.0"
		});

	},

	loadPopup : function(){  
		$("#backgroundPopup").fadeIn(300);
		$("#popup").fadeIn(300);
	},

	closePopup : function (){  
		$("#popupInner").html('');
		$("#backgroundPopup").fadeOut(300);   
		$("#popup").fadeOut(300); 
	},
				
	loadHtml : function(tmpHtml) {
		$("#popupInner").html('');
		$("#popupInner").html(tmpHtml);
	},

	loadUrl : function(url) {
		$("#popupInner").html('');
		$("#popupInner").load(url);
	},
	
	quickLoad : function(url, height, width, varInfo) {
		if(!height)height = 470;
		if(!width)width = 640;
		if(!varInfo)varInfo = false;
		
		$("#popupInner").html('');
		$("#popupInner").load(url, {info: varInfo});
		this.centerPopup(width, height);
		this.loadPopup();
	},

	quickLoadHtml : function(html, width, height) {
		if(!height)height = 470;
		if(!width)width = 640;
		$("#popupInner").html('');
		
		this.loadHtml(html);
		this.centerPopup(width, height);
		this.loadPopup();
	}
};
