var divNegro=null, transpa=0.85, tempor=null, d=document, div=null, divT=null,divL=null,divR=null,divB=null,divP=null,divs=new Array(),divPopup=null, tipoTag=0, ruta="";

var FADE_INTERVAL=10;//Temps que esta una foto en escena
var FADE_STEP=0.05;
var opacity=0.8;
var xPopup=700;
var yPopup=700;
var yInicialPopup=30;
var xInicialPopup=0;

window.onresize = function() { setPopupHeight('popup', .8); }
window.onload = function() { setPopupHeight('popup', .8); }


function black(x,y,tipo,rutaSrc)
{
	var id="",inner="";
	divs=new Array();
	heiD=getDocHeight();
	widD=getDocWidth();
	
	xPopup=x;
	yPopup=y;

	heiW=getWinHeight();
	widW=getWinWidth();

	xInicialPopup=(widW/2)-(xPopup/2);
	yInicialPopup=(heiW/2)-(yPopup/2);	
	
	
	switch(tipo)
	{
		case "img":
			tipoTag=1;
			break;	
		case "iframe":
			tipoTag=2;
			break;
		case "flash":
			tipoTag=3;
			break;
	}
	ruta=rutaSrc;
	
	divNegro = document.createElement("div");
	divNegro.id = "negro";
	var imgNegro = document.createElement("img");
	imgNegro.id = "fondoNegro";
	imgNegro.setAttribute("class", "fondo");
	//imgNegro.setAttribute("onclick", "javascript:finalizar2();");
	
	//imgNegro.style.opacity=.9;
	imgNegro.src="img/negro.jpg";
	imgNegro.onclick=function(){finalizar2();};
	
	imgNegro.style.width=widW+"px";
	imgNegro.style.height=heiW+"px";
	divNegro.style.position="absolute";
	divNegro.style.top="0px";
	divNegro.style.left="0px";
	divNegro.style.zIndex="700";
	
	divNegro.appendChild(imgNegro);
	//document.getElementById("total").appendChild(divNegro);
	document.body.appendChild(divNegro);
	
	divNegro.xOpacity=0;
	setOpacity(divNegro);
	divs.push(divNegro);
	
	tempor=setTimeout(fadePrin,FADE_INTERVAL);
	
	
}

function fadePrin()
{
	//alert(divs.length);
	for(i=0;i<divs.length;i++) 
	{	
		divs[i].style.display = "block";
		divs[i].xOpacity = divs[i].xOpacity + FADE_STEP;
		setOpacity(divs[i]);
		//alert(divs[i].xOpacity);
	}

	
	if(verificarOpacitys("pos")) //si ja hem acabat una transició, acabem
	{
		
		clearTimeout(tempor);
			switch(tipoTag)
			{
				case 1:
					insertImg();
					break;	
				case 2:
					insertPopup();
					break;
				case 3:
					insertFlash();
					break;
			}
	} 
	else //mentre no estigui la seguent imatge en escena (sense la imatge "current"), anem actualitzant opacitats
	{
		clearTimeout(tempor);
		tempor=setTimeout(fadePrin,FADE_INTERVAL);
	}	
}



