// JavaScript Document
var popup_params = new Array;
	popup_params.popupHeight = 0;
	
jQuery.popup = function(params){
	//->Define valores padrões para os parametros de entrada;
	//-> Aqui voce encontra todas os valores de entrada;
	params = jQuery.extend({
		 source: false, // Elemento de origem do codigo
		 url: "", // url de origem do codigo
		 width: jQuery(window).width()*0.6,
		 titulo: "",
		 height: 0,
		 close: true,
		 type: "POST",
		 classe: ""
	}, params);
	
	//window.scrollTo(0,0);
	
	popup_params = params;
	
	jQuery(".jquery_popup").remove();
	
	var full = jQuery("<div/>").addClass("jquery_popup").appendTo("body");
	
	var bg = jQuery("<div/>").addClass("POPUP_bg").appendTo(full);
	var popup = jQuery("<div/>").addClass("POPUP_window").appendTo(full);
	if(jQuery.browser.msie){
		var iframe = jQuery("<iframe/>").appendTo(bg);
	}
	
	//jQuery(".POPUP_bg, iframe",full).fechaPopUp();

	var titulo = jQuery("<div/>").addClass("POPUP_titulo").appendTo(popup).css({height: 25});
	var texto = jQuery("<h2/>").html( params.titulo ).appendTo(titulo);
	if(params.classFechar){
		jQuery("<a href='javascript:void(0)'/>").addClass("POPUP_fechar").appendTo(titulo).click(function(){ jQuery.fechaPopUp(); });
	} else if(params.semFechar) {
		jQuery("<img/>").addClass("POPUP_fechar").attr({src:"/css/temas/default/images/popup_fechar.gif",alt:"Fechar",title:"Fechar"}).appendTo(titulo);
	} else {
		jQuery("<img/>").addClass("POPUP_fechar").attr({src:"/css/temas/default/images/popup_fechar.gif",alt:"Fechar",title:"Fechar"}).appendTo(titulo).click(function(){ jQuery.fechaPopUp(); });
	}
	
	/*fechar.click( function(){
		jQuery.fechaPopUp();
	});*/
	
	params.e = full;
	
	if(params.source){
		var cont = jQuery("<div/>").addClass("conteudo "+params.classe).html( jQuery("#"+params.source).html() ).appendTo(popup);
		cont.attr("source",params.source);
		jQuery("#"+params.source).html("");
		jQuery.posicionaPopUp(params);
	}else{
		var cont = jQuery("<div/>").attr({id:"popupIdToAjax"}).addClass("conteudo").appendTo(popup).addClass("carregando");
		/*jQuery.posicionaPopUp(params);
		var dataString = '?';
		if(params.data){
			jQuery.each(params.data,function(i,val){
				dataString += i+"="+val;
			});
		}
		loadXMLDoc("popupIdToAjax",params.url+dataString,'','',true,function(){
			cont.removeClass("carregando");
			jQuery.posicionaPopUp(params);
		});*/
		jQuery.ajax({
			type: params.type,
			url: params.url,
			data: params.data,
			beforeSend: function(){
				jQuery.posicionaPopUp(params);
			},
			success : function(html){
				cont.removeClass("carregando").html(html);
				jQuery.posicionaPopUp(params);
			}
		});
	}
};

jQuery.fn.abrePopUp = function(params)
{
	return this.click( function(){ jQuery.popup(params); });
};

jQuery.fn.fechaPopUp = function(params)
{
	return this.click( function(){ jQuery.fechaPopUp(); });
};

jQuery.fechaPopUp = function(callback){
	jQuery(".jquery_popup").hide();
	var source = jQuery(".jquery_popup .conteudo").attr("source");
	if(source){
		jQuery("#"+source).html( jQuery(".jquery_popup .conteudo").html() )
	}
	if(callback){ callback(); }
	jQuery(".jquery_popup").remove();
}
	
jQuery.posicionaPopUp = function(params){
		if(!params){
			//return false;
			params = popup_params;
		}
		var w = tamanhoTela();
		//Ajusta o tamanho do fundo igual ao tamanho da tela
		var bgHeight  = w[1]>jQuery(document).height()?w[1]:jQuery(document).height();
		jQuery(".POPUP_bg, iframe",params.e)
			.css({
				 width: jQuery(window).width(),
				 height: bgHeight,
				 opacity: .4
			 });
		jQuery("iframe",params.e).css({opacity:0});
		
		//Calcula a LARGURA de acordo com a porcentagem definida no tamanho
		if(String(params.width).indexOf("%") > 0){
			params.perW = params.width;
			params.width = params.perW.replace("%","")/100*w[0];
		}
		//Calcula a ALTURA de acordo com a porcentagem definida no tamanho
		if(String(params.height).indexOf("%") > 0){
			params.perH = params.height;
			params.height = params.perH.replace("%","")/100*w[1];
		}
		
		//se o tamanho da janela for maior que o tamanho do fundo ajusta u funda para que ocupe toda a tela
		if(jQuery(".POPUP_window",params.e).height() > jQuery(".POPUP_bg",params.e).height() ){
			jQuery(".POPUP_bg",params.e).height( jQuery(".POPUP_window",params.e).height() );
		}
		
		//posição centra da esquerda
		var centerLeft = (jQuery(window).width()-params.width)/2;
		if(centerLeft < 0){ centerLeft = 0; }
				
		jQuery(".POPUP_window",params.e)
			.css({
				left: centerLeft,
				top: 20,
				opacity: 1
			 })
			.width(params.width);
			
				
		//Calcula a posição top da popup de forma que ela fique centralizada
		var centerTop = (w[1]-(params.height||jQuery(".POPUP_window",params.e).height()))/2;
		//se a posição top estivel menos que 0 a popup fica para fora da tela
		if(centerTop < 0){ centerTop = 0; }
		//se o tamanho params.height vier vazio
		if(centerTop >= w[1]/2){ centerTop = 20; }
		params.centerTop = centerTop;
		
		var conteudo = jQuery(".POPUP_window .conteudo",params.e);
		
		jQuery(".POPUP_window .conteudo",params.e).width(params.width);
		if(params.height){
			jQuery(".POPUP_window .conteudo",params.e).height(params.height-30);
		}
		
		params.popupHeight = jQuery(".POPUP_window",params.e).height();
		if(params.popupHeight < w[1] ){
			jQuery(".POPUP_window",params.e).css({top:jQuery(window).scrollTop()+centerTop});
		}else{
			window.scrollTo(0,0);
		}
		return this;
};