//*************** CONFIGURACIÓN ***************//

//***** Ruta de las fotografías *****//
var _RUTA_FOTOS = "imagenes/";	// guarda la ruta (relativa) donde se encuentran las fotografías.
							// Por defecto, slide_fotos

//***** Rutas de las imágenes del slideshow y de las css *****//
var _SLIDE_IMG = "imagenes/";
var _SLIDE_CSS = "estilos.css/";

//***** Nombre del campo imagen a utilizar, por defecto *****//
var _SLIDE_NOMBRE_IMG = "_SLIDE_NOMBRE_IMG";

//***** Modos del slide *****//
var _MODO_SLIDE = "normal";	// normal - Solo anterior y siguiente
						// full_mode - Muestra todos los controles

//***** velocidad del slide *****//
var _VELOCIDAD_SLIDE = 1000;	// en milisegundos

//******************* MOTOR *******************//
// constructor
function Slideshow(nombre_objeto){
	if(nombre_objeto == null){
		this.nombre_objeto = "mySlide";
	}else{
		this.nombre_objeto = nombre_objeto;
	}
	this.lista_imagenes = new Array(); // lista de imágenes a usar
	this.posicion = 0; // posición de inicio
	// métodos
	this.agregar_imagen = agregar_imagen;
	this.en_marcha = false;
	
	this.siguiente = siguiente;
	this.anterior = anterior;
	this.primera = primera;
	this.ultima = ultima;
	this.auto = auto;
	this.stop = stop;
	
	this.crear_slide = crear_slide;
}

// agregar imagen
// se pueden agragar varias imágenes a la vez, separadas por comas (,)
function agregar_imagen(lista){
	for(i = this.lista_imagenes.length; i < agregar_imagen.arguments.length; i++){
		this.lista_imagenes[i] = agregar_imagen.arguments[i];
	}
}

// anterior y siguiente
function siguiente(){
	this.posicion++;
	if(this.posicion >= this.lista_imagenes.length){
		this.posicion = 0;
	}
	document.getElementById(this.nombre_imagen).src = _RUTA_FOTOS + this.lista_imagenes[this.posicion];
}

function anterior(){
	this.posicion--;
	if(this.posicion < 0){
		this.posicion = this.lista_imagenes.length - 1;
	}
	document.getElementById(this.nombre_imagen).src = _RUTA_FOTOS + this.lista_imagenes[this.posicion];
}

// primera y última
function primera(){
	this.posicion = 0;
	document.getElementById(this.nombre_imagen).src = _RUTA_FOTOS + this.lista_imagenes[this.posicion];
}

function ultima(){
	this.posicion = this.lista_imagenes.length - 1;
	document.getElementById(this.nombre_imagen).src = _RUTA_FOTOS + this.lista_imagenes[this.posicion];
}

// stop y auto
function auto(){
	this.en_marcha = true;
	if( this.posicion >= this.lista_imagenes.length-1 ){
		this.posicion = 0;
	}else{
		this.posicion++;
	}
	document.getElementById(this.nombre_imagen).src = _RUTA_FOTOS + this.lista_imagenes[this.posicion];
	slide_id = setTimeout(this.nombre_objeto + ".auto()", _VELOCIDAD_SLIDE);
}

function stop(){
	if( this.en_marcha )
		clearTimeout(slide_id);
}
// crear slide
// crea el slide con todos sus comportamientos
function crear_slide(){
	salida = "";
	salida = "<table border='0' cellspacing='0' cellpadding='0' align='center'>";
	salida += "<tr>";
	salida += "<td align='center' valign='middle'";
	if( _MODO_SLIDE == "normal" )	{
		 salida += " colspan='2'>";
	}else{
		salida += " colspan='6'>";
	}
	if(this.lista_imagenes.length == 0 || this.lista_imagenes == ''){
		salida += "<b>No Image</b>";
	}else{
		salida += "<img border='0' id='"+ this.nombre_imagen +"' src='" + _RUTA_FOTOS + this.lista_imagenes[this.posicion] +"'>";
	}
	salida += "</td>";
	if(this.lista_imagenes.length > 1){
		// si hay imágenes definidas
		salida += "</tr>";
		// botón de primera
		if(_MODO_SLIDE == "full_mode" ){
			salida+= "<tr><td height='15'></td></tr>";
			salida+= "<tr valign='middle' width='319' height='19'>";
			salida += "<td align='center' valign='middle'>";
			salida += "<a id='primera' href='#' onClick='" + this.nombre_objeto + ".primera(); return false;'><img src='images/arrow_first.gif' border='0'></a>";
			salida +="</td>";
		}
		// botones de anterior y siguiente
		salida += "<td align='center'>";
		salida += "<a id='anterior' href='#' onClick='" + this.nombre_objeto + ".anterior(); return false;'><img src='images/arrow_l.gif' border='0'></a>";
		salida += "</td>";
		salida += "<td align='center'>";
		salida += "<a id='siguiente' href='#' onClick='" + this.nombre_objeto + ".siguiente(); return false;'><img src='images/arrow_r.gif' border='0'></a>";
		salida += "</td>";
		// botón de última
		if(_MODO_SLIDE == "full_mode" ){
			salida += "<td align='center'>";
			salida += "<a id='ultima' href='#' onClick='" + this.nombre_objeto + ".ultima(); return false;'><img src='images/arrow_last.gif' border='0'></a>";
			salida += "</td>";
		}
		// controles de reproducción automática
		if(_MODO_SLIDE == "full_mode" ){
			salida += "<td align='center'>";
			salida += "<a id='auto' href='#' onClick='" + this.nombre_objeto + ".auto();'><img src='images/arrow_play.gif' border='0'></a>";
			salida += "</td>";
			
			salida += "<td align='center'>";
			salida += "<a id='stop' href='#' onClick='" + this.nombre_objeto + ".stop();'><img src='images/arrow_stop.gif' border='0'></a>";
			salida += "</td>";
		}
		salida += "</tr>";
	}
	salida += "</table>";
	
	document.writeln(salida);
}

//***** FIN *****//
