jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// preload images
$.preloadImages("http://www.lindumhomes.co.uk/images/templates/loading.gif","http://www.lindumhomes.co.uk/images/templates/loading2.gif");

if ($(".validate_me").length > 0) {
	$(".validate_me").validate();
}

if ($("#home").length > 0) {
	$(".step2,.step3,.step4").hide();
	if( !(navigator.userAgent.match(/iPad/i))) { $('#home-search select').selectmenu(); }
	var men1begin = $("#hometype").html();
	var men2begin = $("#numrooms").html();
	var men3begin = $("#pricerange").html();
	$(".step1 select").change(function() {
		// begin repopulate menus 2 and 3
		var men2options = new Array();
		var men3options = new Array();
		var men2selected = $("#numrooms option:selected").val();
		var men3selected = $("#pricerange option:selected").val();
		var optSelected = $(this).val();
		if ($(this).val() != "any") {
			$("#numrooms").attr('disabled','disabled');
			$("#pricerange").attr('disabled','disabled');
			$('#home-search form').append('<img src="/images/templates/loading2.gif" alt="" class="loader">');
			$("#numrooms").html(men2begin);
			if( !(navigator.userAgent.match(/iPad/i))) { $("#numrooms").selectmenu(); }
			$("#numrooms option[value='" + men2selected + "']").attr('selected','selected');
			$("#numrooms option:not(:first-child)").each(function(i){
				if($(this).val() != "any") { men2options[i] = $(this).val(); }
			});
			if (men3selected == "any" || men3selected == "unselected") {
				$.each(men2options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal=' + objValue +'&objSelected=' + optSelected + '&menuSelected=1',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#numrooms option[value='" + objValue + "']").remove(); 
								$("#numrooms").removeAttr('disabled'); 
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#numrooms, #numrooms-button, #numrooms-menu, #numrooms-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled'); 
									$("#numrooms").selectmenu(); 
								} 
							}
				  		}
					});
				});
			} else {
				$.each(men2options, function( intIndex, objValue ){
					$.ajax({
					  	url: '/home-selector/count-entries/',
					  	context: document.body,
					  	data: 'objVal=' + objValue + '&objVal2=' + men3selected + '&objSelected=' + optSelected + '&menuSelected=1',
				      	type: 'post',
					  	success: function(data){
							if(data == 0) { 
								$("#numrooms option[value='" + objValue + "']").remove(); 
								$("#numrooms").removeAttr('disabled'); 
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#numrooms, #numrooms-button, #numrooms-menu, #numrooms-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled'); 
									$("#numrooms").selectmenu(); 
								} 
							}
					  	}
					});
				});
			}
			$("#pricerange").html(men3begin);
			if( !(navigator.userAgent.match(/iPad/i))) { $("#pricerange").selectmenu(); }
			$("#pricerange option[value='" + men3selected + "']").attr('selected','selected');
			$("#pricerange option:not(:first-child)").each(function(i){
				if($(this).val() != "any") { men3options[i] = $(this).val(); }
			});
			if (men2selected == "any" || men2selected == "unselected") {
				$.each(men3options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal2=' + objValue +'&objSelected=' + optSelected + '&menuSelected=1',
			      		type: 'post',
				 		success: function(data){
							if(data == 0) { 
								$("#pricerange option[value='" + objValue + "']").remove();
								$("#pricerange").removeAttr('disabled');
								$(".loader").remove();
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#pricerange, #pricerange-button, #pricerange-menu, #pricerange-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled'); 
									$("#pricerange").selectmenu();
								} 
							}
				  		}
					});
				});
			} else {
				$.each(men3options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal2=' + objValue + '&objVal=' + men2selected + '&objSelected=' + optSelected + '&menuSelected=1',
			      		type: 'post',
				 		success: function(data){
							if(data == 0) { 
								$("#pricerange option[value='" + objValue + "']").remove(); 
								$("#pricerange").removeAttr('disabled');
								$(".loader").remove();
								if( !(navigator.userAgent.match(/iPad/i))) {
									$("#pricerange, #pricerange-button, #pricerange-menu, #pricerange-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled');  
									$("#pricerange").selectmenu(); 
								} 
							}
				  		}
					});
				});
			}
		}
		// end repopulate menus 2 and 3
		$(".step2").slideDown(function() {
			$('#home-search p em').html('2');
		});
	});
	$(".step2 select").change(function() {
		// begin repopulate menus 1 and 3
		var men1options = new Array();
		var men3options = new Array();
		var men1selected = $("#hometype option:selected").val();
		var men3selected = $("#pricerange option:selected").val();
		var optSelected = $(this).val();
		if ($(this).val() != "any") {
			$("#hometype").attr('disabled','disabled');
			$("#pricerange").attr('disabled','disabled');
			$('#home-search form').append('<img src="/images/templates/loading2.gif" alt="" class="loader">');
			$("#hometype").html(men1begin);
			if( !(navigator.userAgent.match(/iPad/i))) { $("#hometype").selectmenu(); }
			$("#hometype option[value='" + men1selected + "']").attr('selected','selected');
			$("#hometype option:not(:first-child)").each(function(i){
				if($(this).val() != "any") { men1options[i] = $(this).val(); }
			});
			if (men3selected == "any" || men3selected == "unselected") {
				$.each(men1options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal=' + objValue +'&objSelected=' + optSelected + '&menuSelected=2',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#hometype option[value='" + objValue + "']").remove(); 
								$("#hometype").removeAttr('disabled');
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#hometype, #hometype-button, #hometype-menu, #hometype-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled');
									$("#hometype").selectmenu(); 
								}
							}
				  		}
					});
				});
			} else {
				$.each(men1options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal=' + objValue + '&objVal2=' + men3selected + '&objSelected=' + optSelected + '&menuSelected=2',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#hometype option[value='" + objValue + "']").remove(); 
								$("#hometype").removeAttr('disabled');
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#hometype, #hometype-button, #hometype-menu, #hometype-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled');
									$("#hometype").selectmenu(); 
								} 
							}
				  		}
					});
				});
			}
			$("#pricerange").html(men3begin);
			if( !(navigator.userAgent.match(/iPad/i))) { $("#pricerange").selectmenu(); }
			$("#pricerange option[value='" + men3selected + "']").attr('selected','selected');
			$("#pricerange option:not(:first-child)").each(function(i){
				if($(this).val() != "any") { men3options[i] = $(this).val(); }
			});
			if (men1selected == "any" || men1selected == "unselected") {
				$.each(men3options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal2=' + objValue +'&objSelected=' + optSelected + '&menuSelected=2',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#pricerange option[value='" + objValue + "']").remove(); 
								$("#pricerange").removeAttr('disabled');
								$(".loader").remove();
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#pricerange, #pricerange-button, #pricerange-menu, #pricerange-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled');
									$("#pricerange").selectmenu(); 
								} 
							}
				  		}
					});
				});
			} else {
				$.each(men3options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal2=' + objValue + '&objVal=' + men1selected + '&objSelected=' + optSelected + '&menuSelected=2',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#pricerange option[value='" + objValue + "']").remove(); 
								$("#pricerange").removeAttr('disabled');
								$(".loader").remove();
								if( !(navigator.userAgent.match(/iPad/i))) {
									$("#pricerange, #pricerange-button, #pricerange-menu, #pricerange-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled'); 
									$("#pricerange").selectmenu(); 
								} 
							}
				  		}
					});
				});
			}
		}
		// end repopulate menus 1 and 3
		$(".step3").slideDown(function() {
				$('#home-search p em').html('3');
		});
	});
	$(".step3 select").change(function() {
		// begin repopulate menus 1 and 2
		var men1options = new Array();
		var men2options = new Array();
		var men1selected = $("#hometype option:selected").val();
		var men2selected = $("#numrooms option:selected").val();
		var optSelected = $(this).val();
		if ($(this).val() != "any") {
			$("#hometype").attr('disabled','disabled');
			$("#numrooms").attr('disabled','disabled');
			$('#home-search form').append('<img src="/images/templates/loading2.gif" alt="" class="loader">');
			$("#hometype").html(men1begin);
			if( !(navigator.userAgent.match(/iPad/i))) { $("#hometype").selectmenu(); }
			$("#hometype option[value='" + men1selected + "']").attr('selected','selected');
			$("#hometype option:not(:first-child)").each(function(i){
				if($(this).val() != "any") { men1options[i] = $(this).val(); }
			});
			if (men2selected == "any" || men2selected == "unselected") {
				$.each(men1options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal=' + objValue +'&objSelected=' + optSelected + '&menuSelected=3',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#hometype option[value='" + objValue + "']").remove(); 
								$("#hometype").removeAttr('disabled');
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#hometype, #hometype-button, #hometype-menu, #hometype-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled');
									$("#hometype").selectmenu(); 
								} 
							}
				  		}
					});
				});
			} else {
				$.each(men1options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal=' + objValue + '&objVal2=' + men2selected + '&objSelected=' + optSelected + '&menuSelected=3',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#hometype option[value='" + objValue + "']").remove();
								$("#hometype").removeAttr('disabled'); 
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#hometype, #hometype-button, #hometype-menu, #hometype-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled');
									$("#hometype").selectmenu(); 
								} 
							}
				  		}
					});
				});
			}
			$("#numrooms").html(men2begin);
			if( !(navigator.userAgent.match(/iPad/i))) { $("#numrooms").selectmenu(); }
			$("#numrooms option[value='" + men2selected + "']").attr('selected','selected');
			$("#numrooms option:not(:first-child)").each(function(i){
				if($(this).val() != "any") { men2options[i] = $(this).val(); }
			});
			if (men1selected == "any" || men1selected == "unselected") {
				$.each(men2options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal2=' + objValue +'&objSelected=' + optSelected + '&menuSelected=3',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#numrooms option[value='" + objValue + "']").remove(); 
								$("#numrooms").removeAttr('disabled');
								$(".loader").remove();
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#numrooms, #numrooms-button, #numrooms-menu, #numrooms-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled');
									$("#numrooms").selectmenu(); 
								} 
							}
				  		}
					});
				});
			} else {
				$.each(men2options, function( intIndex, objValue ){
					$.ajax({
				  		url: '/home-selector/count-entries/',
				  		context: document.body,
				  		data: 'objVal2=' + objValue + '&objVal=' + men1selected + '&objSelected=' + optSelected + '&menuSelected=3',
			      		type: 'post',
				  		success: function(data){
							if(data == 0) { 
								$("#numrooms option[value='" + objValue + "']").remove(); 
								$("#numrooms").removeAttr('disabled');
								$(".loader").remove();
								if( !(navigator.userAgent.match(/iPad/i))) { 
									$("#numrooms, #numrooms-button, #numrooms-menu, #numrooms-menu a").removeAttr('aria-disabled').removeClass('ui-selectmenu-disabled ui-state-disabled');
									$("#numrooms").selectmenu(); 
								} 
							}
				  		}
					});
				});
			}
		}
		// end repopulate menus 1 and 2
		$(".step4").show();
	});
}

