var ie6 = (navigator.userAgent.indexOf("MSIE 6")!= -1);
var ie8 = (navigator.userAgent.indexOf("MSIE 8")!= -1);



function Popup(args){
	var me = this;
	this.idPopup = (args.idPopup)?args.idPopup:'dialog';
	this.conteudo = (args.conteudo)?args.conteudo:'';
	this.Fixed = args.Fixed;
	this.fade = args.fade;
	this.Top = args.Top;
	this.Left = args.Left;
	this.posRelScroll = args.posRelScroll;
	this.mask = args.mask;
	this.closeToEsc = args.closeToEsc;
	this.onOpen = function(){};
	this.onClose = function(){};
	
	if(this.mask){
		this.objMask = $(document.createElement('div'));
		this.maskColor = (args.maskColor)?args.maskColor:'#000';
		this.objMask.attr('id','mask');
		me.objMask.css({
			"display":"none",
			"position":"absolute",
			"background":me.maskColor,
			"top":'0',
			"left":'0',
			"z-index":97
		});	
	}
	this.objPop = $(document.createElement('div'));
	this.objPop.attr('id',me.idPopup);
	this.posicao = "fixed";
	me.objPop.css({
		"z-index":99,
		"display":"none"
	});

	
	var FadeTo = function(obj,vel,opac,callBack){
		obj.css("display","block");
		obj.animate({opacity: opac}, vel,callBack) 
	}
	this.open = function(conteudo){		
		if(me.objMask){
			$(document).find('body').append(me.objMask);
			me.objMask.click(me.close);
		}
		$(document).find('body').append(me.objPop);
		
		var docHeight = $(document).height();
		var docWidth = $(document).width();	
		var winH = $(window).height();
		var winW = $(window).width();
		var conteudo = (conteudo)?conteudo:me.conteudo;	
		var mostrarPop = function(){
			me.objPop.html("").append(conteudo);
			if(!me.Top){
				me.Top = ((winH-me.objPop.height())/2);
				if(me.objPop.height() > winH){
					me.Top =10;
				}
			}
			if(!me.Left){
				me.Left = ((winW-me.objPop.width())/2);
			}

			if(me.Fixed){
				var posicao = (ie6)?"absolute":"fixed";
				if(ie6){						
					$(window).scroll(function(){
						me.objPop.css("top", (topo+$(window).scrollTop())+"px");
					});
				}
				me.objPop.css({
					"position":posicao,
					"top":me.Top+"px",		  
					"left":me.Left+"px"		
				});
			}else{
				var Top = me.Top;
				var Left = me.Left;	
				if(me.posRelScroll){
					Top += $(window).scrollTop();
					Left += $(window).scrollLeft();	
				}
				/*if ((Top+me.objPop.height()) > docHeight){
					Top -= ((Top+me.objPop.height())-docHeight)+30;
				}*/
				me.objPop.css({
					"position":"absolute",
					"top":Top+"px",		  
					"left":Left+"px"		
				});	
			}
			var onReadyShow = function(){
				me.bt_close = $(args.bt_close);
				me.bt_close.click(me.close);
				if(args.ancora)
					document.location = args.ancora	;
				me.onOpen();
			}
			if(me.fade){
				me.objPop.fadeIn(500,onReadyShow);	
			}else{
				me.objPop.css('display','block');	
				onReadyShow();
			}
		}
		if(me.mask){
			docWidth = (ie8 || ie6)?docWidth-22:docWidth;
			me.objMask.css({
				'width':docWidth,
				'height':docHeight,
				opacity: 0.0				
			});	
			FadeTo(me.objMask,400,0.7,mostrarPop);
		}else{
			mostrarPop();	
		}
	}
	this.close = function(){
		if(me.fade){
			me.objPop.fadeOut(300,function(){
				if(me.mask){
					me.objMask.fadeOut(500,function(){
						me.objPop.remove();
						me.objMask.remove();	
						me.onClose();
					});
				}else{
					me.objPop.remove();
					me.onClose();
				}	
			});						
		}else{
			me.objPop.css('display','none');	
			if(me.mask){
				me.objMask.fadeOut(500,function(){
					me.objPop.remove();
					me.objMask.remove();
					me.onClose();
				});
			}else{
				me.objPop.remove();
				me.onClose();
			}	
		}		
	}
	this.pressedKey = function(e){
		if(!e){
			if(window.event){
				e = window.event;
			}else{
				return;
			}
		}
		if(typeof(e.keyCode) == 'number'){
			e = e.keyCode;
		}else if( typeof( e.which ) == 'number' ){
			e = e.which;
		} else if( typeof( e.charCode ) == 'number'  ){
			e = e.charCode;
		}else{
			return;
		}
		if(e==27){
			me.close();
		}
	}
	if(me.closeToEsc){
		document.onkeyup = me.pressedKey;
	}
}
