//var gTimeMax=5;
gTimeMax = gTimeMax || 5;
var gTimeMin=1;
var gHIni=null;
var gId=0;
var gMode=1;
var gFlash=null;

var gWidth = '0px';
var gHeight = '0px';

//PubliOnLoad();

/*********************************************************************/
function PubliOnLoad(){
	var Now= new Date();
	var tam=0;
	gHIni= Now.getTime();
	gFlash=getFlash("JugGame");
	
	
	switch(gMode){
		case 1:
			var lObj = document.getElementById("JugGameDiv");
			if(lObj){
				var lObj2 = document.getElementById("JugGame");	
				if(lObj2){
					gWidth=lObj2.width;
					gHeight=lObj2.height;					
				}else{
					gWidth = '100%';
					gHeight = '100%';						
				}
				//if(lObj.width)lObj.width=0;
				//if(lObj.height)lObj.height=0;
				
				lObj.style.width='1px';
				lObj.style.height='1px';
				//lObj.style.width=gWidth;
				//lObj.style.height=gHeight;
				lObj.style.overflow='hidden';
				lObj.style.visibility='hidden';
				
				lObj.style.display='';
				//lObj.style.display='none';
			}
			var lObj3 = document.getElementById("JugPubli");
			if (lObj3)
				lObj3.style.display = '';
			break;
		case 2:	
			break;
	}
	
	gId = setInterval(SimulaCarga,200);
	/*
	if (FlashIsLoaded(gFlash)){
		gId=setInterval ("SimulaCarga()",200);		
	}else{
		gId=setInterval ("Carga()",200);		
	}
	*/
}
/*********************************************************************/
	function FlashIsLoaded (vFlash){
		try{
	 if (typeof(vFlash) != "undefined"){
	 	var lPer=vFlash.PercentLoaded();
	 	return (lPer == 100 || lPer<0);
	 } 
 	return true;
 	}catch(e){
			     // alert('error en FlashIsLoaded');
	}
 }

/*********************************************************************/
function getFlash(vFlashName) {
	if (document && document[vFlashName])
		return document[vFlashName];
	else
		return window[vFlashName];
}
/*********************************************************************/ 
function SimulaCarga(){	
try{
	var lBarra=document.getElementById("progreso");
	var lNumProg=document.getElementById("progNum");
	if(lBarra){
		var lWidth=0;
		var Ahora= new Date();
		Ahora= Ahora.getTime();
		var ldifT;
		var lMaxTime= gTimeMax*1000;
		var lMinTime= gTimeMin*1000;
		ldifT = Ahora - gHIni;
		lWidth= (ldifT*100)/lMaxTime;
		lWidth= parseInt(lWidth);
		//fake para miguel
		/*
		if(lWidth>100){
			lWidth=100;
		 var Ahora= new Date();
		  gHIni= Ahora.getTime();
		}*/
		
		if(lWidth>100)lWidth=100;
		if(ldifT>lMinTime){
				ShowReady();	
		}
		lBarra.style.width=lWidth+"%";	
		lNumProg.innerHTML=lWidth+"%";	
  		if(lWidth==100){
  			EndLoad();
    	  clearInterval(gId);
  		} 
		}
	}catch(e){
			     // alert('error en SimulaCarga');
	}
}

function ShowReady(){
	try{
			var lLoadEnd=document.getElementById("LoadEnd");
			if(lLoadEnd){
				if(!lLoadEnd.onclick){
				//if(!lLoadEnd.onclick ||	lLoadEnd.style.visibility=='hidden' || !lLoadEnd.className){
				//	lLoadEnd.appendChild(document.createTextNode('Listo para jugar,Click aqui!'));
					lLoadEnd.style.width='100%';
					lLoadEnd.style.height='20px';								
					lLoadEnd.style.visibility='visible';
					lLoadEnd.className='JugRdy';
					lLoadEnd.onclick=function (){EndLoad();}				
				}
				}//
				}catch(e){
			      //alert('error en ShowReady');
	}
}
function EndLoad(){
	try{
		var lobj=null; 
		clearInterval(gId);
		if(gMode==1){			
			lobj = document.getElementById("JugGameDiv");
			if(lobj){
				//if(lobj.width)lObj.width=gWidth;
				//if(lobj.height)lObj.height=gWidth;
				//lobj.style.width='100%';
				//lobj.style.height='100%';
				if (gWidth == parseInt(gWidth))
					lobj.style.width=gWidth+'px';
				else
					lobj.style.width=gWidth;
				
				if (gHeight == parseInt(gHeight))
					lobj.style.height=gHeight+'px';
				else
					lobj.style.height=gHeight;
				
				//lobj.style.overflow='auto';
				lobj.style.visibility='visible';
				//lobj.style.display='';
				
			}
		}
		lobj = document.getElementById("JugPubli");
		if(lobj)lobj.style.display="none";	
		}catch(e){
			     // alert('error en EndLoad');
	}
}
/*********************************************************************/ 
function Carga(){
	try{
	var lBarra=document.getElementById("progreso");
	var lNumProg=document.getElementById("progNum");
	var lFlash;
	var Per;
	var lWidth=0;
	var ldifT;
	var Ahora= new Date();
//	lFlash=getFlash("JugGame");
	Per=gFlash.PercentLoaded();
	//if (Per<0){
//		clearInterval(" Carga()");
//		gId=setInterval ("SimulaCarga()",200);
//	}
	Per=Math.abs(Per);
	Ahora= Ahora.getTime();
	var lMaxTime= gTimeMax*1000;
	var lMinTime= gTimeMin*1000;
	ldifT = Ahora - gHIni;
	lWidth= (ldifT*50)/lMaxTime;
	lWidth+=(Per*50)/100;
	lWidth= parseInt(lWidth);
	if(lWidth>100)lWidth=100;	
	lBarra.style.width=lWidth+"%";	
	lNumProg.innerHTML=lWidth+"%";
	if(Per>=78)ShowReady();
	if(lWidth==100){
		  EndLoad();
   		clearInterval(gId);
		} 
		}catch(e){
			     // alert('error en Carga');
	}
}


/******* BORRAR **************************************************************/ 
	function Test(){
	var obj = document.getElementById("JugPubli");
	if(obj.style.display=="")
	obj.style.display="none";
	else
	obj.style.display="";
}


