/* Basic UI */

function setCurrentPage(page)
{
  var e = document.getElementById(page);
  if(e) e.className = 'currentPage';
}

/* KeymanWeb Integration - dynamically load fonts */

// JavaScript Document: enable loading of style sheets referring to embedded fonts
//   for scripts without a suitable font in Windows XP SP2

var embedFonts_cssAlreadyLoaded = [];
var embedFonts_kbds = [
  ['Keyboard_gff_amh_7', 'GeezWeb'],
  ['Keyboard_gff_awn_7', 'GeezWeb'],
  ['Keyboard_gff_bcq_7', 'GeezWeb'],
  ['Keyboard_gff_byn_7', 'GeezWeb'],
  ['Keyboard_gff_gez_7', 'GeezWeb'],
  ['Keyboard_gff_mym_7', 'GeezWeb'],
  ['Keyboard_gff_sgw_7', 'GeezWeb'],
  ['Keyboard_gff_tir_er_7', 'GeezWeb'],
  ['Keyboard_gff_tir_et_7', 'GeezWeb'],
  ['Keyboard_dinkaweb11', 'LatinWeb'],
  ['Keyboard_naijanfd10', 'LatinWeb'],
  ['Keyboard_Nuer13', 'LatinWeb'],
  ['Keyboard_helabasauni', 'SinhalaWeb'],
  ['Keyboard_lao_2008_basic', 'LaoWeb'],
  ['Keyboard_tibetan_ewts_to_unicode', 'TibetanWeb'],
  ['Keyboard_tibetan_direct_unicode', 'TibetanWeb'],
  ['Keyboard_burmese02', 'MyanmarWeb'],
  ['Keyboard_khmer10', 'KhmerWeb'],
  ['Keyboard_isis_oriya', 'OriyaWeb'],
  ['Keyboard_serbian_latin', 'LatinWeb'],
  ['Keyboard_ekwtamil99uni', 'TamilWeb'],
  ['Keyboard_visual_media_tamil_modular', 'TamilWeb'],
  ['Keyboard_visual_media_tamil_typewriter', 'TamilWeb'],
  ['Keyboard_pasifika20', 'LatinWeb'],  ['Keyboard_ps32', 'LatinWeb'],
  ['Keyboard_mbsindhi', 'SindhiWeb']
];  

var lastActiveKeyboard = '';

var KeymanWeb_onkeyboardloaded = function(kbdname)
{
  if(kbdname != '') lastActiveKeyboard = kbdname;
 try
 {
  pageTracker._trackEvent('Keyboards Loaded', kbdname, location.hostname+location.pathname);
 }
 catch(e) {}
 for(var i=0; i<embedFonts_kbds.length;i++)
 {
  if(kbdname==embedFonts_kbds[i][0])
  {
    if(!embedFonts_cssAlreadyLoaded[kbdname])
      {
        var e = document.createElement('link');
        if(location.hostname == 'keymanweb.com' || location.hostname == 'www.keymanweb.com')
          e.href = 'http://s.keymanweb.com/fonts/' + kbdname + '.css';
        else
          e.href = 'http://s.keymanweb.tavultesoft.local/fonts/' + kbdname + '.css';
        e.type = 'text/css';
        e.rel = 'stylesheet';
        var eh = document.getElementsByTagName('head');
        if(eh.length > 0) 
        {
          eh[0].appendChild(e);
          embedFonts_cssAlreadyLoaded[kbdname] = true;
        }
      }
      break;
    }
  }  
}

if(typeof(KeymanWeb) != 'undefined') KeymanWeb.onkeyboardloaded=KeymanWeb_onkeyboardloaded;

/* Keyboard Changed - IE font switching and language example */


KeymanWeb_onkeyboardchange = function(kbdname)
{
  if(kbdname != '') lastActiveKeyboard = kbdname;
  updateExample(kbdname);
  updateLink(kbdname);
  if(typeof(KeyboardChange_EmbedFonts) != 'undefined') KeyboardChange_EmbedFonts(kbdname);
}
if(typeof(KeymanWeb) != 'undefined') KeymanWeb.onkeyboardchange = KeymanWeb_onkeyboardchange;

function updateLink(kbdname)
{
  var e = document.getElementById('bookmarklink');
  if(e)
  {
    kbdname = kbdname.substr(9);
    var re = /^\/(.+)\/(.+)\.php$/; 
    var rex = re.exec(location.pathname);
    if(rex != null)
      e.href = location.protocol + '//' + location.hostname + '/go/'+KeymanWeb_Toolbar_SelectedLanguage+'/'+kbdname+'?page='+rex[2]+'&lang='+rex[1];
  }
}

/* Language Examples AJAX */

var langExamples = [];

