var slideshow =
{
	_chapterIndex: 		0,
	_isFirstIn: 		false,
	_itemDescriptions: 	[], 			// jk: an array of objects. should match the number of chapters in the xml. [{title:"Title",description:"Item description"}]
	_actionscript: 		{},
	_speed: 			200,
	
	initialize: function($external_interface_proxy,$itemDescriptions)
	{
		slideshow._actionscript 		= $external_interface_proxy;
		slideshow._itemDescriptions 	= $itemDescriptions;

		slideshow._actionscript.addReceiver("chapterComplete",slideshow.chapterNext);
		slideshow._actionscript.addReceiver("chapterPrevious",slideshow.chapterPrevious);
		slideshow._actionscript.addReceiver("chapterSetItem",slideshow.itemSetText);
		
		// jk: init to first item	
		slideshow._chapterIndex = 0;
		slideshow.itemSetText({itemIndex: 0});

		$("#_slideshowNav li").click(function()
		{
			slideshow.goToChapter($("#_slideshowNav li").index($(this)));
			if( self.swapImages ) swapImages();
		});
	},
	
	chapterNext: function($data) {
		if(slideshow._chapterIndex < slideshow._itemDescriptions.length-1)
			slideshow.goToChapter(slideshow._chapterIndex+1);
		if( self.swapImages )
			swapImages();
	},

	chapterPrevious: function($data) {
		if(slideshow._chapterIndex > 0)
			slideshow.goToChapter(slideshow._chapterIndex-1);
		if( self.swapImages )
			swapImages();
	},

	itemSetText: function($data)
	{
		if(slideshow._isFirstIn)
		{
			var obj = slideshow._itemDescriptions[slideshow._chapterIndex][$data.itemIndex];
				if(obj.title) { $("#_title").fadeOut(slideshow._speed,function() {
						$("#_title").html(obj.title);
						$("#_title").fadeIn(slideshow._speed,function() {
							// IE hack for disappearing titles
							$("#_title").css("display", "block");
						});		
					});
				}
		
				$("#_description").fadeOut(slideshow._speed,function() {
					$("#_description").html(obj.description);
					$("#_description").fadeIn(slideshow._speed);
				});
		}

		slideshow._isFirstIn = true;
	},

	goToChapter: function($chapter)
	{
		$("#_slideshowNav li:eq(" + slideshow._chapterIndex + ")").removeClass("active");
		slideshow._chapterIndex = $chapter;
		$("#_slideshowNav li:eq(" + slideshow._chapterIndex + ")").addClass("active");
		slideshow._actionscript.sendAS("chapterSelect",{ chapterIndex: slideshow._chapterIndex });
	}
};