$(document).ready(function(){
	$('#headerImages').cycle({
		timeout: 8000,
		speed: 2000
	});
	$('#featuredProperties').cycle({
		fx: 'scrollVert',
		timeout: 8000,
		speed: 2000,
		before: featBefore,
		pause: 1
	});
	
	var firstSlideHeight = $("#featured_0").height();
	$("#featuredProperties").animate({
		height:firstSlideHeight + "px"
	},'slow');
	/*	
	var safeMargin = $("#searchBox").height();
	safeMargin = parseInt(safeMargin) + 40;
	$("#safePurchase").css("margin-top", safeMargin + "px");
	*/
	window.onscroll = function() {
		if( window.XMLHttpRequest ) {
			if (document.documentElement.scrollTop > 227 || self.pageYOffset > 227) {
				$('#searchBox').css('position', 'fixed');
				$('#searchBox').css('top', '5px');
				$('#searchBox').css('left', '50%');
				if ($.browser.msie) {
					if ($.browser.version == "8.0") {
						$('#searchBox').css('margin-left', '-495px');
					} else {
						$('#searchBox').css('margin-left', '-496px');
					}
				} else {
					$('#searchBox').css('margin-left', '-495px');
				}
			} else {
				$('#searchBox').css('position', 'absolute');
				$('#searchBox').css('top', '0');
				$('#searchBox').css('left', '0');
				$('#searchBox').css('margin-left', '0');
			}
		}
	}
	
	$("#mainMenu li").hover(
	function() {
		$(this).children('a').addClass('hovered');
		$('ul', this).slideDown(100);
	},
	function() {
		$(this).children('a').removeClass('hovered');
		$('ul', this).slideUp(100);
	});
	
	var mouseIsInsideRef = false;
	
	$(document).mouseup(function(){
		var refNum = $('#refNum').val();
		if(!mouseIsInsideRef) {
			if (refNum == "") {
				$('#refNum').val("by Reference Number");
			}
		} else {
			if (refNum == "by Reference Number") {
				$('#refNum').val("");
			}
		}
	});
	
	$('#refNum').hover(function(){
		mouseIsInsideRef = true;
	}, function(){
		mouseIsInsideRef = false; 
	});
	
	$("#location").change(function() {
		var searchLocation = $("#location").val();
		$.ajax({
			type: "GET",
			url: "/searchPopulate.php",
			data: 'searchLocation=' + searchLocation,
			dataType: "xml",
			success: function(xml) {
				var selectMenu = $('#city');
				$("#city > option").remove();
				var menuTitle = $(xml).find('menuTitle').text();
				selectMenu.append("<option value=''>" + menuTitle + "</option>");
				
				$(xml).find('menuItem').each(function(){
					var itemCity = $(this).text();
					selectMenu.append("<option value='" + itemCity + "'>" + itemCity + "</option>");
				});
			}
		});
	});
	
	$('#searchForm select').change(function(){
		var searchHref = "http://www.almeriaselectproperty.com/search";
		selectInputs = {"location" : "location", "city" : "city", "property-type" : "type", "min-price" : "price", "beds" : "beds", "pool" : "pool" };
		
		$.each(selectInputs, function(key, value) {
			var searchVar = $("#" + value).val();
			if (searchVar == null || searchVar == "") {
				if (this == "price") {
					if (searchVar == "any" || searchVar == "") {
						searchVar = "any";
					}					
				} else if (this == "beds" || this == "pool") {
					searchVar = "any";
				} else {
					searchVar = "all";
				}
			}
			searchHref = searchHref + "/" + key + "/" + encodeURIComponent(searchVar);
			if (this == "price") {
				//alert(searchVar);
				if (searchVar == "any") {
					searchHref = searchHref + "/max-price/200000000";
				} else {
					var priceMax = parseInt(searchVar)
					priceMax = priceMax + 49999;
					searchHref = searchHref + "/max-price/" + priceMax;
				}
			}
		});
		searchHref = searchHref + "/page/1.html";
		$("#searchForm").attr('action',searchHref);
	});
	
	function featBefore(curr,next,opts) {
		var currentDiv = parseInt(opts.currSlide) + 1;
		if (currentDiv == 3) {
			currentDiv = 0;
		}
		var nextHeight = $("#featured_" + currentDiv).height();
		//alert("CURRENT: " + currentDiv + " - NEXT HEIGHT: " + nextHeight + "px");
		$("#featuredProperties").delay(2000).animate({
			height:nextHeight + "px"
		},'slow');
	}
});