function insertPopup()
{
	var p = document.createElement("div");
	p.id = "pop";
	p.setAttribute("class", "emerge");
	
	var iframe = document.createElement("iframe");
	iframe.id = "popup";
	iframe.setAttribute("class", "fondo2");
	//iframe.setAttribute("onclick", "javascript:acabar();");
	iframe.style.top=yInicialPopup+"px";
	iframe.style.left=xInicialPopup+"px";
	
	iframe.src=ruta;
	iframe.frameborder="1";
	
	iframe.style.width=xPopup+"px";
	iframe.style.height=yPopup+"px";
	
	p.appendChild(iframe);
	document.body.appendChild(p);
}
function insertImg()
{
	//alert("S");
	var p = document.createElement("div");
	p.id = "pop";
	p.setAttribute("class", "emerge");
	//p.xOpacity = "1";
	setOpacity(p);
		
		
	var iframe = document.createElement("img");
	iframe.id = "popup";
	iframe.setAttribute("class", "fondo2");
	//iframe.setAttribute("onclick", "javascript:acabar();");
	p.style.position="absolute";
	p.style.zIndex="701";
	p.style.top=yInicialPopup+"px";
	p.style.left=xInicialPopup+"px";
	
	iframe.src=ruta;
	iframe.frameborder="1";
	iframe.onclick=function(){finalizar2();};
	
	iframe.style.width=xPopup+"px";
	iframe.style.height=yPopup+"px";
	
	
	//Per crear la ma com a punter al passar per la "X" de tancar
	var cerrarImg = document.createElement("img");
	cerrarImg.src="img/empty.gif";
	cerrarImg.style.position="absolute";
	cerrarImg.style.top=(yInicialPopup+30)+"px";
	cerrarImg.style.left=(xInicialPopup+xPopup-60)+"px";
	cerrarImg.style.position="absolute";
	cerrarImg.style.width="50px";
	cerrarImg.style.height="50px";
	cerrarImg.id = "cerrar";
	cerrarImg.setAttribute("class", "cerrar");
	cerrarImg.onclick=function(){finalizar2();};
	p.appendChild(cerrarImg);
	
	if(ruta == "img/regaloRelax.png"){
	    var pdfIMG = document.createElement("img");
	    pdfIMG.src="img/empty.gif";
	    pdfIMG.style.top=(282+yInicialPopup)+"px";
	    pdfIMG.style.left=(xInicialPopup+20)+"px";
	    pdfIMG.style.position="absolute";
	    pdfIMG.style.width="165px";
	    pdfIMG.style.height="36px";
	    pdfIMG.id = "cerrar";
	    //pdfIMG.style.backgroundColor="black";
        //pdfIMG.setAttribute("class", "cerrar");
	    pdfIMG.onclick=function(){
	        window.location = "img/Ficha_Instante_Relax.pdf";
	    };
	    p.appendChild(pdfIMG);
	}

	
	p.appendChild(iframe);
	//divNegro.appendChild(p);
	document.body.appendChild(p);
}
function insertFlash()
{
	//alert("S");
	var p = document.createElement("div");
	p.id = "pop";
	p.setAttribute("class", "emerge");
	p.style.position="absolute";
	p.style.zIndex="701";
	p.style.top=yInicialPopup+"px";
	p.style.left=xInicialPopup+"px";
	
		
	var iframe = document.createElement("div");
	iframe.id = "popup";
	iframe.setAttribute("class", "fondo2");
	//iframe.src=ruta;
	iframe.frameborder="1";
	iframe.style.width=xPopup+"px";
	iframe.style.height=yPopup+"px";
oImga = document.createElement('OBJECT'); 
oImga.setAttribute('TYPE' , "application/x-shockwave-flash");
oImga.setAttribute('WIDTH' , xPopup);
oImga.setAttribute('HEIGHT' , yPopup);
oImga.setAttribute('id',"PruebaEnvio"); // Give id to it
oImga.setAttribute('classid',"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"); // Give id to it
oImga.setAttribute('codebase',"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,42,0"); 

prm=document.createElement('PARAM');  // Create dynamically object tag
prm.setAttribute('NAME' , "MOVIE");
prm.setAttribute('VALUE' , ruta);  //source file
oImga.appendChild(prm);
prm2=document.createElement('PARAM');  
prm2.setAttribute('NAME' , "quality");
prm2.setAttribute('VALUE' , "high");  
oImga.appendChild(prm2);
prm3=document.createElement('PARAM');  
prm3.setAttribute('NAME' , "bgcolor");
prm3.setAttribute('VALUE' , "#000000");  
oImga.appendChild(prm3);
prm4=document.createElement('embed');  
prm4.setAttribute('NAME' , "PruebaEnvio");
prm4.setAttribute('src' , ruta);  
prm4.setAttribute('quality' , "high");
prm4.setAttribute('WIDTH' , xPopup);
prm4.setAttribute('HEIGHT' , yPopup);
prm4.setAttribute('swLiveConnect' , "true");
prm4.setAttribute('bgcolor' , "#000000"); 
prm4.setAttribute('pluginspage' , "http://www.macromedia.com/go/getflashplayer");
prm4.setAttribute('type' , "application/x-shockwave-flash"); 
oImga.appendChild(prm4);

iframe.appendChild(oImga);
	//iframe.innerHTML=pintaflash('clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',xPopup,yPopup,ruta,'#000000');
	
	
	
	
	

	
	p.appendChild(iframe);
	//divNegro.appendChild(p);
	document.body.appendChild(p);
}
function pintaflash(classid,ancho,alto,nombre,colorfondo,transparencia)
{
	var str="";
	var vartransparencia=''
	if (transparencia!=undefined){ 
		vartransparencia='wmode="'+transparencia+'"'
	}
	
	str=str+'<object classid="'+classid+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,42,0" id="PruebaEnvio" width="'+ancho+'" height="'+alto+'">'
	str=str+'  <param name="movie" value="'+nombre+'">';
	str=str+'  <param name="quality" value="high">';
	str=str+'  <param name="bgcolor" value="'+colorfondo+'">';
	
	if (transparencia!=undefined){
		str=str+'  <param name="wmode" value="'+transparencia+'">';
	}
	
	str=str+'    <embed name="PruebaEnvio" '+vartransparencia+' src="'+nombre+'" quality="high" bgcolor="'+colorfondo+'" swLiveConnect="true" width="'+ancho+'" height="'+alto+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
	str=str+'</object>';
	
	return str;
}





