/************************************

  "waka" is a Japanese poetry 
         composed of 31 syllables. 

  (c) 2005 Pixel-Apes.

  -----------------------------------

  Links and dfn markup:
  
  http://yandex.ru
  www.yandex.ru
  mailto:mendokusee@gmail.com
  mendokusee@gmail.com
  google:waka

  ((http://yandex.ru Yandex))
  [[http://yandex.ru Yandex]]
  ((google:waka poetry == What is waka))

  (?dfn DFN means definition?)

*************************************/

function wakaLinks() {}
         wakaLinks.prototype = new wakaToken();
         wakaLinks.prototype.constructor = 
         wakaLinks;

// static
wakaLinks.prototype.getRegexpPart = function()
{
  return "((http|ftp|https)://[+~'A-Za-z0-9_!=&?\\.\\-/%]+(,[+~'A-Za-z0-9_!=&?\\.\\-/%]+)*)"+
         "|"+
         "(www\\.[+~'A-Za-z0-9_!=&?\\.\\-/%]+(,[+~'A-Za-z0-9_!=&?\\.\\-/%]+)*)"+
         "|"+
         "((mailto:)?([A-Za-z\\.\\-_0-9]+)@([A-Za-z\\.\\-0-9]+\\.[A-Za-z]+))"+
         "|"+
         "([A-Za-z]+:\\S+)"+
         "|"+
         "(\\(\\?.*?\\?\\))"+
         "|"+
         "(\\(\\(.*?\\)\\))"+
         "|"+
         "(\\[\\[.*?\\]\\])";
}
wakaLinks.prototype.openInNewWindow = true;
wakaLinks.prototype.interwiki = {
         "yandex"    : "http://www.yandex.ru/yandsearch?rpt=rad&text=",
         "google"    : "http://www.google.com/search?ie=windows-1251&oe=windows-1251&q=",
         "wackowiki" : "http://wackowiki.com/"
                                };
wakaLinks.prototype.isREprotocol = new RegExp( "^(http|ftp|https)://", "i" );
                                    //1        2
wakaLinks.prototype.isREinterwiki = /^([a-z]+):(.+)$/i; 
                                 //1      2            3    4      5
wakaLinks.prototype.isREcustom = /^(\({2}|(\(\?)|\[{2})(.*?)(\){2}|(\?\))|\]{2})$/i;

wakaLinks.prototype.isRE = new RegExp( "^("+wakaLinks.prototype.getRegexpPart() + ")$" );

wakaLinks.prototype.is = function( outerText )
{
  var _m = this.isRE.exec( outerText );
  if (_m === null) return false;
  
  var url  = outerText;
  var text = outerText;
  var isAbbr = false;

  var matches = this.isREcustom.exec( outerText );
  if (matches != null)
  {
    if (matches[1] == "(?") isAbbr = true;
    var contents = [];
    if (matches[3].indexOf("==") < 0)
      contents = matches[3].split(" ");
    else
      contents = matches[3].split("==");
    url  = this.trim(contents[0]);
    contents[0] = "";
    if (matches[3].indexOf("==") < 0)
      text = this.trim(contents.join(" "));
    else
      text = this.trim(contents.join("==")).substr(2);
  }

  var result = this.prepareUrl( url );
  result.content = text;
  result.text    = outerText;
  result.isAbbr  = isAbbr;

  if (result.content == "") result.content = outerText;
  return result;
}

wakaLinks.prototype.prepareUrl = function( url )
{
  var matches = this.isREinterwiki.exec( url );
  if (matches != null)
  {
    var lower = matches[1].toLowerCase();
    if (this.interwiki[lower] != this.undef())
      return {
               protocol : "",
               url      : this.interwiki[lower] + escape(matches[2])
             };
  }

  var result = { url : url, protocol : "http://" };

  var matches = this.isREprotocol.exec( url );
  if (matches !== null)
  {
    result.protocol = matches[0];
    result.url = url.substr( matches[0].length );
  }
  if (url.indexOf("@") != -1)
  {
    result.protocol = "mailto:";
    result.url      = url.replace( /^mailto:/, "" );
  }

  return result;
}


// private (compilation)
wakaLinks.prototype._compile   = function()
{
  if (this._data.isAbbr)
    this._html = '<dfn title="'+this._data.content+'">'+this._data.url+'</dfn>';
  else
    this._html = '<a '+
                 (this.openInNewWindow?' target="_blank" ':"")+
                 ' href="'+this._data.protocol+this._data.url+'">'+this._data.content+'</a>';

  // ignore for next pass of formatter
  this._html = "<"+wakaFormatter.prototype.ignoreTagName+">"+
               this._html+
               "</"+wakaFormatter.prototype.ignoreTagName+">";
}

