var PNB = function() {
  return {
    save_textiled: function(update, elem) {
     var id = this.get_id(elem);
      new Ajax.Updater(update, '/edit/inline_textile_editor/' + id + '.textiled', {
        method:'put',
        parameters:Form.serialize(elem),
        evalScripts:true
      });
      return false;
    },
   
    reorder_pages: function(list, id) {
      params = Sortable.serialize(list);
      // new Ajax.Request()
    },
   
    get_id: function(elem) {
      return elem.replace(/[^\d]/g, '');
    },
    
    initSlugs: function() {
      var target = $$('input.slug_target')[0];
      try {
        Event.observe(target, 'change', function(e) {
          $$('input.slug')[0].value = PNB.slugify(this.value);
        });
      } catch (e) {}
    },
    
    slugify: function(str) {
      return str.replace(/[\W\s]+/gi, '_').toLowerCase();
    }
  };
}();


document.observe('dom:loaded', function(){
  initLogin();
  PNB.initSlugs();
});


var initLogin = function() {
  try {
    if ($('client_login')) {
      $('client_login').hide();

      $('login').onclick = function(){
        Effect.toggle('client_login','blind', 0.7);
        Effect.toggle('ad','appear', 0.4);
        return false;
      };
      $('user').observe('focus', function(){
        if (!this.original_value || this.getValue() == this.original_value) {
          this.original_value = this.getValue();
          this.clear();
          this.addClassName('focused');
        }
      });
      $('user').observe('blur', function(){
        if (trim(this.getValue())==''){
          this.removeClassName('focused');
          this.value = this.original_value;
        } 
      });
      $('pass').observe('focus', function(){
        if (!this.original_value || this.getValue() == this.original_value) {
          this.original_value = this.getValue();
          this.clear();
          this.addClassName('focused');
        }
      });
      $('pass').observe('blur', function(){
        if (trim(this.getValue())==''){
          this.removeClassName('focused');
          this.value = this.original_value;
        } 
      });
    }
  } catch (e) {}
};


function trim(stringToTrim) {
 return stringToTrim.replace(/^\s+|\s+$/g,"");
}