/*************************************************
* HDCustom                                       *
**************************************************
* AlbumesDestacados v1.1                         *
**************************************************/

if (typeof HDModAlbumesDestacados != 'function') {

	function HDModAlbumesDestacados_playstop() {
		if (this.intId) {
			this.para();
		} else {
			this.dale();
		}		
	}

	function HDModAlbumesDestacados_para() {				

		if (this.intId) {						
			clearInterval(this.intId);			
			this.btnPlayStop.innerHTML = 'Reiniciar';
			this.btnPlayStop.className = 'btnPlayStop btnPlay';			
			this.intId = null;
		}
		
	}
	
	function HDModAlbumesDestacados_dale(s) {

		if (!this.iFoto) {
			this.iFoto = 0;
		}		
								
		this.verFoto(this.iFoto);
		
		if (this.intId) {
			clearInterval(this.intId);
		}
		
		this.intId = setInterval(new Function("getMod(document.getElementById('" + this.imgFoto.getAttribute('id') + "')).siguiente()") , this.v);								
		
		this.btnPlayStop.innerHTML = 'Detener';
		this.btnPlayStop.className = 'btnPlayStop btnStop';

		return true;
			
	}
	
	function HDModAlbumesDestacados_pasa(s) {
		
	}
	
	function HDModAlbumesDestacados_anterior() {				
		
		if (!this.AlbumesDestacados) return;
		
		if (this.iFoto == null || this.iFoto <= 0) {
			this.iFoto = this.AlbumesDestacados.length - 1;
		} else {
			this.iFoto--;
		}
		
		this.verFoto(this.iFoto);	
	}
	
	function HDModAlbumesDestacados_siguiente() {		
		
		if (!this.AlbumesDestacados) return;
		
		if (this.iFoto == null || this.iFoto >= (this.AlbumesDestacados.length - 1)) {
			this.iFoto = 0;
		} else {
			this.iFoto++;
		}
			
		this.verFoto(this.iFoto);	
	}
	
	function HDModAlbumesDestacados_avanza() {
		
		this.para();	
		this.siguiente();
		
	}
	
	function HDModAlbumesDestacados_rebobina() {
		this.para();
		this.anterior();
	}
	
	function HDModAlbumesDestacados_verFoto(i) {
	
		if (!this.AlbumesDestacados) return;
	
		var album = this.AlbumesDestacados[i];
	
		if (album) {			
			
			/*
	
			this.descripcion.innerHTML = ' ';
			this.destino.innerHTML = '';
			this.nombre.innerHTML = '';					
			this.img.src = this.img.alt = this.img.title = '';				
			*/
									
			this.nFoto.innerHTML = (i + 1);
			this.nFotos.innerHTML = this.AlbumesDestacados.length;
			
			this.destino.innerHTML = album.destino;
			this.nombre.innerHTML = album.nombre;
			this.descripcion.innerHTML = album.descripcion;
			
			if (this.url_salida) {					
				var url = addToURL(this.url_salida, 'AlbumId', album.id);
				this.lnkVer.href = addToURL(this.url_salida, 'AlbumId', album.id);
			} else {
				this.lnkVer.href = null;
			}
			
			if (album.url_foto) {
				
				this.img.src = album.url_foto;				
				
				if (1 || !this.img.complete) {
				
					document.addClass('cargando', this.imgFoto);
				
					this.img.onload = function() { document.removeClass('cargando', getMod(this).imgFoto); };							
					
				} else {
					
					document.removeClass('cargando', this.imgFoto);
					
				}
				
				
				this.img.alt = this.img.title = album.nombre;
				
				document.removeClass('vacia', this.imgFoto);
				
				
			} else {
				
				this.img.src = null;
				document.addClass('vacia', this.imgFoto);
				document.removeClass('cargando', this.imgFoto);			
			}													
									
		}
		
	}
	
	function HDModAlbumesDestacados_carga() {
			
		this.iFoto = 0;		
		
		function callback(oAjax, mod) {		
					
			if (oAjax.readyState == 4) {												
												
				albumesDestacados = oAjax.responseXML.documentElement.getElementsByTagName('Album');
				
				mod.AlbumesDestacados = new Array;
				
				if (albumesDestacados.length < 1) {
					// No hay fotos
				} else {
					
					mod.nFotos.innerHTML = albumesDestacados.length;
					//siguiente();
										
					for (var i = 0; i < albumesDestacados.length; i++) {
						
						var album = albumesDestacados[i];
						var a = new Object;
						
						a.id  = album.getElementsByTagName('Id').item(0).childNodes.item(0).nodeValue;	
						a.nombre = album.getElementsByTagName('Nombre').item(0).childNodes.item(0).nodeValue;
						
						try {
							a.destino = album.getElementsByTagName('Destino').item(0).childNodes.item(0).nodeValue;
						} catch(e) {
							a.destino = '';
						}
						
						try {
							a.nombre = album.getElementsByTagName('Nombre').item(0).childNodes.item(0).nodeValue;
						} catch(e) {
							a.nombre = '';
						}		
						try {
							a.descripcion = album.getElementsByTagName('Descripcion').item(0).childNodes.item(0).nodeValue;
						} catch(e) {
							a.descripcion = '';
						}				
						
						try {
							
							a.url_foto = album.getElementsByTagName('Foto').item(0).childNodes.item(0).nodeValue;							

							if (mod.img.offsetWidth && mod.img.offsetHeight) {		
								a.url_foto = addToURL(album.url_foto, 'w', mod.img.offsetWidth);
								a.url_foto = addToURL(album.url_foto, 'h', mod.img.offsetHeight);
							}
								
							
						} catch(e) {
							
							a.url_foto = false;
							
						}		
						
						if (a.url_foto) mod.AlbumesDestacados[i] = a;
						
					}				
					
					mod.verFoto(0);
					
				}
				
			}
		}				
		
		// LLamamos al método
		var oAjax = dameAjax();
		var mod = this;
		with (oAjax) {		
			open('get', this.url_ws, true);		
			onreadystatechange = function() {
				callback(oAjax, mod);
			};
			send(null);
		}
	}
	
	HDModAlbumesDestacados = function(mod_id, url_ws, url_salida, v, lon) {		

		var mod 			= getMod(document.getElementById(mod_id));

		mod.AlbumesDestacados = null;
		mod.iFoto 			= null;
		mod.intId 			= null;
		mod.url_ws 			= url_ws;
		mod.url_salida 		= url_salida;		
		mod.v 				= v || 1000;
		mod.btnPlayStop 	= document.getElementById(mod_id + '_btnPlayStop')[0];	
		mod.imgFoto 		= document.getElementById(mod_id + '_imgFoto')[0];

		mod.destino 		= document.getElementById(mod_id + '_destino')[0];
		mod.nombre 			= document.getElementById(mod_id + '_nombre')[0];
		mod.descripcion		= document.getElementById(mod_id + '_descripcion')[0];
		mod.img 			= mod.imgFoto.childNodes[0];
		mod.nFoto 			= document.getElementById(mod_id + '_nFoto')[0];
		mod.nFotos 			= document.getElementById(mod_id + '_nFotos')[0];		
		
		if (url_salida) {
			mod.lnkVer 		= document.getElementById(mod_id + '_lnkVer')[0].getElementsByTagName('a')[0];
		}				

		mod.carga 			= HDModAlbumesDestacados_carga;
		mod.verFoto 		= HDModAlbumesDestacados_verFoto;
		mod.rebobina 		= HDModAlbumesDestacados_rebobina;
		mod.avanza 			= HDModAlbumesDestacados_avanza;
		mod.siguiente 		= HDModAlbumesDestacados_siguiente;
		mod.anterior 		= HDModAlbumesDestacados_anterior;
		mod.dale 			= HDModAlbumesDestacados_dale;
		mod.para 			= HDModAlbumesDestacados_para;
		mod.playstop 		= HDModAlbumesDestacados_playstop;

		// Buffer
		imgBuffer = document.createElement('img');
		imgBuffer.style.display = 'none';		
		
		mod.imgBuffer 		= mod.imgFoto.appendChild(imgBuffer);

		mod.carga();
		
		mod.dale();
		
		return true;
	
	}
	
}
