//Declarar los datasets	
var dsIdiomaGeneral = null;
var dsMenu1 = null;
var dsMenu1Observer = new Object;

var numFotoIndex = 0;
var elegidos = new Array(30);	// Array que contendrá los índices de las imágenes aleatorias

Spry.Utils.addLoadListener (initIndex);

function preloadImage(path, numImages) {
	var imagepreloader = new Spry.Utils.ImageLoader();
	
	for (var i=1;i<=numImages;i++)
		imagepreloader.load(path + '/' + i + '.jpg');
		
	loadedImages();
}

function loadedImages(){
	fadeOut = new Spry.Effect.Fade('preloader', { from: 100, to: 0 });
	fadeOut.start();

	fadeIn = new Spry.Effect.Fade('fotoIndex', { from: 0, to: 100 });
	fadeIn.start();
}

function initIndex(){
	//initIdiomaGeneral();
	generarListaAleatorios();
	putFirstFotoIndex();
	preloadImage('/img/index', 30);
	setInterval("cambiaFotoIndex()",10000);	
}

function initIdiomaGeneral(){
	dsIdiomaGeneral = new Spry.Data.XMLDataSet("/xml/global/general.xml", "/oab/general", { useCache: false});
	dsMenu1 = new Spry.Data.NestedXMLDataSet(dsIdiomaGeneral, "menu1/idioma" + idiomaClien, { useCache: false});
	dsIdioma = new Spry.Data.NestedXMLDataSet(dsIdiomaGeneral, "idioma/idioma" + idiomaClien, { useCache: false});
	//dsBuscador = new Spry.Data.NestedXMLDataSet(dsIdiomaGeneral, "buscador/idioma" + idiomaClien, { useCache: false});
	//dsTexto = new Spry.Data.NestedXMLDataSet(dsIdiomaGeneral, "texto/idioma" + idiomaClien, { useCache: false});
	dsMenu1.addObserver(dsMenu1Observer);
}

function cambiaFotoIndex(){
	numFotoIndex++;
	if(numFotoIndex == 30)
		numFotoIndex = 0;
	document.getElementById("fotoIndex").src = "/img/index/" + elegidos[numFotoIndex] + ".jpg";
}

function putFirstFotoIndex(){
	document.getElementById("fotoIndex").src = "/img/index/" + elegidos[0] + ".jpg";
}

/*function selectIdioma(id){
	if (id == "castellano"){
		newXPath("/index/menu1Es");
	}
	else if (id == "english"){
		newXPath("/index/menu1En");
	}
	idiomaSeleccionado = true;
	//cambiaColorIdioma(id);
}*/
function selectIdioma(id){
    if (id == "castellano"){
        //newXPath("menu1/idioma" + idiomaClien);
        idIdioma = 1;
    }
    else if (id == "english"){
        //newXPath("menu1/idioma" + idiomaClien);
        idIdioma = 2;
    }
    idiomaSeleccionado = true;
    //cambiaColorIdioma(id);
    cambiaIdioma(idIdioma);
}

function newXPath(thepath){
	//alert(thepath);
	//dsMenu1.setXPath(thepath);
	//dsMenu1.loadData();
}

/*function cambiaColorIdioma(id){
	if (id == "castellano"){
		Spry.$$("div#" + id +" a:link").setStyle("color:#FF0000");
		id = "english";
		Spry.$$("div#" + id +" a:link").setStyle("color:#FFFFFF");
	}
	else if (id == "english"){
		Spry.$$("div#" + id +" a:link").setStyle("color:#FF0000");
		id = "castellano";
		Spry.$$("div#" + id +" a:link").setStyle("color:#000000");
	}
}*/

function generarListaAleatorios(){
	//console.log('generarListaAleatorios');
	var numFotos = 30;
	var numeros = new Array(30);
	
	for (var i=0; i<numFotos; i++){
		numeros[i] = i+1;
	}

	var numElegidos = 0;
	while (numElegidos < numFotos){
		//console.log('máximo aleatorio');
		//console.log(numFotos-numElegidos);
		var aleat = Math.floor(Math.random()*(numFotos-numElegidos));
		//console.log('aleat');
		//console.log(aleat);
		elegidos[numElegidos] = numeros[aleat];
		//console.log('elegidos');
		//console.log(elegidos);
		
		if (aleat == 0){	// Primer lugar del array
			//console.log('Primer lugar del array');
			numeros = numeros.slice(1, numFotos-numElegidos);
		}
		else if (aleat == numFotos-numElegidos){	// Último lugar del array
			//console.log('Último lugar del array');
			numeros = numeros.slice(0, numFotos-aleat);
		}
		else{ // Posición intermedia del array
			//console.log('Posición intermedia del array');
			var arr1 = numeros.slice(0, aleat);
			var arr2 = numeros.slice(aleat+1, numFotos-numElegidos)
			//console.log('arr1');
			//console.log(arr1);
			//console.log('arr2');
			//console.log(arr2);
			numeros = arr1.concat(arr2);
		}
		
		//console.log('numeros');
		//console.log(numeros);

		numElegidos++;
	}
	
	//console.log(elegidos);
}