$(document).ready(function() {

	/*
	# Cufon
	*/
	Cufon.replace('h1');
	Cufon.replace('div.tab div.column div.text h3');
	Cufon.replace('div#main div.contact div.text h2');
	Cufon.replace('div#main div.request h2');
	
	/*
	# PDF Downloads open in a new window
	*/
	$('.download').attr('target','_blank');
	
	/*
	# Force last class on thumbs
	*/
	if ( !$('#suitesThumbs a:last-child').hasClass('last') )
	{
		$('#suitesThumbs a:last-child').addClass('last')
	}
	
	/*
	# Thumb functionality
	*/
	$('#suitesThumbs a').each(function() {
		$('<img>')
			.attr('src', $(this).attr('href'))
			.addClass('preload')
			.hide()
			.appendTo('body');
	});
	//$('.preload').remove();		
		
		$('#suitesMain')
			.wrap('<div style="height:390px;margin:0 0 2px 0;" />');
	$('#suitesThumbs a').click(function(e) {
		e.preventDefault();
		var src = $(this).attr('href');
		var text = $(this).children('img').attr('alt');		
		$('#suitesMain').fadeOut('fast', function() {
			$(this)
				.attr('src', src)
				.hide()
				.delay(10)
				.fadeIn('fast');
			if ( text != '' ) $('#suitesDescription').text(text);
		});
	});
	
	/*
	# Datepicker
	*/
	$('#book .text').attr('autocomplete', 'off');
	$('#book .text').datepicker({
		dateFormat: 'dd-mm-yy'
	});
		
	/*
	# Book this suite drop down
	*/
	var bookInUse = false;;
	$('#book').hide();
	$('#bookThisSuite').click(function(e) {
		e.preventDefault();
		$('#book').slideDown('fast').css('display', 'block');
		var t = setTimeout(function() {
			if ( !bookInUse ) $('#book').slideUp('fast');
		}, 10000);
	});
	
	$('#book .text').focus(function() {
		bookInUse = true;
	});
	
	$('#book .text').blur(function() {
		bookInUse = false;
		var t = setTimeout(function() {
			if ( !bookInUse ) $('#book').slideUp('fast');
		}, 10000);
	});
	
	
	
	
	/*
	# Tabs
	*/
	$('.tab').hide();
	$('.tab').eq(0).fadeIn();
	$('#subNavBar li a').each(function(i, el) {
		if ( !$(this).hasClass('download') ) {
			$(this).click(function(e) {
				e.preventDefault();
				$('.tab').hide();
				$('.tab').eq(i).fadeIn();
				$('#subNavBar li').removeClass('current');
				$('#subNavBar li').eq(i).addClass('current');
			});
		}
	});
	
	$('#specification .text a').click(function(e) {
		e.preventDefault();
		$('.tab').hide();
		$('.tab').eq(1).fadeIn();
		$('#subNavBar li').removeClass('current');
		$('#subNavBar li').eq(1).addClass('current');
	});
	
	/*
	# Present active tab
	*/	
	if(window.location.hash) {
		var tab = window.location.hash;
		switch(tab)
		{
			case '#versatility':
				$('.tab').hide();
				$('.tab').eq(1).fadeIn();
				$('#subNavBar li').removeClass('current');
				$('#subNavBar li').eq(1).addClass('current');
				break;
			case '#specification':
				$('.tab').hide();
				$('.tab').eq(2).fadeIn();
				$('#subNavBar li').removeClass('current');
				$('#subNavBar li').eq(2).addClass('current');
				break;
			case '#floorplan':
				$('.tab').hide();
				$('.tab').eq(3).fadeIn();
				$('#subNavBar li').removeClass('current');
				$('#subNavBar li').eq(3).addClass('current');
				break;
		}
	}
	
	/*
	# Drop Down Menus
	*/
	$("#mayfairNav li,#nav li").each(function(i, el) {
		if ( $(el).find('ul') )
		{
			$(el).find('ul').css('display','none');
			$(el).delay(50).hover(
				function() {
					$(el).find('ul').slideDown(100);
				},
				function() {
					$(el).find('ul').slideUp(50);
				}
			);
		}
	});
	
});
