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

var site_prefix;
var stage_left;
var stage_right;
var quotes;

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

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

		var index = 0;
		var images = slides;
		var $target = $(this);
		var $source = $target.clone().css('margin', 0).prependTo($target);
		var callback = false;

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

		function slideshow_crossfade() {
			++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);
			});
			callback = false;
			var next = index + 1;
			if (next >= images.length) next = 0;
			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 $target = $(this);

		function quote_crossfade() {

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

			$target.children('p').
				fadeOut('slow', function() {
					$target.children('.source').html(quotelist[index][0]);
					$target.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(){

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

	// set-up slideshows
	var delay = 2500;
	var interval = 5000;
	if (typeof(stageleft) != 'undefined') {
		if (typeof(stageright) != 'undefined') interval += 5000;
		$('#stage .large').slideshow(stageleft, delay, interval);
		delay += 5000;
	}
	if (typeof(stageright) != 'undefined') {
		$('#stage .small').slideshow(stageright, delay, interval);
	}

	// set-up quotes
	if (typeof(quotes) != 'undefined') {
		$('#quote').quoteshow(quotes, 5000, 10000);
	}

	// initialise lightbox
	if (typeof($.fn.prettyPhoto) == 'function') {
		$(".lightwindow").prettyPhoto({
			theme: 'light_square',
			width: 800,
			height: 600,
			iframe: true
		});
	}

	// date picker
	$(".date-pick").datepicker({
		dateFormat: 'dd/mm/yy',
		buttonImageOnly: true,
		buttonImage: site_prefix+'img/calendar.gif',
		duration: '',
		firstDay: 5,
		minDate: new Date,
		numberOfMonths: 2,
		showOn: 'button',
		beforeShow: function() {
			if (this.id != 'departure') return {};
			var arrival = $('#arrival').val().split('/');
			if (arrival.length != 3) return {};
			return { minDate: new Date(new Number(arrival[2]), new Number(arrival[1]) - 1, new Number(arrival[0])) };
		}
	});

	// us states

	function showStateOrCounty() {
		var country = $('#country').val();
		if (country == 'US' || country == 'UM') {
			$('#show-state').show();
			$('#show-county').hide();
		} else {
			$('#show-state').hide();
			$('#show-county').show();
		}
	}

	$('#country').change(showStateOrCounty);
	$('#country').keypress(showStateOrCounty);
	showStateOrCounty();

	// highlight open menu items
	$('#nav .expandable').hover(function() {
		$(this).children('.closed')
			.removeClass('closed')
			.addClass('open');
	}, function() {
		$(this).children('.open')
			.removeClass('open')
			.addClass('closed');
	});
	$('#nav .expandable ul:visible')
		.removeClass('closed')
		.addClass('open');

	// insert bookmark widget
	$('#accreditations').click(function () {
		document.location = site_prefix+'accreditations';
	});

	// insert bookmark widget
	$('#social ul').append('<li><a id="bookmark" href="#"><!-- BOOKMARK --></a></li>');

	$('#bookmark').click(function() {

		try {
			if ($.browser.mozilla) {
				// mozilla
				window.sidebar.addPanel(document.title, window.location.href, "");
				return false;
			} else if ($.browser.opera) {
				// opera
				var mbm = document.createElement('a');
				mbm.setAttribute('rel','sidebar');
				mbm.setAttribute('href', window.location.href);
				mbm.setAttribute('title', document.title);
				mbm.click();
				return false;
			}
			if ($.browser.msie) {
				// msie
				window.external.addFavorite(window.location.href, document.title);
				return false;
			}
		} catch (err) {
		}

		var ctrl = ($.browser.mac) ? 'Cmd' : 'Ctrl';
		alert("Your browser does not support this feature.\nPlease close this dialog and press "+ctrl+"-D\nto bookmark this page.");
		return false;

	});

	// fix footers in IE
	if ($.browser.msie) {
		$('.footlinks li')
			.not('.last')
			.css('border', 'none')
			.append('/ ');
	}

});