
/**
* Main functionality for Bed and Breakfast in Yorkshire
*
* @prerequisite jQuery.js
* @prerequisite websell_common.js
*
* @author Websell Masters Ltd
* @copyright Websell Masters Ltd
* @date 17/08/09
*/

/**
* On load
*/
$(function() {
	/**
	* Make all hyperlinks open in a seperate window
	*/
	$("a").live("click", openBlankWindow);
	
	function openBlankWindow() {
		window.open($(this).attr("href"));
		return false;
	}
	
	/**
	* Show the 'keywords' hint in the search field
	*/
	markKeywordsHint();
	
	function markKeywordsHint() {
		if ($.trim($("#keywords").val())=="") {
			$("#keywords").addClass("keywords_box");
		} else {
			$("#keywords").removeClass("keywords_box");
		}
	}
	
	$("#keywords").focus(function() { $(this).removeClass("keywords_box"); });
	$("#keywords").blur(markKeywordsHint);
	
	/**
	* Replace the search button, making it pretty and usable
	*/
	$("#search_button").replaceWith("<a href=\"#\" class=\"button\" id=\"search_button\"><span>Search</span></a>");
		
	/**
	* Scroll the top images
	* NOTE: very slow on IE (rubbish javascript engine)
	* KEEP DISABLED ON IE 6/7/8
	*/
	/*var speed=0.05;
	
	if (!$.browser.msie) {
		scrollImage($("#top_images"));
		
		//Freeze the scrolling on hover
		$("#top_images").hover(function() {
			$(this).stop(true);
		}, function() {
			scrollImage($(this));
		});
	}
	
	//Scroll the contents of an object until the first item has been traversed
	function scrollImage(obj){
		var firstImage = $(obj).find("a:first");
		var imageWidth = firstImage.width();
		var time=((imageWidth+parseInt($(obj).css("text-indent")))/speed)*2;

		$(obj).animate({textIndent:-imageWidth}, time, "linear", function(){
			//push the image to the back of the pile
			firstImage.remove();
			$(obj).append(firstImage);
			
			//go again
			$(obj).css("text-indent",0);
			scrollImage($(obj));
		});
	}*/
	
	/**
	* Animate the 'start' hand
	*/
	/**
	* Bob the hand right and back (in a loop)
	*/
	/*var start_hand = $("#start_hand");
	var repeat_times = 8;
	var hand_bobbing = window.setInterval(function() {
		//to stop it being annoying, stop after 8 bobs
		if (repeat_times<1) {
			window.clearInterval(hand_bobbing);
			start_hand.fadeOut("slow");
		}
		
		//make the end bob back  and forth
		start_hand.animate({
			left: "730px"
		}, 300, "swing", function() {
			start_hand.animate({
				left: "690px"
			}, 300, "swing");
		});
		
		repeat_times--;
	}, 700);*/
	
	/**
	* Filter towns from selection
	*/
	filterTowns();
	$("#town").change(filterTowns);
	
	function filterTowns() {
		if ($("#town").val()!=-1) {
			var selectedTown = "#listings_" + escape($("#town").val());
		
			//keep this bit simple as very time consuming (>1sec)
			$(".town_listings:not(" + selectedTown + ")").css("display","none");
			$(selectedTown).css("display","block");
		} else {
			//make all listings visible
			$(".town_listings").css("display","block");
		}
	}
	
	/**
	* Highlight the current listing/suggestion we are hovering over
	*/
	$(".listing, #suggestions li").live("mouseover", function() {
		$(this).addClass("hover");
	});
	
	$(".listing, #suggestions li").live("mouseout", function() {
		$(this).removeClass("hover");
	});
	
	/**
	* Fill suggestions box with titles and address word-matches
	*/
	var suggestions = $("#suggestions");
	var search_text = $("#keywords");
	
	search_text.attr("autocomplete","off");

	//Update suggestions list
	function updateSuggestions() {
		var search_value = $.trim(search_text.val());
		
		//clear the current suggestions
		suggestions.html("");
		
		if (search_value!="") {
			//retrieve the suggestions from the titles
			var suggestions_data = $(".listing h4:visible:suggestion(" + search_value + ")");
			
			/* 
			* The following code can be used to add together sources
			var data_suggestions = $("#.listing address:visible:suggestion(" + search_value + ")");
			var suggestions_data = title_suggestions.add(data_suggestions);
			*/
			
			//do not include the current search term in the suggestions
			var filtered_data = new Array();
			$.each(suggestions_data, function() {
				if ($(this).text().toUpperCase()!=search_value.toUpperCase() && $.inArray($(this).text(),filtered_data)==-1) {
					filtered_data.push($(this).text());
				}
			});
			
			filtered_data.sort();
			
			//fill the suggestions						
			if (filtered_data.length>0) {
				suggestions.show();
				suggestions.attr("scrollTop","0");
				$.each(filtered_data, function() {
					var suggestion = this.replace(/\&/g,"&amp;"); //escape 
					
					suggestions.append("<li>" + suggestion + "</li>");
				});
			} else {
				//hide the suggestions box
				suggestions.hide();
			}
		} else {
			//hide the suggestions box
			suggestions.hide();
		}
	};
	
	//update the search text with the selected suggestion
	function selectSuggestion(obj) {
		var selected_text = $.trim($(obj).text());
		
		if (selected_text!="") {
			search_text.val(selected_text);
			suggestions.hide();
		}
	}
	
	updateSuggestions();
	
	search_text.keyup(updateSuggestions);
	search_text.focus(updateSuggestions);
	search_text.hover(function(event) {
		$(this).addClass("hover");
	}, function(event) {
		$(this).removeClass("hover");
	});
	
	$("#suggestions li").live("click", function() {
		selectSuggestion($(this));
		return false;
	});
	
	$(window).click(function(event) {
		if (!search_text.hasClass("hover")) {
			suggestions.hide();
		}
	});
	
	/**
	* Override default search operation with javascript search of current information
	*/
	$("#search_button").click(function() {
		highlightSearchTerms();
		return false;
	});
	
	$("#search_form").attr("action","#");
	$("#search_form").submit(function() {
		highlightSearchTerms();
		return false;
	});
	
	function highlightSearchTerms() {
		//remove any previous results
		$(".found_term").each(function(data) {
			$(this).replaceWith($(this).html());
		});
		
		//filter all of the listings by town
		$(".listing").css("display","block");
		filterTowns();
		
		//show all listings if no search term was given
		if ($.trim($("#keywords").val())=="") {
			return;
		}
		
		//run the search (do not include elements with html sub-elements)
		scanContents($(".town_listings:visible h3 strong"));
		scanContents($(".town_listings:visible h4 a"));
		scanContents($(".town_listings:visible address"));
		
		showHideTownListings();
	}
	
	function showHideTownListings() {
		//hide all listings and towns
		$(".listing").css("display","none");
		$(".town_listings").css("display","none");
		
		//if the term has been found in a listing, display it and it's town category
		$(".found_term").parents(".listing").css("display","block")
			.parents(".town_listings").css("display","block");
	}
	
	function scanContents(container) {
		//recursively accumulate the text in a container (and it's sub-containers)
		var search_term = $.trim($("#keywords").val());
		
		//split the search terms into individual terms
		var search_terms = search_term.split(" ");
		
		$.each(search_terms, function(i, obj) {
			$.each($(container), function(i, obj2) {
				if ($.trim($(obj2).html())!="") {
					var words = $(obj2).html().split(" ");
					var word_position = words.find(obj);
					var found_term = word_position;
					
					//if the word is found, wrap a class around it
					if (word_position!=-1) {
						while(found_term!=-1) {
							words[word_position] = "<span class=\"found_term\">" + words[word_position] + "</span>";
						
							found_term = words.shrink(word_position+1).find(search_term);
							word_position += found_term+1;
						}
						$(this).html(words.join(" "));
					}
				}
			});
		});
	}
});

