var globalPag;

var dsRegsitros = new Spry.Data.JSONDataSet(null, null);
var dsObserver = null;
var bValorIni = true;


function fCargarDatosJson(){

	
	fCargandoPantalla ("divGaleria","<p>&nbsp;</p><p>&nbsp;</p>Cargando Datos ...");
	
	sArchivo = "json/juegos_json.php?pagina="+globalPag;
	if (bValorIni){
		dsRegsitros = new Spry.Data.JSONDataSet(sArchivo,{useCache: false, subPaths:["rRegistroJson","total","previous","next","totalitems","items"]});
		dsRegsitros.addObserver(fCargaDatos);
		dsRegsitros.loadData();
	}else{
		dsRegsitros.setURL(sArchivo, {useCache: false, subPaths:["rRegistroJson","total","previous","next","totalitems","items"]});
		dsRegsitros.addObserver(fCargaDatos);
		dsRegsitros.loadData();
	}
	bValorIni=false;
}



function fCargaDatos(nType, notifier, data){
	if(nType == 'onPostLoad'){
		var arrGridTitulo = new Array();
		var arrDatosUsuarios = new Array();
		
		rows = dsRegsitros.getData();
		numArreglo = rows.length;
		//alert("ds1=" + numArreglo);
		
		sGenera = "";
		sGenera += "<div style=\"border:0px solid red; width:290px; height:20px;\"></div>";
		
		bPasa = false;
		
		for (i = 0; i < rows.length; i++){
			uiContador= rows[i]["rRegistroJson.uiC"];
			if (uiContador!=0){
				uiContador2= rows[i]["rRegistroJson.uiC2"];
				uiId= rows[i]["rRegistroJson.uiId"];
				sNombre= rows[i]["rRegistroJson.sNombre"];
				sImg= rows[i]["rRegistroJson.sImg"];
				swf= rows[i]["rRegistroJson.swf"];
				tamanow= rows[i]["rRegistroJson.tamanow"];
				tamanoh= rows[i]["rRegistroJson.tamanoh"];
				
				bPasa = true;
				
				if (uiContador2==1 || uiContador2==4 ){
					sGenera += "<div style=\"border:0px solid red; width:90px; height:90px; float:left\">";
					sGenera += "	 <div style=\"border:0px solid red; width:90px; height:60px; cursor:pointer; text-align:center;\" ";
					sGenera += "	 onclick=\"fJuegoAbrir('"+swf+"','"+tamanow+"','"+tamanoh+"');\">";
					sGenera += "		<img src=\""+sImg+"\" />";
					sGenera += "	 </div>";
						 
					sGenera += "	 <div style=\"clear:left\"></div>";
						 
					sGenera += "	 <div style=\"border:0px solid red; width:90px; height:20px; float:left; text-align:center\">";
					sGenera += "		<div style=\"cursor:pointer\" onclick=\"fJuegoAbrir('"+swf+"','"+tamanow+"','"+tamanoh+"');\" > ";
					sGenera += sNombre;
					sGenera += "		</div>";
					sGenera += "	 </div>";
					sGenera += "	 <div style=\"clear:left\"></div>";
					sGenera += "</div>";
				}else if (uiContador2==2 || uiContador2==5 ){
					sGenera += "<div style=\"border:0px solid red; width:90px; height:90px; float:left\">";
					sGenera += "	 <div style=\"border:0px solid red; width:90px; height:60px; cursor:pointer; text-align:center;\" ";
					sGenera += "	 onclick=\"fJuegoAbrir('"+swf+"','"+tamanow+"','"+tamanoh+"');\">";
					sGenera += "		<img src=\""+sImg+"\" />";
					sGenera += "	 </div>";
						 
					sGenera += "	 <div style=\"clear:left\"></div>";
						 
					sGenera += "	 <div style=\"border:0px solid red; width:90px; height:20px; float:left; text-align:center\">";
					sGenera += "		<div style=\"cursor:pointer\" onclick=\"fJuegoAbrir('"+swf+"','"+tamanow+"','"+tamanoh+"');\" > ";
					sGenera += sNombre;
					sGenera += "		</div>";
					sGenera += "	 </div>";
					sGenera += "	 <div style=\"clear:left\"></div>";
					sGenera += "</div>";
				}else if (uiContador2==3 || uiContador2==6 ){
					sGenera += "<div style=\"border:0px solid red; width:90px; height:90px; float:left\">";
					sGenera += "	 <div style=\"border:0px solid red; width:90px; height:60px; cursor:pointer; text-align:center;\" ";
					sGenera += "	 onclick=\"fJuegoAbrir('"+swf+"','"+tamanow+"','"+tamanoh+"');\">";
					sGenera += "		<img src=\""+sImg+"\" />";
					sGenera += "	 </div>";
						 
					sGenera += "	 <div style=\"clear:left\"></div>";
						 
					sGenera += "	 <div style=\"border:0px solid red; width:90px; height:20px; float:left; text-align:center\">";
					sGenera += "		<div style=\"cursor:pointer\" onclick=\"fJuegoAbrir('"+swf+"','"+tamanow+"','"+tamanoh+"');\" > ";
					sGenera += sNombre;
					sGenera += "		</div>";
					sGenera += "	 </div>";
					sGenera += "	 <div style=\"clear:left\"></div>";
					sGenera += "</div>";
				}			
				
			}
		}
		
		if (bPasa){
		
			fCargandoPantalla ("divGaleria",sGenera);
			
			//paginacion --------------------------------------------
			
			regresarActivo = rows[0]["previous.enable"];
			regresarValor = rows[0]["previous.value"];
			
			siguienteActivo = rows[0]["next.enable"];
			siguienteValor = rows[0]["next.value"];
			
			total= rows[0]["total"];
			
			//alert(regresarActivo);
			
			sGenera2 = "";
			sGenera3 = "";
			
			if (total > 6) {
			
				sGenera3 += "<div style=\" border:0px solid red; width:220px; height:20px; float:left;\"></div>";            
				
				if (regresarActivo=="true"){
					
					sGenera3 += " <div style=\" border:0px solid red; width:22px; height:22px; float:left; cursor:pointer;\" onclick=\"fCargaPag("+regresarValor+");\">";
					sGenera3 += "	<img src=\"imagenes/juegos_reg.png\" />";
					sGenera3 += "</div>";
				}else{
				
					sGenera3 += " <div style=\" border:0px solid red; width:22px; height:22px; float:left; \" >";
					sGenera3 += "</div>";
				}		
				
				sGenera3 += "<div style=\" border:0px solid red; width:5px; height:20px; float:left;\"></div>";
	            	             
				if (siguienteActivo=="true"){
		        
					sGenera3 += "<div style=\" border:0px solid red; width:22px; height:22px; float:left; cursor:pointer;\" onclick=\"fCargaPag("+siguienteValor+");\">";
					sGenera3 += "	<img src=\"imagenes/juegos_avanzar.png\" />";
					sGenera3 += "</div>";
				}else{
					
					sGenera3 += "<div style=\" border:0px solid red; width:22px; height:22px; float:left;\">";
					sGenera3 += "</div>";
				}
			}
			
			fCargandoPantalla ("divNav",sGenera3);
			
		}else{
			fCargandoPantalla ("divGaleria","");
		}
		
	}
}

