// JavaScript Document
/************************
*	@Author: George Willian Condomitti
*	@Date: 15/02/2011
*	@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
*							});
*
*		NOTA: Essa biblioteca faz uso das seguintes bibliotecas:
*				jquery-ui
*				loader (3mw)
*
*******************************/

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 fadeIn = args.comeUpWithFade;
	var overflow = args.overflow;


	if(showPopup == null){
		showPopup = true;
	}

	if(overflow == null){
		overflow = false;
	}
	

	if(showPopup){	
	
			if(resetFields){
				$(o).find("input").each(function(){
					$(this).val('');
				});
			}
	
			if(modal != null && modal){
				var maskHeight = $(window).height();
				var maskWidth = $(window).width();
				
				if($('#popup-mask').length == 0){
					$('body').prepend('<div id="popup-mask"></div>');
					$('#popup-mask').click(function(){
						$(o).fadeOut();
						$('#popup-mask').fadeOut();
					});
				}
					$('#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'); 
			if(overflow) {
				$(o).css('overflow',  'auto');
			} else {
				$(o).css('overflow',  'none');
			}
			$(o).css('z-index',  '9000');
			
			if($(o).height() >= winH){
				$(o).css('top', '0'); 
				$(o).css('position',  'absolute'); 		
			} else {
				$(o).css('top',  winH/2-$(o).height()/2);
			}
			$(o).css('left', winW/2-$(o).width()/2);
		
			$(o).addClass('popup-container-shadow');
			
			if(draggable){
				$(o).draggable();
			}
	
			if(fadeIn){
				$(o).fadeIn();
			} else {
				$(o).show();
			}
			
	} else {
			$('#popup-mask').fadeOut();
			$(o).fadeOut();
	}

};

jQuery.fn.tooltip = function(){
	
	var o;
	var args = arguments[0];
	var eX = args.eX;
	var eY = args.eY;
	var showPopup = args.show;
	var modal = args.modal;
    	var _id = args._id;
	var draggable = args.draggable;	
	var fadeIn = args.comeUpWithFade;	
	

	var url = args.url;
	var urlParams = args.urlParams;
	var popUp_Y = args.y;
	var popUp_W = args.w;
	var customClass = args.customClass;
	var center = args.center;

	if(showPopup == null){
		showPopup = true;
	}
	
	
	if(showPopup){	
	
		
			if(modal != null && modal){
				var maskHeight = $(document).height();
				var maskWidth = $(document).width();
				
					if($('#popup-mask').length == 0){
						$('body').prepend('<div id="popup-mask"></div>');
					}
					
					if($('#'+_id).length == 0){
						$('body').prepend('<div id="'+_id+'"></div>');
					}
	
					$('#popup-mask').css({'width':maskWidth,'height':maskHeight});
					$('#popup-mask').fadeTo("slow",0.5);
				}
			

			o=$('#'+_id);
			
			$('#popup-mask').click(function(){
				$(o).tooltip({show : false , _id : _id});
			});
			
			
			
			if(url != ""){
				update_div_content(_id,url,urlParams,center);
			}
			
			$(o).css('position',  'fixed');  

			var winH = $(window).height();
			var winW = $(window).width();

			if(popUp_W != ""){
				$(o).css('width',  popUp_W); 
			}
			if(popUp_Y != ""){
				$(o).css('height',  popUp_Y);  
			}
			
			if(center){
				$(o).css('top',  winH/2-$(o).height()/2);
				$(o).css('left', winW/2-$(o).width()/2);
			} else {
				$(o).css('top',  eY);
				$(o).css('left', eX);
			}

			if(customClass == null) {
				$(o).addClass('popup-container-shadow');
			} else {
				$(o).addClass(customClass);	
			}

			if(draggable){
				$(o).draggable();
			}
			
			if(fadeIn){
				$(o).fadeIn();
			} else {
				$(o).show();
			}
	} else {
			$('#popup-mask').fadeOut();
			$('#' + _id).fadeOut();
	}

};

function update_div_content(_id, url, urlParams, updateCenter){
	showLoading(_id);
	$.post(url + '?' + urlParams,
			function(result){
				$("#" + _id).html(result);
				if(updateCenter){
					var winH = $(window).height();
					var winW = $(window).width();
					$("#" + _id).css('top',  winH/2-$("#" + _id).height()/2);
					$("#" + _id).css('left', winW/2-$("#" + _id).width()/2);					
				}
				hideLoading(_id);
			}
	);
}

function showLoading(_id){
	if(typeof(jQuery.fn.loader) != 'undefined'){
		$('#' + _id).loader();
	}
}

function hideLoading(_id){
	if(typeof(jQuery.fn.loader) != 'undefined'){
		$('#' + _id).loader({ show : false });
	}
}

var MouseCoord = function(event) {
  event = event || window.event;
 
  /**
  * Get mouse coordinates
  */
  var e = {
    _x: (event.pageX || event.clientX + (document.documentElement || document.body).scrollLeft),
    _y: (event.pageY || event.clientY + (document.documentElement || document.body).scrollTop)
  };
 
  return {
    X: e._x,
    Y: e._y

  };
};


