//Slideshow variables
var items, textItems, CurrentImage, CurrentTitle, CurrentDescription, Next, Previous, CurrentLink;
var index, maxIndex, previousIndex;
var images, links;

$(document).ready(init);
function init()
{
	//Slideshow setup
	items = $('.sideGallery div');
	index = 0;
	previousIndex = 0;
	maxIndex = items.length;
	//images = $('.sideGallery div .full');
	links = $('.sideGallery div a');
	
	//Hide all slides except the first
	for (var x=0; x<maxIndex; x++)
	{
		items.get(x).id = 'slide'+x;
		$('#' + items.get(x).id).hide();
		$('#' + items.get(x).id + ' .full').remove().insertBefore('#' + items.get(x).id + ' h1');
//		$('#' + items.get(x).id + ' .full')
	}
	$('#' + items.get(index).id).show();
	
	//Set up link
	$('.sideGallery div .full').bind('click', followLink);
	
	
	//Set up filmStrip view
	$('.sideGallery').append('<div id="filmStripContainer"></div>');
	//filmStrip
	$('#filmStripContainer').append('<div id="filmStripBack"></div>');
	$('#filmStripContainer').append('<div id="filmStrip"></div>');
    

	if (!textdisplay)
	{
	    $('.sideGallery div .thumb').appendTo('#filmStrip');
	    //thumbnails
	    for (var x=0; x<maxIndex; x++)
		    $('#filmStrip .thumb').get(x).index = x;
	    $('#filmStrip .thumb').click(function(){set($(this).get(0).index);});
	    $("#filmStrip .thumb").wrap("<div class='wrap0'><div class='wrap1'><div class='wrap2'>" +
         "<div class='wrap3'></div></div></div></div>");
	}
	else
	{
		$('.sideGallery div a').appendTo('#filmStrip');
	    $('#filmStrip a').removeAttr("href");
		textItems = $('.sideGallery div a');
	    for(x=0; x<maxIndex; x++)
	         $('#filmStrip a').get(x).index = x;
	    $('#filmStrip a').click(function(){set($(this).get(0).index);});
	     /*   $('#'+items.get(x).id+' a').get(0).index = x;
	        $('#'+items.get(x).id+' a').removeAttr("href");
	        $('#'+items.get(x).id+' a').click(function(){set(x);});*/
        $('.sideGallery div a').show();
	}
	 $(".full").wrap("<div class='wrap0'><div class='wrap1'><div class='wrap2'>" +
         "<div class='wrap3'></div></div></div></div>");
	//Setup user defined width and height
	$('#filmStripContainer').css("width", thumbWidth + 40);
	$('#filmStripContainer').css("height", maxHeight + 50);
	/*$('.sideGallery').css("width", maxWidth + 50);
	$('.sideGallery').css("height", maxHeight + 72);
	$('.sideGallery div p').css("width", maxWidth + 30);
	$('#Next, #Previous').css("top", (maxHeight + 72)/2-24);
	$('#filmStrip, #filmStripBack').css("height", thumbHeight + 28);
	*/
}

function set(NewIndex)
{
	previousIndex = index;
	index = NewIndex;
	$('#' + items.get(previousIndex).id + ' h1').hide();
	$('#' + items.get(previousIndex).id + ' p').hide();
	$('#' + items.get(previousIndex).id).fadeOut(800, function(){
			$('#' + items.get(index).id).fadeIn(800, function() {
				$('#' + items.get(index).id + ' p').show();
				//if (!textdisplay)
				    $('#' + items.get(index).id + ' h1').show();
			});
		});
}

function followLink()
{
	window.location = links.get(index).href;
}