function fCargaPag(uiValor){
	//alert(uiValor);
	globalPag = uiValor;
	fCargarDatosJson();
}

function fGuardarImg_update(req){
	//alert(req.xhRequest.responseText);
}
function fGuardarImg(uiValor){
	//alert(uiValor);
	sContenido = "?uiTabla=3";
	sContenido += "&uiId="+uiValor;
		
	sURL= "lib/guardar_img.php"+sContenido;
	window.open(sURL);
	//Spry.Utils.loadURL("POST", sURL, true, fGuardarImg_update, { postData: ""});

}
function fCargandoPantalla(sDiv,sDivValor){
	if(Spry.$(sDiv)!=null)Spry.Utils.setInnerHTML(sDiv,sDivValor);
}

function fIniJuegos(){
	globalPag=1;
	fCargarDatosJson();
	//alert(globalPag);
}

//========================================================================
var gbl_sJuego;
var gbl_uiW;
var gbl_uiH;

function fJuegoAbrir(sJuego,uiW,uiH){
	
	sParametros = "?sJuego=admin/juegos/files/swf/"+sJuego;
	sParametros += "&uiW="+uiW;
	sParametros += "&uiH="+uiH;
	sUrlF = "juegos_popup2.php"+sParametros;
	//alert(sUrlF);
	uiX = (screen.width-uiW) / 2;
	uiY = (screen.height-uiH) / 2;
	pantalla = window.open(sUrlF, 'mismonitos_juegos', 'left='+uiX+',top='+uiY+',width='+uiW+', height='+uiH+',scrollbars=0, resizable=no, toolbar=0'); 
}

function fJuegoAbrirTemp(sJuego,uiW,uiH){
	//alert(uiW);
	
	if (sJuego == 0){
		alert("Error, no se puede abrir el juego");
	}else{
	
		gbl_sJuego=sJuego;
		gbl_uiW=uiW;
		gbl_uiH=uiH;
		
		sUrl = "juegos_popup.php?sJuego="+sJuego+"&uiW="+uiW+"&uiH="+uiH;	
		
		idSpry=setInterval("fJavascript()", 1000);
		
		jQuery.facebox(function() {
			jQuery.get(sUrl, function(data) {
				jQuery.facebox(data)
			})
		})
	
	}
}
function fJavascript(){
	if(document.getElementById('pantalla_swf')!=null){
		clearInterval(idSpry);
		fCargarDatosJuegoSwf();
	}
}
function fCargarDatosJuegoSwf(){
	//alert("entro");
	Spry.$("pantalla_swf").style.display='block';
	//var foswf = new FlashObject("admin/juegos/files/swf/"+gbl_sJuego, "juego_swf", gbl_uiW, gbl_uiH, "8", "#FFFFFF");
	//foswf.addParam("wmode","window");
	//foswf.write("juego_swf");
	
	sGenera ="";
	sGenera +="<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\""+gbl_uiW+"\" height=\""+gbl_uiH+"\"><PARAM NAME=movie VALUE=\"admin/juegos/files/swf/"+gbl_sJuego+"\"> <param name=\"quality\" value=\"high\"><embed src=\"admin/juegos/files/swf/"+gbl_sJuego+"\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+gbl_uiW+"\" height=\""+gbl_uiH+"\"></embed></OBJECT>";
    fCargandoPantalla("juego_swf",sGenera);
	
}
function fCargandoPantalla(sDiv,sDivValor){
	//alert(Spry.$(sDiv));
	if(Spry.$(sDiv)!=null)Spry.Utils.setInnerHTML(sDiv,sDivValor);
}



function flash(){

sGenera ="";

sGenera +='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="420" height="420"><PARAM NAME=movie VALUE="admin/juegos/files/swf/1_mismonitos-p85Hi4bF.swf"> <param name="quality" value="high"><embed src="admin/juegos/files/swf/1_mismonitos-p85Hi4bF.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="420" height="420"></embed></OBJECT>';
fCargandoPantalla("divFlash",sGenera);

}
