/* 
 * form handler for modboxen 
 * by georg zimmer 
 * see mb_dispatch.php for more documentation. 
*/
 
//boxlist contains a list of all the boxes.  it is used by notify system, as well as by post handler.
var boxList=new Array();

function FormHandler(xid,xclassname){
  var seq=0;
  var f = function(xid,xclassname){  
      //private vars  kept alive by closure     
      var id=xid;
      var classname=xclassname;
      var errorCallback=function (box,message,exception){
           //alert("errorCallback:" + box.id());
           };
      var postCallback=function (box,action,data){
           //alert("postCallback:" + box.id() + ":" + box.name()+" has no post event");
           };
      var notifyCallback=function(box,event,data){
           //alert("notifyCallback:" + box.id() + ":" + box.name()+" has no notify event");
          };
          
      //private methods       
      function makePost(fields){
          var j='';       
          jQuery.each(fields, function(i, field){
            if (j) j=j+',';
            j=j+'"'+field.name+'":"'+escape(field.value)+'"';
          });     
    
          return '{'+j+'}';      
      }
      
      function dispatchError(XMLHttpRequest,textStatus,errorThrown){
         errorCallback(box,textStatus,errorThrown);
      }
      
      //public methods
      return {
          init : function(){ 
             alert("replace me with your own");
          },
          //use to override default callback
          setPostCallback : function (callback){
            postCallback=callback;
          },
          //use to set notify function
          setNotifyCallback : function (callback){
            notifyCallback=callback;
          },
          //use to set error callback function
          setErrorCallback : function (callback){
            errorCallback=callback;
          },
          name : function () {
             return classname;
          },
          id : function() {
             return id;
          },
          notify : function(box,event,data){
              // replace this with your own notify handler.
              notifyCallback(box,event,data);
          },
          post : function (remoteMethod) {      
              var fields = $("#"+id+" .mb").serializeArray();
              a=makePost(fields);
              box=boxList[id];              
              myGetJSON('/mb_dispatch.php',{'class': classname, 'method': remoteMethod,'args': a,'seq' : ++seq }, 
                 function(data){
                   postCallback(box,remoteMethod,data);
                   if (data['notify']){
                     box.notifyAll(data['notify']['event'],data);
                   }
                 },
                 dispatchError
                 );    
              return false;     
          },
          //call this method to notify the other boxes that something happened.  The boxes interested in an event
          //should register a notifycallback and respond to it.  
          //This method is called automatically if a php method requests it by passing an event name 
          notifyAll : function (event,data){
             for (var xid in boxList){
               box=boxList[xid];
               box.notify(box,event,data);
             }
          }
       }    
    }(xid,xclassname);
    //save this handler in boxlist.
    boxList[xid]=f;     
    return f;
}


//like getJSON but has onError event handler
function myGetJSON(url,data,onSuccess,onError){
      $.ajax({
        url : url,
        cache: false,
        type : "GET",
        dataType : "json",
        data : data,
        success : onSuccess,
        error: onError
        });
      return true;
}

