/**
 * Scripts for Lyrics Online
 * @author Kenichi Maehashi
 * 
 * Copyright (C) 2009-2010 Kenichi Maehashi. All Rights Reserved.
 */

// constants
var kGoogleCustomSearchId = "011131431656538328849:6inodaalfdy";

// global varviables
var gMonitorInterval = 200; // monitor every <gMonitorInterval> ms. (set to 0 to disable monitoring)
var gPopularKeywordsDefault = 5; // number of popular keywords to be shown by default

var gCustomSearchControl; // instance of Google Custom Search Control [google.search.CustomSearchControl]
var gLastQuery = getQueryHash(); // the last executed search query
var gPopularQueryElement = document.getElementById("popular"); // where to render popular keywords cloud

// call on load
(function () {
	// Initialize the Google Custom Search element
	// See: http://code.google.com/intl/ja/apis/ajaxsearch/documentation/reference.html#_class_GSearchControl
	google.load("search", "1", {style: google.loader.themes.SHINY});
	google.setOnLoadCallback(function() {
		// Setup custom search control
		gCustomSearchControl = new google.search.CustomSearchControl(kGoogleCustomSearchId);
		gCustomSearchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
		gCustomSearchControl.setNoResultsString(getHTML("NoResultsString"));
		gCustomSearchControl.setSearchStartingCallback(this, handleSearchStarted);
		gCustomSearchControl.setSearchCompleteCallback(this, handleSearchCompleted);	

		// Setup options
		var options = new google.search.DrawOptions();
		//options.setAutoComplete(true);

		// Draw
		gCustomSearchControl.draw("search", options);
		gCustomSearchControl.execute(getQueryHash());
		gCustomSearchControl.input.focus();

		// Start monitoring hash string and search results
		monitorQueryHash();
		//monitorClearAllResults();
	}, true);


})();

// fired when the user starts new search
function handleSearchStarted(searchControl, searcher, query) {
	setQueryHash(query);
	gLastQuery = query;
	gCustomSearchControl.input.blur();
}

// fired when the search is completed
function handleSearchCompleted(searchControl, searcher) {
	document.forms.telllink.url.value = getCurrentURL();
	document.getElementById("tellothers").style["display"] = "inherit";
}


// fired by monitorClearAllResults when the user cleard search results
/*
function handleClearAllResults() {
	setQueryHash("");
	gLastQuery = "";
	gCustomSearchControl.input.focus();
	document.getElementById("tellothers").style["display"] = "none";
}
*/

// get hidden HTML source for internal use
function getHTML(name){
	return document.getElementById("hidden-" + name).innerHTML;
}

// set the browser's hash string to the query
function setQueryHash(query) {
	if (getQueryHash() !== query) {
		location.hash = encodeURIComponent(query);
	}
}

// get the current browser's hash string
function getQueryHash() {
	return decodeURIComponent(location.hash).substr(1);
}

// get the current browser's search string
function getSearchString() {
	return decodeURIComponent(location.search).substr(1);
}

// get the current URL for the page
function getCurrentURL() {
	return location.protocol + "//" + location.hostname + location.pathname + "#" + getQueryHash();
}

// check form content to fire handleClearAllResults event
/*
function monitorClearAllResults() {
	if (gMonitorInterval <= 0) {return;}
	var currentFormContent = gCustomSearchControl.input.value;

	if (currentFormContent === "" && gLastQuery !== "") {
		gLastQuery = "";
		handleClearAllResults();
	}

	// call me back later!
	setTimeout(monitorClearAllResults, gMonitorInterval);	
}
*/

// watch the browser's hash strings and fire new search if changed
function monitorQueryHash() {
	if (gMonitorInterval <= 0) {return;}
	var currentQuery = getQueryHash();

	if (currentQuery !== gLastQuery) {
		if (currentQuery === "") {
			gCustomSearchControl.clearAllResults();
			document.getElementById("tellothers").style["display"] = "none";
		} else {
			gCustomSearchControl.execute(currentQuery);
		}
		gLastQuery = currentQuery;
	}

	// call me back later!
	setTimeout(monitorQueryHash, gMonitorInterval);	
}

// show popular queries - based on "http://www.google.co.jp/cse/query_renderer.js"
function renderPopularQueries(queries) {
	var separator;
	var queryElement = gPopularQueryElement;
	for (var i = 0; i < queries.popularQueries.length; i++) {
		var a = document.createElement("a");
		a.setAttribute("href", "#" + queries.popularQueries[i].query);
		a.setAttribute("class", "popular-keyword");
		a.appendChild(document.createTextNode(queries.popularQueries[i].query));
		if (i === gPopularKeywordsDefault) {
			var more = document.createElement("a");
			more.setAttribute("id", "popular-more");
			more.setAttribute("href", "#");
			more.setAttribute("onclick", "return showMorePopularKeywords();");
			more.appendChild(document.createTextNode("more..."));

			queryElement = document.createElement("span");
			queryElement.setAttribute("id", "popular-more-keywords");
			gPopularQueryElement.appendChild(more);
			gPopularQueryElement.appendChild(queryElement);
		}
		queryElement.appendChild(a);
		queryElement.appendChild((separator = document.createTextNode(", ")));
	}
	queryElement.removeChild(separator);
	gPopularQueryElement.style["display"] = "inherit";
	// Not supported on IE:
	// gPopularQueryElement.style.setProperty("display", "inherit", "important");
}

// show "more..." popular keywords; must return false
function showMorePopularKeywords() {
	document.getElementById("popular-more").style["display"] = "none";
	document.getElementById("popular-more-keywords").style["display"] = "inherit";
	return false;
}

