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

var site_prefix;
var stage_images;
var stage_delay;
var stage_interval;

jQuery.fn.slideshow = function(slides, delay, interval) {

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

		var index = 0;
		var images = slides;
		var callback = false;
		var $target = $(this);
		var $source = $target.clone().insertAfter($target);

		function slideshow_preload(imgsrc, wait) {
			jQuery("<img>")
				.bind('load', function () {
					if (!callback) callback = setTimeout(slideshow_crossfade, wait);
				})
				.attr("src", imgsrc);
		}

		function slideshow_crossfade() {

			// crossfade images...

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

			var imgcss = 'url('+images[index]+')';
			$target.css('background-image', imgcss);
			$source.animate({opacity: 0}, 1000, function() {
				$source
					.css('background-image', imgcss)
					.css('opacity', 1);
			});
			var next = index + 1;
			if (next >= images.length) next = 0;
			callback = false;

			// preload next image

			slideshow_preload(images[next], interval);

		}

		if (images.length > 1) slideshow_preload(images[1], delay);
		else callback = setTimeout(slideshow_crossfade, delay);

	});

}

$(document).ready(function(){

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

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

	// set-up slideshows
	var delay = (typeof(stage_delay) != 'undefined') ? stage_delay : 5000;
	var interval = (typeof(stage_interval) != 'undefined') ? stage_interval : 7000;
	var step = interval;

	if ($('#stage span').length) {
		if (typeof(stage_images) != 'undefined' && stage_images.length > 1) {
			$('#stage span').empty();
			$('#stage span').slideshow(stage_images, delay, interval);
			delay += step;
		}
	}

	// set-up hotspots

	$('.hotspots li').removeClass('nojs');
	$('.hotspots a').hover(function () {
		$(this).siblings('.prod-detail.').fadeIn();
	}, function () {
		$(this).siblings('.prod-detail.').fadeOut();
	});

	if (!$.fn.flash.hasFlash(9.0)) return;

	$('#slideshowpro').flash({
		src: site_prefix+'swf/slideshowpro.swf',
		width: 542,
		height: 411,
		wmode: 'opaque',
		bgcol: '#000000',
		base: site_prefix,
		allowfullscreen: 'true',
		flashvars: {
			paramXMLPath: site_prefix+'swf/param.xml',
			initialURL: escape(document.location)
		}
	});

});