/*
  Readbeforecomment plugin
  (P) PSNet, 2008 - 2011
  http://psnet.lookformp3.net/
*/

var Readbeforecomment_PublishButton = null;           // button object
var Readbeforecomment_CommentField;                   // text field object
var Readbeforecomment_TimeoutProcessID = null;
var Readbeforecomment_OriginalButtonMessage = null;   // button`s text (once saved)
// tic tac
var Readbeforecomment_IntervalProcessID = null;
var Readbeforecomment_IntervalSecondsLefts = 0;       // seconds lefts

// --- events ---

function Readbeforecomment_StopTimersIfTheyAreActivated () {
  if (Readbeforecomment_TimeoutProcessID) clearTimeout (Readbeforecomment_TimeoutProcessID);
  if (Readbeforecomment_IntervalProcessID) clearInterval (Readbeforecomment_IntervalProcessID);
}

// ---

function Readbeforecomment_DenyPublishAccess () {
  // stop events if they are runned
  Readbeforecomment_StopTimersIfTheyAreActivated ();
  
  // calc total seconds
  Readbeforecomment_IntervalSecondsLefts = parseInt (Readbeforecomment_Time_For_Holding_Button_Disabled / 1000);
  
  // block button
  Readbeforecomment_CommentField.highlight ('#FAFAFA');
  Readbeforecomment_BlockButton (true);
  
  // make a delay and then unblock button
  Readbeforecomment_TimeoutProcessID = setTimeout ('Readbeforecomment_BlockButton (false);', Readbeforecomment_Time_For_Holding_Button_Disabled);
  
  // show seconds lefts count
  Readbeforecomment_IntervalProcessID = setInterval ('Readbeforecomment_TicTacEverySec ();', 1000);
}

// ---

function Readbeforecomment_SetOriginalButtonsName () {
  Readbeforecomment_PublishButton.setAttribute ('value', Readbeforecomment_OriginalButtonMessage);
}

// ---

function Readbeforecomment_BlockButton (ActionType) {
  // find button and make its access
  if (!Readbeforecomment_PublishButton) {
    Readbeforecomment_PublishButton = $ (Readbeforecomment_CommentField).getParent ().getChildren ('input[name="submit_comment"]') [0];
  }
  if (!Readbeforecomment_OriginalButtonMessage) {
    Readbeforecomment_OriginalButtonMessage = Readbeforecomment_PublishButton.getAttribute ('value');
  }
  Readbeforecomment_PublishButton.disabled = ActionType;
  if (ActionType) {
    Readbeforecomment_PublishButton.setAttribute ('value', Readbeforecomment_Wait_For_Msg + Readbeforecomment_IntervalSecondsLefts);
  } else {
    Readbeforecomment_SetOriginalButtonsName ();
  }
}

// ---

function Readbeforecomment_TicTacEverySec () {
  if (Readbeforecomment_IntervalSecondsLefts <= 1) {
    if (Readbeforecomment_IntervalProcessID) clearInterval (Readbeforecomment_IntervalProcessID);
    return false;
  }
  Readbeforecomment_IntervalSecondsLefts --;
  Readbeforecomment_PublishButton.setAttribute ('value', Readbeforecomment_Wait_For_Msg + Readbeforecomment_IntervalSecondsLefts);
}

// --- attach event ---

function Readbeforecomment_Init () {
  // leave this here coz user can click on another "answer" link when counter is actived, so stop them if they are working now
  Readbeforecomment_StopTimersIfTheyAreActivated ();
  
  Readbeforecomment_PublishButton = null;
  Readbeforecomment_CommentField;
  Readbeforecomment_TimeoutProcessID = null;
  //Readbeforecomment_OriginalButtonMessage = null; // do twice the same? no.
  Readbeforecomment_IntervalProcessID = null;
  Readbeforecomment_IntervalSecondsLefts = 0;

  Readbeforecomment_CommentField = $ ('form_comment_text');
  if (Readbeforecomment_CommentField) {
    Readbeforecomment_CommentField.addEvent ('keyup', Readbeforecomment_DenyPublishAccess);
  }
}

// --- Mod of lsCmtTreeClass ---

function Readbeforecomment_ExtendMainCommentsClass () {
  if (typeof (lsCmtTreeClass) != 'function') return false;
  var CommentsObjectCopy = new Class ();
  CommentsObjectCopy.prototype.toggleCommentForm = lsCmtTreeClass.prototype.toggleCommentForm;
  lsCmtTreeClass.prototype.toggleCommentForm = function () {
    Readbeforecomment_BlockButton (false);
    CommentsObjectCopy.prototype.toggleCommentForm.apply (this, arguments);
    Readbeforecomment_Init ();
  }
}

// ---

window.addEvent ('domready', function () {
  Readbeforecomment_ExtendMainCommentsClass ();
  Readbeforecomment_Init ();
});

