var popMe = function(tmp_obj){
	var me = this;
	var tmp = '';
	if(!is_object(tmp_obj)){//se o parametro for uma string
		tmp = tmp_obj;
	}
	me.option = {
		url:tmp,					//url do conteúdo a ser utilizado, pode ser imagens ou paginas.
		template:'default',			//possibilidades atuais [default,image,gpadmin]
		type:'',					//define o tipo de carregamento do conteúdo, possibilidades ['','iframe','html','image']. se for vazio, tenta identificar entre html ou image
		width:'',					//define um valor fixo de largura para o conteúdo
		height:'',					//define um valor fixo de altura para o conteúdo
		overflow:'hidden',			//propriedade overflow padrão da div onde será posto o conteúdo
		title:'',					//título utilizado em alguns templates
		onComplete:function(){},	//função executada ao finalizar o carregamento do conteúdo
		minMarginTop:20				//margem mínima do topo da tela em px
	};
	$.extend(me.option,tmp_obj);
	
	var body = $('body');
	var child;
	var html = '';
	var template_w;
	var template_h;
	var title;

	me.init = function(){
		$('#popme_child').show();
		$('#popme_loader').hide();
	};

	me.calcdados = function(child_w,child_h,template_w,template_h){
		var arr = new Array();

		arr['child_w'] = child_w;
		arr['child_h'] = child_h;
		arr['popme_result_w'] = child_w - template_w;
		arr['popme_result_h'] = child_h - template_h;
		arr['left'] = ((($(window).width() / 2) - (child_w / 2)) + $(document).scrollLeft());
		arr['top'] = ((($(window).height() / 2) - (child_h / 2)) + $(document).scrollTop());

		if(arr['top'] < me.option.minMarginTop)
			arr['top'] = me.option.minMarginTop;

		return arr;
	};

	me.onComplete = function(){
		me.option.onComplete();
	};

	switch(me.option.template){
		case 'default':
				template_w = 0;
				template_h = 0;
				html += '<div id="popme_result"></div>';
				child = $('<div id="popme_child">').html(html);
			break;
		case 'image':
				template_w = 0;
				template_h = 50;
				html += '<div id="popme_result"></div>';
				html += '<div class="popme_btfechar popMeX"></div>';
				child = $('<div id="popme_child">').html(html);
			break;
		case 'gpadmin':
				template_w = 20;
				template_h = 50;
				if(me.option.title){
					title = me.option.title;
				}else{
					title = me.option.url;
				}
				html += '<div class="popMeX" style="cursor:pointer; float:right; position:relative"><h3>X</h3></div>';
				html += '<h3>'+title+'</h3>';
				html += '<hr/>';
				html += '<div id="popme_result"></div>';
				child = $('<div id="popme_child popme_gpadmin" style="background:#dadada; padding:10px;">').html(html);
			break;
		default:
				alert('Template Inválido');
				return false;
			break;
	}
	var bg = $('<div id="popme_bg">').html('<div id="popme_loader"></div>');
	$(bg).css({opacity:0,width:$(document).width()+'px',height:$(document).height()+'px'});
	$(bg).click(function(){
		$('#popme_child').hide();
		$(this).hide('fast');
	});
	body.prepend(bg);
	$(bg).css({display:'block'});
	$(bg).animate({opacity:0.8},250);
	body.prepend(child);

	var child = $('#popme_child');
	var popme_result = $('#popme_result');

	child.find('.popMeX').click(function(event){
		event.preventDefault();
		popMeX();
	});

	popme_result.css({'overflow':me.option.overflow});

	var type;

	if(me.option.type == '' || me.option.type == undefined){
		if((me.option.url.indexOf('.jpg') == -1) && (me.option.url.indexOf('.jpeg') == -1) && (me.option.url.indexOf('.png') == -1) && (me.option.url.indexOf('.gif') == -1)){
			type = 'html';
		}else{
			type = 'image';
		}
	}else{
		type = me.option.type;
	}

	switch(type){
		case 'html':
				$.ajax({
					url: me.option.url,
					dataType:'html',
					success: function(data){
						me.init();
						popme_result.html(data);
						var child_w = popme_result.width()+template_w;
						var child_h = popme_result.height()+template_h;

						if(me.option.width){
							child_w = me.option.width;
						}
						if(me.option.height){
							child_h = me.option.height;
						}

						var arrdados = calcdados(child_w,child_h,template_w,template_h);

						child.animate({width:arrdados['child_w']+'px',height:arrdados['child_h']+'px',marginLeft:arrdados['left']+'px',marginTop:arrdados['top']+'px'},250,function(){
							me.onComplete();
						});
						popme_result.css({width:arrdados['popme_result_w']+'px',height:arrdados['popme_result_h']+'px'});

					}
				});
			break;
		case 'image':
				var i = new Image();
				i.onload = function(){
					me.init();
					var child_w = i.width+template_w;
					var child_h = i.height+template_h;

					if(me.option.width){
						child_w = me.option.width;
					}
					if(me.option.height){
						child_h = me.option.height;
					}

					var arrdados = calcdados(child_w,child_h,template_w,template_h);

					popme_result.html('<img src="'+me.option.url+'" width="'+i.width+'" height="'+i.height+'" />');
					child.animate({width:(arrdados['child_w'])+'px',height:(arrdados['child_h'])+'px',marginLeft:arrdados['left']+'px',marginTop:arrdados['top']+'px'},250,function(){
						me.onComplete();
					});
					popme_result.css({width:arrdados['popme_result_w']+'px',height:arrdados['popme_result_h']+'px'});

				}
				i.src = me.option.url;
			break;
		case 'iframe':
				me.init();

				var arrdados = calcdados(me.option.width,me.option.height,template_w,template_h);

				child.css({width:arrdados['child_w']+'px',height:arrdados['child_h']+'px',marginLeft:arrdados['left']+'px',marginTop:arrdados['top']+'px'});
				popme_result.css({width:arrdados['popme_result_w']+'px',height:arrdados['popme_result_h']+'px'});

				popme_result.html('<iframe src="'+me.option.url+'" width="'+arrdados['popme_result_w']+'" height="'+arrdados['popme_result_h']+'" border="0" frameborder="0" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" allowtransparency="true" style="border:0; overflow:'+me.option.overflow+'; background:transparent;"></iframe>');

				me.onComplete();
			break;
	}

};
var popMeX = function(){
	$('#popme_bg').click();
};

