/**
 * HighlighterConfig class
 * Handle the global configuration of the script.
 * Change your settings here.
 */
function HighlighterConfig () {
  this.lang        = "fr";
  this.tutorialUrl = "";
}

var config = new HighlighterConfig();

/**
 * i18n
 */
var i18n = {
  "fr": {
    "full text": "texte",
    "colored": "coloré"
  }
}

function _(s) {
  if (typeof(i18n) != "undefined" && i18n[config.lang] && i18n[config.lang][s])
    return i18n[config.lang][s];
  return s;
}

/**
 * Snippet class
 * Represents one snippet of code.
 */
function Snippet(data, elem, colored_html) {
  this.data = data;
  this.elem = elem;
  this.colored_html = colored_html;
  
  this.highlight();
}

/**
 * Highlight a snippet of code.
 */
Snippet.prototype.highlight = function() {
  if (this.elem.parentNode.tagName == "PRE") {
      this.text_html       = this.elem.parentNode;
      parent               = this.text_html.parentNode;
    } else {
      this.text_html       = this.elem;
      parent               = this.elem.parentNode;
    }
    
    /* Create the container */
    this.snippet           = document.createElement("div");
    this.snippet.className = "ajax-syntax-highlighter";
    
    /* Create the menu bar */
    var bar                = document.createElement("ul");
    bar.className          = "bar";
    
    /* Display the language */
    var language           = document.createElement("li");
    language.className     = "language";
    
    var txt                = document.createTextNode(this.data.language);
    language.appendChild(txt);
    bar.appendChild(language);

    /* Display the switch mode button */
    this.mode               = document.createElement("li");
    this.mode.className         = "tools";
    var txt                = document.createTextNode(_("full text"));
    this.mode.appendChild(txt);
    bar.appendChild(this.mode);
      
    this.snippet.appendChild(bar);

    /* Create the colored version */
    this.code              = document.createElement("div");
    this.code.innerHTML    = this.colored_html;
    this.snippet.appendChild(this.code);
    
    /* Hide the semantic version and display the colored */
    parent.replaceChild(this.snippet, this.text_html);
    
    /* Create and hide the textarea */
    this.textarea_html              = document.createElement("textarea");
    this.textarea_html.value        = this.elem.firstChild.nodeValue;
    
    //YAHOO.util.Dom.setStyle(this.textarea_html, "height", YAHOO.util.Dom.getStyle(this.code, "height"));
    $(this.textarea_html).css("height", $(this.code).css("height"));
    this.textarea_html.style.display = "none";
    //YAHOO.util.Dom.addClass(this.textarea_html, "codeash");
    $(this.textarea_html).addClass("codeash");

    this.snippet.appendChild(this.textarea_html);

    
    this.colored            = true;
      
    //YAHOO.util.Event.addListener(this.mode, "click", this.changeMode, this);
    $(this.mode).bind("click", {param: this}, this.changeMode);
};

/**
 * Change the selected mode when clicking on the button.
 */
Snippet.prototype.changeMode = function(e) {
    var me = e.data.param;
  if (me.colored) {
    me.code.style.display = "none";
    me.textarea_html.style.display = "block";
    
    /* Change the button's text */
    me.mode.firstChild.nodeValue = _("colored");
    
    /* Select the text */
    me.textarea_html.focus();
    me.textarea_html.select();

   
     me.colored = false;
  } else {
    me.textarea_html.style.display = "none";
    me.code.style.display = "block";
    
    /* Change the button's text */
    me.mode.firstChild.nodeValue = _("full text");
   
    me.colored = true;
  }
};


/**
 * Highlighter class
 * Select all the codes blocks and creates the snippets.
 */
function Highlighter() {
  var snippets = new Array();
  var nodes = $("code[class^=language-]");
  for (var i = 0; i < nodes.length; i++) {
    //var language = nodes[i].className.substring(9);
    //var source = nodes[i].firstChild.nodeValue;
    snippets.push({"language": nodes[i].className.substring(9), "source": nodes[i].firstChild.nodeValue});
    //highlight(source, language, nodes[i]);
  }
  this.highlight(snippets, nodes, this);
}

Highlighter.prototype.highlight = function(snippets, nodes, me)
{
  var code;
  var reponseSuccess = function(o) {
    var to_delete;
    var parent;
    var highlighted      = $.secureEvalJSON(o);//YAHOO.lang.JSON.parse(o);

    for (var i = 0; i < nodes.length; i++) {
      new Snippet(snippets[i], nodes[i], highlighted[i]);      
    }
  };

/*
  var callback = {
    success: reponseSuccess
  };
*/

  postData = "snippets=" + encodeURIComponent($.toJSON(snippets));
  //var transaction = YAHOO.util.Connect.asyncRequest("POST", "/includes/ash/highlighter.php", callback, postData);
    $.ajax({
        type: "POST",
        url: "/includes/ash/highlighter.php",
        data: postData,
        success: reponseSuccess
    });
};

function run_highlighter() {
  highlighter = new Highlighter();
}

$(document).ready(run_highlighter);