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

if (typeof site_prefix == 'undefined') var site_prefix;
if (typeof stage_left == 'undefined') var stage_left;
if (typeof stage_middle == 'undefined') var stage_middle;
if (typeof stage_right == 'undefined') var stage_right;
if (typeof stage_delay == 'undefined') var stage_delay;
if (typeof stage_interval == 'undefined') 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}, 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(){

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

	if (typeof(stage_left) != 'undefined') {
		if (typeof(stage_middle) != 'undefined') interval += step;
		if (typeof(stage_right) != 'undefined') interval += step;
		$('#mainstage span').slideshow(stage_left, delay, interval);
		delay += step;
	}
	if (typeof(stage_middle) != 'undefined' && stage_middle.length) {
		$('#stage .stagemiddle span').slideshow(stage_middle, delay, interval);
		delay += step;
	}
	if (typeof(stage_right) != 'undefined' && stage_right.length) {
		$('#stage .stageright span').slideshow(stage_right, delay, interval);
	}

	// initialise lightbox
	if (typeof($.fn.prettyPhoto) == 'function') {

		$("a[rel^='lightbox']").prettyPhoto({
			theme: 'light_square'
		});

		$(".lightwindow").prettyPhoto({
			width: 800,
			height: 600,
			theme: 'light_square',
			iframe: true
		});
	}

	// click blocks
	$('.clickblock,.cottagelist li').click(function(e) {
		e.preventDefault();
		href = $(this).find('a').attr('href');
		if (href) document.location = href;
	});

	// gallery slider
	$('.gallery').each(function() {
		
		var scroll = 127;
		var $this = $(this);
		var $propertylist = $this.find('.viewport ul');
		var end = 635 - parseInt($propertylist.css('width'));
		$this.find('.prev-pic').click(function(e) {
			e.preventDefault();
			var offset = parseInt($propertylist.css('left'));
			if (!offset) offset = 0;
			offset += scroll;
			if (offset > 0) offset = 0;
			$propertylist.animate({left: offset+'px'}, 'fast');

		});
		$this.find('.next-pic').click(function(e) {
			e.preventDefault();
			var offset = parseInt($propertylist.css('left'));
			if (!offset) offset = 0;
			offset -= scroll;
			if (offset < end) offset = end;
			$propertylist.animate({left: offset+'px'}, 'fast');
		});

	});

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

});
