// JavaScript Document
var total = 0;
var alto_foto = 0;
var ancho_foto = 0;
var proporcion_utilizada;
var position = new Array();

var altura = new Boolean(false);

//function inicializar (num_fotos,ancho,alto,horizontal,vertical) {
function inicializar (horizontal,vertical) {
	var obj = document.getElementById("carrusel");
	num_fotos = obj.getElementsByTagName("img").length;
	if (num_fotos > 0) {
		alto_foto = obj.getElementsByTagName("img").item(0).height;//alto;
		ancho_foto = obj.getElementsByTagName("img").item(0).width;//ancho;
	}
	if (horizontal == true) {
		obj.style.width = (ancho_foto * num_fotos) + "px";
		obj.style.height = alto_foto + "px";
		proporcion_utilizada = ancho_foto;
		altura = false;
	} else {
		obj.style.height = (alto_foto * num_fotos) + "px";
		obj.style.width = ancho_foto + "px";
		proporcion_utilizada = alto_foto;
		altura = true;
	}
	total = num_fotos;
	for (var i = 0; i < total; i ++) {
		position.push(proporcion_utilizada * - i);
	}
	
	
	setInterval('seguir()',8000);
}

var vel = 0;
var pos = 0;
var estado;
var move;
var posfinal = 0;
var coef;
var dir;
var activo = true;


	function seguir() {
		if (posfinal == -1) {
	//		posfinal = 1;
		//	dispara(true,5);
		}
		else
			dispara(false,5);
	}

function dispara (p,c) {
	if (activo) {
		estado = false;
		coef = c;
		dir = p;
		if (!dir) {
			if (posfinal < position.length - 1) {
				posfinal = posfinal + 1;
				move = setInterval(moving,60);
				activo = false;
	/*			if (posfinal == position.length - 1) {
					// quitamos la flecha de desplazamiento
					document.getElementById("right").innerHTML = "";
				}
				if (document.getElementById("left").childNodes.length != 1) {
					cadena = "<a href=\"javascript:dispara(true,"+ coef +");\">";
					cadena += "<img src=\"img/arrow_left.gif\" alt=\"\" /></a>";
					document.getElementById("left").innerHTML = cadena;
				}
*/			} else {
				posfinal = -1;
				/*dir = true;
				activo = true;
*/
				time = setInterval('fadeIn()',100);


			}
		} else {
			if (posfinal > 0) {
				posfinal = posfinal - 1;
				move = setInterval(moving,60);
				activo = false;
/*				if (posfinal == 0) {
					document.getElementById("left").innerHTML = "";			
				}
				if (document.getElementById("right").childNodes.length != 1) {
					cadena = "<a href=\"javascript:dispara(false,"+ coef +");\">";
					cadena += "<img src=\"img/arrow_right.gif\" alt=\"\" /></a>";
					document.getElementById("right").innerHTML = cadena;
				}
*/			} else {
				activo = true;
			}
		}
	}
}

function moving() {
	if (!dir) {
		if (pos > position[posfinal] + (proporcion_utilizada/2)) {
			vel-= coef;
		} else {
			if (!estado) {
				pos = position[posfinal] + (proporcion_utilizada/2);
				vel-= coef;
			}
			vel+= coef;
			estado = true;
		}
		pos+= vel;
		if (pos <= position[posfinal]) {
			pos = position[posfinal];
			clearInterval(move);
			vel = 0;
			activo = true;
		}
	} else {
		if (pos <= position[posfinal] - (proporcion_utilizada/2)) {
			vel+= coef;
		} else {
			if (!estado) {
				pos = position[posfinal] - (proporcion_utilizada/2);
				vel+= coef;
			}
			vel-= coef;
			estado = true;
		}
		pos+= vel;
		if (pos >= position[posfinal]) {
			pos = position[posfinal];
			clearInterval(move);
			vel = 0;
			activo = true;
		}	
	}
	
	// miramos la proporciona a tocar
	if (altura == true)
		document.getElementById('carrusel').style.top = pos +'px';
	else
		document.getElementById('carrusel').style.left = pos +'px';
}




var time;
var transparency = 0;

function fadeIn() {
  //
  //incrementamos el valor
  transparency += 5; 
  //si termino la trnsicion borramos el intervalo
  //seteamos al div como objeto para poder usarlo
  obj = document.getElementById('carrusel');
  obj.style.left = '0px';
  if (transparency == 100) {
	  posfinal = 0;
	  pos = 0;
	  clearInterval(time);
  }
//  transparency = (transparency == 100) ? clearInterval(time) : transparency;
  //programamos cada navegador por separado
  if (document.all) { 
    //esto es para IE, como siempre hay q programarlo a parte
    obj.style.filter = 'alpha(opacity='+transparency+')';
  } else {
    // Safari 1.2, posterior Firefox y Mozilla, CSS3
    obj.style.opacity = transparency /100;
    
    // anteriores Mozilla y Firefox
    obj.style.MozOpacity = transparency /100;
    
    // Safari anterior a 1.2, Konqueror
    obj.style.KHTMLOpacity = transparency /100;  
  } 
};



