window.onload = initImageGallery;

var Swap_Gallery_Interval = 4000;
var Swap_Gallery_Speed = 10;
var Swap_Gallery_Index = -1;
var Swap_Gallery_Images = new Array();
var Swap_Gallery_Opacity;


function showGallery()
{
	Swap_Gallery_Opacity = 100;
	Swap_Gallery_Index = (Swap_Gallery_Index == Swap_Gallery_Images.length - 1)? 0 : Swap_Gallery_Index + 1;
	var next_image = (Swap_Gallery_Index + 1 == Swap_Gallery_Images.length)? 0 : Swap_Gallery_Index + 1;
	
	Swap_Gallery_Images[next_image].style.display = 'block';

	revealImage();
}

function revealImage()
{
	var next_image = (Swap_Gallery_Index + 1 == Swap_Gallery_Images.length)? 0 : Swap_Gallery_Index + 1;
	
	Swap_Gallery_Opacity--;
	
	if (document.all)
	{
		Swap_Gallery_Images[Swap_Gallery_Index].style.filter = 'alpha(opacity='+Swap_Gallery_Opacity+')';
		Swap_Gallery_Images[next_image].style.filter = 'alpha(opacity='+(100-Swap_Gallery_Opacity)+')';
	}
	else
	{
		Swap_Gallery_Images[Swap_Gallery_Index].style.opacity = Math.max(0.01,Swap_Gallery_Opacity/100);
		Swap_Gallery_Images[next_image].style.opacity = Math.min(0.99,(1 - (Swap_Gallery_Opacity/100)));
	}
	
	if (Swap_Gallery_Opacity > 0)
		setTimeout('revealImage()', Swap_Gallery_Speed);
	else
	{
		Swap_Gallery_Images[Swap_Gallery_Index].style.display = 'none';	
		setTimeout('showGallery()', Swap_Gallery_Interval);
	}
}

function initImageGallery()
{
	var container = document.getElementById('imagen_destacada');
	
	container.getElementsByTagName('img')[0].setAttribute('id', 'galleryImage0');
	Swap_Gallery_Images[Swap_Gallery_Images.length] =  container.getElementsByTagName('img')[0];
	
	for (var x = 1; x < Swap_Gallery.length; x++)
	{
		var img = document.createElement('img');
		img.setAttribute('id', 'galleryImage' + x);
		img.setAttribute('src', '../img/' + Swap_Gallery[x][0]);
		img.setAttribute('alt', Swap_Gallery[x][2]);
		img.setAttribute('title', Swap_Gallery[x][2]);
		img.setAttribute('width', '428');
		img.setAttribute('height', '268');
		img.style.display = 'none';
		
		container.appendChild(img);
		
		Swap_Gallery_Images[Swap_Gallery_Images.length] =  img;
	}

	setTimeout('showGallery()', Swap_Gallery_Interval);
}