if ($("#tabs").length > 0) {
	var $tabs = $('#tabs').tabs({ selected: 1 });
	$(function() {
		$tabs.tabs({ selected: 0 });
	});
	function init() {
		$tabs.tabs({ selected: 0 });
	}
	window.onload = init;
	$('.ui-tabs-nav').css({'display':'block'});
}

function onBefore() { 
	$('#lb-usps p').css({'visibility':'hidden'});
} 

function onAfter() { 
	$('#lb-usps p').css({'visibility':'visible'});
}

if ($("#lb-usps").length > 0) {
	$("#home #lb-usps").after('<div class="controls" id="usp-controls"><a class="usp-prev" href="#">Prev</a><a class="pause" href="#">Pause</a><a class="usp-next" href="#">Next</a></div>');
	$('#lb-usps').cycle({
		fx: 		'fade',
		timeout: 	8000,
		before: 	onBefore, 
		after:   	onAfter,
		next:   	'.usp-next', 
		prev:   	'.usp-prev'
	});
	$("#usp-controls .pause").click(function() {
		if ($(this).hasClass('paused')) {
			$(this).removeClass('paused');
		} else {
			$(this).addClass('paused');
		}
		$('#lb-usps').cycle('toggle');
		return false;
	});
	$("#usp-controls .usp-prev, #usp-controls .usp-next").click(function() {
		return false;
	});
}

