/*
    LIGHTBOX
*/

function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll); 
	return arrayPageScroll;
}

function getPageSize(){
  	var xScroll, yScroll;

  	if (window.innerHeight && window.scrollMaxY) {	
  		xScroll = document.body.scrollWidth;
  		yScroll = window.innerHeight + window.scrollMaxY;
  	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  		xScroll = document.body.scrollWidth;
  		yScroll = document.body.scrollHeight;
  	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  		xScroll = document.body.offsetWidth;
  		yScroll = document.body.offsetHeight;
  	}

  	var windowWidth, windowHeight;

  	if (self.innerHeight) {	// all except Explorer
  		windowWidth = self.innerWidth;
  		windowHeight = self.innerHeight;
  	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  		windowWidth = document.documentElement.clientWidth;
  		windowHeight = document.documentElement.clientHeight;
  	} else if (document.body) { // other Explorers
  		windowWidth = document.body.clientWidth;
  		windowHeight = document.body.clientHeight;
  	}	
  	var pageHeight, pageWidth;

  	// for small pages with total height less then height of the viewport
  	if(yScroll < windowHeight){
  		pageHeight = windowHeight;
  	} else { 
  		pageHeight = yScroll;
  	}

  	// for small pages with total width less then width of the viewport
  	if(xScroll < windowWidth){	
  		pageWidth = windowWidth;
  	} else {
  		pageWidth = xScroll;
  	}

  	return {
  		pageWidth: pageWidth ,
  		pageHeight: pageHeight , 
  		windowWidth: windowWidth, 
  		windowHeight: windowHeight
  	};
}

var largura;
var topl;


function evalJS(){
	/*
		var re = '<script[^>]*>([\\S\\s]*?)<\/script>';
		code = $(this).text().replace(new RegExp(re, 'img'), '');
		eval(RegExp.$1);	
	*/
	
	$('.javascript').each(function() {
		eval($(this).text());
	});	  	
}
var lightbox_aberto = false;
var rsIntervalFundo = null;
var block_nextprev = false;
function slidePop(direcao, tipoPopUp, id){
	if(block_nextprev == true){
		return false;
	}
	block_nextprev = true;
	
	var url;
	var mdata;	
	$("#ajaxloading").show();

	if(direcao == 'prev'){
		url = site_url + tipoPopUp + '/get_prev/' + id;
	}else{
		url = site_url + tipoPopUp + '/get_next/' + id;
	}

	$.post(url, function(data){
		$("#lightbox").fadeOut('def',function(){
			$("#ajaxloading").hide();
			$("#lightbox")[0].innerHTML = data;
			evalJS();
			$('#lboxpadrao').pngFix();
			$("#lightbox").fadeIn('def',function(){
				block_nextprev = false;
				
				clearInterval(rsIntervalFundo);
				rsIntervalFundo = window.setInterval(function(){ajustaFundo()}, 1000);				
				
			});	
		    
			var w = document.body.scrollWidth;
		    var h = getPageSize().pageHeight;
		    $("#fundo_lightbox")[0].style.width=w+"px";
		    $("#fundo_lightbox")[0].style.height=h+"px";    
		});
	});	
}
function resizePop(largura){
	var scrollTela = getPageScroll();	
  	topl = scrollTela[1];	
    var w = document.body.scrollWidth;
    var h = getPageSize().pageHeight;
    var windW = getPageSize().windowWidth;
   	var windH = getPageSize().windowHeight;
    var meio_w = windW/2;
  	var meio_h = windH/2;
    var largura2 = largura/2;
    var left = meio_w-largura2-40;
    $("#fundo_lightbox")[0].style.width=w+"px";
    $("#fundo_lightbox")[0].style.height=h+"px";
    $("#lightbox")[0].style.width=largura+'px';
    $("#lightbox")[0].style.top=topl+'px';
    $("#lightbox")[0].style.left=(left+30)+'px';		
}

