//replace all internal references with THIS.fn


this.ZenkokuClass = function() 
{
var englishArray = new Array( "Aichi", "Akita", "Aomori", "Chiba", "Ehime", "Fukui", "Fukuoka", "Fukushima", "Gifu", "Gunma", "Hiroshima", "Hokkaido", "Hyogo", "Ibaraki", "Ishikawa", "Iwate", "Kagawa", "Kagoshima", "Kanagawa", "Kochi", "Kumamoto", "Kyoto", "Mie", "Miyagi", "Miyazaki", "Nagano", "Nagasaki", "Nara", "Nigata", "Oita", "Okayama", "Okinawa", "Osaka", "Saga", "Saitama", "Shiga", "Shimane", "Shizuoka", "Tochigi", "Tokushima", "Tokyo", "Tottori", "Toyama", "Wakayama", "Yamagata", "Yamaguchi", "Yamanashi");
var kanjiArray = new Array("°¦ÃÎ¸©", "½©ÅÄ¸©", "ÀÄ¿¹¸©", "ÀéÍÕ¸©", "°¦É²¸©", "Ê¡°æ¸©", "Ê¡²¬¸©", "Ê¡Åç¸©", "´ôÉì¸©", "·²ÇÏ¸©", "¹­Åç¸©", "ËÌ³¤Æ»", "Ê¼¸Ë¸©", "°ñ¾ë¸©", "ÀÐÀî¸©", "´ä¼ê¸©", "¹áÀî¸©", "¼¯»ùÅç¸©", "¿ÀÆàÀî¸©", "¹âÃÎ¸©", "·§ËÜ¸©", "µþÅÔÉÜ", "»°½Å¸©", "µÜ¾ë¸©", "µÜºê¸©", "Ä¹Ìî¸©", "Ä¹ºê¸©", "ÆàÎÉ¸©", "¿·³ã¸©", "ÂçÊ¬¸©", "²¬»³¸©", "²­Æì¸©", "ÂçºåÉÜ", "º´²ì¸©", "ºë¶Ì¸©", "¼¢²ì¸©", "Åçº¬¸©", "ÀÅ²¬¸©", "ÆÊÌÚ¸©", "ÆÁÅç¸©", "ÅìµþÅÔ", "Ä»¼è¸©", "ÉÙ»³¸©", "ÏÂ²Î»³¸©", "»³·Á¸©", "»³¸ý¸©", "»³Íü¸©" );
var linkArray = new Array( "23/index.html", "05/index.html", "02/index.html", "12/index.html", "38/index.html", "18/index.html", "40/index.html", "07/index.html", "21/index.html", "10/index.html", "34/index.html", "01/index.html", "28/index.html", "08/index.html", "17/index.html", "03/index.html", "37/index.html", "46/index.html", "14/index.html", "39/index.html", "43/index.html", "26/index.html", "24/index.html", "04/index.html", "45/index.html", "20/index.html", "42/index.html", "29/index.html", "15/index.html", "44/index.html", "33/index.html", "47/index.html", "27/index.html", "41/index.html", "11/index.html", "25/index.html", "32/index.html", "22/index.html", "09/index.html", "36/index.html", "13/index.html", "31/index.html", "16/index.html", "30/index.html", "06/index.html", "35/index.html", "19/index.html" );
	
var currentLink = 0;
this.mouseOverEnabled = true;

	this.keyboardNavigation = function( event )
		{
		document.getElementById('englishInput').value = "";
		
		this.mouseOverEnabled = false;
		this.showPrefecturesList();
		switch (event.keyCode) {
			case 8: return false; //Disable BackSpace key

			case 13: return false;//Handled by enterKeyPress below

			case 27: this.hidePrefecturesList(); document.getElementById('search').style.display='none'; return; //ESC
			case 37: newLink = currentLink - 1; break;
			case 39: newLink = currentLink + 1; break;
			case 191: showSearch(); this.hidePrefecturesList(); return; // '/' move focus to search box
			default: newLink = findNext( String.fromCharCode(event.keyCode) );
			}
			
		if (newLink != currentLink) 
			{
			this.highlightSpan( document.getElementById(newLink) );
			
			//Reset previously highlighted link
			if (currentLink>0) {
				this.unHighlightSpan( document.getElementById(currentLink) );
				}
				
			currentLink = newLink;
			}
		}
	this.enterKeyPressed = function() {
		this.gone(currentLink);
		return false; //stop form submit
		}
		
	function findNext( searchKey )
		{
		var i = currentLink + 1; //start cycle from immediate next link
		for ( i; i<=47; ++i )
			if (englishArray[i-1].charAt(0)==searchKey) 
				return i;		
		//If cycled to end with no result
		for ( i=1; i<currentLink; ++i )
			if (englishArray[i-1].charAt(0)==searchKey) 
				return i;
		//If still nothing
		return currentLink;
		}
		
		
		
	this.highlightSpan = function( item )
		{
		item.innerHTML = item.innerHTML.replace("unselected","selected");
		this.setTitle( item.id );
	
		myMouseOver( englishArray[item.id - 1] );
		}
	this.unHighlightSpan = function( item )
		{
		item.innerHTML = item.innerHTML.replace("selected","unselected");
		myMouseOut( englishArray[item.id - 1] );
		}
		

	function myMouseOver(pref) {
		document.getElementById(pref + '0').style.backgroundRepeat = "repeat-x";
		}
	function myMouseOut(pref) {
		document.getElementById(pref + '0').style.backgroundRepeat = "no-repeat";
		}


	//Textlink mouseover - CSS achieves almost same, but conflicts with keypress highlighting ***FIX***?
	this.textlinkMOver = function(newLink) { return;
		if (!this.mouseOverEnabled) return;
		
		//this.highlightSpan( newLink );
		//if (currentLink>0 && currentLink!=newLink.id) //make sure we have a previous link to reset
		//	this.unHighlightSpan( document.getElementById(currentLink) );		
		currentLink = Number(newLink.id);
		}
	this.textlinkMOut = function(lostLink) { return;
		//this.unHighlightSpan( lostLink );
		}
		

	//************************************************************************************************
	
	this.showPrefecturesList = function()
		{
		document.getElementById('links').style.display = 'block';
		document.getElementById('background').style.display = 'block';
		document.getElementById('ShowPrefectures').style.display = 'none';
		}
	this.hidePrefecturesList = function()
		{
		document.getElementById('links').style.display = 'none';
		document.getElementById('background').style.display = 'none';
		document.getElementById('ShowPrefectures').style.display = 'block';
		if (currentLink>0) this.unHighlightSpan( document.getElementById(currentLink) );	
		currentLink=0;
		this.clearTitle();
		}
	
	/* *** ************************************************************************************* *** */

	this.setTitle = function( linkNumber )
		{
		document.getElementById('englishTitle').innerHTML = englishArray[linkNumber - 1];
		document.getElementById('japaneseTitle').innerHTML = kanjiArray[linkNumber - 1];
		document.getElementById('englishInput').value = "";
		}
	this.clearTitle = function() {
		document.getElementById('englishTitle').innerHTML = "";
		document.getElementById('japaneseTitle').innerHTML = "";
		}
	this.resetTitle = function() { 
		this.hidePrefecturesList();//calls clearTitle		
		}
	/* *** ************************************************************************************* *** */

	this.gone = function( prefecture )
		{
//		if (currentLink>0) prefecture=currentLink; //'enter' keypress sometimes coming from focused map-item, NOT pref list as expected!
//		else currentLink = prefecture;
		
		this.hidePrefecturesList();

		document.getElementById('loadingPleaseWait').style.display = "block";
		document.getElementById("resultsFrame").src = "fopen.php?url=" + linkArray[prefecture-1];
//XXX		document.getElementById("resultsFrame").src = "JBrowse.html";
		document.getElementById("resultsFrame").style.display="block";
		
		document.getElementById("pref").value = linkArray[prefecture-1].substr(0,2);

		obj = new autoCompleteClass();
			var english = englishArray[prefecture-1] + (prefecture==41?" Met": " Pref");
			obj.setTitleEntries( english, kanjiArray[prefecture-1], linkArray[prefecture-1] );
	}


	//For consistency with AutoComplete.js
	this.goHistory = function( ref ) {
		return false; //No use on first page
		}
} //End of Zenkoku class

