// JavaScript Document

var data, json, slideFX;

window.addEvent('domready', function() {
	if ($('view_houses'))
	$('view_houses').onclick = function() {
		if (json && (json.ids.length > 0)) {
			$('home_content').innerHTML = "<h1>Search Results</h1><p><img src=\"/images/ajax-loader(2).gif\" id=\"loading_icon\"> Please wait while loading search results.</p>";
			new Request( { method: 'post', url: '/properties.html.php', noCache:true, onSuccess: function(html) {
				$('home_content').innerHTML = html;
		}}).send(	"ids="+json.ids.join(","));
		return false;
	} }
});

function getCount() {
	$('num_properties').innerHTML = "&nbsp;<img src=\"/images/ajax-loader.gif\">&nbsp;";
	
	var facilities = Array();
		
	$$('#ajax_filter .facilities').each(function(el) { if (el.checked) facilities.push(el.name.split("_")[1]); });
	
	data = new Request( { method: 'post', url: '/properties.json.php', noCache:true, onSuccess: function(resp) {
		json = eval("("+resp+")");
		$('num_properties').innerHTML = json.count;
	} } ).send(	"area="+$('area').options[$('area').options.selectedIndex].value+
							"&sleeps="+$('people').options[$('people').options.selectedIndex].value+
							"&stars="+$('star_rating').options[$('star_rating').options.selectedIndex].value+
							"&dogs="+$('dogs').checked+
							"&date="+escape($('date').value)+
							"&range="+escape($('price_range').value)+
							"&facilities="+facilities.join("|")+
							"&cottage="+escape($('cottage_name').value)+
							"&nights="+$('nights').value);
}

window.addEvent('domready', function() {
	if ($('ajax_filter')) {
		$('area').onchange = function() { getCount() }
		$('people').onchange = function() { getCount() }
		$('nights').onchange = function() { getCount() }
		$('dogs').onclick = function() { getCount() }
		$('star_rating').onchange = function() { getCount() }
		$('price_range').onchange = function() { getCount() }
		$('cottage_name').onkeyup = function() { data.cancel(); getCount() }
		$('clear_search').onclick = function() {
			$('area').options.selectedIndex = 0;
			$('people').options.selectedIndex = 9;
			$('nights').options.selectedIndex = 0;
			$('dogs').checked = false;
			$('star_rating').options.selectedIndex = 0;
			$('price_range').options.selectedIndex = 0;
			$('date').value = '';
			$('date').getNext("input").value = '';
			$$('#ajax_filter .facilities').each(function(el) {
				el.checked = false;
			});
			$('cottage_name').value = "";
			slideFX.slideOut();
			getCount();
			return false;
		}
	}
});

window.addEvent('domready', function() {
	if ($('ajax_filter')) {
		$('ajax_filter').onsubmit = function() { return false; }
	}
});

window.addEvent('domready', function() {
	if ($('ajax_filter')) {
		slideFX = new Fx.Slide('more_search_options');
		slideFX.hide();
		$('more_options').addEvent('click', function() { slideFX.toggle(); return false; });
	}
});

window.addEvent('domready', function() {
	if ($('date')) {
		var d = new Date();
				d.setDate(d.getDate() + 2);
		var curr_date = d.getDate();
		var curr_month = d.getMonth();
		var curr_year = d.getFullYear();
		new DatePicker($('date'), {
			pickerClass:'datepicker_dashboard',
			format:'d/m/Y',
			allowEmpty: true,
			minDate: { date: curr_date + "/" + curr_month + "/" + curr_year, format: 'd/m/Y' },
			maxDate: { date: curr_date + "/" + curr_month + "/" + (curr_year+2), format: 'd/n/Y' },
			onSelect: function() { getCount() }
		});
		$('people').selectedIndex = 9;
		$$('#ajax_filter .facilities').each(function(el) {
			el.onclick = function() {
				getCount();
			}
		});
		getCount();
	}
});

var counter=0,ims;

var images = Array(
	"/images/header-images/marsh-farm-ellough-suffolk.jpg",
	"/images/header-images/belstead-house-aldeburgh-suffolk.jpg",
	"/images/header-images/marshlands-nr-orford-suffolk.jpg",
	"/images/header-images/old-hall-barn-darsham-suffolk.jpg",
	"/images/header-images/rowan-house-westleton-suffolk.jpg"
);
	
var links = Array(
	"http://www.bighouseholidays.co.uk/cottage/suffolk/000165/MarshFarm.html",
	"http://www.bighouseholidays.co.uk/cottage/suffolk/000198/BelsteadHouse.html",
	"http://www.bighouseholidays.co.uk/cottage/suffolk/000156/Marshlands.html",
	"http://www.bighouseholidays.co.uk/cottage/suffolk/000085/OldHallBarn.html",
	"http://www.bighouseholidays.co.uk/cottage/suffolk/000166/RowanHouse.html"
);

window.addEvent('domready', function() {

	ims = [$('image1'),$('image2')];
	if (images.length > 0) carousel.delay(6000);
	
});

var carousel = function () {
	counter = (counter + 1) % images.length;
	var newImage = new Asset.image(images[counter],
		{
		onload: function() {
			var link0 = ims[0].getParent();
			var link1 = ims[1].getParent();
			ims[1].src = ims[0].src;
			link1.href = link0.href;
			ims[1].fade("show");
			ims[0].src = this.src;
			link0.href = links[counter];
			ims[1].fade("out", {duration:3000});
			carousel.delay(6000);
		}
});
}

window.addEvent('domready', function() {
	if ($('photos')) {
		$('photos').getChildren("a").each(function(el) {
			el.addEvent('click', function(e) {
				var popup = window.open(this.href, "pictures", "width=800,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
				popup.focus();
				return false;
			});
		});
	}
});