if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

function link(anchor,source) {
	if (anchor.href.indexOf("?") == -1) {
		if (anchor.href.indexOf("mycareer") != -1) {
			// this pre-space works around a bug in IE6 when there's an @ in the anchor text. all browsers trim() url
			var url = " " + anchor.href + "?s_rid=WS%3Astayz%3A" + source;
			anchor.href = url;
		} else {
			// this pre-space works around a bug in IE6 when there's an @ in the anchor text. all browsers trim() url
			var url = " " + anchor.href + "?s_rid=WS:stayz:" + source;
			anchor.href = url;
		}
	}
	return true;
}
function linktop(anchor) {
	return link(anchor, "top");
}
function linktail(anchor) {
	return link(anchor, "tail");
}

$(document).ready(function() {
	$(".More").show();
	$("#checkin").datepicker({minDate:0, dateFormat:"dd/mm/yy"});
	$("#location").change(function() {
		$("#locId").val("0");
		$("#locLevel").val("");
	});
	$("#location").autocomplete("/ajax/locationSearch.action",
		{'formatItem': function(row, pos, num, term) {
			return row[3] + " " + row[0];
		}, 'formatResult': function(row, pos, num) {
			return row[0];
		}
	});
	$("#location").result(function(e, d, f) {
		$("#locId").val(d[1]);
		$("#locLevel").val(d[2]);
	});
	$('.Rating select').before("<span id='StarRating'><input id='rating' type='hidden' value='" + $(".Rating select").val() + "' name='rating'/><ul></ul></span>");
	$('.Rating select option').each(function() {
		if ($(this).val() > 0)
			$('#StarRating ul').append("<li class='ratingOff'><span style='display:none;'>"+$(this).val()+"</span></li>");
	});
	$('.Rating select').remove();
	$('#StarRating ul li').click(function() {
		$('#rating').val($(this).find("span").html());
		pivotRating($(this).find('span').html());
	}).hover(function() {
		pivotRating($(this).find('span').html());
	}, function() {
		pivotRating($('#rating').val());
	});
	$("#searchbox form").submit(function() {
		if (($("#filterPriceMin").val() != 0 && $("#filterPriceMax").val() != 0) && parseInt($("#filterPriceMin").val()) > parseInt($("#filterPriceMax").val())) {
			alert("The minimum price per night cannot be higher than the maximum price.");
			return false;
		} else if ($("#location").val() == "") {
			alert("Please correct the following problems to search:\n-  You must enter a location or property name to search");
			return false;
		} else {
			return true;
		}
	});
	pivotRating($('#rating').val());
});

function pivotRating(pivot) {
	$('#StarRating ul li').each(function() {
		if ($(this).find('span').html() <= pivot)
			$(this).addClass("ratingOn").removeClass("ratingOff")
		else
			$(this).addClass("ratingOff").removeClass("ratingOn");
	});
}

function showFeatures() {
	$('#searchFeatures').slideDown(200);
	$('#featuresLinkMore').hide();
	$('#featuresLinkLess').show();
}

function hideFeatures() {
	$('#searchFeatures').slideUp(200);
	$('#featuresLinkMore').show();
	$('#featuresLinkLess').hide();
}