var category_tag = null;
var category_name = null;
var timer = null;

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();
	}
	
	url += "/" + jQuery(this).val();

	// track to Google Analytics
	var tag = "";
	if(jQuery("#search_main").hasClass("maximised")) {
		tag = "/search-advanced" + url;
	} else {
		tag = "/search-simple" + url;
	}
	//alert("track to: " + tag);
	pageTracker._trackPageview(tag);
	
	self.location.href = 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", "130px");
	
	// 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", "130px");
	
	// 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", "320px");
	
	// 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;
}
