// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


jQuery.noConflict();

var PinboardIndicator = {}

PinboardIndicator = {
  loading: function() {
    Element.show('pinboard-indicator');
  },

  failure: function() {
    Element.hide('pinboard-indicator');
  },

  success: function() {
    Element.hide('pinboard-indicator');
    Effect.toggle('pinboard-form-container', 'blind');
    Form.reset('pinboard-form');
  }
}

var ProfileIndicator = {}

ProfileIndicator = {
  loading: function() {
    Element.show('profile-indicator');
  },

  failure: function() {
    Element.hide('profile-indicator');
  },

  success: function() {
    Element.hide('profile-indicator');
    Effect.toggle('profile-form', 'blind');
    Form.reset('edit-profile-form');
  }
}


var VoteIndicator = {}

VoteIndicator = {
  loading: function(n) {
    Element.show('vote-indicator');
  },

  failure: function(n) {
    Element.hide('vote-indicator');
  },

  success: function(n) {
    Element.hide('vote-indicator');
  }
}


var AccountIndicator = {}

AccountIndicator = {
  loading: function(id) {
    Element.show('account-indicator-' + id);
  },

  failure: function(id) {
    Element.hide('account-indicator-' + id);
  },

  success: function(id) {
    Element.hide('account-indicator-' + id);
    Effect.toggle('edit-account-' + id, 'blind');
    Form.reset('edit-account-' + id + '-form');
  }
}


var UploadProgress = {
  uploading: null,
  barWidth: 206,

  monitor: function(upid) {
    if(!this.periodicExecuter) {
      this.periodicExecuter = new PeriodicalExecuter(function() {
        if(!UploadProgress.uploading) return;
        new Ajax.Request('/video/upload_progress?upload_id=' + upid);
      }, 3);
    }

    this.uploading = true;
    $('progress-bar').setStyle({width: this.barWidth+'px'});
    $('results').innerHTML = '<span class="Kleiner">Upload wird gestartet...</span>';
    Element.show('progress-bar-back');
    Element.show('progress-bar');
  },

  update: function(total, current) {
    if(!this.uploading) return;
    var status     = current / total;
    var statusHTML = Math.floor(status * 100);
    var width = this.barWidth - Math.floor(this.barWidth * status);

    $('results').innerHTML   = '<span class="Kleiner">' + statusHTML + "% (" + current.toHumanSize() + ' von ' + total.toHumanSize() + ")</span>";
    $('progress-bar').setStyle({width: width+'px'});
  },
  
  finish: function(msg) {
    this.uploading = false;
    this.periodicExecuter.stop();
    this.periodicExecuter = null;
    Element.hide('progress-bar-back');
    $('results').innerHTML = '<strong>' + msg + '</strong>';
    Form.reset('video-upload-form');
  },
  
  cancel: function(msg) {
    if(!this.uploading) return;
    this.periodicExecuter.stop();
    this.periodicExecuter = null;
    this.uploading = false;
    // if(this.StatusBar.statusText) this.StatusBar.statusText.innerHTML = msg || 'abgebrochen';
    $('results').innerHTML = msg || 'abgebrochen';
  }  
}

Number.prototype.bytes     = function() { return this; };
Number.prototype.kilobytes = function() { return this *  1024; };
Number.prototype.megabytes = function() { return this * (1024).kilobytes(); };
Number.prototype.gigabytes = function() { return this * (1024).megabytes(); };
Number.prototype.terabytes = function() { return this * (1024).gigabytes(); };
Number.prototype.petabytes = function() { return this * (1024).terabytes(); };
Number.prototype.exabytes =  function() { return this * (1024).petabytes(); };
['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte', 'petabyte', 'exabyte'].each(function(meth) {
  Number.prototype[meth] = Number.prototype[meth+'s'];
});

Number.prototype.toPrecision = function() {
  var precision = arguments[0] || 2;
  var s         = Math.round(this * Math.pow(10, precision)).toString();
  var pos       = s.length - precision;
  var last      = s.substr(pos, precision);
  return s.substr(0, pos) + (last.match("^0{" + precision + "}$") ? '' : '.' + last);
}

// (1/10).toPercentage()
// # => '10%'
Number.prototype.toPercentage = function() {
  return (this * 100).toPrecision() + '%';
}

Number.prototype.toHumanSize = function() {
  if(this < (1).kilobyte())  return this + " Bytes";
  if(this < (1).megabyte())  return (this / (1).kilobyte()).toPrecision()  + ' KB';
  if(this < (1).gigabytes()) return (this / (1).megabyte()).toPrecision()  + ' MB';
  if(this < (1).terabytes()) return (this / (1).gigabytes()).toPrecision() + ' GB';
  if(this < (1).petabytes()) return (this / (1).terabytes()).toPrecision() + ' TB';
  if(this < (1).exabytes())  return (this / (1).petabytes()).toPrecision() + ' PB';
                             return (this / (1).exabytes()).toPrecision()  + ' EB';
}


function popupPlayer(url) {
  player = open(url,"player","width=450,height=330,left=80,top=80,scrollbars=no,resizable=no,dependent=yes");
  player.focus();
}