﻿$(window).load(function(){

	// letterbox
	if($('#letterbox #images img').length > 1) {
		$('#letterbox #images').nivoSlider({
			effect: 'boxRainReverse',
			pauseTime: 4000,
			controlNav: false,
			captionOpacity: 1,
			prevText: '',
			nextText: ''
		});
	}
	
	// faq
	if($('#faqs dl').length != 0) {

		$('dt').prepend('<span>+</span> ');
		$('dd').hide();
		
		$('dt').click(function(ev){
			var dd = $(this).next();
			if(dd.hasClass('open')) {
				// dd.slideUp().removeClass('open');
			} else {
				if($('dd.open').length != 0) {
					$('dd.open').prev().find('span').html('+');
					$('dd.open').removeClass('open').slideUp(
						400,
						function() {
							dd.slideDown().addClass('open');
						}
					);
				} else {
					dd.slideDown().addClass('open').prev().find('span').html('-');
				}
				dd.prev().find('span').html('-');
			}
		});
	
	}
		
	// polaroids
	if($('#polaroids').length != 0) {

		// make sure each polaroid starts at 75% height of the previous, to avoid too much overlapping
		$('#polaroids > div:gt(0)').each(function(){
			var total = 0;
			$(this).prevAll().each(function(){
				total = total + $(this).outerHeight();
			});
			total = Math.ceil(total * 0.75);
			
			$(this).css('top', total + 'px');
			
		});
		
		// grow container to fit polaroids
		var last = $('#polaroids > div:last');
		var height = parseInt(last.css('top')) + last.outerHeight();
		$('#polaroids').css('height', height + 'px');
		
		var dimensions = new Object;
				
		var diff = (parseInt($('#polaroids').offset().top) + parseInt($('#polaroids').outerHeight())) - (parseInt($('#awards').offset().top) + parseInt($('#awards').outerHeight()));
		
		while(diff > 0 && $('#polaroids > div').length > 0) {
			$('#polaroids > div:last').remove();
			var last = $('#polaroids > div:last');
			var height = parseInt(last.css('top')) + last.outerHeight();
			$('#polaroids').css('height', height + 'px');
			diff = (parseInt($('#polaroids').offset().top) + parseInt($('#polaroids').outerHeight())) - (parseInt($('#awards').offset().top) + parseInt($('#awards').outerHeight()));
		}		
		
	}
	
	// directions sidebar
	if($('#directionsSidebar').length != 0) {
	
		$('#directionsSidebar').show();
	
		// handle an enter key in the field
		$('#postcode').keypress(function(ev){
			if(ev.which == 13) {
				$('#get-directions').click();
			}
		});
		
		// handle a click on the button
		$('#get-directions').click(function(ev){
			ev.preventDefault();
			var postcode = $('#postcode').val();
			if(postcode == null || postcode == '') {
				return false;
			}
			var url = 'http://maps.google.co.uk/maps?saddr=' + encodeURIComponent(postcode) + '&daddr=ox29+9sx%20(Audley%20Travel)';
			window.open(url, 'directions');
		});
	
	}

});
