// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var MAX_TEXT_LENGTH = 25000;

function mark_suggestion_for_destroy(element) {
    $(element).next('.should_destroy').value = 1;
    $(element).up('.suggestion').hide();
}

function controleren() {
    var textlength = $('text').value.length;
    if(textlength > MAX_TEXT_LENGTH) {
        alert("De tekst is te lang. \n" + 
                "De door u ingevoerde tekst is " + textlength + " karakters lang.\n" + 
                "Het maximum is " + MAX_TEXT_LENGTH +".");
        return false;
    }
    else {
        Element.hide('editlinks');
        Element.show('back');
        Element.show('spinner');
        options = {text: $('text').value};
        make_request(options);
        return false;    
    }
}

function verder_met_bewerken() {
    ['div', 'back', 'links', 'msg', 'algemene_problemen', 'spinner'].each(Element.hide);
    ['text', 'editlinks'].each(Element.show);
}

function nieuwe_tekst() {
    $('text').value = '';
    setBackground();
}

function tooltip(html, element) {
    var parent = Element.up(element);
    if((getY(parent)+Element.getHeight(parent))>getY(element)+Element.getHeight(element))
        Tip(html, FIX, [getX(element), (getY(element)+Element.getHeight(element))], 
            DELAY, 0, FOLLOWMOUSE, false, CLICKCLOSE, true, STICKY, true);
    else
        Tip(html, FIX, [getX(element), (getY(parent)+Element.getHeight(parent))], 
            DELAY, 0, FOLLOWMOUSE, false, CLICKCLOSE, true, STICKY, true);
}

function update_word(boundaries_begin, boundaries_end, replace_by) {
    verder_met_bewerken();
    Element.hide('editlinks');
    Element.show('back');
    Element.show('spinner');
    options = {text: $('text').value, boundaries_begin: boundaries_begin, 
        boundaries_end: boundaries_end, replace_by: replace_by};
    make_request(options);
    return false;  
}

function make_request(options) {
    new Ajax.Request('/spellcheck/check', {
        asynchronous:true, evalScripts:true, parameters: options ,method: 'post'});
}

/*
 * Replaces an element's inner HTML
 */
function replace_by_id(id, new_text) {
	var element = document.getElementById(id); 
	element.innerHTML = new_text;
}

function get_inner_text_by_id(id) {
    var hasInnerText =  (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
    var str;
    if(hasInnerText) {
        str = $(id).innerText;
    }else{
        str = $(id).textContent;
    }
    /*str = $(id).innerHTML;
    str = str.replace("<br/>", "\n");
    str = str.replace(/[\<\>]/gi, "");*/
    
    return str;
}

function getY( oElement )
{
  var iReturnValue = 0;
  var scroll = oElement.parentNode.scrollTop;
  while( oElement != null ) {
  iReturnValue += oElement.offsetTop;
  oElement = oElement.offsetParent;
  }
  return iReturnValue - scroll;
}

function getX( oElement )
{
  var iReturnValue = 0;
  var scroll = oElement.parentNode.scrollLeft;
  while( oElement != null ) {
  iReturnValue += oElement.offsetLeft;
  oElement = oElement.offsetParent;
  }
  return iReturnValue - scroll;
}

function resetBackground()
{
    document.getElementById('text').style.background="none";
}

function setBackground()
{
    textarea = document.getElementById('text');
    if(textarea.value == "")
        textarea.style.background="transparent url('/images/background.png') no-repeat";
}