if ($(".lightbox").length > 0) {
	$('.lightbox').cycle({
		fx: 		'fade',
		timeout: 	8000
	});
	$(".lightbox").append('<div class="controls"><a class="prev" href="#">Prev</a><a class="pause" href="#">Pause</a><a class="next" href="#">Next</a></div>');
	$("body:not(#home) .lightbox").append('<div class="frame-lg"></div>');
	$("#home .lightbox").cycle('pause').find('.pause').addClass('paused');
	$(".lightbox .pause").click(function() {
		if ($(this).hasClass('paused')) {
			$(this).removeClass('paused');
		} else {
			$(this).addClass('paused');
		}
		$('.lightbox').cycle('toggle');
		return false;
	});
	$(".next").click(function() {
		$('.lightbox').cycle('next');
		return false;
	});
	$(".prev").click(function() {
		$('.lightbox').cycle('prev');
		return false;
	});
}

if ($("#home-selector").length > 0) {
	$(".submit-btn").remove();
	//$('#simple-search select option').removeAttr("selected");
	if( !(navigator.userAgent.match(/iPad/i))) {
		$('#simple-search select').selectmenu({
			change: function(event, options) {
				$('.property-grid').html('<li><img src="/images/templates/loading.gif" alt=""></li>');
				var hometype = $('#hometype').val();
				var numrooms = $('#numrooms').val();
				var pricerange = $('#pricerange').val();
				$.ajax({
			  		url: "/home-selector/" + hometype + "/" + numrooms + "/" + pricerange + "/",
			  		context: document.body,
			  		cache: false,
			  		success: function(data){
						var cont = $('.property-grid li', $(data));
			    		$('.property-grid').html(cont);
			  		}
				});
			}
		});
	}
}
