var category_tag = null;
var category_name = null;
var timer = null;
var g_search_default_value = "Free text search:";

jQuery(document).ready(function(){
								loadKeywords();
								
								if (jQuery("#inp-search").val() == "") {
									jQuery("#inp-search").val(g_search_default_value);
								}
								jQuery("#inp-search").click(g_searchClick);
								jQuery("#inp-search").blur(g_searchBlur);
								});

function g_searchClick() {
	if (jQuery(this).val() == g_search_default_value) {
		jQuery(this).val("");
	}
}
function g_searchBlur() {
	if (jQuery.trim(jQuery(this).val()) == "") {
		jQuery(this).val(g_search_default_value);
	}
}

function initSearch() {
	jQuery("#search_step1 li").hover(onItemMouseIn, onItemMouseOut);
	jQuery("#search_step1 li").bind("click", onCategorySelected);
	
	jQuery("#breadcrumbs img").bind("click", keywordRemoved); //a keyword was removed
	jQuery("#showmore").bind("click", showMoreText);
	jQuery("#closemore").bind("click", closeMoreText);
	
	jQuery("#search_step2").bind("mouseout", function() { restartCloseTimer();	});
	jQuery("#search_step2").bind("mouseover", function() { disableCloseTimer(); });

	jQuery("#search_step2_back").bind("click", function() { closeKeywords(); });
	
	jQuery(".search_select").bind("change", searchSelectChange);
	jQuery("#quick_search").bind("click", showQuickSearch);
	jQuery("#adv_search").bind("click", showAdvMaxSearch);
	jQuery("#search_handle").bind("click", onSearchHandleClick);
	
	//alert(readCookie("SEARCHMODE"));
	if (!jQuery(".nosearch").html()) {
		if (readCookie("SEARCHMODE")) {		
			if (readCookie("SEARCHMODE") == "quick") { showQuickSearch(); }
			else if (readCookie("SEARCHMODE") == "minimised") { showAdvMinSearch(); }
			else if (readCookie("SEARCHMODE") == "maximised") { showAdvMaxSearch(); }
		} else {
			showQuickSearch();
		}
	}
}

function onItemMouseIn(event) {
	jQuery(this).addClass("selected");
}

function onItemMouseOut(event) {
	jQuery(this).removeClass("selected");
}

function onCategorySelected(event) {
	// get the seo_tag of the selected category (remove item_ from the beginning) 
	category_tag = jQuery(this).attr("id").substring(5);

	// get the label of the selected type
	category_name = jQuery(this).html();
	jQuery("#search_step2_heading").html("You Selected: " + category_name);
	
	jQuery("#search_step1").fadeOut("slow");

	showKeywords();

}

function showKeywords(event) {

	// construct ajax call url for retrieving the keywords, based on the current keywords
	var ajax_url = '/content/supersearch-list.php?type=keywords&category=' + category_tag + '&data=' + getSeoTags(',');	
		
		jQuery("#search_loading").fadeIn("slow");
		jQuery("#search_step2_inner").load(ajax_url, function () {
		jQuery("#search_loading").hide();
		
		//reset scroll position
		jQuery("#search_step2_inner").scrollTo(0,0);
		
		//show keyords
		jQuery("#search_step2_bg").slideDown("slow");
		jQuery("#search_step2 li").hover(onItemMouseIn, onItemMouseOut);
		jQuery("#search_step2 li").bind("click", onKeywordSelected); //add click handlers	
	});
}

// use with comma to get the ajax calls data or with slash to get the new search url data
function getSeoTags(separator) {
	var tags = jQuery("#breadcrumbs img");
	var result = "";		  
	for (var i=0; i<tags.length; i++) {
		if (jQuery(tags[i]).hasClass("removed")) continue;
		var id = jQuery(tags[i]).attr("id");
		if (!id) continue; // because of "back to results"
		var tag = id.substring(4); // remove tag_ from the beginning
		if (result!="") { result += separator; } // add separator
		result += tag; // add tag
	}
	return result;
}

