/*
 * 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_images == 'undefined') var stage_images;
if (typeof stage_texts == 'undefined') var stage_texts;
if (typeof stage_delay == 'undefined') var stage_delay;
if (typeof stage_interval == 'undefined') var stage_interval;

function label2value(){

	var inactive = "inactive";
	var active = "active";
	var focused = "focused";

	$(".label2value label").each(function(){
		obj = document.getElementById($(this).attr("for"));
		if(($(obj).attr("type") == "text") || (obj.tagName.toLowerCase() == "textarea") || (obj.tagName.toLowerCase() == "select")){
			var text = $(this).text();
			$(this).css("display","none");
			if ($(obj).val() != "" && $(obj).val() != text) $(obj).addClass(active);
			else {
				$(obj).val(text);
				$(obj).addClass(inactive);
			}
			$(obj).focus(function(){
				$(this).addClass(focused);
				$(this).removeClass(inactive);
				$(this).removeClass(active);
				if($(this).val() == text) $(this).val("");
			});
			$(obj).blur(function(){
				$(this).removeClass(focused);
				if($(this).val() == "") {
					$(this).val(text);
					$(this).addClass(inactive);
				} else {
					$(this).addClass(active);
				};
			});
		};
	});

	$("form.label2value").submit(function() {
		$(this).children('label').each(function() {
			if ($(this).css('display') != 'none') return;
			var text = $(this).text();
			var obj = document.getElementById($(this).attr("for"));
			if ($(obj).val() == text) $(obj).val('');
		});
	});

};

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

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

		var index = 0;
		var images = slides;
		var texts = captions;
		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 txt = !texts[index] ? '' : texts[index];
			var imgcss = 'url('+images[index]+')';
			$target.css('background-image', imgcss);
			$source.animate({opacity: 0}, 1000, function() {
				$source
					.css('background-image', imgcss)
					.css('opacity', 1);
				if (txt) $target.find('em').html(txt);
			});
			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(){

	// hide form labels
	label2value();

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

	// 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 div').slideshow(stage_images, stage_texts, delay, interval);
		delay += step;
	}

	// initialise lightbox
	$("a[rel^='lightbox'],.lightwindow").prettyPhoto({
		width: 800,
		height: 600,
		theme: 'dark_rounded',
		iframe: true
	});

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

	// tabs
	var $tabs =$('.tablist dt');
	var $tabcontents =$('.tablist dd');
	if ($tabs.length) {
		$('.tablist').addClass('jsenabled');
		$('.tablist dt a').click(function(e) {
			e.preventDefault();
			var s = $tabs.index($(this).parent());
			$tabs.removeClass('selected');
			$tabcontents.removeClass('selected');
			$tabs.eq(s).addClass('selected');
			$tabcontents.eq(s).addClass('selected');
		});
	}

	// booking form
	if ($('.bookingform').length && !$('.bookingform').hasClass('active')) {
		$('.bookingform').before('<a class="booknow">Make provisional booking</a>').hide();
		$('a.booknow').click(function(e) {
			e.preventDefault();
			$(this).hide();
			$('.bookingform').slideDown();
		});
	}

});
