function closeMessageBoxSuccess(entries)
{
	$("#searchingMessage").html( "<div style='text-align:center'><br /><br />Your search has found <strong>" + entries + "</strong> matching results</div>" );
	setTimeout("$.fn.colorbox.close()", 2000);
}
function closeMessageBoxFail(message)
{
	$("#searchingMessage").html( "<div style='text-align:center'><br /><br />"+message+"</div>" );
	setTimeout("$.fn.colorbox.close()", 2000);
}
$(document).ready(function(){
	var tmpl = $("#lodgingList ul li:first").clone();				// get LI template for search results to use
	
	$("body").append("<div style='display:none'><div id='resetSearchingMessage'><div>Resetting Search for Mount Rainier Lodging</div><div class='ajax-loading'></div></div></div>");
	$("body").append("<div style='display:none'><div id='originalSearchingMessage'><div>Searching for Mount Rainier Lodging</div><div class='ajax-loading'></div></div></div>");
	$("body").append("<div style='display:none'><div id='searchingMessage'></div></div>");
	$("body").append("<pre style='text-align:left;background-color:#fff;font-size:9pt;clear:both;'><div id='ldbg'></div></pre>");

	function snipTextIfNeeded(s)
	{
		var max = 240;
		var result="";
		if (typeof s == "string") {
			if (s.length > max) {
				result = s.substr(0,max) + "...";
			} else {
				result = s;
			}
		}
		return result;
	}
	function firstImgURL(mediaItems) 
	{
		var result = "";
		$.each(mediaItems, function(i, item) {
			if ( ! item.url.match(/youtube/i) ) {				// 
				result = conf.IMG_SERVER_URL + "&size=120x90&u="+item.url;
				return false;						// does a "break;" for the $.each loop
			}
		});

		return result;
	}
	$("#lodgingSearch form").ajaxForm({
		type: "post",
		dataType: "json",
		beforeSubmit: function(formData, jqForm, options) {
			var submitForm = true;
			$.each(formData, function(i, item) {
				if ((item.name=="submit") && (item.value=="Submit")) {
					$("#searchingMessage").html( $("#originalSearchingMessage").html() );
					$.fn.colorbox( {open: true, inline: true, href: "#searchingMessage"} );
					return false;					// does a "break;" for the $.each loop
				} else
				if ((item.name=="submit") && (item.value=="Reset")) {
					$(":checkbox").attr("checked", false);
					$("input.date").val("");
					$("#atoz").attr('checked', true);
					submitForm = false;				// do not submit form
					setTimeout(function(){
						$("#searchingMessage").html( $("#resetSearchingMessage").html() );
						$.fn.colorbox( {open: true, inline: true, href: "#searchingMessage"} );
						$("#lodgingSearch form").submit();
					},250);
					return false;					// does a "break;" for the $.each loop
				}
			});
			return submitForm;
		},
		success: function(data, statusText) {
			$("#lodgingList ul").html("");
			var ul = $("#lodgingList ul");

//$("#ldbg").html( $.dump(data) );
			if (data.entries > 0) {
				$.each(data.data, function(i, row) {
					var li = $(tmpl).clone();
					$(li).find("div.title").html(row.property_id_name);
					$(li).find("div.description").html( snipTextIfNeeded(row.blurb));
					$(li).find("div.photo img").attr("src", firstImgURL(row.mediaItems));
					$(li).find("div.moreInfo a").attr({
						"href": conf.SITE_BASE_URL +"/lp/" + row.id,
						"target": "_blank"
					});
							
					$(ul).append(li);
				});
				$("#lodgingList ul a[target=_blank]").colorbox( { iframe: true, width: "95%", height: "95%" } );
				$("div.photo img").each(function(){
					if ($(this).attr('src')=="") {
						// insert a default image, if there is no image there
						$(this).attr('src',conf.IMG_SERVER_URL+"&size=120x90&u=http://www.visitrainier.com/media/lodging/28/img_0305jpg.jpg");
					}
				});
				setTimeout("closeMessageBoxSuccess("+data.entries+")", 1000);
			} else {
				var message = "No results for your search query. Try broadening your serach by removing some constraints";
				setTimeout("closeMessageBoxFail('"+message+"')", 1000);
			}

			return false;
		}
	});
	$(".XstartOverButton").bind("click", function(){ 
		$(":checkbox").attr("checked", false);
		$("input").val("");

		return false;
	});
});