function onKeywordSelected(event) {
	// get the seo_tag of the selected type (remove item_ from the beginning) 
	var seo_tag = jQuery(this).attr("id").substring(5);
	goToSearchResults(seo_tag);
}

function goToSearchResults(seo_tag) {
	// construct the url of the search page
	var search_url = '/';				// start with slash (root)
	var tags = getSeoTags('/');			// get tags from previous keywords
	if (tags != '') {					// if there are any, add them to URL
		search_url += tags + '/';
	}
	
	if (seo_tag) {
		search_url += seo_tag;				// get tag from currently selected keyword
	}
	
	window.location = search_url;
}

function keywordRemoved(event) {
	var tags = jQuery("#breadcrumbs img");
	if (tags.length == 1 ) {
		// if just the year keyword remains
		window.location = '/';	// go to the home page
	} else {
		jQuery(this).addClass("removed");
		var search_url = '/' + getSeoTags('/'); // get new URL
		window.location = search_url; // go there
	}
}


function restartCloseTimer() {
	disableCloseTimer();
	timer = window.setInterval(function() {
			disableCloseTimer();
			closeKeywords();
		}, 1000); // miliseconds
}

function disableCloseTimer() {
	window.clearInterval(timer);
}

function closeKeywords() {
	disableCloseTimer();
	jQuery("#search_step2_bg").slideUp("slow", function() {
		disableCloseTimer(); // we are still getting the mouse events when closing
	});
	jQuery("#search_step1").fadeIn("slow");
}


function showMoreText(event) {
	jQuery("#seo-more-text").show();
	return false;
}

function closeMoreText(event) {
	jQuery("#seo-more-text").hide();
	return false;	
}

function searchSelectChange(event) {
	
	var url = "";
	if (jQuery("#keywords_url").val()) {
		url += jQuery("#keywords_url").val();
	}
	
	var keywords = url.split('/');
	var new_url = "";
	var new_keyword = jQuery(this).val();
	
	// remove previous departure keyword
	for (var i = 0; i < keywords.length; i++) {
		if (new_keyword.search("departure-") > -1) {
			if (keywords[i].search("departure-") > -1) {
				continue;
			}
		}
		
		if (keywords[i] != "") {
			new_url += "/" + keywords[i];
		}
	}
	new_url += "/" + jQuery(this).val();
	
	// track to Google Analytics
	var tag = "";
	if(jQuery("#search_main").hasClass("maximised")) {
		tag = "/search-advanced" + new_url;
	} else {
		tag = "/search-simple" + new_url;
	}
	//alert("track to: " + tag);
	pageTracker._trackPageview(tag);
	
	self.location.href = new_url;
}

function onSearchHandleClick(event) {
	
	if (jQuery(this).attr("class").substr(0, 12) == "showadvanced") { 
		showAdvMaxSearch();	
	}
	else { showAdvMinSearch(); }
}