function finalizar2()
{

	divPopup=document.getElementById("pop");
	divs.push(divPopup);
	
	//divPopup=document.getElementById("popup");
	acabar4();
	
	//Per acabar amb fade
	//tempor=setTimeout(acabar2,FADE_INTERVAL);
	
}
function acabar2()
{	
	nova = divPopup.style.height;
	nova = nova.replace("px","");
	nova = parseInt(nova)-8;
	divPopup.style.height=nova+"px";
	//alert(nova);
	

	if(nova<=5) //si ja hem acabat una transició, acabem
	{
		clearTimeout(tempor);
		tempor=setTimeout(acabar3,FADE_INTERVAL);
	} 
	else //mentre no estigui la seguent imatge en escena (sense la imatge "current"), anem actualitzant opacitats
	{
		clearTimeout(tempor);
		tempor=setTimeout(acabar2,FADE_INTERVAL/3);
	}
}
function acabar3()
{
	//alert(divs.length);
	for(i=0;i<divs.length;i++) 
	{	
		divs[i].style.display = "block";
		divs[i].xOpacity = divs[i].xOpacity - FADE_STEP;
		setOpacity(divs[i]);
		//alert(divs[i].xOpacity);
	}

	if(verificarOpacitys("neg")) //si ja hem acabat una transició, acabem
	{
		clearTimeout(tempor);
		acabar4();
	} 
	else //mentre no estigui la seguent imatge en escena (sense la imatge "current"), anem actualitzant opacitats
	{
		clearTimeout(tempor);
		tempor=setTimeout(acabar3,FADE_INTERVAL);
	}	
}
function acabar4()
{
	divPop=d.getElementById("pop");
	divPop.parentNode.removeChild(divPop);
	//alert("Siiiii");
	for(i=0;i<divs.length;i++) 
	{	
		divs[i].innerHTML="";
		divs[i].parentNode.removeChild(divs[i]);
	}
}










function setPopupHeight(id, h) 
{
	if (document.getElementById) 
	{
		heiW=getWinHeight();
		widW=getWinWidth();
		
		var theIframe = document.getElementById(id);
		var div = document.getElementById("fondoNegro");
		var cerrarImg = document.getElementById("cerrar");
		
		if (theIframe) 
		{
			//alert(id);
			div.style.width="0px";
			div.style.height="0px";
			
			xInicialPopup=(widW/2)-(xPopup/2);
			yInicialPopup=(heiW/2)-(yPopup/2);
			
			heiW=getWinHeight();
			widW=getWinWidth();
			
			div.style.width=widW+"px";
			div.style.height=heiW+"px";
			
			theIframe.style.left=xInicialPopup+"px";
			theIframe.style.top=yInicialPopup+"px";
			
			//Per crear la ma com a punter al passar per la "X" de tancar
			cerrarImg.style.top=(yInicialPopup)+"px";
			cerrarImg.style.left=(xInicialPopup+xPopup-30)+"px";
		}
	}
}

function verificarOpacitys(moment) 
{
	flag=true;
		
	for(i=0;i<divs.length;i++) 
	{	
		if (moment=="pos")
		{
			//alert("divs[i].xOpacity>=opacity; "+divs[i].xOpacity+" "+opacity);
			if(divs[i].xOpacity>=opacity)
			{
				divs[i].xOpacity = opacity;
				setOpacity(divs[i]);
			}
			else
				flag=false;
		}
		else
		{
			if(divs[i].xOpacity<=0)
			{
				divs[i].xOpacity = 0;
				setOpacity(divs[i]);
			}
			else
				flag=false;
		}
	}
	
	return flag;
}

function setOpacity(obj) 
{
	if(obj.xOpacity>opacity)
		obj.xOpacity = opacity;
	
	if (obj.xOpacity<0)
		obj.xOpacity=0;
		
	obj.style.opacity = obj.xOpacity;
	obj.style.MozOpacity = obj.xOpacity;
	obj.style.filter = "alpha(opacity="+(obj.xOpacity*100)+")";
}

function getWinHeight() 
{
	var D = document;
	return Math.max
	(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight)
	);
}

function getWinWidth() 
{
	var D = document;
	//alert("Width: "+D.documentElement.scrollWidth+" Width2: "+D.body.scrollWidth);
	return Math.max
	(
		Math.max(D.body.scrollWidth, D.documentElement.scrollWidth)
	);
}

function getDocHeight() 
{
	var D = document;
	return Math.max
	(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

function getDocWidth() 
{
	var D = document;
	return Math.max
	(
		Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
		Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
		Math.max(D.body.clientWidth, D.documentElement.clientWidth)
	);
}
