Event.observe(window, 'load', init);

window.onbeforeunload = function () {
   // This fucntion does nothing.  It won't spawn a confirmation dialog
   // But it will ensure that the page is not cached by the browser.
}

function init () {
	flashMessage();
	initSearchInput();
		/*
	var jar = new CookieJar ({
		expires: 60,
		path: '/'
	});

	var poll = { 
		name: 'Poll',
		voted: 'false'
	};
	jar.put('polls',poll);
	polls = jar.get('polls');
	alert(polls.name);
	
	jar.remove('polls');*/
}

function flashMessage() {
	if($('flashMessageContainer') != null) {
		$('flashMessage').visualEffect('Highlight',{delay: 1, startcolor: '#FFCC00', endcolor: '#FFFFFF'});
		$('flashMessage').visualEffect('Fade',{delay: 5});
	}
}

/**
* A modded function from www.radeef.com,
* original function was created by Jawish. 
* dude I hope you wont get pissed about this.
*/
function DhivehiKey(e) {
	if (!e) var e = window.event;
	if (e.keyCode) pressedCode = e.keyCode;
	else if (e.which) pressedCode = e.which;
	var pressed = String.fromCharCode(pressedCode);
	var elem = getKeyPressEventTarget(e);
    var en = "hSnrbLkwvmfdtlgNsDzTypjcXHKJRCBMYZWGQVaAiIuUeEoOqF,;?";
    var dv = ["\u0780", "\u0781", "\u0782", "\u0783", "\u0784", "\u0785", "\u0786", "\u0787", "\u0788", "\u0789", "\u078A", "\u078B", "\u078C", "\u078D", "\u078E", "\u078F", "\u0790", "\u0791", "\u0792", "\u0793", "\u0794", "\u0795", "\u0796", "\u0797", "\u0798", "\u0799", "\u079A", "\u079B", "\u079C", "\u079D", "\u079E", "\u079F", "\u07A0", "\u07A1", "\u07A2", "\u07A3", "\u07A4", "\u07A5", "\u07A6", "\u07A7", "\u07A8", "\u07A9", "\u07AA", "\u07AB", "\u07AC", "\u07AD", "\u07AE", "\u07AF", "\u07B0", "\uFDF2", "\u060C", "\u061B", "\u061F"];
	var pos = en.indexOf(pressed);
	if (pos != -1 && e.ctrlKey == false) {
		if (document.selection) {
            sel = document.selection.createRange();
            sel.text = dv[pos];
			//alert(sel.text);
        } else {
           var selstart = elem.selectionStart;
           elem.value = elem.value.substring(0, selstart) + dv[pos] + elem.value.substring(elem.selectionEnd);
        }
        return false;
	} else {
		//
	}	
} /* end DhivehiKey() */

/**
* Checks if the input charactor is Thaana
* @e {event}, keboard pressed event.
* @return boolean
*/
function checkThaana(e) {
	if (!e) var e = window.event;
	if (e.keyCode) pressedCode = e.keyCode;
	else if (e.which) pressedCode = e.which;
	var pressed = String.fromCharCode(pressedCode);
	var elem = getKeyPressEventTarget(e);
	var thaana=false;
	var dv = ["\u0780", "\u0781", "\u0782", "\u0783", "\u0784", "\u0785", "\u0786", "\u0787", "\u0788", "\u0789", "\u078A", "\u078B", "\u078C", "\u078D", "\u078E", "\u078F", "\u0790", "\u0791", "\u0792", "\u0793", "\u0794", "\u0795", "\u0796", "\u0797", "\u0798", "\u0799", "\u079A", "\u079B", "\u079C", "\u079D", "\u079E", "\u079F", "\u07A0", "\u07A1", "\u07A2", "\u07A3", "\u07A4", "\u07A5", "\u07A6", "\u07A7", "\u07A8", "\u07A9", "\u07AA", "\u07AB", "\u07AC", "\u07AD", "\u07AE", "\u07AF", "\u07B0", "\uFDF2", "\u060C", "\u061B", "\u061F"];
	dv.each(function(id,i){
		if (id==pressed) {
			thaana = true;
		}
	});
	return thaana;
} /* end checkThaana */


function getKeyPressEventTarget (ev) {
	var targ=null;
	if(ev.target){
		targ=ev.target;
	} else {
		if(ev.srcElement) {
			targ=ev.srcElement;
		}
	}
	if(targ.nodeType==3) {
		targ=targ.parentNode;
	}
	return targ;
}

/**
* Original Function from www.radheef.com
* JavaScript Unicode Keyboard Handler
* By : Jawish Hameed
* jaa.technova.com.mv
* Version: 2.0 ( Jan 2006 )
*/
function juk_HandleKeyPress(evt) {
    var evt = evt ? evt : window.event;
    var elem = evt.target ? evt.target : evt.srcElement;
    var keycode = evt.which ? evt.which : evt.keyCode;
    var english_ascii = "hSnrbLkwvmfdtlgNsDzTypjcXHKJRCBMYZWGQVaAiIuUeEoOqF,;?";
    var dhivehi_unicode = ["\u0780", "\u0781", "\u0782", "\u0783", "\u0784", "\u0785", "\u0786", "\u0787", "\u0788", "\u0789", "\u078A", "\u078B", "\u078C", "\u078D", "\u078E", "\u078F", "\u0790", "\u0791", "\u0792", "\u0793", "\u0794", "\u0795", "\u0796", "\u0797", "\u0798", "\u0799", "\u079A", "\u079B", "\u079C", "\u079D", "\u079E", "\u079F", "\u07A0", "\u07A1", "\u07A2", "\u07A3", "\u07A4", "\u07A5", "\u07A6", "\u07A7", "\u07A8", "\u07A9", "\u07AA", "\u07AB", "\u07AC", "\u07AD", "\u07AE", "\u07AF", "\u07B0", "\uFDF2", "\u060C", "\u061B", "\u061F"];
    var pos = english_ascii.indexOf(String.fromCharCode(keycode));
    if (pos != -1 && evt.ctrlKey == false) {
        if (document.selection) {
            sel = document.selection.createRange();
            sel.text = dhivehi_unicode[pos];
        } else {
            var selstart = elem.selectionStart;
            elem.value = elem.value.substring(0, selstart) + dhivehi_unicode[pos] + elem.value.substring(elem.selectionEnd);
        }
        if (navigator.appName == "Microsoft Internet Explorer") {
            evt.keyCode = 0;
        }
        selstart = selstart + 1;
        elem.setSelectionRange(selstart, selstart);
        return false;
    } else {
        if (keycode == 32 || keycode == 10 || keycode == 13) {
            document.getElementById("msg").innerText = "a" + String.fromCharCode(keycode).length + "a";
            var selstart = elem.selectionStart + 1;
            elem.value = elem.value.substring(0, selstart) + String.fromCharCode(keycode) + elem.value.substring(elem.selectionEnd);
            selstart = selstart + 1;
            elem.setSelectionRange(selstart, selstart);
            return false;
        }
    }
} /* end juk_HandleKeyPress() */

/**
* Checks if native keyboard is enabled, if not
* emulate thaana keyboard
*/
function initSearchInput() {
	if ($('siteSearch') != null) {
		if ($('siteSearch').hasClassName('dv')){
			$('inputSearch').onkeypress = DhivehiKey;
		}
	}
} /* end initKeyboard */


/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
} /* end doHighlight */


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
  
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
  document.body.innerHTML = bodyText;
  return true;
} /* end highlightSearchTerms */