function abreLightbox(url, l, t)
{
	if(t == null || t == 'undefined' || t == NaN){
		t = 0;
	}
	
	var scrollTela = getPageScroll();	
	largura = l;
  	topl = t + scrollTela[1];

    //pega a resolução do visitante
    var w = document.body.scrollWidth;
    var h = getPageSize().pageHeight;
    
    var windW = getPageSize().windowWidth;
   	var windH = getPageSize().windowHeight;
   	
    $("#fundo_lightbox").show();
    $("#fundo_lightbox").css('opacity',0);
    $("#fundo_lightbox").fadeTo("slow", 0.7);

    $("#fundo_lightbox")[0].style.width=w+"px";
    $("#fundo_lightbox")[0].style.height=h+"px";

  	$.post(url, function(data){
  		lightbox_aberto = true;

	    var meio_w = windW/2;
	  	var meio_h = windH/2;
	    //diminui o valor da metade da resolução pelo tamanho da janela, fazendo com q ela fique centralizada
	    var largura2 = l/2;
	    //var top = meio_h-(altura2/2);
	    var left = meio_w-largura2;
	    
	    $("#lightbox")[0].style.width=largura+'px';
	    $("#lightbox")[0].style.top=topl+'px';
	    $("#lightbox")[0].style.left=left+'px';
		$("#lightbox")[0].innerHTML = data; /*eval(data.replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, ""));*/

	    /* HACK EVAL JAVASCRIPT */
	    evalJS();
	    
	    $("#lightbox").fadeIn("slow");

		/* FIX PNG BORDA */
		$('#lbox').pngFix();
		$('#lboxpadrao').pngFix();
		
	    var w = document.body.scrollWidth;
	    var h = getPageSize().pageHeight;
	    $("#fundo_lightbox")[0].style.width=w+"px";
	    $("#fundo_lightbox")[0].style.height=h+"px";  
		
		clearInterval(rsIntervalFundo);
		rsIntervalFundo = window.setInterval(function(){ajustaFundo()}, 1000);		
		  		
	  });
	  
	  
}


function hideFundoLightbox(){
	if(lightbox_aberto == true){
		hideLightbox();
	}
}


function hideLightbox()
{
	clearInterval(rsIntervalFundo);

	$("#lightbox").fadeOut("slow");
	$("#fundo_lightbox").fadeOut("slow", function(){$('#player_home, #home_banner_swf').css('visibility','visible');});
	lightbox_aberto = false;
}

function redimensiona()
{
	if(lightbox_aberto){
		//pega a resolução do visitante
		var w = document.body.scrollWidth;
		var h = getPageSize().pageHeight;
		var windW = getPageSize().windowWidth;
		var windH = getPageSize().windowHeight;
		
		//divide a resolução por 2, obtendo o centro do monitor
		var meio_w = windW/2;
		var meio_h = windH/2;
		//diminui o valor da metade da resolução pelo tamanho da janela, fazendo com q ela fique centralizada
		
		var left = (windW - $("#lightbox").width()-50) / 2;
		left = (left<0)?0:left;
		
		$("#fundo_lightbox")[0].style.width=w+"px";
		$("#fundo_lightbox")[0].style.height=h+"px";
		
		//$("#lightbox")[0].style.width=largura+'px';
		$("#lightbox")[0].style.top=topl+'px';
		$("#lightbox")[0].style.left=(left+15)+'px';
	}
}

function ajustaFundo(){
	if(lightbox_aberto){	
	    var w = document.body.scrollWidth;
	    var sc = getPageScroll();
	    
	    h = $("#lightbox").offset().top + $("#lightbox").height();
	    if(getPageSize().pageHeight < h){
			$("#fundo_lightbox")[0].style.height=h+"px";			
		}else{
			$("#fundo_lightbox")[0].style.height=getPageSize().pageHeight+"px";			
		}
	    $("#fundo_lightbox")[0].style.width=w+"px";
	}
}


window.onresize=redimensiona;
