first_comments = [];
comment_count = 0;
panel_size = 10;
xcurl = '/cgi-bin/mt/mt-comments.fcgi';  
last_comment = 0;
last_comment_memory = 50;
tabsshow_hash = null;

function panelUpdate(id) {
  $('#comment-panels').addClass("submitted");
  $.get(xcurl, {__mode:'xomment',id:id,a:comment_count,r:0},
  function(data) {
    addComments(data);
    $('#comment-panels').removeClass("submitted");
  });
  return false;
}
function findPanel(id) {
  var panel = 0;
  for(panel=0;panel+1 < first_comments.length && id>=first_comments[panel+1];++panel)
    ;
  return panel;
}
function lastCommentCookie(id) {
  var path = window.location.pathname;
  var now = new Date();
  mtFixDate(now);
  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
  var ret = 0;
  var a = mtGetCookie('xomment.last_comment.a').split('|', 2*last_comment_memory);
  if(a.length == 1)
    a = [];
  for(var i=0;i<a.length;i+=2) {
    if(a[i] != path)
      continue;
    ret = a[i+1];
    a.splice(i,2);
    break;
  }
  id = id ? id : ret;
  if(id)
    a.unshift(path, id);
  mtSetCookie('xomment.last_comment.a', a.join('|'), now, '/', '', '');
  return ret;
}
function initPanel() {
  var h = window.location.hash;
  if( h == '#comment-print') {
    return null;
  }
  var id = 0;
  var res = h.match(/^#comment-(\d+)$/);
  var cook = lastCommentCookie(last_comment);
  if(res) {
    id = res[1];
    tabsshow_hash = h;
  } else {
    if(cook) {
      id = cook;
      if(h == '#comment-last')
        tabsshow_hash = '#comment-'+cook;
    } else if(h == '#comment-last') {
      tabsshow_hash = '#comments';
    }
  }
  return findPanel(id);
}
function checkHashOnClick(event) {
  var res;
  var rhere = new RegExp('(^|'+window.location.pathname+')(#comment-.*)$', '');
  if(!(res = this.href.match(rhere)))
    return;
  event.preventDefault();
  var id = 0;
  var h = res[2];
  if(h == '#comment-print') {
    return;
  } else if(h == '#comment-last') {
    id = last_comment;
  } else if(res = h.match(/^#comment-(\d+)$/)) {
    id = res[1];
  }
  if(id) {
    var panel = findPanel(id);
    var $tabs = $('#comment-panels');
    if(panel != $tabs.data('selected.tabs')) {
      tabsshow_hash = '#comment-'+id;
      $tabs.tabs('select', panel);
    } else {
      window.location.hash = '#comment-'+id;
    }
  }
}
function addComments(data){
    $('#comment-panels div.comment.new').removeClass('new');
    if(data == '' || data.length == 0) {
      $('#comment-update-message').show().find('#comment-update-message-text').html('no new comments');
      return;
    }
    var $data = $(data).filter('div.comment');
    lastCommentCookie($data.filter(':last').attr('id').replace('comment-', ''));
    var pcount = ((comment_count-1)%panel_size)+1;
    if(pcount%2 == 1) {
      $data.filter(':even').removeClass('odd').addClass('even');
      $data.filter(':odd').removeClass('even').addClass('odd');
    }
    $data.addClass('new');
    var lastp = Math.ceil(comment_count/panel_size);
    if(lastp == 0) {
      lastp = 1;
      $('#comment-panels').show();
    }
    var space = panel_size-pcount;
    var did = $data.attr('id');
    last_comment = did.replace('comment-', '');
    var $tabs = $('#comment-panels');
    var callback = function(event, ui) {
      if($data.length <= space) {
        $('#comment-update').before($data);
      } else {
        var p = lastp;
        var $pan = $('#Comment_Panel_'+p);
        for(var i=0;i<$data.length;++i) {
          if((comment_count+i)%panel_size == 0)
          {
            var myid = '#Comment_Panel_'+(++p);
            var pp = p-1;
            var $dummy = $('#comment-panels > ul.dummy ');
            $tabs.tabs('add', myid, p)
              .find('ul:first li:last > a')
              .attr('title', 'Comment Panel '+p)
              .parent().clone().hover(
                function(){ $(this).addClass('ui-state-hover'); },
                function(){ $(this).removeClass('ui-state-hover'); }
              ).find('a').click(function(event){
                event.preventDefault();
                $('#comment-panels').tabs('select', pp);
                window.location.hash = '#comment-panels';
              }).focus(function(){ $(this).closest('li').addClass('ui-state-focus'); })
              .blur(function(){ $(this).closest('li').removeClass('ui-state-focus'); })
              .end().appendTo($dummy);
            first_comments.push(parseInt($data.eq(i)
              .attr('id').match(/comment-(\d+)/)[1]));
            $pan = $(myid);
            $tabs.append($dummy);
          }
          $pan.append($data.get(i));
        }
        $('#comment-update').appendTo($pan);
      }
      comment_count += $data.length;
      $('#comments-count').html(comment_count);
      $('#xomment-a').val(comment_count);
      $('#comment-update-message').show().find('#comment-update-message-text').html($data.length + ' new comment'
        + (($data.length>2) ? 's' : '') );
      if(space == 0)
        setTimeout(function(){
          tabsshow_hash = '#' + did;
          $tabs.tabs("select", lastp);
          },10);
      else
        window.location.hash = '#' + did;
    }
    $tabs.triggerHandler('xommentadd', [$data]);
    if($tabs.data('selected.tabs') == lastp-1)
      callback();
    else {
      $tabs.one('tabsshow', callback);
      $tabs.tabs('select', lastp-1);
    }
}
function quoteComment(id) {
  $('#comment-text').addClass('submitted');
  $.get(xcurl, {__mode:'xomment',q:id}, function(responseText, statusText){
    var $ct = $('#comment-text');
    $ct.val($ct.val()+responseText).removeClass('submitted');
  });
  return true;
}
function onLoadComments(wcf) { $(function(){
  wcf(document);
  $('#comment-panels').bind('tabsload', function(event, ui) {
    wcf(ui.panel);
  }).bind('xommentadd', function(event, data) {
    wcf(data);
  });
  $('#comments-form').bind('xommentadd', function(event, data) {
    wcf(data);
  });
});}
function xommentFormOkay() {
  mtEntryOnUnload();
  var $form = $('#comments-form');
  $form[0].preview.value = '';
  $form.removeClass('submitted');
}

$(function(){
  comment_count = $('#comments-count').html();
  comment_count = (comment_count == 'No') ? 0 : parseInt(comment_count);
  $('#comments').prepend('<div id="comment-last"></div>');
  $('#comments-open-footer').before('<div id="comment-preview-box"></div>');
  $('#comments-form').append('<input type="hidden" name="xomment-a" id="xomment-a" value="'+comment_count+'"/>');
  onLoadComments(function(data) {
    $(data).find('a[href*=#comment-]').click(checkHashOnClick);
  });
 
  $('#comment-panels').tabs({
    spinner: '',
    cache: true,
    selected: initPanel(),
    select: function(event, ui) {
      var $panels = $('#comment-panels');
      $panels.addClass("submitted");
    },
    show: function(event, ui) {
      $('#comment-panels > ul.dummy > li')
        .removeClass('ui-tabs-selected ui-state-active')
        .find('a[href$='+ui.tab.hash+']')
        .parent()
        .addClass('ui-tabs-selected ui-state-active');
      if(tabsshow_hash){
        window.location.hash = tabsshow_hash;
        tabsshow_hash = null;
      }
      $('#comment-panels').removeClass("submitted");
    }
  });
  
  $('#comment-panels > ul:first').clone()
    .addClass('dummy').appendTo('#comment-panels');
  $('#comment-panels > ul.dummy > li').hover(
    function(){ $(this).addClass('ui-state-hover'); },
    function(){ $(this).removeClass('ui-state-hover'); }
  );
  $('#comment-panels > ul.dummy > li a').each(function(i) {
    $(this).click(function(event){
      event.preventDefault();
      $('#comment-panels').tabs("select", i);
      window.location.hash = '#comment-panels';
    }).focus(function(){ $(this).closest('li').addClass('ui-state-focus'); })
    .blur(function(){ $(this).closest('li').removeClass('ui-state-focus'); });
  });
  if(comment_count == 0)
    $('#comment-panels').hide();
    
  // Stop MT from firing errant submit() if people are logged in
  var $mtForm = $('#comments-form').get(0);
  if ($mtForm)
    $mtForm.submit = function() { return false; }
 
  $('#comments-form').ajaxForm({
    beforeSubmit: function(data, obj, opt) {
      obj.addClass('submitted');
      return true;
    },
    success: function(responseText, statusText) {
      var smHead = '<div class="comment-success ui-widget"><div class="ui-state-highlight ui-corner-all"><p><span class="ui-icon ui-icon-info"></span>';
      var emHead = '<div class="comment-error ui-widget"><div class="ui-state-error ui-corner-all"><p><span class="ui-icon ui-icon-alert"></span>';
      var mTail = '</p></div></div>';
      var $data = $(responseText);
      if(statusText != "success") {
        $('#comment-preview-box').html(emHead+'Comment submission or preview failed.'+mTail);
      } else if($data.find('#generic-error').length) {
        $('#comment-preview-box').html(emHead+$data.find('#generic-error').contents().get(1).data+mTail);
      } else if($data.hasClass('comment-preview')) {
        $('#comments-form').triggerHandler('xommentadd', [$data]);
        $('#comment-preview-box').html($data);
      } else if($data.hasClass('comment-error')) {
        $('#comment-preview-box').html($data);
      } else if($data.hasClass('comment-pending')) {
        $('#comment-text').val('');
        mtHide('comment-form-reply');
        $('#comment-reply')[0].checked = false;
        $('#comment-preview-box').html(responseText);
      } else if($data.hasClass('comment-success')) {
        addComments($data.children());
        $('#comment-text').val('');
        mtHide('comment-form-reply');
        $('#comment-reply')[0].checked = false;
        $('#comment-preview-box').html(smHead+'Your comment was submitted successfully.'+mTail);
      } else {
          $('#comment-preview-box').html(emHead+'Comment submission or preview failed: '+responseText+mTail);
      }
      xommentFormOkay();
    }
  });
});

function growl_wrap() {
  $.growlUI('Processing...', 'Please wait while your request processes.', 0); 
}

$().ajaxStart(growl_wrap).ajaxStop($.unblockUI);
