$(document).ready(function() {
	$('#slideshow').cycle({
		fx: 'fade'
	});

	$('.priceForm').submit(function(event) {
		var $form = $(this);
			code = $form.find('input[name="code"]').val(),
			arrivalDate = $form.find('input[name="arrivalDate"]').val(),
			numberNights = $form.find('select[name="numberNights"]').val();
		$('#priceDiv'+code).html('Fetching... <img src="images/ajax-loader.gif" />');
		$('#priceDiv'+code).load('http://www.corkholidayhomes.com/index.php/home/ajax_get_single_price/' +code+ '/' +arrivalDate+ '/' +numberNights, function()
			{
				$('#formPrice'+code).val($('#priceDiv'+code).html());
				$('#fromDate'+code).val(arrivalDate);
				$('#toDate'+code).val('calc');
				$('#nights'+code).val(numberNights);
				
				});
		event.preventDefault();
	});

	$(function() {
		$('input').filter('.listDate').datepicker({dateFormat: 'dd-mm-yy', showOn: 'both', buttonImage: 'http://www.corkholidayhomes.com/images/calendar.jpg', buttonImageOnly: true, changeMonth: true, changeYear: true, rangeSelect: true, constrainInput: true, buttonText: 'Arrival date',  altField: '#datepicker2', minDate: "0D", maxDate: "+1Y" });
	});

	$('.finalForm').submit(function(event){
	var $form = $(this);
		code = $form.find('input[name="price"]').val(),
		arrivalDate = $form.find('input[name="arrivalDate"]').val();
		if(isNaN(code) || code == ''){
			alert('You have entered an invalid date! Please try again');
		event.preventDefault();
		};
	});
	
	$('a.info').live('click', function(getData) {
	    var type = $(this).attr('href');
	    var id = $(this).attr('id');
	    var button = '#buttonInfo'+id;
	    $('#extraInfo'+id).empty().html('<p>Loading... <img src="images/ajax-loader.gif" /></p>');
	    $('#extraInfo'+id).load('http://www.corkholidayhomes.com/index.php/home/ajax_get_more/'+id);
		$('#ratesInfo'+id).hide();
		$('#ratesHeading'+id).hide();
	    $('#infoHeading'+id).toggle();
	    $('#extraInfo'+id).toggle();
	    getData.preventDefault();
	});

	$('a.rates').live('click', function(getData) {
	    var type = $(this).attr('href');
	    var id = $(this).attr('id');
	    $('#ratesInfo'+id).empty().html('<p>Loading... <img src="images/ajax-loader.gif" /></p>');
	    $('#ratesInfo'+id).load('http://www.corkholidayhomes.com/index.php/home/ajax_get_rates/'+id);
		$('#infoHeading'+id).hide();
		$('#extraInfo'+id).hide();
		$('#ratesInfo'+id).toggle();
		$('#ratesHeading'+id).toggle();
	    getData.preventDefault();
	});
	
	$('a.listing').live('click', function(getData) {
	    var code = $(this).attr('href');
	    var id = $(this).attr('id');
	    $('#listBoxDetails'+code).load('http://www.corkholidayhomes.com/index.php/home/ajax_get_'+id+'/'+code);
	    $('#listBoxDetails'+code).show('fast');
	    $('#listPhoto' +code).load('http://www.corkholidayhomes.com/index.php/home/ajax_get_photos/'+code);
	    getData.preventDefault();
	});

	$(function(){
		$(".listBoxCalendar").each(function(){
			var id = $(this).attr('id');
			var calCode = $(this).attr('title');
			$('#'+id).load('http://www.corkholidayhomes.com/index.php/self_catering/availability_calendar/'+calCode);
		});
	});

	$('a.availability').live('click', function(getData) {
	    var code = $(this).attr('href');
	    var id = $(this).attr('id');
	    var currentYear = (new Date).getFullYear();
	    var jsMonth = (new Date).getMonth();
	    var realWorldMonth = jsMonth+1;
	    $('#listBoxCalendar'+code).load('http://www.corkholidayhomes.com/index.php/self_catering/availability_calendar/'+code+'/'+currentYear+'/'+realWorldMonth);
	    $('#listBoxCalendar'+code).show('fast');
	    getData.preventDefault();
	});
	
	$('a.nextPrev').live('click', function(getData) {
	    var id = $(this).attr('id');
	    var path = $(this).attr('href');
	    $('#'+id).load(path);
	    $('#'+id).show('fast');
	    getData.preventDefault();
	});

	$(function() {
		$("#datepicker1").datepicker({dateFormat: 'dd-mm-yy', showOn: 'both', buttonImage: 'http://www.corkholidayhomes.com/images/calendar.jpg', buttonImageOnly: true, changeMonth: true, changeYear: true, rangeSelect: true, constrainInput: true, buttonText: 'Arrival date',  altField: '#datepicker2', minDate: "0D", maxDate: "+1Y", beforeShow: function() {
		    $('#selectDateTd').addClass('chosenLeft');
		    $('#selectMonthTd').removeClass('chosenRight');
		    $('#dateSelectType').val('Specific');
		    }
		});
	});
	
	$(function() {
		$("#datepicker2").datepicker({dateFormat: 'dd-mm-yy', showOn: 'both', buttonImage: 'http://www.corkholidayhomes.com/images/calendar.jpg', buttonImageOnly: true, changeMonth: true, changeYear: true, rangeSelect: true, constrainInput: true, buttonText: 'Departure date', minDate: "0D", maxDate: "+1Y", beforeShow: function() {
		    $('#selectDateTd').addClass('chosenLeft');
		    $('#selectMonthTd').removeClass('chosenRight');
		    $('#dateSelectType').val('Specific');
		    }
		});
	
	});

	<!-- Hover functions -->
	$('#allTownsTd').bind('mouseenter mouseleave', function() {
	  $(this).toggleClass('selectedLeftSmall');
	});
	$('#selectTownTd').bind('mouseenter mouseleave', function() {
	  $(this).toggleClass('selectedRightSmall');
	});
	
	$('#selectDateTd').bind('mouseenter mouseleave', function() {
	  $(this).toggleClass('selectedLeft');
	});
	$('#selectMonthTd').bind('mouseenter mouseleave', function() {
	  $(this).toggleClass('selectedRight');
	});
	
	
	<!-- Click functions -->
	$('#allTownsTd').bind('click', function(){
		$(this).addClass('chosenLeftSmall');
		$('#selectTownTd').removeClass('chosenRightSmall');
		$('#townSelectType').val('Loose');
		if (document.searchForm.selectAllTown.checked == false) {
			document.searchForm.selectAllTown.click();
		};
	});
	
	$('#selectTownTd').bind('click', function(){
		if (document.searchForm.selectAllTown.checked == true) {
			document.searchForm.selectAllTown.click();
		};
		$(this).addClass('chosenRightSmall');
		$('#allTownsTd').removeClass('chosenLeftSmall');
		$('#townSelectType').val('Specific');
	});
	
	$('#selectDateTd').bind('click', function(){
		$(this).addClass('chosenLeft');
		$('#selectMonthTd').removeClass('chosenRight');
		$('#foo').val('foo')
		$('#dateType').val('Specific');
		$('#dateSelectType').val('Specific');
	});
	
	$('#selectMonthTd').bind('click', function(){
		$(this).addClass('chosenRight');
		$('#selectDateTd').removeClass('chosenLeft');
		$('#dateSelectType').val('Loose');
	});	
});

