var sThickboxPath = "/wp-includes/js/thickbox/";
var sPluginPath = "/wp-content/plugins/TwitterComments/";

var tb_pathToImage = sThickboxPath+"loadingAnimation.gif";
var tb_closeImage = sThickboxPath+"tb-close.png";

jQuery.fn.TwitterComments = function(options)
{
  options = jQuery.extend({
    formId:"commentform",
    commentId:"comment",
    submitId:"submit"
  }, options);
  
  var form = jQuery("#"+options.formId);
  var comment = jQuery("#"+options.commentId);
  var submit = jQuery("#"+options.submitId);
  var chk;
  
  //add checkbox to form
  jQuery(comment).parent().before(
    jQuery("<p><input type='checkbox' name='TwitterComments' id='TwitterComments' style='width: auto;' /><label for='TwitterComments'><small>Twitter this comment?</small></label></p>")
  );
  
  //store newly added checkbox
  chk = jQuery("#TwitterComments");
  
  //add click event to show thickbox
  jQuery("#"+options.submitId).click(function()
  {
    if (jQuery(this).data("disableTwitterComments") != true && jQuery(chk).is(":checked") == true)
    {
      var sComment = jQuery(comment).val();
      if (sComment.length > 140) {
        sComment = sComment.substring(0, 140);
      }
      var sURL = document.location.href;
      tb_show("Tweet This Comment", sPluginPath+"php/Form.php?comment="+encodeURIComponent(sComment)+"&url="+encodeURIComponent(sURL)+"&KeepThis=true&TB_iframe=true&height=350&width=350");
      return false;
    }
  });
};

//call plugin on load
jQuery(function(){
  jQuery("body").TwitterComments({
    formId: TwitterCommentsOptions.formId,
    commentId: TwitterCommentsOptions.commentId,
    submitId: TwitterCommentsOptions.submitId
  });
});

function TwitterCommentsSubmitForm()
{
  jQuery("#"+TwitterCommentsOptions.submitId).data("disableTwitterComments", true);

  tb_remove();
  
  jQuery("#"+TwitterCommentsOptions.submitId).click();
}
