// JavaScript Document
/************************
*	@Author: George Willian Condomitti
*	@Date: 30/09/2010
*	@Version: 1.0
*
*	Use:
*		Para chamar a função de popup basta passar
*		os parâmetros desejados ou chamar a função sem argumentos.
*		$("#identificador").popup();
*
*		Ao chamar a função sem argumentos  o comportamento padrão
*		exibirá o container passado, em forma de popup (centralizado na página).
*
*		Para ocultar o popup:
*		$("#identificador").popup({ show : false });
*		Apesar de não ser necessário o parâmetro 'true' para a exibição,
*		não é errado colocá-lo na chamada do método:
*		$("#identificador").popup({ show : true }); //exibe o popup
*		
*		Outros parâmetros possíveis:
*
*		$("#identificador").popup({
*								modal : true ,
*								resetFields : true ,
*								draggable : true
*							});
*
*******************************/

jQuery.fn.popup = function(){
	var o = $(this[0]);
	var args = arguments[0];

	var showPopup = args.show;
	var modal = args.modal;
	var resetFields = args.resetFields;
	var draggable = args.draggable;
	var vCenter = args.vCenter;


	if(showPopup == null){
		showPopup = true;
	}
	

	if(showPopup){	
	
			if(resetFields){
				$(o).find("input").each(function(){
					$(this).val('');
				});
			}

			//Se wrapperDetalhes estiver ativo, ocultá-lo para antes exibir janela de cadastro
			if($('#wrapperDetalhes').css('display') == "block"){
				$('#wrapperDetalhes').fadeOut();
			} else {
	
				if(modal != null && modal){
					var maskHeight = $(document).height();
					var maskWidth = $(document).width();
				
					if($('#popup-mask').length == 0){
						$('body').prepend('<div id="popup-mask"></div>');
					}
						$('#popup-mask').css({'width':maskWidth,'height':maskHeight});
						$('#popup-mask').fadeIn();
						$('#popup-mask').fadeTo("slow",0.5);
				
				}
			}
			
			var winH = $(window).height();
			var winW = $(window).width();
		
			$(o).css('position',  'fixed');  
			$(o).css('overflow',  'auto');  	
			$(o).css('z-index',  '9001');

			if(typeof(vCenter) != 'undefined' && vCenter == true){
				if($(o).height() >= winH){
					$(o).css('top', '0'); 
					$(o).css('position',  'absolute'); 		
				} else {
					$(o).css('top',  winH/2-$(o).height()/2);
				}
			} else {
				$(o).css('top', '100px'); 
				$(o).css('position',  'absolute');
			} 		
			
			$(o).css('left', winW/2-$(o).width()/2);
		
			$(o).addClass('popup-container-shadow');
			
			if(draggable){
				$(o).draggable();
			}
	
			$(o).fadeIn();
			
	} else {
			if(typeof(openedWrapperDetalhes) != 'undefined' && openedWrapperDetalhes > 0) {
				$('#wrapperDetalhes').fadeIn();
			} else {
				$('#popup-mask').fadeOut();
			}
			$(o).fadeOut();
	}


};