function updateExample(kbdname)
{
	var keymanExample = document.getElementById("keymanExample");
	if (!keymanExample)
		return false;
		
  if(kbdname == '')
  {
    keymanExample.innerHTML = 'Select a keyboard from the Keyboard Toolbar above';
    return true;
  }
  
  if(langExamples[KeymanWeb_Toolbar_SelectedLanguage + '_' + kbdname])
  {
    keymanExample.innerHTML = langExamples[KeymanWeb_Toolbar_SelectedLanguage + '_' + kbdname];
    return true;
  }
  
  langExamples[KeymanWeb_Toolbar_SelectedLanguage + '_' + kbdname] = 'Loading...';
		
	var xmlhttp;
	if(window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
	else if(window.ActiveXObject) xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	else return false;
	
  xmlhttp.onreadystatechange = function()
	{
	  if(xmlhttp.readyState == 4)
	  {
      langExamples[KeymanWeb_Toolbar_SelectedLanguage + '_' + kbdname] = keymanExample.innerHTML = xmlhttp.responseText;
	  }
	}
	
	keymanExample.innerHTML = 'Loading...';
		
	xmlhttp.open('GET', '/prog/languageexample.php?keyboard='+kbdname+'&language='+KeymanWeb_Toolbar_SelectedLanguage, true);
	xmlhttp.send(null);
	
	//keymanExample.style.display = 'block';
	//keymanExample.innerHTML = "Example: To enter <span class='highlightExample'>தமிழ்</span>, type <span class='highlightExample'>thamiz</span> on your keyboard";
}

/* Email captcha */

function cancelCaptcha()
{
  var fld = document.getElementById('recaptcha_response_field');
  fld.value = '';
  var e = document.getElementById('captchaBox');
  e.className = '';
  if(window.event) window.event.returnValue = false;
  return false;
}
  
function showCaptcha()
{
  if(typeof(KeymanWeb) != 'undefined') KeymanWeb.Init();
  var fld = document.getElementById('recaptcha_response_field');
  if(fld.value != '') return true;
  var e = document.getElementById('captchaBox');
  e.className = 'captchaBoxVisible';
  window.setTimeout(function() { fld.focus(); }, 100);
  if(window.event) window.event.returnValue = false;
  return false;
}

/* Promo code */

function downloadSelectedKeyboard()
{
  var kn = lastActiveKeyboard; //KeymanWeb.GetActiveKeyboard();
  if(kn == '') openLink("http://www.keymankeyboards.com/");
  else
  {
    var k = KeymanWeb.GetKeyboardDetail(kn);
    if(k == null) openLink("http://www.keymankeyboards.com/");
    else
    {
      if(k.KeyboardID == 0)
        openLink("http://www.keymankeyboards.com/l:iso:"+k.LanguageCode);
      else
        openLink("http://www.keymankeyboards.com/"+k.KeyboardID);
    }
  }   
}

/* Google Analytics and link targets */

function load()
{
  updateALinks();
}

function isTwitterOAuthLink(href)
{
  var re = /^http:\/\/twitter.com\/oauth\/authorize/;
  return href.match(re);
}

function updateALinks()
{
  for(var aa = document.getElementsByTagName('a'), i = 0; i < aa.length; i++)
  {
    if(aa[i].onclick == null && 
      (aa[i].href.substr(0, 5) == 'http:' ||
      aa[i].href.substr(0, 6) == 'https:'))
    {
      if(isHelpLink(aa[i].href)) aa[i].target='KeymanWebHelp';
      else if(isExternalLink(aa[i].href) && !isTwitterOAuthLink(aa[i].href)) aa[i].target='_blank';
      aa[i].onclick = openALink;
    }
  }
}

function openLink(href)
{
  if(typeof pageTracker != 'undefined')
    return pageTracker._link(href);
  location.href = href;
  return false;
}

function isExternalLink(href)
{
  var re = new RegExp("^"+location.protocol+"//"+location.hostname);
  if(href.match(/^http(s?):/))
    return !href.match(re);
  return false;
}

function isHelpLink(href)
{
  var re = /^http(s?):\/\/((help\.keymanweb\.com)|(help\.keymanweb\.tavultesoft\.local))/i;
  return href.match(re);
}

function isTavultesoftLink(href)
{
  var re = /^http(s?):\/\/(((.+\.)?tavultesoft\.com)|((.+\.)?keymankeyboards.com)|((.+\.)?keymanweb.com)|(.+\.tavultesoft\.local))/i;
  return href.match(re);
}

function openALink(e)
{
  var elem;
  if(typeof e != 'undefined' && typeof e.href != 'undefined') elem = e;
  else if(typeof this.href == 'undefined') return true;
  else elem = this;
  
  if(isExternalLink(elem.href))
  {
    if(isTavultesoftLink(elem.href))
    {
      var url = pageTracker._getLinkerUrl(elem.href);
      window.open(url, elem.target);
      return false;
    }
    pageTracker._trackEvent('Outgoing links', elem.href, location.hostname+location.pathname);
  }
  return true;
}

/* Dropdown UI */

var dropDownMenu = new function()
{
  var timeout         = 500;
  var closetimer      = 0;
  var ddmenuitem      = 0;

  // open hidden layer
  this.open = function(id)
  {
  	// cancel close timer
  	this.cancelCloseTimeout();
  
  	// close old layer
  	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
  
  	// get new layer and show it
  	ddmenuitem = document.getElementById(id);
  	ddmenuitem.style.visibility = 'visible';
  }
  
  // close showed layer
  this.close = function()
  {
  	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
  }
  
  // go close timer
  this.closeTimeout = function()
  {
  	this.closetimer = window.setTimeout(this.close, timeout);
  }
  
  // cancel close timer
  this.cancelCloseTimeout = function()
  {
  	if(this.closetimer)
  	{
  		window.clearTimeout(this.closetimer);
  		this.closetimer = null;
  	}
  }
  
  if(document.addEventListener)
    document.addEventListener('click', this.close, false);
  else if(document.attachEvent)
    document.attachEvent('onclick', this.close);
  else
    document.onclick = this.close;
}



