document.observe("dom:loaded", function() {
   // *************************************************************************
   // Chatbox auto refresh
   // *************************************************************************

   if ($("xchatbox_posts")) {
      function spyonChatBox() {
         setTimeout(function() {
            $("cmessage").observe("keyup", function(ev) {
               if (ev.element().getValue().length > 0 && !chatUpdater.stopped) {
                  chatUpdater.stopped = true;
                  chatUpdater.stop();
               } else if (ev.element().getValue().length == 0 && chatUpdater.stopped) {
                  chatUpdater.stopped = false;
                  chatUpdater.start();
               }
            });
            $("chat_submit").observe("click", function(ev) {
               chatUpdater.stopped = false;
               setTimeout(function() {
                  spyonChatBox();
                  chatUpdater.start();
               }, 2500);
            });
         }, 1500);
      }
      var params = {chat_submit: "Submit", cmessage: ""};
      if ($("chatbox_ajax")) {
         params.chatbox_ajax = $("chatbox_ajax").getValue();
      }
      var chatUpdater = new Ajax.PeriodicalUpdater(
         "chatbox_posts",
         "http://localhost/plugins.e107.org/e107_plugins/chatbox_menu/chatbox_menu.php",
         {
            parameters: params,
            frequency:  1,
            decay:      2,
            onSuccess: function() {
               console.log(new Date()+"jshelper onSuccess");
               spyonChatBox();
            }
         }
      );
   }

   // *************************************************************************
   // JSCaptcha
   // *************************************************************************
   // See if we have a form we need to observe
   //var forms = $w("chatbox searchform")
   var forms = $w("")
   forms.each(function(id) {
      var url = "http://localhost/plugins.e107.org/e107_plugins/jshelpers/ajax_jscaptcha.php";
      var f = $(id);
      if (f) {
         var did = id+"_jscaptcha";
         f.insert({top: new Element("div", {id:did})});
         new Ajax.Updater(did, url, {
            method:"get",
            parameters: "get."+id
         });

         f.observe("submit", function(ev) {
            ev.stop();

            new Ajax.Request(url, {
               method:     "get",
               parameters: "check."+$F(id+'_code')+"."+$F(id+'_verify'),
               onSuccess: function(transport) {
                  if (transport.responseText == "true") {
                     //TODO - duplicate submit button as some forms check for it being submitted and it isn't if you don't actually click it
                     //TODO - what if more none?
                     //TODO - what if more than 1?
                     var subbutton = f.select("input[type=submit]")[0];
                     f.insert({top: new Element("input", {type:"hidden",name:subbutton.name,value:subbutton.value})});
                     f.submit();
                  } else {
                     new Ajax.Updater(did, url, {
                        method:"get",
                        parameters: "get."+id+"."+transport.responseText
                     });
                  }
               }
            });
         });
      }
   });

   // Admin menus
   $$("[onclick]").each(function (el) {
      if (el.readAttribute("onclick").match(/^showhide/)) {
         var matches = el.readAttribute("onclick").match(/^.*\'(.*)\'\)/);
         el.onclick = null;
         el.observe("click", showhideit2.curry(el, matches[1]));
      }
   })

   // Admin form submit
   $('jshelper_save_prefs').observe('click', function() {
      $('jshelper_admin_page_form').submit();
   });

   // Admin File Serving radio buttons
   $$("input[name=jshelper_file_serving]").each(function (el) {
      el.observe("click", function(ev) {
         var options = {duration: 0.25};
         $(ev.element().id+"_info").show().siblings().invoke('hide');
      });
   })
});

// e107 requires the plugin to define this function
function showhideit2(srcel, targetid) {
   targetel = $(targetid);
   srcel = $(srcel);
   if (targetel.next().visible()) {
      return;
   }
   var options = {duration: 0.75};
   var elnext = targetel.next();

   new Effect.Pulsate(srcel);
   new Effect.Highlight(srcel);
   // Find all siblings and make sure they are hidden (there should only be 1!)
   targetel.siblings().each(function (sib) {
      if (sib != elnext && sib.visible() && sib.hasClassName("accordion_content")) {
         new Effect.BlindUp(sib, options);
      }
   });
   if (!targetel.next().visible()) {
      new Effect.BlindDown(targetel.next(), options);
   }
}

// Redefine e107 function eover
eover = function(object, over) {
   // Honour what e107 would do
   object.className = over;
   object = $(object);
   //new Effect.Shake(object, {queue: "jshelperq_eover"});
}

