pb_block = function(s) {
   this.text = {
      list : 'Refresh data...',
      save : 'Saving data...',
      del_confirm : 'You are going to permanently remove data. Are you sure?',
      del : 'Removing data...',
      edit : 'Loading data...'
   };

   this.state = s.state;
   this.scripts = s.scripts;
   this.after_list = s.after_list;
   this.after_save = s.after_save;
   this.after_del = s.after_del;
   this.after_get = s.after_get;
   if (s.container_id) this.container_id = s.container_id;
   if (s.text) for (var i in s.text) this.text[i] = s.text[i];
}

pb_block.prototype = {

   list : function(page) {
      var self = this;
      pb_common.progressStart(this.text.list);
      if (!page) this.state.page = this.page; else this.state.page = page;
      if (!this.container) this.container = document.getElementById(this.container_id);
      $(this.container)
      .load( this.scripts.list, this.state, function(){ pb_common.progressStop(); if (self.after_list) self.after_list(); } );
   },

   save : function (f) {
      pb_common.progressStart(this.text.save);

      var self = this;

      $.ajax({
         type: 'POST',
         url: this.scripts.save,
         data: $(f).serialize(),
         success: function(msg){
            pb_common.progressStop();
            try {
               eval('var d = '+msg);
               if (d.status == 'error') {
                  pb_common.alert(d.data);
               } else {
                  if (self.after_save) self.after_save(d); else self.list();
               }
            } catch (e) {
               console.debug(e)
               pb_common.alert('Error server response...');
            }
         },
         error: function(){
            pb_common.progressStop();
            pb_common.alert('Error server response');
         }
      });

   },

   del : function (id){

      if (id) {
         var self = this;
         pb_common.confirm(
            self.text.del_confirm,
            function(){
               pb_common.progressStart(self.text.del);

               $.ajax({
                  type: 'POST',
                  url: self.scripts.del,
                  data: (pb_common.isForm(id) ? $(id).serialize() : 'id='+id),
                  success: function(msg){
                     pb_common.progressStop();
                     try {
                        eval('var d = '+msg);
                        if (d.status == 'error') {
                           pb_common.alert(d.data);
                        } else {
                           if (self.after_del) self.after_del(id, d); else self.list();
                        }
                     } catch (e) {
                        console.debug(e);
                        pb_common.alert('Error server response...');
                     }
                  },
                  error: function(){
                     pb_common.progressStop();
                     pb_common.alert('Error server response');
                  }
               });
            }
         );
      }

   },

   edit : function (id){

      if (id) {
         var self = this;
         pb_common.progressStart(this.text.edit);

         $.ajax({
            type: 'POST',
            url: this.scripts.edit,
            data: 'id='+id,
            success: function(msg){
               pb_common.progressStop();
               try {
                  eval('var d = '+msg);
                  if (d.status == 'error') {
                     pb_common.alert(d.data);
                  } else {
                     if (self.after_get) self.after_get(id, d);
                  }
               } catch (e) {
                  pb_common.alert('Error server response...');
               }
            },
            error: function(){
               pb_common.progressStop();
               pb_common.alert('Error server response');
            }
         });

      } else {
         if (this.after_get) this.after_get(null);
      }

   }

}

