
// pega o tamanho da página
function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

//adiciona a layer escura
function overlay(){
	if (typeof document.body.style.maxHeight === "undefined") {// ie 4
		$("body","html").css({height: "100%", width: "100%"});
		$("html").css("overflow","hidden");
		
		if (document.getElementById("selectHack") === null) {//iframe para ie (hack para os selects)
			$("body").append("<iframe id='selectHack'></iframe><div id='overlay'></div><div id='overlayContent'  style='display:none'></div>");
			$("#overlay").click(removeOverlay);
		}
	}else{//outros	    
		if($("#overlay").length == 0){
			$("body").append("<div id='overlay'></div><div id='overlayContent' style='display:none'></div>");
			$("#overlay").click(removeOverlay);
		}
	}
		
	if(detectMacXFF()){
		$("#overlay").addClass("overlayMac");//usa png de fundo para ff no mac
	}else{
		$("#overlay").addClass("overlayBackground");//usa background e opacity
	}
	document.onkeydown = function(e){
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 27){ // fecha
			removeOverlay();
		}
	}
	$('#overlayContent').fadeIn();
}
// detecta se é firefox pra mac
function detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

//remove as layers e os eventos
function removeOverlay(){
	
	$("#overlay").fadeOut("fast",function(){
		$('#overlayContent').fadeOut("fast",function(){
			$('#overlayContent').trigger("unload").unbind().remove();
		})
		$('#overlay, #selectHack, document').trigger("unload").unbind().remove();
	});
	if (typeof document.body.style.maxHeight === "undefined") {// ie 6
		$("html").css("overflow","auto");
	}
}


//funcao que carrega o conteudo da layer
function loadLayer(page){
	try{
		overlay();
		$('#overlayContent').load(rootUrl+"shared/"+page, function(){
			$('#overlayContent').css({"margin-left":"-"+$('#overlayContent').width()/2+"px", "margin-top":"-"+$('#overlayContent').height()/2+"px"});
			$("#closeLayer").click(function(){
				removeOverlay();
			});
		});
	}catch(e){
		console.log(e);
	}
}

function loadPage(page){
	
    try{
        overlay();
        $('#overlayContent').load(page, function(){
            $('#overlayContent').css({"margin-left":"-"+$('#overlayContent').width()/2+"px", "margin-top":"-"+$('#overlayContent').height()/2+"px"});
            $("#closeLayer").click(function(){
                removeOverlay();
            });
        });
    }catch(e){
        //console.log(e);
    }
}

function loadErrors(element) {
    try {
        overlay();
        $('#overlayContent').append(element.clone().show());
        $('#overlayContent').css({"margin-left":"-"+$('#overlayContent').width()/2+"px", "margin-top":"-"+$('#overlayContent').height()/2+"px"});
        $(element).remove();
        $('#closeLayer').click(function(){          
            removeOverlay();
            return false;
        });        
    } catch(e) {
        //console.log(e);        
    }
}

function loadMessage(type,message) {
    try{
        overlay();
        $('#overlayContent').load(rootUrl+'flash/'+type,{message:message},function() {
            $('#error_message').show();                       
			$('#overlayContent').css({"margin-left":"-"+$('#overlayContent').width()/2+"px", "margin-top":"-"+$('#overlayContent').height()/2+"px"});	       
			$('#closeLayer').click(function(){          
			    removeOverlay();
			    return false;
			}); 
        });
    } catch(e) {
        
    }
}

//Esqueci minha senha
$(document).ready(function(){
    if ($('a.forgot-password').length > 0) {
        $('a.forgot-password').click(function(){
            if (!$.trim($('#player_email').val())) {
                loadMessage('error','Informe seu email cadastrado para receber a senha.');                
            } else {
                $.post(rootUrl+'player/forgotPassword/',{email:$('#player_email').val()},function(transport){                    
                    loadMessage('success',transport);
                });
            }
        });	        
    }
})

//redimensiona os estilos das páginas
$(document).ready(function(){
	var pageSize = getPageSize();
	if(pageSize[0] <= 1008){
		$("html, body, #nav, #nav embed").css({"width":"1008px"});
	}
	else{
		$("html, body, #nav, #nav embed").css({"width":"100%"});
		$("html").css({"overflow-x":"hidden"});
		
	}
});
$(window).resize(function(){
	pageSize = getPageSize();
	if(pageSize[0] <= 1008){
		$("html, body, #nav, #nav embed").css({"width":"1008px"});
		$("html").css({"overflow-x":"auto"});
	}else{
		$("html, body, #nav, #nav embed").css({"width":"100%"});
		$("html").css({"overflow-x":"hidden"});
	}
});


//abre a popup do jogo
$(document).ready(function(){   
	if ($('a#play').length > 0) {
		$('a#play').click(function(){
			var game = window.open(this.href,'Warland','height=774,width=1015, menubar=no, location=no, toolbar=no, status=no');
			if (window.focus){
				
				game.focus()
			}
			return false;
		});
	}
});
function windowClose(){
	var conf = confirm("Deseja realmente sair do jogo?");
	if(conf){        
		this.focus();                
		//self.opener = this;
		if (self.opener) {
            self.opener.location.reload()
        }
		self.close();
	}
}