$(function() {
	// font replacement
	Cufon.replace('.logo-nav li a, h1#logo span, h3.form-subhead, h2.upcoming-walks', { fontFamily: 'nevis', hover: true });
	Cufon.replace('.form-head h2, div.booking-form fieldset legend', { fontFamily: 'ChunkFive', hover: true });
	Cufon.replace('#footer .moduletable_links a, #footer .moduletable_sitemap a, div.button-grey a, div.search, div.search select, input.button', { fontFamily: 'DIN-Black', hover: true });
	Cufon.replace('.nickname', { fontFamily: 'Hand Of Sean', hover: true });
	
	// set slideshow image width
	var width = document.body.offsetWidth;
	$('#header .djslider ul').css({'width':width+'px'});
	$('#header .djslider li').each(function(){
		$(this).css('width',width+'px');
	});
	
	// add class for IE
	$("div#sidebar li").hover( function() {
		$(this).addClass("iehover");
	}, function() { 
		$(this).removeClass("iehover");
	});
	$("div.item-pagefeatured p:first-child").addClass("first-child");
	
	// listing image swap
	$('div.thumbnail-left a').click(function() {
		var tthis = $(this);
		if($(this).hasClass('video')) {
			var rel = $(this).attr("rel");
			$("div#main-image").hide();
			$('div#youtube-video').show().html('<iframe width="480" height="360" src="http://www.youtube.com/embed/'+rel+'" frameborder="0" allowfullscreen></iframe>');
		}
		else {
			$("div#main-image").show();
			$('div#youtube-video').hide();
			var thumbhref = $(this).attr("href");
			var thumbalt = $(this).find("img").attr("alt");
			var mainimagesrc = $("div#main-image").find("img").attr("src");
			var mainimagesrc_s = $("div#main-image").find("img").attr("src").replace("/m/", "/s/");
			var mainimagealt = $("div#main-image").find("img").attr("alt");
			
			$("div#main-image").find("img").stop().fadeTo(250, 0.5, function(){
				$(this).attr("src", thumbhref).fadeTo(250,1);
				$(this).attr("alt", thumbalt);
			});
		}
		return false;
	});
	
	// Listing detail book form collapsible
	$('div.booking-form legend').click(function() {
		if(!$(this).parent().hasClass('opened')) $(this).parent().addClass('opened').siblings().removeClass('opened').find('option:first-child').attr('selected', 'selected');
	});
	$('div.booking-form ul li span.date').click(function() {
		if(!$(this).parent().hasClass('opened')) $(this).parent().addClass('opened').siblings().removeClass('opened').find('option:first-child').attr('selected', 'selected');
	});
	
	$('#listing-detail .google-map iframe').attr({'width': '234', 'height': '206'}); // change google frame width and height
	$('#destination-detail .google-map iframe').attr({'width': '234', 'height': '206'}); // change google frame width and height
	
	// request form
	$('form#request-form').submit(function(){
		var name = $('input[name=name]').val();
		var email = $('input[name=email]').val();
		var phone = $('input[name=phone]').val();
		var bookingTime = $('#displayedDate').text();
		var number_of_adults = $('select[name=number_of_adults] option:selected').val();
		var number_of_children = $('select[name=number_of_children] option:selected').val();
		var more_info = $('textarea[name=more_info]').val();	
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		if(name == '' || name == 'undefined') {
			alert('Please enter name');
			$('input[name=name]').focus();
		}
		else if(email == '' || email == 'undefined') {
			alert('Please enter email');
			$('input[name=email]').focus();
		}
		else if(bookingTime == '' || bookingTime == 'undefined' || bookingTime == 'Select date') {
			alert('Please select date');
		}
		else if(!emailReg.test(email)) {
			alert('Email entered is invalid');
			$('input[name=email]').focus();
		}
		else if(number_of_adults == '0' && number_of_children == '0') {
			alert('Please select number of adults or children');
		}
		else {
			var dataString = 'name='+name+'&email='+email+'&phone='+phone+'&bookingTime='+bookingTime+'&number_of_adults='+number_of_adults+'&number_of_children='+number_of_children+'&more_info='+more_info;
			$.ajax({
				type: 'POST',
				url: 'http://localhost/udu/bin/request_form.php',
				data: dataString,
				beforeSend: function(data) {
					$('input.button').attr('disabled', 'disabled');
				},
				success: function(data) {
					if(data == "success") {
						$('form#request-form').hide();
						$('#form-success').show();
					}
					else {
						alert(data);
					}
				},
				complete: function(data) {
					$('input.button').removeAttr('disabled');
				}
			});
		}
		return false;
	});
});

$(document).ready(function(){  });
