function refresh_lang(){
 refresh_charPalette();
 refresh_charIndex();
 refresh_translate_flag();
 refresh_browse();
 refresh_search_form();
 refresh_search_results();
 refresh_resources();
 refresh_dictionaries();
 focus_text();
}

function refresh_lang_wordbank_switch(){
 refresh_charPalette();
 refresh_charIndex();
 refresh_translate_flag();
 refresh_keyboard_icon();
 set_text();
 set_item();
 refresh_browse();
 clear_timer();
 focus_text();
}

function is_wordbank(){
	return get_main_form().elements.lang;
}



function get_lang(){
	var main_form = get_main_form();
	if (main_form.elements.lang){
		return main_form.elements.lang.value;
	} else {
		return get_dictionary(main_form.elements.direction.value);
	}
}

function get_dictionary(direction){
  var langs = direction.split('-');
  var dictionary = (langs[1] == 'en') ? langs[2] : langs[1];
  return dictionary;
}

function refresh_translate_flag(){
  var lang = get_lang();
  var flag_img = ebi('translate_flag');
  if (flag_img){
  	flag_img.src = 'images/' + lang + '.jpg';
  }
  var charIndex_img = ebi('open_index');
  if (charIndex_img){
  	charIndex_img.src ='images/charindex-' + lang + '.gif';
  }
}

function refresh_keyboard_icon(){
  var lang = get_lang();
  var keyboard_img = ebi('open_palette');
  if (keyboard_img){
	  keyboard_img.src = '/images/keyboard_' + lang + '.gif';
  }
}

function refresh_dictionaries(){
 if (ebi('dictionaries')){
   var direction = get_main_form().elements.direction.value;
   var oldDirection = get_main_form().elements.oldDirection;
   switch_home_direction(get_dictionary(oldDirection.value), get_dictionary(direction));
   oldDirection.value = direction;
 }
}

function refresh_charPalette(){
 if (ebi('charPalette_wrapper')){
  var url = '/util/CharPaletteSubPage.html?direction=' + get_direction();
  get_request(url, handle_char_switch);
 }
}

function refresh_charIndex(){
 if (ebi('charIndex_wrapper')){
  var url = '/charIndex/EntireCharIndexSubPage.html?direction=' + get_direction();
  get_request(url, handle_index_switch);
 }
}

function refresh_browse() {
 if (ebi('wordwheel')){
  var url = is_wordbank()? '/wordbank/WordbankWordwheelSubPage.html': '/browse/WordwheelSubPage.html';
  post_form(url, handle_browse, get_main_form());
  start_timer();
 }
}

function refresh_search_results(){
	if (ebi('search_results_content') || ebi('no_results')){
		submit_search();
	}
	if (ebi('entrySearch_marker')){
		load_search();
		
	}
}

function refresh_resources(){
 if (ebi('resource_wrapper')){
  var direction = get_main_form().elements.direction.value;
  var url = '/util/HomeResourcesSubPage.html?direction=' + direction;
  get_request(url, handle_resources_switch);
 }
 if (ebi('resource_link_wrapper')){
  var direction = get_main_form().elements.direction.value;
  var url = '/util/ResourceLinkSubPage.html?direction=' + direction;
  get_request(url, handle_resource_link_switch);
 }
}

function refresh_compounds(){
 if (!ebi('wordwheel')){
  var direction = get_main_form().elements.direction.value;
  if (direction == 'b-zh-en'){
	var input = ebi('go_text').value;
	if (input == "" || /^[a-zA-Z]/.test(input)){
		ebi('compounds_dropdown_wrapper').innerHTML = '';
	} else {
		var url = '/util/CompoundsSubPage.html?sp=' + input;
		get_request(url, handle_compounds);
	}
  }	 
 } 
}


function load_oldo_page(url){
	window.location = add_direction(url);
}

function clear_form(){
	ebi('translate_form').reset();
}

function get_direction(){
	if (is_wordbank()){
		return 'b-en-' + ebi('translate_languages_choices').value;
	} else {
		return ebi('translate_languages_choices').value;
	}
}

function get_other_direction(){
	var index = get_other_direction_index();
	return ebi("translate_languages_choices").options[index].value;	
}

function add_direction(url){
 	var direction = get_direction();
 	var term = 'direction=' + direction;
 	if (url.indexOf('?') < 0){
 		return url + '?' + term;
 	} else {
 		return url + '&' + term;
 	}
}

function add_lang(url){
	var lang = get_lang();
 	var term = 'lang=' + lang;
 	if (url.indexOf('?') < 0){
 		return url + '?' + term;
 	} else {
 		return url + '&' + term;
 	}
	
}

function switch_to_search(){
  post_form('/search/SearchSubPage.html', handle_side_switch, get_main_form());
  clear_timer();
}

function switch_to_browse(){
	if (ebi("side_wrapper")){
  	  post_form('/browse/BrowseSubPage.html', handle_side_switch, get_main_form());
	} else {
	  load_oldo_page('/browse/BrowsePage.html')
	}
}

function handle_article (req) {
  switch_main(req);
}

function get_other_direction_index(){
    var select = ebi("translate_languages_choices");
    var index = select.selectedIndex;
    var dict = Math.floor(index / 2);
    var dir = index % 2;
    //reverse direction.
    return (dict * 2 + (dir? 0 : 1));
}

