/*
Developer Formater
http://wordpress.org/extend/plugins/devformatter/
Developer Formatter system to WordPress. Powered by <a href="http://qbnz.com/highlighter/">GeSHi</a> server-side highlight system.
Version: 2009.0.1.14
Author: Gilberto Saraiva
Author URI: http://gsaraiva.projects.pro.br/

Copyright (c) 2008 Gilberto Saraiva
Released under the GNU General Public License (GPL)
http://www.gnu.org/licenses/gpl.txt
*/
String.prototype.pad = function(l, s, t){
  pad = ((t != 0)? new Array(Math.ceil(l) + 1) : new Array(Math.ceil(l / 2) + 1)).join(s).substr(this.length);
  return ((t != 0)? ((t == 1)? this + pad : pad + this) : pad + this + pad);
}

String.prototype.urlEncode = function(){
  var valids = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
  var hex = "0123456789ABCDEF";
  var encoded = "";
  for (var i = 0; i < this.length; i++ ){
    var ch = this.charAt(i);
    if (ch == " "){
      encoded += "+";
    }else if(valids.indexOf(ch) != -1){
      encoded += ch;
    }else{
      var charCode = ch.charCodeAt(0);
      if(charCode <= 255){
        encoded += "%";
        encoded += hex.charAt((charCode >> 4) & 0xF);
        encoded += hex.charAt(charCode & 0xF);
      }
    }
  }
  return encoded;
}

devfmt_createLinesCount = function(ABlockObj, AIdent){
  block = jQuery(ABlockObj);
  opts = block.find("input[type=hidden]").val();
  ShowLinesNumber = opts.indexOf('lines') > -1;

  html = block.find("pre." + AIdent + "").html();
  htmlLines = html.split('\n');

  output = "<table cellspacing=\"0\" style=\"padding:0;margin:0;background:transparent\" class=\"" + AIdent + "\" width=\"100%\"><tbody>";
  PadSize = ("" + htmlLines.length).length;
  LastOpenTags = "";
  TrOdd = false;
  for(i = 0; i < htmlLines.length; i++){
    htmlLines[i] = LastOpenTags + htmlLines[i];
    tmp = htmlLines[i];
    matchs = true;
    while(matchs){
      matchs = /(<(\w+)([^>]*)>(.*?)<\/)/.exec(tmp);
      if(matchs){
        reg = new RegExp("(<("+matchs[2]+")([^>]*)>([^<]*)<\/"+matchs[2]+">)");
        tmp = tmp.replace(reg, "");
      }
    }

    LastOpenTags = "";
    matchs = true;
    while(matchs){
      matchs = /(<(\w+)([^>]*)>)/.exec(tmp);
      if(matchs){
        tmp = tmp.replace(matchs[0], "");
        LastOpenTags += matchs[0];
      }
    }
    output += "<tr>" +
      (
        (ShowLinesNumber)?
          "<td class=\"" + AIdent + "lines" + (TrOdd? " " + AIdent + "linesodd" : "") + "\" width=\"1px\">" +
          ("" + (i + 1)).pad(PadSize, '0', 2) + "</td>":
          ""
      ) +
      "<td class=\"" + AIdent + "linesarea" + (TrOdd? " " + AIdent + "linesareaodd" : "") + "\"><pre class=\"" + AIdent + " devcodeline\">" +
      htmlLines[i] + "</pre></td></tr>";
    TrOdd = !TrOdd;
  }
  output += "</tbody></table>";
  block.find("table." + AIdent + "").remove();
  block.find("pre." + AIdent + "").hide();
  block.append(output);
}

devfmt_credits = function(){
  window.open('http://wordpress.org/extend/plugins/devformatter/');
}

devfmt_createTools = function(AIdent){
  jQuery("." + AIdent + "block").each(function(){
    devfmt_createLinesCount(this, AIdent);
    jQuery(this).find('." + AIdent + "tools').remove();
    jQuery(this).prepend("<div class=\"" + AIdent + "tools\" style=\"width:"+parseInt(jQuery(this).find("table." + AIdent + "").width())+"px\">"+
      "<div class=\"" + AIdent + "tools\" style=\"width:"+parseInt(jQuery(this).width())+"px\">"+
      "<span>&nbsp;" + this.title + "&nbsp;|&nbsp;" + DevFmtCopyText + "</span>&nbsp;|&nbsp;<span title='DevFormatter Plugin' onclick='devfmt_credits()' style='cursor:pointer'>?</span></div></div>");
    jQuery(this).find("." + AIdent + "tools span").click(function(){
      devfmt_copyToClipboard(jQuery(this).parent().parent().parent().find("pre." + AIdent + "")
        .get(0)[((document.all)? "innerText" : "textContent")]);
    });
  });
}

devfmt_copyToClipboard = function(AText){
  if(window.clipboardData){
    window.clipboardData.setData("Text",AText);
  }else{
    var flashcopier = 'flashcopier';
    if(jQuery('#flashcopier').length == 0)
      jQuery('body').append("<div id='flashcopier' style='position:absolute'></div>");
    jQuery('#flashcopier').html('');
    jQuery('#flashcopier').html('<embed src="' + DevFmtUrl + '_clipboard.swf"'+
      ' FlashVars="clipboard=' + (AText.urlEncode()) + '" width="0"'+
      ' height="0" type="application/x-shockwave-flash"></embed>');
  }
}