function showQuickSearch() {
	
	if (jQuery(".nosearch").html()) {
		return false;
	}

	
	// set height of header
	jQuery("#header").css("height", "150px");
	
	// show search_strap
	jQuery("#search_strap").hide();
	
	// remove minimised and maximised class
	jQuery("#search_main").removeClass("minimised").removeClass("maximised");
	
	// set active tab
	jQuery("#tabs ul li a#quick_search").parent().addClass("select");
	jQuery("#tabs ul li a#adv_search").parent().removeClass("select");
	
	// show select boxes for Advanced search - Minimised
	/*
	jQuery(".advmin").hide(0, function() {
		jQuery(".advmax").hide(0, function() {
			jQuery(".quick").show();
		});
	});
	*/
	jQuery(".advmin").hide();
	jQuery(".advmax").hide();
	jQuery(".quick").show();
	
	// hide search_handle
	jQuery("#search_handle").hide();
	
	// save searchmode into cookie
	createCookie("SEARCHMODE", "quick", 30);
	return false;
}
function showAdvMinSearch() {
	
	// set height of header
	jQuery("#header").css("height", "150px");
	
	// show search_strap
	jQuery("#search_strap").hide();
	
	// set minimised class
	jQuery("#search_main").removeClass("maximised").addClass("minimised");
	
	// set active tab
	jQuery("#tabs ul li a#quick_search").parent().removeClass("select");
	jQuery("#tabs ul li a#adv_search").parent().addClass("select");
	
	// show select boxes for Advanced search - Minimised
	jQuery(".quick").hide();
	jQuery(".advmax").hide();
	jQuery(".advmin").show();
	
	// show search_handle
	jQuery("#search_handle").html("Show");	
	jQuery("#search_handle").removeClass("hide").addClass("showadvanced").show();
	
	// save searchmode into cookie
	createCookie("SEARCHMODE", "minimised", 30);
	return false;
}
function showAdvMaxSearch() {
	
	if (jQuery(".nosearch").html()) {
		return false;
	}
	
	// set height of header
	jQuery("#header").css("height", "340px");
	
	// show search_strap
	jQuery("#search_strap").show();
	
	// set minimised class
	jQuery("#search_main").removeClass("minimised").addClass("maximised");
	
	// set active tab
	jQuery("#tabs ul li a#quick_search").parent().removeClass("select");
	jQuery("#tabs ul li a#adv_search").parent().addClass("select");
	
	// show select boxes for Advanced search - Minimised
	jQuery(".quick").hide();
	jQuery(".advmin").hide();
	jQuery(".advmax").show();
	
	// show search_handle
	jQuery("#search_handle").html("Hide");
	jQuery("#search_handle").removeClass("showadvanced").addClass("hide").show();
	
	// save searchmode into cookie
	createCookie("SEARCHMODE", "maximised", 30);
	return false;
}

function loadKeywords(){
//	var path = window.location.pathname.split('/');
//	var data = path.join(',');
	var data = '';
	if(jQuery('#valid_tags').val()){
		data += jQuery('#valid_tags').val();
	}
	jQuery('#searchblock').hide();
	jQuery('#searchload').show();
	jQuery.ajax({
			  url: '/content/search-keywords.php?data='+data,
//			  dataType: 'html',
			  success: function(data) {
			//		jQuery(data).appendTo('#ajaxsearch');				  			
			var obj = jQuery(data);
			var target = jQuery();
			obj.filter('select').each(function(){
				cls = jQuery(this).attr('class');
				if(cls.indexOf('disable') > -1){
					jQuery('#'+this.id).attr('disabled', 'disabled');				   
				}
				jQuery('#'+this.id).attr('class', cls);
				html = jQuery(this).html();
//validator fix				jQuery(html).appendTo(jQuery('#'+this.id));
				jQuery('#'+this.id).html(html);
			 });
/*
				addKeywords('continents', data);
				 addKeywords('countries', data);
				 addKeywords('price', data);
				 addKeywords('duration', data);
				 addKeywords('citytownresorts', data);
				 addKeywords('rivers', data);
				 addKeywords('regions', data);
				 addKeywords('types', data);
				 addKeywords('lifestyleandinterests', data);
				 addKeywords('tourpace', data);
				 addKeywords('railoperators', data);
				 addKeywords('touroperators', data);
				 addKeywords('landmarksandattractions', data);
	*/
				jQuery('#searchload').hide();
				jQuery('#searchblock').show();
			  }
	});
}


function addKeywords(elementId, data){
	var part = jQuery(data).filter('#search_'+elementId+'_slct');
	if (part){
		cls = part.attr('class');
		if(cls.indexOf('disable') > -1){
			jQuery('#search_'+elementId+'_slct').attr('disabled', 'disabled');	
		}
		jQuery('#search_'+elementId+'_slct').attr('class', cls);
	//	html = jQuery(data).filter('#search_'+elementId+'_slct').html();
	var html = part.html();
		if(html){
			jQuery(html).appendTo('#search_'+elementId+'_slct');
		}
		html = null;
	}
	part = null;
}

