if (!jQuery.powercost) jQuery.powercost = { };

jQuery.powercost = function(options) {
  return new (function(options){
    var self = this;
    this.options = jQuery.extend({
      method: 'POST',
      debug: false,
      async: true
    },options);

    self.tablecontrols = function(table,targetShow) {
      $(table).find('.controls > a').addClass('ui-state-default ui-corner-all').mouseover(function() {
        $(this).addClass('ui-state-active');
      }).mouseout(function(){
        $(this).removeClass('ui-state-active');
      });

      $(table).find('.controls > a.new').each(function(){
        $(this).html('<span class="ui-icon ui-icon-plus"></span> hinzufügen');
        $(this).click(function(){
          bs.formagic(this.href,null,bs.getparams(this.href));
          return false;
        });
      });

      $(table).find('.controls > a.show').each(function(){
        $(this).html('<span class="ui-icon ui-icon-search"></span>');
        $(this).click(function(){
          bs.load(this.href,targetShow);
          $(targetShow).show();
          return false;
        });
      });

      $(table).find('.controls > a.edit').each(function(){
        $(this).html('<span class="ui-icon ui-icon-pencil"></span>');
        $(this).click(function(){
          $(targetShow).hide().empty();
          bs.formagic(this.href, {model_id: bs.getparam("id",this.href)});
          return false;
        });
      });

      $(table).find('.controls > a.delete').each(function(){
        $(this).html('<span class="ui-icon ui-icon-trash"></span>');
        $(this).click(function(){
          $(targetShow).hide().empty();
          bs.formagic(this.href);
          return false;
        });
      });
    }

    self.serializeInputs = function(selector) {
      var o = {};
      $(selector).each(function(){
        if (this.value != "") {
          o[this.name] = this.value;
        }
      });
      return o;
    };

    self.getSorting = function(selector) {
      var o = {};
      $(selector).each(function(){
        if ($(this).is(".sort-asc")) {
          o[this.name] = "asc";
        } else if ($(this).is(".sort-desc")) {
          o[this.name] = "desc";
        }
      });
      return o;
    };

    return this;
  })(options);
};

var powercost = jQuery.powercost();

jQuery.scrollTo = function(elemOrPos, time) {
  time = time || 800;

  var pos = 0;
  switch ( typeof elemOrPos ) {
    case 'number': pos = elemOrPos; break;
    case 'object': pos = $(elemOrPos).offset().top; break;
    case 'string': pos = parseInt(elemOrPos); break;
  }

  $("html,body").animate({scrollTop: pos}, time);
};

