//<!--    Open comment for non-compatible browsers

// JavaScript Document

FADE_OUT_SPEED = 300;
FADE_IN_SPEED = 300;
QUOTE_TIMER = 7000;
var galleryImageDisplayed = '';
var pageName = "";
var imageCount = 0;
var fileName = "";
var firstTimeForSideSlider = true;



$(document).ready(function(){
	$('#mainImage').cycle({ 
		timeout:       	5000,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         	2000,  // speed of the transition (any valid fx speed value) 
		delay:			-2500
	});
	$('#quote1').cycle({ 
		timeout:       	QUOTE_TIMER,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         	2000,  // speed of the transition (any valid fx speed value) 
		delay:			-2500
	});
	$('#quote2').cycle({ 
		timeout:       	QUOTE_TIMER,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         	2000,  // speed of the transition (any valid fx speed value) 
		delay:			-2500
	});
	$('#quote3').cycle({ 
		timeout:       	QUOTE_TIMER,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         	2000,  // speed of the transition (any valid fx speed value) 
		delay:			-2500
	});
	$('#quote4').cycle({ 
		timeout:       	QUOTE_TIMER,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         	2000,  // speed of the transition (any valid fx speed value) 
		delay:			-2500
	});
	$('#quote5').cycle({ 
		timeout:       	QUOTE_TIMER,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         	2000,  // speed of the transition (any valid fx speed value) 
		delay:			-2500
	});
	$('#quote6').cycle({ 
		timeout:       	QUOTE_TIMER,  // milliseconds between slide transitions (0 to disable auto advance) 
		speed:         	2000,  // speed of the transition (any valid fx speed value) 
		delay:			-2500
	});
	launchGallerySelector();
	launchPrevNext();
});


function launchGallerySelector(){
	//Count the number of images
	$("#thumbs li").each(function(){
		imageCount ++;
	});
	//alert("Number of images: " + imageCount);
	
	//pageName = getFolderName();
	//galleryImageDisplayed = 'gallery/' + pageName +'/'+ pageName+'_' + 1 + '.jpg';
	galleryImageDisplayed = $("#chosenImage img").attr("id");
	//alert("Gallery Displayed = " + galleryImageDisplayed);
	
	$("#thumbs a").click(function(){
		var nextImage = $(this).attr("href");
		fileName = nextImage;
		galleryImageDisplayed = $(this).parent().attr("id");
		
		var nextImagePath = '<img src="'+ nextImage +'" alt="" height="400" />';
		
		var caption = $(this).attr('title');
		
		if ($(this).parent().hasClass('group')){
			groupImageDisplay(nextImagePath, caption, $(this).parent().attr('class'));
		}
		else {
			standardImageDisplay(nextImagePath, caption);
		}
		return false;
	});

}

function launchPrevNext(){
	$("#prevNext a").click(function(){
		var selected = $(this).attr('title')
		
		//Work out the next image to display
		
		//Get the current image NUMBER
		var currentImage = parseInt(galleryImageDisplayed);
		
		if (selected.indexOf('prev') != -1){
			var newImage = currentImage - 1;
			if (newImage < 1){
				newImage = imageCount;
			}
		} else {
			var newImage = currentImage + 1;
			if (newImage > imageCount){
				newImage = 1;
			}
		}
		
		galleryImageDisplayed = newImage;
		var nextImagePath = "";
		var caption = "";

		$("#thumbs li").each(function(){
			if ($(this).attr("id") == newImage){
				caption = $(this).children().attr("title");
				nextImagePath = '<img src="' + $(this).children().attr("href") + '" alt="" height="400"/>';
				if ($(this).hasClass('group')){
					groupImageDisplay(nextImagePath, caption, $(this).attr('class'));
				}
				else {
					standardImageDisplay(nextImagePath, caption);
				}
				return false;
			}
		});
		return false;
	});
}

function getFolderName(){
	var src = $("#chosenImage img").attr("src");
	if (src != null){
		var temp = src.split("/")
		return temp[1];
	}
}

function splitImageString(imgString){
	var temp = imgString.split("_");
	var temp2 = temp[1].split(".");
	return temp2[0];
}

function standardImageDisplay(path, caption){
	$("#caption").fadeOut(FADE_OUT_SPEED);
		$("#chosenImage").fadeOut(FADE_OUT_SPEED,function(){
			$("#caption").html(caption);
			$("#chosenImage").html('<img style="margin: 200px 0px 0px 0px;" src="images/spinner.gif" />');
			$("#chosenImage").show();
			$(path).load(function(){
				$("#chosenImage").hide()
				$("#chosenImage").html(path);
				$("#chosenImage").fadeIn(FADE_IN_SPEED);
				$("#caption").fadeIn(FADE_IN_SPEED);
			});
		});
}

//-->
