var ruta = 'http://213.60.23.33/js/up.swf';
var imagenes = new Array(0);
var nImagenes = 0;
var cargadas = 0;
var cargando = false;
//carga el swf
function loadMExp(nombreJS){

/*
	var multiFile =
			'<object id="'+nombreJS+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" ' +
			  ' width="1" height="1" allowScriptAccess="always">' +
	          '<param name="movie" value="'+ruta+'">' +
	          '<param name="quality" value="high">' +
	          '<param name="FlashVars" value="numOption=2" />' +
			  '<param name="allowScriptAccess" value="always">' +
	          '<embed name="'+nombreJS+'" ' +
	          'src="'+ruta+'" width="1" height="1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowScriptAccess="always" FlashVars="numOption=2"></embed>' +
		  	'</object>';
	capaCont = document.createElement('div');
	capaCont.id = "contMExp001";
	capaCont.innerHTML = multiFile;
	capaCont.style.display = "block";
	document.getElementsByTagName('body')[0].appendChild(capaCont);
	document.getElementById(nombreJS).outerHTML = document.getElementById(nombreJS).outerHTML;
*/
	//cargadores
	contCargador = document.createElement('div');
	contCargador.id = "contLoaders";
	contCargador.style.display = "none";
	contCargador.style.position = "absolute";
	document.getElementsByTagName('body')[0].appendChild(contCargador);

 	//bloquea();//!!
}
//devuelve el clip de pelicula que se le pasa
function thisMovie(movieName) {
   if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
        return document[movieName];
    }
}
//bloquea la subida
function seleccionadas(){
	//alert("nueva carga");
	imagenes = new Array(0);
	nImagenes = 0;
	cargadas = 0;
	document.getElementById("contLoaders").style.display = "block";
	bloquea();

}
//llama al explorador de archivos
function explora(nombreJS, rutaSrv){
	imagenes = new Array(0);
	nImagenes = 0;
	cargadas = 0;

	var mExp = thisMovie(nombreJS);
	if(mExp){
		mExp.fExplora(rutaSrv);
	}
	else{
		if(confirm('No se ha cargado el componente,\n ¿quiere tratar de cargarlo de nuevo ?'))loadMExp(nombreJS);
	}

	document.getElementById("contLoaders").style.display = "block";

	bloquea();
}
//muestra el array de imagenes que se han subido
function muestraImagenes(){
	var auxPrueba = "";
	for(i=0;i<imagenes.length; i++){
		auxPrueba += "\n"+i+" => "+imagenes[i];
	}
	auxPrueba += "\nnImagenes => "+nImagenes;
	auxPrueba += "\ncargadas => "+cargadas;
	//alert(auxPrueba);
}
//tamaño del navegador
function tam(){
	tamano = new Array();
	if (navigator.appName.indexOf("Microsoft") != -1) {
		tamano[0] = truebody().scrollLeft + truebody().clientWidth;
		//tamano[1] = truebody().clientHeight;
		//tamano[1] = Math.min(truebody().scrollHeight, truebody().clientHeight);
		tamano[1] = truebody().scrollHeight + truebody().clientHeight;
	}
	else{
		tamano[0] = pageXOffset + window.innerWidth-15;
		tamano[1] = Math.min(window.innerHeight);
	}
	return tamano;
}
function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
//desactiva la capa de bloqueo
function desbloquea(){
	//alert("desactivando bloqueo");
	ocultaCapa('multiUpload', true);
	document.getElementById("capaBloqueo").style.display = "none";
	document.getElementById("capaBloqueo").outerHTML = "";
	document.getElementById("contLoaders").style.display = "none";
	document.getElementById("contLoaders").outerHTML = "";
}
//calcula el maximo zIndex
function getNextHighestDepth(){
    var tCol=document.getElementsByTagName('*');
    var z=0;
        for(var i=0;i<tCol.length;i++){
            if(tCol[i].style.zIndex>z){
                z=tCol[i].style.zIndex;
            }
        }
    return ++z;
}
//activa la capa de bloqueo
function bloquea(){
//	alert( "truebody().clientWidth : "+truebody().clientWidth + " - truebody().clientHeight : " + truebody().clientHeight);
//	alert("window.document.body.clientHeight : " + window.document.body.clientHeight + " - screen.height : " + screen.height);

	//ocultaCapa('multiUpload', false);

	contBloqueo = document.createElement('div');
	contBloqueo.id = "capaBloqueo";
	contBloqueo.innerHTML = "<tr><td></td></tr>"
	contBloqueo.style.display = "block";

	//cargadores
	contCargador = document.createElement('div');
	contCargador.id = "contLoaders";
	contCargador.style.display = "none";
	contCargador.style.position = "absolute";
	document.getElementsByTagName('body')[0].appendChild(contCargador);

	tamano = tam();
	//alert("tamaño -> ("+tamano[0]+" , "+tamano[1]+")");

	document.getElementsByTagName('body')[0].appendChild(contBloqueo);

	document.getElementById("contLoaders").style.zIndex = getNextHighestDepth();

	contBloqueo.style.zIndex = document.getElementById("contLoaders").style.zIndex - 1;

	contBloqueo.style.width = tamano[0] + "px";
	contBloqueo.style.height = tamano[1] + "px";

	document.getElementById("contLoaders").style.width = (tamano[0] * 0.9) + "px";
	document.getElementById("contLoaders").style.left = (tamano[0] * 0.05) + "px";

	document.getElementById("contLoaders").style.height = (tamano[1] * 0.9) + "px";
	document.getElementById("contLoaders").style.top = (tamano[1] * 0.05) + "px";

	document.getElementById("contLoaders").style.overflow = "auto";

}
//devuelve la posicion del nombre en el array
function nombreCapa(nombre){
	for(i=0;i<imagenes.length; i++){
		if(imagenes[i] == nombre) {
		 return i;
		 break;
		}
	}
}
//se ejecuta tantas veces como imagenes haya seleccionado el usuario
function nuevaCarga(nombre){

	imagenes[nImagenes] = nombre;
	genLoader(nImagenes);
	nImagenes ++;
a	//muestraImagenes();
}
//se ejecuta cada vez que se ha completado la carga de un archivo
function completado(nombre){
	//alert("carga de "+nombre+" completada");
	indice = nombreCapa(nombre);
	capaNombre = "n" + indice;
	capaProgreso = "p" + indice;
	nombreImagen = imagenes[indice];

	//document.getElementById(capaProgreso).innerHTML = "OK";

	cargadas++;
	if(cargadas == nImagenes) {
		//muestraImagenes();
		loader = document.getElementById("contLoaders");
		loader.innerHTML = "";
		document.getElementById("contLoaders").style.display = "none";
		//desbloquea();
		var nuevaURL = location.href;
		nuevaURL.replace("idImagen", "ertwert");
		nuevaURL.replace("&ac", "&adfgasdfgs");
		//alert(nuevaURL);
		location.href = nuevaURL;

		//location.reload();
	}
}
//genera el html de un loader que se le pasa
function genLoader(indice){
	capaNombre = "n" + indice;
	capaProgreso = "p" + indice;
	nombreImagen = imagenes[indice];

	html = '<ul id="contSubida">' +
	'<li id="' + capaNombre + '" class="archivo">'+ nombreImagen +'</li>' +
	'<li class="carga"><div id="fondoProgreso"><div class="progresoCarga" id="'+ capaProgreso +'"></div></li>' +
	'</ul>';

	contenedor = document.getElementById("contLoaders");
	contenedor.style.display = "block";
	contenedor.innerHTML += html;

	//contenedor.style.top = "100px";
	//contenedor.style.left = "100px";

}
//actualiza el progreso de carga del archivo que se le pasa
function actualizaProgreso(nombre, porcentaje){
	indice = nombreCapa(nombre);
	capaNombre = "n" + indice;
	capaProgreso = "p" + indice;
	nombreImagen = imagenes[indice];

	//document.getElementById(capaProgreso).innerHTML = porcentaje;
	document.getElementById(capaProgreso).style.width = porcentaje+"%";
}
//se ejecuta cuando hay un cambio en el progreso de carga de un archivo
function progreso(nombre, cargados, total){
	//alert(nombreCapa(nombre)+ " : " + nombre + " : cargados " +cargados+ " de "+total);
	porcentaje = Math.floor((cargados/total) * 100);
	actualizaProgreso(nombre, porcentaje);
}
//errores de seguridad, http, io
function error(nombre, mensaje){
	alert("error al subir " + nombre + ":\n" + mensaje);
	desbloquea();
}
//cancelado por el usuario
function cancel(nombre){
	alert("cancelado " + nombre);
	desbloquea();
}
//al abrir un archivo para empezar a enviarlo
function abierto(nombre){
	//alert("abierto " + nombre);
}
