/*
 * Subject to copyright.
 *
 * Web Development - LOOKsystems Limited
 * mailto:info@looksystems.ltd.uk
 * http://www.looksystems.ltd.uk
 *
 */

var stage_delay = 10000;
var stage_interval = stage_delay;
var stage_default_caption = '';
var stage_offset = 0;
var stage_captions;
var stage_images;

function stage_slideshow() {

	if (!stage_images || stage_images.length < 1) return;

	var delay = (stage_delay > 0) ? stage_delay : 10000;
	var interval = (stage_interval > 0) ? stage_interval : stage_delay;
	var index = (stage_offset >= 0 && stage_offset < stage_images.length) ? stage_offset : 0;
	var images = stage_images;
	var captions = stage_captions;
	var caption_default = stage_default_caption;

	var callback = false;
	var $image = $('#stage .image a');
	if (!$image.length) $image = $('#stage .image');
	var $caption = $('#stage .caption a');
	if (!$caption.length) $caption = $('#stage .caption');

	function slideshow_preload(imgsrc, wait) {
		var start = new Date;
		jQuery("<img>")
			.bind('load', function () {
				if (!callback) {
					var now = new Date();
					var preloadTime = parseInt(now.getTime()) - parseInt(start.getTime());
					if (preloadTime < 0) preloadTime = 0;
					if (preloadTime < wait) callback = setTimeout(slideshow_crossfade, wait - preloadTime);
					else slideshow_crossfade();
				}
			})
			.attr("src", imgsrc);
	}

	function slideshow_crossfade() {

		// crossfade images...

		++index;
		if (index >= images.length) index = 0;

		var imgsrc = images[index];
		var caption = (typeof(captions[index]) != 'undefined') ? captions[index] : ''; 
		if (!caption) caption = caption_default;
		$caption.html(caption);
		$image.css('background-image', 'url('+imgsrc+')');
		$image.children('img').animate({opacity: 0}, 1500, function() {
			$(this).attr('src', imgsrc);
			$(this).css('opacity', 1);
		});
		var next = index + 1;
		if (next >= images.length) next = 0;
		callback = false;

		// preload next image

		slideshow_preload(images[next], interval);

	}

	slideshow_preload(images[1], delay);

}

/*
jQuery.fn.quoteshow = function(quotes, delay, interval) {

	if (!delay) delay = 2500;
	if (!interval) interval = 5000;
	return this.each(function() {

		var index = 0;
		var quotelist = quotes;
		var callback = false;
		var $image = $(this);

		function quote_crossfade() {

			++index;
			if (index >= quotelist.length) index = 0;

			$image.children('p').
				fadeOut('slow', function() {
					$image.children('.source').html(quotelist[index][0]);
					$image.children('.citation').html('&#8220;'+quotelist[index][1]+'&#8221;');
					$(this).fadeIn('slow');
				});
			if (!callback) callback = setInterval(quote_crossfade, interval);

		}

		setTimeout(quote_crossfade, delay);

	});

}
*/

$(document).ready(function(){

	// set-up slideshow
	if (typeof(stage_images) != 'undefined') stage_slideshow();

	// set focus
	var $input = $("input[type='text'],input[type='password'],textarea");
	if ($input.length) $input.get(0).focus();

	// initialise legacy script
	if (typeof(init) == 'function') init();

});