var currentGalleryItem = 0;
var galleryInterval;
var galleryIntervalTime = (8 * 1000);
var fadeTime = 400	// ms

function galleryItem(type, imgLarge, imgThumb, title, rTitle, caption, url)	{
	this.type = type
	this.imgThumb = imgThumb
	this.imgLarge = imgLarge
	this.title = title
	this.rTitle = rTitle
	this.caption = caption
	this.url = url
}

function startGallery()	{	

	// Set interval so the images cycle
	galleryInterval = setInterval('nextImage()' , galleryIntervalTime)

	// Get the proper template for asset type
	tpl = getTemplate(currentGalleryItem);

	// Fill template with data
	insertTemplate(tpl);

	// squirt in first record
	thisItem = fillGalleryItem(currentGalleryItem);

	// Squirt item into page
	$('#highlight').html(thisItem);

}

function kickStart()	{
	// Start the cycle again
	nextImage();
	galleryInterval = setInterval('nextImage()' , galleryIntervalTime);
}

function nextImage()	{
	// set the array index
	currentGalleryItem = currentGalleryItem + 1;

	if (currentGalleryItem > (arrGallery.length  - 1))	{
		currentGalleryItem = 0;
	}

	$('#galleryImg').fadeTo(fadeTime, 0).queue(function()	{
		thisItem = fillGalleryItem(currentGalleryItem);
		$('#galleryImg').fadeTo(fadeTime, 1);
		$('#galleryImg').dequeue();
	});
/*
	$('#galleryImg').queue(function()	{
		$('#galleryImg').fadeTo(fadeTime, 0);
		$('#galleryImg').dequeue();
	});
	$('#galleryImg').queue(function()	{
		thisItem = fillGalleryItem(currentGalleryItem);
		$('#galleryImg').fadeTo(fadeTime, 1);
		$('#galleryImg').dequeue();
	});
*/
}



function getTemplate(arrID)	{
	//alert(arrID)
	if (arrGallery[arrID].type == 0)		{	// Img
		return imgTpl;
	}	else	{								// video
		// stop it ticking
		window.clearInterval(galleryInterval);

		return videoTpl;
	}
}

function insertTemplate(tpl)	{

	gi = tpl

	gi = gi.replace('%%img%%', arrGallery[0].imgLarge);
	gi = gi.replace('%%thumb%%', arrGallery[0].imgThumb);
	gi = gi.replace('%%rFocus%%', arrGallery[0].rTitle);
	gi = gi.replace('%%title%%', arrGallery[0].title);
	gi = gi.replace('%%copy%%', arrGallery[0].caption);

	gi = gi.replace('%%linkCap%%', 'Read more');
	gi = gi.replace('%%url%%', arrGallery[0].url);

	gi = gi.replace('%%img%%', arrGallery[0].imgLarge);
	gi = gi.replace('%%thumb%%', arrGallery[0].imgThumb);

	// Squirt item into page
	$('#highlight').html(gi);
}

function fillGalleryItem(arrItem)	{
	//alert(arrGallery[arrItem].imgLarge);
	$('#galleryImg').attr('src', arrGallery[arrItem].imgLarge);
	$('#gFocus').html(arrGallery[arrItem].rTitle);
	$('#gTitle').html(arrGallery[arrItem].title);
	$('#gCopy').html(arrGallery[arrItem].caption);
	$('#gURL').html('<a href="'+arrGallery[arrItem].url+'">Read more</a>');
}
