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

var stage_images;
var stage_delay;
var stage_interval;

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

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

		var index = 0;
		var images = slides;
		var callback = false;
		var $target = $(this);
		var $source = $target.clone().css({margin: 0, left: 0, top: 0}).prependTo($target);
		$source.empty();

		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}, 1500, 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();

	// initialise lightbox

	if (typeof($.fn.prettyPhoto) == 'function') {
		$("a[rel^='lightbox']").prettyPhoto({theme: 'light_square'});
		$(".lightbox-start").click(function() {
			$("a[rel^='lightbox']:first").click();
			return false;
		});
	}

	// set-up slideshows
	var delay = (typeof(stage_delay) != 'undefined') ? stage_delay : 5000;
	var interval = (typeof(stage_interval) != 'undefined') ? stage_interval : 5000;
	var step = interval;
	if (typeof(stage_images) != 'undefined' && stage_images.length > 1) {
		$('#stage span').slideshow(stage_images, delay, interval);
		delay += step;
	}

	// flash text

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

	$('body').addClass('hasFlash');

	var swffile = (typeof(fontfile) == 'undefined') ? false : fontfile;
	var swfcol = (typeof(fontcol) == 'undefined') ? '#ffffff' : fontcol;
	function swfreplace(htmlOptions) {
			htmlOptions.flashvars.txt = this.innerHTML.replace('+', '%2B');
			this.innerHTML = '<div>'+this.innerHTML+'</div>';
			var $alt = $(this.firstChild);
			htmlOptions.height = $alt.height();
			htmlOptions.width = $alt.width();
			$alt.addClass('alt');
			$(this)
				.addClass('flash-replaced')
				.prepend($.fn.flash.transform(htmlOptions));	
	}

	$('#content h1').flash(
		{
			src: swffile,
			wmode: 'opaque',
			bgcolor: '#94a333',
			flashvars: {
				css: '* { color: #ffffff; leading: 6px; }'
			}
		},
		{ version: 7 },
		swfreplace
	);
	$('#content h2').flash(
		{
			src: swffile,
			wmode: 'opaque',
			bgcolor: '#94a333',
			flashvars: {
				css: '* { color: #ffffff; }'
			}
		},
		{ version: 7 },
		swfreplace
	);

});