function kwExtLinks(data) {
  $(data).find('a[href][rel*=external]').each(function(i) {
    this.target = '_blank';
  });
}

var base64chars = '0123456789:;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split("");
var base64inv = new Array(256); for(var i=0;i<base64chars.length;i++){base64inv[base64chars[i].charCodeAt(0)] = i;}
function kwMailLinks(data) {
  $(data).find('a[href^=mailto:]').each(function(i) {
    var xr = this.href.match(/^mailto:([A-Za-z0-9:;+]+)$/);
    var k = 223 & 255;
    if(!xr) return;
    var s = xr[1];
    var a = new Array();
    var p = (s.charAt(s.length-1) == '+' ? (s.charAt(s.length-2) == '+' ? 'AA' : 'A') : '');
    var r = '';
    s = s.substr(0, s.length - p.length) + p;
    for (var c = 0; c < s.length; c += 4) {
      var n = (base64inv[s.charCodeAt(c)] << 18) + base64inv[s.charCodeAt(c+3)] +
              (base64inv[s.charCodeAt(c+1)] << 12) + (base64inv[s.charCodeAt(c+2)] << 6);
      a.push((n >>> 16) & 255, (n >>> 8) & 255, n & 255);
    }
    if(p.length >= 1) { a.pop();}
    if(p.length == 2) { a.pop();}
    var b = 0;
    s = '';
    for(var i=0;i<a.length;i++) {
      s += String.fromCharCode(b ^= a[i]^k);
    }
    xr = s.split(/\|/);
    this.href = 'mailto:' + xr[0];
    if(xr.length > 1) $(this).html(xr[1]);
  });
}

// Random encryption key feature by Andrew Moulden, Site Engineering Ltd
// This code is freeware provided these four comment lines remain intact
// A wizard to generate this code is at http://www.jottings.com/obfuscator/

var __MTTBLINK__;
var __MTTBID__;
function obfuscator(coded, key, mode, path, hidden) {
        shift = coded.length;
        link = "";
        
        for(i=0;i<coded.length;i++) {
                if (key.indexOf(coded.charAt(i))==-1) {
                        ltr = coded.charAt(i);link+=(ltr);
                } else {
                        ltr = (key.indexOf(coded.charAt(i)) - shift + key.length) % key.length;
                        link += (key.charAt(ltr));
                }
        }
        if(mode == 'hidden_input') {
                document.write('<input type="hidden" name="CCode" value="' + link + '" />');
        } else if(mode == '__MTTBLINK__') {
                __MTTBLINK__ = path + link;
                if(hidden) return;
                document.write(link);
        } else if(mode == '__MTTBID__') {
                __MTTBID__ = link;
                if(hidden) return;
                document.write(link);
        }
}

$(function(){
  $('div.toggle-show').each(function(i){
    $this = $(this);
    $this.wrapInner('<div></div>').children().hide().end()
      .prepend('<a class="hide" href="javascript:void(0);">'+$this.attr('title')+'</a>')
      .attr('title', '')
      .children(':first').click(function(event){
        event.preventDefault();
        $(this).toggleClass('hide').siblings().slideToggle('normal');
      });
  });
});

$(function() {
  $('#comments-form').bind('form-pre-serialize',
  function(event, form, opt, veto) {
    var text = $('#comment-text').val();
    text = text.replace(/\r?\n/g, '[:br:]');
    text = text.replace(/\s+/g, ' ');
    //var $text = $('<div></div>').html(text);
    var $text = $('<div></div>');
    innerXHTML($text[0], text);
    $text = innerXHTML($text[0]);

    $text = $text.replace(/<img start="fileopen" loop="1"/g, '<img');
    $text = $text.replace(/<((br|img|hr)[^>]*)><\/\2>/g, '<$1/>');
    $text = $text.replace(/<(\/?\w+)>\s*<\/\1>/g, '');
    $text = $text.replace(/\[:br:\]/g, '\n');

    text = text.replace(/\[:br:\]/g, '\n');

    if(text == $text) {
      veto.veto = false;
      return;
    }
    var dmp = new diff_match_patch();
    var d = dmp.diff_main(text, $text);
    dmp.diff_cleanupSemantic(d);
    var ds = dmp.diff_prettyXHtml(d);

    $('#comment-preview-box').html('<div class="comment-error"><p>Comment submission or preview failed: Invalid format detected.  Please review the following corrections that have been made to your comment.</p><pre class="diff">'+ds+'</pre></div>');
    var text = $('#comment-text').val($text);
    xommentFormOkay();
    veto.veto = true;
  });
});

if('function' === typeof onLoadComments) {
  onLoadComments(kwExtLinks);
  onLoadComments(kwMailLinks);
} else {
  $(function(){
    kwExtLinks(document);
    kwMailLinks(document);
  });
}

$(function(){
  $('table.football-rankings tr[title]')
    .cluetip({splitTitle: '|',width: 250});
  $('table.football-rankings').each(function(t) {
     $(t).find('tr:even').addClass('even').end();
  });
});

