(function(){
	var popup = null;
	var container = null;
	
	var template = '\
		<div id="window_popup"> \
			<div id="window_popup_container"></div> \
		</div> \
		';
	
	var shadeTemplate = '\
		<div id="window_popup_shade"> \
			<img src="/Comunidade/images/loader.gif" class="window_popup_loader"> \
		</div> \
		';
	
	var P = {
		load : function(content){
			if(container != null) WindowPopUp.close();
			
			if(container == null){
				WindowShade.load();
			
				Mustache.to_html(template, {}, {}, function(val){
					if(container == null){
						$('body').append(val);
					}
				});
			}
			
			popup = $('#window_popup');
			container = $('#window_popup_container');
			
			container.html(content);
			
			WindowPopUp.center();
		},
		setWidth : function(width){
			popup.width(width + 20);
		},
		raise : function(size){
			$(popup).css('top',  ($(window).height() - $(popup).height()) / 2 + $(window).scrollTop() - size);
		},
		center : function(){
			var left = ($(window).width() - $(popup).width()) / 2;
			var top = ($(window).height() - $(popup).height()) / 2 + $(window).scrollTop();
			
			$(popup).css('left', left);
			$(popup).css('top', top);
			
			$(popup).show();
		},
		close : function(){
			if(popup == null) return;
			
			$(popup).remove();
			WindowShade.unload();
			
			popup = null;
			container = null;
		},
		loadShade : function(){
			Mustache.to_html(shadeTemplate, {}, {}, function(val){
				$('#window_popup').append(val);
			});
			
			$('#window_popup_shade').height($('#window_popup').height());
			$('#window_popup_shade').width($('#window_popup').width());
			$('#window_popup_shade').show();
		},
		unloadShade : function(){
			$('#window_popup_shade').remove();
		}
	};
	
	window.WindowPopUp = P;
})();
