// Amara, universalsubtitles.org
//
// Copyright (C) 2013 Participatory Culture Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see
// http://www.gnu.org/licenses/agpl-3.0.html.

var angular = angular || null;
(function(){
    var module = angular.module('amara.SubtitleEditor.subtitles.filters', []);
    var HIDES_ON = ['Deleted', 'Private']

    function leftPad(number, width, character) {
        /*
        * Left Pad a number to the given width, with zeros.
        * From: http://stackoverflow.com/a/1267338/22468
        *
        * Returns: string
        */

        character = character || '0';
        width -= number.toString().length;

        if (width > 0) {
            return new Array(width + (/\./.test(number) ? 2 : 1))
                                .join(character) + number;
        }
        return number.toString();
    };

    /*
    * Display a human friendly format.
     */
    function displayTime(milliseconds, showFraction) {
        if (milliseconds === -1 ||
            isNaN(Math.floor(milliseconds)) ||
            milliseconds === undefined ||
            milliseconds === null) {
                return "--";
            }
        var time = Math.floor(milliseconds / 1000);
        var hours = ~~(time / 3600);
        var minutes = ~~((time % 3600) / 60);
        var seconds = time % 60;

        var components = [];
        if(hours) {
            components.push(hours);
            components.push(leftPad(minutes, 2));
        } else {
            components.push(minutes);
        }
        components.push(leftPad(seconds, 2));
        var result = components.join(":");

        if(showFraction) {
            var fraction = Math.round((milliseconds % 1000) / 10);
            result += '.' + leftPad(fraction, 2);
        }
        return result

    };

    module.filter('displayTime', function(){
        return function(milliseconds) {
            return displayTime(milliseconds, true);
        }
    });
    module.filter('displayTimeSeconds', function(){
        return function(milliseconds) {
            return displayTime(milliseconds, false);
        }
    });
    module.filter('versionDropDownDisplay', function(){
        return function (versionData){
            var res =  'Version ' + versionData.version_no +
                        (HIDES_ON.indexOf(versionData.visibility) > -1 ?
                            " (" + versionData.visibility + ")":
                            "");
            return res;
        }
    })
    module.filter('metadataTypeName', function(){
        // FIXME: the labels should be localized
        var typeNameMap = {
            'speaker-name': 'Speaker Name',
            'location': 'Location',
        };
        return function metadataTypeName(typeName) {
            if(typeName in typeNameMap) {
                return typeNameMap[typeName];
            } else {
                return typeName;
            }
        }
    })

}).call(this);

@@ -939,524 +939,15 @@
 '%5D%0A%0A
-    function leftPad(number, width, character) %7B%0A        /*%0A        * Left Pad a number to the given width, with zeros.%0A        * From: http://stackoverflow.com/a/1267338/22468%0A        *%0A        * Returns: string%0A        */%0A%0A        character = character %7C%7C '0';%0A        width -= number.toString().length;%0A%0A        if (width %3E 0) %7B%0A            return new Array(width + (/%5C./.test(number) ? 2 : 1))%0A                                .join(character) + number;%0A        %7D%0A        return number.toString();%0A    %7D;%0A%0A
     /*%0A
+
     
@@ -985,16 +985,15 @@
 at.%0A
- 
     */%0A
+
     
@@ -1263,25 +1263,83 @@
 var 
-time = Math.floor
+date;%0A        if (showFraction)%0A            date = new Date(10 * Math.round
 (mil
@@ -1352,18 +1352,17 @@
 nds / 10
-00
+)
 );%0A     
@@ -1368,35 +1368,81 @@
     
-var hours = ~~(time / 36
+else %0A            date = new Date(1000 * Math.round(milliseconds / 10
 00)
+)
 ;%0A  
@@ -1455,50 +1455,79 @@
 var 
-minutes = ~~((time %25 3600) / 60);%0A
+hours = date.getUTCHours(), minutes = date.getUTCMinutes(),%0A   
         
 var 
@@ -1522,19 +1522,16 @@
         
-var
  seconds
@@ -1537,265 +1537,289 @@
 s = 
-time %25 60;%0A%0A        var components = %5B%5D;%0A        if(hours) %7B%0A            components.push(hours);%0A            components.push(leftPad(minutes, 2));%0A        %7D else %7B%0A            components.push(minutes);%0A        %7D%0A        components.push(leftPad(seconds, 2));
+date.getUTCSeconds(), cents = Math.floor(date.getUTCMilliseconds() / 10);%0A        var result = %22%22 + (hours ? (hours + %22:%22 + (%220%22 + minutes).slice (-2) + %22:%22 + (%220%22 + seconds).slice (-2)) :%0A                                   (minutes ? (minutes + %22:%22 + (%220%22 + seconds).slice (-2)) :
 %0A   
@@ -1827,198 +1827,126 @@
     
-var result = components.join(%22:%22);%0A%0A        if(showFraction) %7B%0A            var fraction = Math.round((milliseconds %25 1000) / 10);%0A            result += '.' + leftPad(fraction, 
+                                      seconds ));%0A        if (showFraction) result += %22.%22 + (%220%22 + cents).slice (-
 2);%0A
-        %7D%0A
     
@@ -1966,17 +1966,15 @@
 sult
-%0A
+;
 %0A    %7D
-;%0A
 %0A   

Fix bug in cents of seconds display, rounding inaccuracy and simplify implementation of time display, for issue #995