// value object for image set
function MPImgSet(setKey, navID, stageID)
{
	this.setKey = setKey;
	this.navID = navID;
	this.stageID = stageID;
	this.selectedClass = 'selected';
	this.selectedIndex = 0;
	this.links = [];
	this.imgTemplate = '<p><img src="[src]" alt="(image)" \/><\/p>';
	this.captTemplate = '<p class="caption">[caption]</p>';
	this.playTemplate = '<p class="play-pause"><a href="javascript:void(0);" onclick="[click]">play</a> &raquo;<\/p>';
	this.pauseTemplate = '<p class="play-pause"><a href="javascript:void(0);" onclick="[click]">pause</a> ||<\/p>';
	this.layoutTemplate = "[img]\n[caption]\n[playpause]";
	this.playStatus = false;
	this.playSpeed = 5000;
	this.playHander = false;
	this.animate = true;
	// load image set default selection and assign click events
	this.load = function()
	{
		this.activateLinks();
		this.detectSelected();
	};
	// activate links for slideshow
	this.activateLinks = function()
	{
		var allLinks = jQuery('a', jQuery('#'+this.navID));
		var i = 0;
		var c = allLinks.length;
		for (var x=0; x<c; x++)
		{
			jQuery(allLinks[x]).bind('click', {setKey: this.setKey}, function(e){
				e.preventDefault();
				var setKey = e.data.setKey;
				MPImgSets[setKey].pause();
				var thisTitle = jQuery(this).attr('title');
				if (thisTitle == 'previous') MPImgSets[setKey].loadPrevious();
				else if (thisTitle == 'next') MPImgSets[setKey].loadNext();
				else MPImgSets[setKey].loadLink(this);
			});
			var thisTitle = jQuery(allLinks[x]).attr('title');
			if (thisTitle != 'previous' && thisTitle != 'next') this.links.push(allLinks[x]);
		}
	};
	// determine current stage image source
	this.getCurrentImgSrc = function()
	{
		var currentImgSrc = jQuery('img:first', jQuery('#'+this.stageID)).attr('src');
		return currentImgSrc;
	};
	// determine index of currently active link
	this.getSelectedIndex = function()
	{
		var selectedIndex = -1;
		var fixURLs = false;
		var stageImg = this.getCurrentImgSrc();
		if (stageImg.match('://'))
		{
			fixURLs = true;
			stageImg = stageImg.split('resources/');
			stageImg = (stageImg.length == 2) ? stageImg[1] : '';
		}
		var c = this.links.length;
		for (var x=0; x<c; x++)
		{
			var linkImg = jQuery(this.links[x]).attr('href');
			if (fixURLs)
			{
				linkImg = linkImg.split('resources/');
				linkImg = (linkImg.length == 2) ? linkImg[1] : '!';
			}
			if (stageImg == linkImg)
			{
				selectedIndex = x;
				break;
			}
		}
		return selectedIndex;
	};
	// pause any currently playing image sets in group
	this.pauseAll = function()
	{
		for (setKey in MPImgSets)
		{
			MPImgSets[setKey].pause();
		}
	}
	// detect currently selected link and update class settings
	this.detectSelected = function()
	{
		jQuery('a', jQuery('#'+this.navID)).each(function(){
			jQuery(this).removeClass('selected');
		});
		var selectedIndex = this.getSelectedIndex();
		if (selectedIndex >= 0) jQuery(this.links[selectedIndex]).addClass('selected');
	};
	// load specified link
	this.loadLink = function(thisLink)
	{
		var imgSrc = jQuery(thisLink).attr('href');
		var caption = jQuery(thisLink).attr('title');
		var imgHTML = this.imgTemplate.replace('[src]', imgSrc);
		if (caption) var captHTML = this.captTemplate.replace('[caption]', caption);
		else captHTML = '';
		if (this.playTemplate && !this.playStatus)
			var playHTML = this.playTemplate.replace('[click]', 'MPImgSets[\''+this.setKey+'\'].play()');
		else if (this.pauseTemplate && this.playStatus)
			var playHTML = this.pauseTemplate.replace('[click]', 'MPImgSets[\''+this.setKey+'\'].pause()');
		else playHTML = '';
		var html = this.layoutTemplate;
		html = html.replace('[img]', imgHTML);
		html = html.replace('[caption]', captHTML);
		html = html.replace('[playpause]', playHTML);
		if (this.animate) jQuery('#'+this.stageID).hide().empty().html(html).fadeIn(500);
		else jQuery('#'+this.stageID).empty().html(html);
		this.detectSelected();
	};
	// load slide by index
	this.reset = function()
	{
		this.loadLink(this.links[0]);
	};
	// reload (refresh) current image
	this.reloadCurrent = function()
	{
		var selectedIndex = this.getSelectedIndex();
		if (selectedIndex >= 0) this.loadLink(this.links[selectedIndex]);
	};
	// activate next image link in set
	this.loadPrevious = function()
	{
		var selectedIndex = this.getSelectedIndex();
		var newIndex = selectedIndex - 1;
		if (newIndex < 0) newIndex = (this.links.length)-1;
		this.loadLink(this.links[newIndex]);
	};
	// activate next image link in set
	this.loadNext = function()
	{
		var selectedIndex = this.getSelectedIndex();
		// if (this.setKey == 'sampleImages') alert('selectedIndex: '+selectedIndex);
		var newIndex = selectedIndex + 1;
		if (newIndex > (this.links.length)-1) newIndex = 0;
		this.loadLink(this.links[newIndex]);
	};
	// start auto-rotation of images
	this.play = function()
	{
		this.pause();
		this.playStatus = true;
		// MPImgSets[this.setKey].playNext();
		MPImgSetTOHandler = setTimeout("MPImgSets['"+this.setKey+"'].playNext()", this.playSpeed);
	};
	this.playNext = function()
	{
		if (this.playStatus)
		{
			this.loadNext();
			MPImgSetTOHandler = setTimeout("MPImgSets['"+this.setKey+"'].playNext()", this.playSpeed);
		}
	};
	// stop auto-rotation of images
	this.pause = function()
	{
		if (MPImgSetTOHandler)
		{
			clearTimeout(MPImgSetTOHandler);
			MPImgSetTOHandler = false;
		}
		this.playStatus = false;
	};
}
// list of existing image sets (cache)
var MPImgSets = [];
var MPImgSetTOHandler = false;