// 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);
        }
}

jQuery.fn.spoil = function() {
  return this.each(function(){
     $(this).addClass('spoiled').wrapInner('<div></div>').children().hide().end()
      .prepend('<a class="hide" href="javascript:void(0);">Spoiler</a>')
      .children(':first').click(function(event){
        event.preventDefault();
        $(this).toggleClass('hide').siblings().slideToggle('fast');
      });
  });
};

if('function' === typeof onLoadComments) {
  onLoadComments(function(data) {
    $(data).find('div.spoiler').spoil();
  });
} else {
  $(function(){ $('div.spoiler').spoil();});
}

$(function() {
  $('#comments-form').bind('form-pre-serialize', function(event, form, opt, veto) {
    var depth = 0;
    var a = [];
    var text = form[0].text.value;
    var tags = text.match(/<\/?(?:sp|sp1|sp2|spoiler)>/g);
    if(!tags)
      return;
    $.each(tags, function(i, val) {
      if(this.match(/\//)) {
        depth -= 1;
        if(depth < 0) {
          a.push(i);
          return false;
        }
        a.pop(i);
      } else {
        depth += 1;
        a.push(i);
      }
    });
    if(depth == 0)
      return;
    var prob = a.pop();
    var tag = tags[prob];
    var msg = 'unmatched ' + ((depth > 0) ? 'start' : 'end') + ' tag, "'
      + tag.replace(/</, '&lt;').replace(/>/, '&gt;') + '"';
    var texts = text.split(/(<\/?(?:sp|sp1|sp2|spoiler)>)/);
    texts = $.map(texts, function(n, i) {
      n = n.replace(/</, '&lt;').replace(/>/, '&gt;');
      return (i == 2*prob+1) ? '<span class="error-tag">' + n + '</span>' : n;
    });
    texts = texts.join('');
    $('#comment-preview-box').html('<div class="comment-invalid"><p>Invalid Comment Format: '+ msg +'</p><pre>'+texts+'</pre></div>');
    xommentFormOkay();
     veto.veto = true;
  });
});