function switch_direction(){
    var select = ebi("translate_languages_choices");
    select.selectedIndex = get_other_direction_index();
    refresh_lang();
}

function switch_home_direction(from, to){
  var from_img = ebi('home_' + from + '_img');
  if (from_img){
  	from_img.src='images/home_' + from + '_off.gif';
  }
  var to_img = ebi('home_' + to + '_img');
  if (to_img){
  	to_img.src='images/home_' + to + '_on.gif';
  }
}

function set_direction(direction){
    var select = ebi("translate_languages_choices");
	select.value=direction;
	refresh_lang();
}

function switch_wordbank_lang(lang){
    var select = ebi("translate_languages_choices");
	select.value=lang;
//	var browse_field = ebi('go_text');
//	browse_field.value = text;
//	var url = "/wordbank/WordbankWordwheelSubPage.html";
	refresh_lang_wordbank_switch();
//	post_form(url, handle_browse, get_main_form());
//	open_wordbank_entry(id);
}

function set_text(){
	var lang = get_lang();
	var text_input = ebi('browse_text_' + lang);
	if (text_input){
		var new_value = text_input.value
		new_value = new_value.replace('\\"', '','g');
		ebi('go_text').value = new_value;
	}
}

function set_item(){
	var wordbank_id = ebi('wordbank_id');
	if (wordbank_id){
		open_wordbank_entry(wordbank_id.value);
	}
}

function handle_entry(req){
  switch_main(req);
}

function handle_side_switch(req){
  switch_side(req);
}

function handle_char_switch(req){
  replace_element('charPalette_wrapper', req);
}

function handle_index_switch(req){
  replace_element('charIndex_wrapper', req);
}

function handle_resources_switch(req){
  replace_element('resource_wrapper', req);
}

function handle_resource_link_switch(req){
  replace_element('resource_link_wrapper', req);
}

function load_entry(uri){
  window.location = ('/view/EntryPage.html?sp=' + uri /* + '&' + window.location.search */ ); 
}

function open_entry (uri){
	open_entry_with_search(uri, '');
}

function open_entry_with_search(uri, search){
  clear_timer();
  var url;
  url = add_direction('/view/EntrySubPage.html?sp=' + uri);
  if (search.length > 0){
  	url += '&_hi=' + search + '&resultsLink=1';
  }
  get_request(url, handle_entry);   
}

function open_wordbank_entry(uri){
  var url = add_lang('/wordbank/WordbankSubPage.html?sp=S' + uri);
  get_request(url, handle_entry);
}

function click_char(c){
    insertAtCursor(ebi("go_text"),c);
    keyup();
}

function click_no_char(){
	doNothingAtCursor(ebi("go_text"));
}

var ccja_diacritics = '\u309b\u309c';
var ccja_combiners = ['\u304b\u304d\u304f\u3051\u3053\u3055\u3057\u3059\u305b\u305d\u305f\u3061\u3064\u3066\u3068\u306f\u3072\u3075\u3078\u307b\u30ab\u30ad\u30af\u30b1\u30b3\u30b5\u30b7\u30b9\u30bb\u30bd\u30bf\u30c1\u30c4\u30c6\u30c8\u30cf\u30d2\u30d5\u30d8\u30db','\u306f\u3072\u3075\u3078\u307b\u30cf\u30d2\u30d5\u30d8\u30db'];

function click_char_ja(c){
	var field = ebi("go_text");
    insertAtCursor(field,c);
	if (ccja_diacritics.indexOf(c) >= 0){
		//normalize field contents
		var text = field.value;
		var text_n = normalize_ja(text)
		field.value = text_n;
	}
    keyup();
}

function normalize_ja(text){
	var n = 1; //start with second char
	while (n < text.length){
		var c = text.charAt(n);
		var d = ccja_diacritics.indexOf(c);
		if (d >= 0){
			var pre_c = text.charAt(n-1);
			if (ccja_combiners[d].indexOf(pre_c) >= 0){
				//create altered char.
				var code = pre_c.charCodeAt(0);
				code += d + 1;
				//alter text
				text = text.substring(0,n-1) + String.fromCharCode(code) + text.substring(n+1);
				n--;
			}
		}
		n++;
	}
	return text;
}

function charPalette_up(){
 ebi("charPalette_lower").style.display="none";
 ebi("charPalette_upper").style.display="block";

}
function charPalette_down(){
 ebi("charPalette_upper").style.display="none";
 ebi("charPalette_lower").style.display="block";
}

function clear_text(){
	if (ebi('go_text')){
		ebi('go_text').value = '';
	}
}

function focus_text(){
	if (ebi('go_text')){
		ebi('go_text').focus();
	}
}

/**
 * onClick of the wordwheel term, populate the browse list with the selected term
 * see bug 0027
 **/
function populate_browse_field(li) {

   var browse_field = ebi('go_text');
   var hw;
   if(li.childNodes[0].href)
      hw = li.childNodes[0].innerHTML;
   else
      hw = li.childNodes[1].innerHTML;
      
	/* FIXME - is there a better regex for all combining diacritics?
	   this only covers combining acute; \u301 */
   hw = hw.replace(/[\u0301\|]+/, '');
   
   browse_field.value = hw;
   var spanIndex = browse_field.value.indexOf('<');
   if(spanIndex>0)
     browse_field.value = browse_field.value.substring(0, spanIndex);
}

