var globalPag;

var dsRegsitros = new Spry.Data.JSONDataSet(null, null);
var dsObserver = null;
var bValorIni = true;


function fCargarDatosJson(){

	
	fCargandoPantalla ("divGaleria","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cargando Datos ...");
	
	sArchivo = "json/msn_avi_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 += "";
		
		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"];
				
				bPasa = true;
				
				if (uiContador2==1 || uiContador2==5 || uiContador2==9 ){
					sGenera += "<div style=\"border:0px solid red; width:40px; height:130px; float:left;\"></div>";
					sGenera += "<div style=\"border:0px solid green; width:100px; height:130px; float:left; \">";
					sGenera += "<div style=\"border:2px solid #000000; width:98px; height:96px; background:url("+sImg+") no-repeat #ffffff; cursor:pointer;\" onclick=\"fGuardarImg("+uiId+");\"></div>";
					sGenera += "	<div style=\"border:0px solid blue; width:100px; height:20px; text-align:center;\">"+sNombre+"</div>";
					sGenera += "</div>";
				}else if (uiContador2==2 || uiContador2==6 || uiContador2==10){
					sGenera += "<div style=\"border:0px solid red; width:20px; height:130px; float:left;\"></div>";
					sGenera += "<div style=\"border:0px solid green; width:100px; height:130px; float:left; \">";
					sGenera += "<div style=\"border:2px solid #000000; width:98px; height:96px; background:url("+sImg+") no-repeat #ffffff; cursor:pointer;\" onclick=\"fGuardarImg("+uiId+");\"></div>";
					sGenera += "	<div style=\"border:0px solid blue; width:100px; height:20px; text-align:center;\">"+sNombre+"</div>";
					sGenera += "</div>";
				}else if (uiContador2==3 || uiContador2==7 || uiContador2==11){
					sGenera += "<div style=\"border:0px solid red; width:20px; height:130px; float:left;\"></div>";
					sGenera += "<div style=\"border:0px solid green; width:100px; height:130px; float:left; \">";
					sGenera += "<div style=\"border:2px solid #000000; width:98px; height:96px; background:url("+sImg+") no-repeat #ffffff; cursor:pointer;\" onclick=\"fGuardarImg("+uiId+");\"></div>";
					sGenera += "	<div style=\"border:0px solid blue; width:100px; height:20px; text-align:center;\">"+sNombre+"</div>";
					sGenera += "</div>";
				}else if (uiContador2==4 || uiContador2==8 || uiContador2==12){
					sGenera += "<div style=\"border:0px solid red; width:20px; height:130px; float:left;\"></div>";
					sGenera += "<div style=\"border:0px solid green; width:100px; height:130px; float:left; \">";
					sGenera += "<div style=\"border:2px solid #000000; width:98px; height:96px; background:url("+sImg+") no-repeat #ffffff; cursor:pointer;\" onclick=\"fGuardarImg("+uiId+");\"></div>";
					sGenera += "	<div style=\"border:0px solid blue; width:100px; height:20px; text-align:center;\">"+sNombre+"</div>";
					sGenera += "</div>";
                
	                sGenera += "<div style=\"clear:left\"></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 = "";
			
			if (total > 12) {
			
				sGenera2 += "	<div style=\"border:0px solid red; width:400px; height:20px; float:left;\"></div>";
	            sGenera2 += "    <div style=\"clear:left\"></div>";
	                
	            sGenera2 += "    <div style=\"border:0px solid red; width:120px; height:35px; float:left;\"></div>";
	            
				if (regresarActivo=="true"){
					sGenera2 += "    <div style=\"border:0px solid red; width:138px; height:35px; float:left; cursor:pointer;\" onclick=\"fCargaPag("+regresarValor+");\">";
					sGenera2 += "        <img src=\"imagenes/btnAnterior.jpg\" width=\"136\" height=\"32\" />";      
					sGenera2 += "    </div>";
				}else{
					sGenera2 += "    <div style=\"border:0px solid red; width:138px; height:35px; float:left; \">";
					sGenera2 += "    </div>";
				}		
				
	            sGenera2 += "    <div style=\"border:0px solid red; width:30px; height:35px; float:left;\">";
	                
	            sGenera2 += "    </div>";
	             
				if (siguienteActivo=="true"){
		            sGenera2 += "    <div style=\"border:0px solid red; width:138px; height:35px; float:left; cursor:pointer;\" onclick=\"fCargaPag("+siguienteValor+");\">";
		            sGenera2 += "        <img src=\"imagenes/btnSiguiente.jpg\" width=\"136\" height=\"32\" />";
		            sGenera2 += "    </div>";
				}else{
					sGenera2 += "    <div style=\"border:0px solid red; width:138px; height:35px; float:left; \">";
		            sGenera2 += "    </div>";
				}
			}
			
			fCargandoPantalla ("divNav",sGenera2);
			
		}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=1";
	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 fIniAvi(){
	globalPag=1;
	fCargarDatosJson();
	//alert(globalPag);
}
