﻿// Slideshow settings variable
var slideshow = {
	frame_duration: 4000,
	status: "playing",
	timeout: null
};

$(document).ready(function () {	
	// menu: hover background colour change
	try {
		$(".rtsLI").hover(
			function () {
				$(this).addClass("rtsLIhover");
			},
			function () {
				$(this).removeClass("rtsLIhover");
		}).click( function () { 
			location.href = $(this).find("a").attr("href");
		});
	}
	catch (err) {}
	
	try {
	// menu: add class to current and override TabStrip stupidity
		$(".rtsSelected").parent().addClass("current");
	}
	catch (err) {}
	
	
	$(".SearchResult:odd").addClass("stripe");
	
	// smooth scroll
	if ($.localScroll) {
		$.localScroll({
		   offset: -10
		});
	}	    

	// add the Adobe Reader icon when needed
	$(".case #content_main").append("<div id='get_reader'><p><a href='http://www.adobe.com/go/EN_US-H-GET-READER' title='Get Adobe Acrobat Reader'>Get Adobe Acrobat Reader</a>You need Adobe Acrobat Reader to view the articles above, which you can get by clicking this button. </p></div>");
	
	if ($("#carousel ul li").length > 0) {
		thumb_init();

		// add the play/pause button
		$("#imagery").prepend('<a href="#" id="play_pause" class="pause">play/pause</a>');

		// Setup initial image statuses
		$("#ul_imagery li:first img").css({ opacity: "1.0", display: "block" }).addClass("current").parent().addClass("current");
		$("#ul_imagery li:gt(0) img").css({ opacity: "0.0", display: "block" });
		$("#carousel li:first").addClass("current");
		
		$("#ul_imagery li:gt(0) img").css({ opacity: "0.0", display: "block" });
		
		$("#ul_imagery li img").load(function() {
			var img_height = $(this).height();
			var img_width = $(this).width();	
					
			//console.log(main_image[i] + i + ": " + img_height + " (height) * " + img_width + " (width)");
			
			if (img_height > img_width) {
				$(this).addClass("portrait");
				var spacer = (500 - img_width) / 2;			
				$(this).css("margin-left", spacer + "px");			
			} else if (img_height < img_width) {
				$(this).addClass("landscape");			
				var spacer = (500 - img_height) / 2;
				$(this).css("margin-top", spacer + "px");
			}
		});
		
		// change images when thumbnail is clicked
		$("#carousel li")
			.click(carousel_click);

		$("#play_pause")
			.click(play_pause_click);

		$("#carousel").jcarousel({
			animation: "fast",
			scroll: 3,
			visible: 6
		});
		
		slideshow.timeout = setTimeout(play_slideshow, slideshow.frame_duration);
	}

});


function thumb_init() {
	$("#carousel li img").preload({
		placeholder:'_Assets/css/Durham/carousel/loading-small.gif'
	});
}


function carousel_click() {
	var prod_img = $(this).find("img").attr("id").replace("thumb", "large"); 			// Image ID in the ul_imagery array

	change_image("#ul_imagery img.current", "#" + prod_img);

	clearTimeout(slideshow.timeout);
	
	slideshow.timeout = setTimeout(play_slideshow, slideshow.frame_duration);
	slideshow.status = "playing";
	$("#play_pause").addClass("pause");

	// add current class to selected image
	$(this).siblings(".current").removeClass("current");
	$(this).addClass("current");
	return false;
}


function play_pause_click() {
	switch(slideshow.status){
		case "playing":
			$("#play_pause").removeClass("pause");
			clearTimeout(slideshow.timeout);			
			slideshow.status = "paused";
			break;
			
		case "paused":
			$("#play_pause").addClass("pause");
			play_slideshow();
			slideshow.status = "playing";
			break;
	}
	
	return false;
}


function play_slideshow() {
	// Work out index of currently selected image
	var index = $("#ul_imagery li").index($("#ul_imagery li.current"));

	index = index == ($("#ul_imagery li").length - 1) ? 0 : index + 1;
			
	change_image("#ul_imagery img.current", "#ul_imagery li:eq(" + index + ") img", function() {
		// Sort out selected on carousel
		$("#carousel ul li.current")
		.removeClass("current")
		.next("li")
			.addClass("current");
	});	

	slideshow.timeout = setTimeout(play_slideshow, slideshow.frame_duration);	
}


function change_image(old_image, new_image, callback) {	
	if($(old_image).attr("id") != $(new_image).attr("id")) {
		$(old_image)
			.fadeTo(750, 0.0)
			.removeClass("current")
			.parent()
				.removeClass("current");

		$(new_image)
			.fadeTo(500, 1.0, callback)
			.addClass("current")
			.parent()
				.addClass("current");
	}
}