//verifica se o type é igual a um array
function is_object (mixed_var) {
    if (mixed_var instanceof Array) {       
		return false;
    } else {
        return (mixed_var !== null) && (typeof(mixed_var) == 'object');
    }
}

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,template:'default',width:'',height:'',overflow:'',titulo:''};
	$.extend(me.option,tmp_obj);
	var body = $('body');
	var child;
	var html = '';
	var template_w;
	var template_h;
	var titulo;
	switch(me.option.template){
		case 'default':
				template_w = 0;
				template_h = 0;
				html += '<div style="margin:0 auto;" id="result"></div>';
				child = $('<div id="child" style="background:transparent; position:absolute; z-index:99; display:none; top:0px; left:0px;">').html(html);
			break;
		case 'image':
				template_w = 0;
				template_h = 0;
				html += '<div class="popme_btfechar popMeX"></div>';
				html += '<div style="margin:0 auto;" id="result"></div>';
				child = $('<div id="child" style="background:transparent; position:absolute; z-index:990; display:none;">').html(html);
			break;
		case 'gpadmin':
				template_w = 20;
				template_h = 50;
				if(me.option.titulo){	
					titulo = me.option.titulo;
				}else{
					titulo = me.option.url;
				}
				html += '<div class="popMeX" style="cursor:pointer; float:right; position:relative"><h3>X</h3></div>';
				html += '<h3>'+titulo+'</h3>';
				html += '<hr/>';
				html += '<div style="margin:0 auto;" id="result"></div>';
				child = $('<div id="child" style="background:#dadada; position:absolute; z-index:98; padding:10px; display:none;">').html(html);
			break;
	}
	var bg = $('<div id="popbg">').html('<div id="popme_loader"></div>');
	$(bg).css({display:'none',opacity:0.0,background:'#000',width:($(document).width())+'px',height:$(document).height()+'px',position:'absolute','z-index':'98','top':'0px','left':'0px'});
	$(bg).click(function(){
		$('#child').hide();
		$(this).hide('fast');
	});
	body.prepend(bg);
	$(bg).css({display:'block'});
	$(bg).animate({opacity:0.8},250);
	body.prepend(child);
	
	var child = $('#child');
	var result = $('#result');
	
	var overflow = 'hidden';
	if(me.option.overflow){
		overflow = me.option.overflow;
	}
	result.css({'overflow':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){		
						init();
						result.html(data);
						var child_w = result.width()+template_w;
						var child_h = result.height()+template_h;
						var result_w;
						var result_h;
						var l;
						var t;				
						if(me.option.width){				
							child_w = me.option.width;
						}
						if(me.option.height){
							child_h = me.option.height;
						}						
						result_w = child_w - template_w;
						result_h = child_h - template_h;
						l = ((($(window).width() / 2) - (child_w / 2)) + $(document).scrollLeft());
						t = ((($(window).height() / 2) - (child_h / 2)) + $(document).scrollTop());
						
						if(t < 0)//alinha ao topo da página, caso a altura da mesma não comporte o tamanho do item a ser aberto
							t = 0;
						
						child.css({width:child_w+'px',height:child_h+'px',marginLeft:l+'px',marginTop:t+'px'});
						result.css({width:result_w+'px',height:result_h+'px'});						
					}
				});
			break;
		case 'image':
				var i = new Image();
				i.onload = function(){
					init();
					var child_w = i.width+template_w;
					var child_h = i.height+template_h;
					var result_w;
					var result_h;
					var l;
					var t;				
					if(me.option.width){				
						child_w = me.option.width;
					}
					if(me.option.height){
						child_h = me.option.height;
					}
					
					result_w = child_w - template_w;
					result_h = child_h - template_h;
					l = ((($(window).width() / 2) - (child_w / 2)) + $(document).scrollLeft());
					t = ((($(window).height() / 2) - (child_h / 2)) + $(document).scrollTop());
					
					if(t < 0)//alinha ao topo da página, caso a altura da mesma não comporte o tamanho do item a ser aberto
						t = 0;
					
					result.html('<img src="'+me.option.url+'" width="'+i.width+'" height="'+i.height+'" />');
					child.css({width:(child_w)+'px',height:(child_h)+'px',marginLeft:l+'px',marginTop:t+'px'});
					result.css({width:result_w+'px',height:result_h+'px'});					
				}
				i.src = me.option.url;
			break;
		case 'iframe':
				init();	
				var child_w = me.option.width;
				var child_h =  me.option.height;
				var result_w;
				var result_h;
				var l;
				var t;		
				
				result_w = child_w - template_w;				
				result_h = child_h - template_h;
				l = ((($(window).width() / 2) - (child_w / 2)) + $(document).scrollLeft());
				t = ((($(window).height() / 2) - (child_h / 2)) + $(document).scrollTop());
				
				if(t < 0)//alinha ao topo da página, caso a altura da mesma não comporte o tamanho do item a ser aberto
					t = 0;
				
				child.css({width:child_w+'px',height:child_h+'px',marginLeft:l+'px',marginTop:t+'px'});
				result.css({width:result_w+'px',height:result_h+'px'});				
				
				result.html('<iframe src="'+me.option.url+'" scrolling="yes" width="'+result_w+'" height="'+result_h+'"></iframe>');
			break;
	}
	
	child.find('.popMeX').click(function(event){
		event.preventDefault();
		popMeX();
	});
	
};
var popMeX = function(){
	$('#popbg').click();
};

var init = function(){
	$('#child').show();
	$('#popme_loader').hide();	
};
