var galleries=new Array();
var videos=new Array();
var intervals=new Array();

// Position trackers
var pos=0;

// Image object
function Image(src,alt)
{
	this.src=src;
	this.alt=alt;
}

// Video object
function Video(src,alt,txt)
{
	this.src=src;
	this.alt=alt;
	this.txt=txt;
}

// Gallery object
function Gallery()
{
	this.pos=0;
	this.images=new Array();
}

// Add an image to the end of the array
function addImage(src,alt,gallery)
{
	if (galleries[gallery]==null)
		galleries[gallery]=new Gallery();
	var gal=galleries[gallery];
	gal.images[gal.images.length]=new Image(src,alt);
}

// Add an image to the end of the array
function addVideo(src,alt,gallery,subtext)
{
	if (videos[gallery]==null)
		videos[gallery]=new Gallery();
	var gal=videos[gallery];
	gal.images[gal.images.length]=new Video(src,alt,subtext);
}

// Change the image
function changeImage(id,gallery)
{
	// Change the image
	var gal=galleries[gallery];
	var image=document.getElementById(id);
	if (image!=null)
	{
		image.filters[0].apply();
		if (gal.pos>=gal.images.length)
			gal.pos=0;
		image.src=gal.images[gal.pos].src;
		if (gal.images[gal.pos].alt!=null)
			image.alt=gal.images[gal.pos].alt;
		image.filters[0].play();
		gal.pos++;
	}
}

// Reset the array
function resetImages()
{
	galleries=new Array();
	videos=new Array();
	stopAllIntervals();
	intervals=new Array();
}

// Stop all intervals
function stopAllIntervals()
{
	for (i in intervals)
		clearInterval(intervals[i]);
}

// Run the slideshow
function runSlideshow(id,gallery)
{
	if(galleries[gallery]!=null)
	{
		changeImage(id,gallery);
		intervals[gallery]=setInterval('changeImage(\''+id+'\',\''+gallery+'\');',10000);
	}
}

// Stop a slideshow
function stopSlideshow(gallery)
{
	clearInterval(intervals[gallery]);
	intervals[gallery]=null;
}

// List the events
function listGalleries(isVideo)
{
	var html="";

	for (i in events)
	{
		html +=	'<div class="indent" id="'+i+'">'
		+	'<a href="#" onclick="openGalleryCategory(\''+i.replace("'","\\'")+'\','+isVideo+');return false;">open</a>&nbsp;'
		+	i
		+	'</div>';
	}
	return html;
}

// Open an event category
function openGalleryCategory(id,isVideo)
{
	var html=	'<a href="#" onclick="closeGalleryCategory(\''+id.replace("'","\\'")+'\','+isVideo+');return false;">close</a>&nbsp;'
		+	id
		+	'<br/><br/>';

	for (i in events[id])
	{
		var event=events[id][i];
		
		var galleryImages=null;
		if (isVideo)
		{
			if (videos[event.title]!=null)
				galleryImages=videos[event.title].images;
		}
		else
		{
			if (galleries[event.title] != null)
				galleryImages=galleries[event.title].images;
		}

		if (galleryImages!=null && galleryImages.length>0)
		{
			html+=	'<div class="indent">'
			+			'<b>'+event.title+'</b><br/><br/>'
			+			event.text + '<br/>'
			+			'<img class="slideshow" id="gallery' + i + '" src="' + galleryImages[0].src + '" alt="' + galleryImages[0].alt + '" width="600"/><br/><br/>';

			for (j in galleryImages)
			{
				if (j>0 && j%10==0 & !isVideo)
					html +='<br/>';

				if (!isVideo)
					html += '<a href="#" onclick="displayImage(\'' + event.title + '\',\'' + j + '\',\'' + i + '\');return false;"><img src="' + galleryImages[j].src + '" alt="' + galleryImages[j].alt + '" width="50"/></a>&nbsp;';
				else
					html += '<a href="' + galleryImages[j].src + '"><img src="images/play.gif" alt="play video - ' + galleryImages[j].txt + '"/></a>&nbsp;' + galleryImages[j].txt + '<br/>';
			}

			html +=	'</div><br/>';
		}
	}
	document.getElementById(id).innerHTML=html;
}

// Display an image
function displayImage(gallery,index,id)
{
	var image = document.getElementById('gallery' + id);
	var newImage = galleries[gallery].images[index];
	if (image != null)
	{
		image.filters[0].apply();
		if (newImage.src!=null)
			image.src = newImage.src;
		if (newImage.alt!=null)
			image.alt = newImage.alt;
		image.filters[0].play();
	}
}

// Close an event category
function closeGalleryCategory(id,isVideo)
{
	var html=	'<a href="#" onclick="openGalleryCategory(\''+id.replace("'","\\'")+'\','+isVideo+');return false;">open</a>&nbsp;'
		+	id;

	document.getElementById(id).innerHTML=html;
}

function openPhotoGallery()
{
	document.getElementById('galleries').innerHTML = listGalleries(false);
}

function openVideoGallery()
{
	document.getElementById('galleries').innerHTML = listGalleries(true);
}