// Copyright 2014 Globo.com Player authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

var _ = require('underscore');
var Moment = require('moment');

var extend = function(protoProps, staticProps) {
  var parent = this;
  var child;

  if (protoProps && _.has(protoProps, 'constructor')) {
    child = protoProps.constructor;
  } else {
    child = function(){ return parent.apply(this, arguments); };
  }

  _.extend(child, parent, staticProps);

  var Surrogate = function(){ this.constructor = child; };
  Surrogate.prototype = parent.prototype;
  child.prototype = new Surrogate();

  if (protoProps) _.extend(child.prototype, protoProps);

  child.__super__ = parent.prototype;

  child.super = function(name) {
    return parent.prototype[name];
  };

  child.prototype.getClass = function() {
    return child;
  }

  return child;
};

var zeroPad = function(number, size) {
  return (new Array(size + 1 - number.toString().length)).join('0') + number;
};

var formatTime = function(time, showMillis) {
  var duration = Moment.duration(time * 1000);
  var str = duration.minutes() + ':' + zeroPad(duration.seconds(), 2);
  if (duration.hours())
    str = duration.hours() + ':' + str;
  if (showMillis)
    str += '.' + duration.milliseconds();
  return str;
};

var Fullscreen = {
  isFullscreen: function() {
    return document.webkitIsFullScreen || document.mozFullScreen;
  },
  requestFullscreen: function(el) {
    if(el.requestFullscreen) {
      el.requestFullscreen();
    } else if(el.webkitRequestFullscreen) {
      el.webkitRequestFullscreen();
    } else if(el.mozRequestFullScreen) {
      el.mozRequestFullScreen();
    } else if(el.msRequestFullscreen) {
      el.msRequestFullscreen();
    }
  },
  cancelFullscreen: function() {
    if(document.exitFullscreen) {
      document.exitFullscreen();
    } else if(document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen();
    } else if(document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if(document.msExitFullscreen) {
      document.msExitFullscreen();
    }
  }
}

module.exports = {
  extend: extend,
  zeroPad: zeroPad,
  formatTime: formatTime,
  Fullscreen: Fullscreen
};

@@ -1142,24 +1142,32 @@
   var str = 
+zeroPad(
 duration.min
@@ -1167,25 +1167,58 @@
 ion.
-minutes() + ':' +
+seconds(), 2);%0A  if (duration.hours()) %7B%0A    str =
  zer
@@ -1235,46 +1235,85 @@
 ion.
-second
+minute
 s(), 2)
-;%0A  if (duration.hours())
+ + ':' + str;%0A    str = duration.hours() + ':' + str;%0A  %7D else %7B
 %0A   
@@ -1320,36 +1320,38 @@
  str = duration.
-hour
+minute
 s() + ':' + str;
@@ -1351,16 +1351,20 @@
  + str;%0A
+  %7D%0A
   if (sh

fix format time function to add zero padding for durations greater than 1 hour