// JavaScript Document

$(window).load(function () {
	
	var today = new Date();
	var mx = new Date(parseInt($('#max').val(),10)*1000);
	
	$('#let_me_help .filter, #pets').click(filterCottages);
	$('#people').change(filterCottages); //#nights, 
	
		
});

$(window).load(function() {
	
	grouped = false;
	
	$('#group_by_price').click(function() {
		
		if (!grouped) {
			grouped = true;
			try {
				_gaq.push(['_trackEvent', 'Help You Choose', 'Click', 'Group By Price']);
			} catch (e) {
				if (console) console.log(e);
			}
	
		}
		
		var active = $('#cottage_sorter .active');
		
		var cClass, 
			className, 
			price_bands = Array();
		
		if ($('#people')[0][$('#people')[0].selectedIndex].text <= 2) {
			cClass = 1;
			className = 'couple';
		} else if ($('#people')[0][$('#people')[0].selectedIndex].text <= 4) {
			cClass = 2;
			className = 'group';
		} else {
			cClass = 3;
			className = 'regular';
		}
		
		var price;
		
		for (var i=0; i<active.length; i++) {
			if (active[i].className.split(" ").length <= 1) continue;
			price = parseInt(active[i].className.split(" ")[cClass].split("_")[1],10);
			if (jQuery.inArray(price, price_bands) == -1) price_bands.push(price);
		}
		
		price_bands.sort().reverse();
		
		var price;
		
		$('#cottage_sorter').prepend('<h2 class="price_band">Other Cottages<span>change \'Let Me Help You Choose\' options to enable these cottages</span></h2>');
				
		for (i=0; i<price_bands.length; i++) {
			price = parseInt(price_bands[i],10)/100;
			$('#cottage_sorter').prepend($('.'+className+'_'+price_bands[i]+'.active'));
			$('#cottage_sorter').prepend('<h2 class="price_band" id="pb'+price_bands[i]+'">From &pound;'+price.toFixed(2)+' (based on '+($('#people')[0].selectedIndex+2)+' sharing)<span>use the \'No. of Guests\' drop down at the top to check the price for a different number of guests</span></h2>');
		}

		var order = []; 
		$(".active").each(function() {
			order.push(this.id.split("_")[1]);
		});
		
		order.reverse();
		
		$('#view_availability_button')[0].href = "/availability/?priority=" + order.join(",");
		
		return false;
	})
	
});

var ajax_filter = null;

function filterCottages() {
	
	try {
		_gaq.push(['_trackEvent', 'Help You Choose', 'Click', this.title, (this.checked?1:0)]);
	} catch (e) {
		if (console) console.log(e);
	}
	
	var opts = [];
	$('#let_me_help .filter').each(function (inx) { if ($(this)[0].checked) opts.push($(this)[0].value); });
	
	$('#cottages_loading')[0].style.display = "block";
	
	if (ajax_filter) { ajax_filter.success = function() {}; ajax_filter.abort(); }
	
	ajax_filter = 
	$.ajax({
		url: '/filter.json.php',
		dataType: 'json',
		data: { 
			people: $("#people").val(),
			pets: $("#pets")[0].checked,
			opts: opts.join(",")
		},
		success: function(data) {
			
			$('#cottages_loading')[0].style.display = "none";
			$('#cottage_sorter h2.price_band').remove();
			
			if (data) {
				
				var cottages = data.split(",");

				if (cottages.length == 0) {
				
					$('#num_of strong')[0].innerHTML = "0";
				
				} else {
					
					$('#num_of strong')[0].innerHTML = cottages.length;
					
					$('#cottage_sorter .cottage').addClass("inactive").removeClass("active");
					$("#c_"+cottages.join(",#c_")).removeClass("inactive").addClass("active").prependTo($('#cottage_sorter'));
					
				}

			} else {
				
				$('#num_of strong')[0].innerHTML = "0";
				$('#cottage_sorter .cottage').addClass("inactive").removeClass("active");
				
			}
			if (grouped) $('#group_by_price').click();
		}
	});
}

function smartColumns() { //Create a function that calculates the smart columns

	//Reset column size to a 100% once view port has been adjusted
	$("#cottages_home_page").css({ 'width' : "100%"});

	var colWrap = $("#cottages_home_page").width() - 40; //Get the width of row
	
	var colNum = Math.floor(colWrap / 232); //Find how many columns of 200px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

	$("#cottages_home_page").css({ 'width' : colWrap + 40}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
	$("#cottages_home_page .cottage").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column	

}	

$(window).load(function() {
	smartColumns();//Execute the function when page loads
});

$(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
	smartColumns();
});


