/**
_______________________________
TIME COMPONENT */

Colibri.timer = function(XMLPresentation, XMLDocuments) {
	this.init(XMLPresentation, XMLDocuments);
};

$.extend(Colibri.timer.prototype, {
	
	position: 0,
	duration: 0,
	
	init: function(XMLPresentation, XMLDocuments) {
		this.position = 0;
    	this.duration = theColibriOptions.playerOptions.vDuration;
  	},

  	sync: function(timecode) {
  		this.position = timecode;
  		$("#time").text(this.format(Math.floor(this.position)) + ' | ' + this.format(this.duration));
  	},
  	
  	/**
  	 * Format a time in seconds to match the following pattern: mm:ss
  	 * @param time A time to format in seconds
  	 */
  	format: function(time) {

  		if(this.duration == null || this.duration == 0)
  	      return '';

  	    mins = Math.floor(time/60);
  	    secs = Math.floor(time%60);
  	    
  	    return this.str_pad(mins, 2, '0', 'STR_PAD_LEFT') + ':' + this.str_pad(secs, 2, '0', 'STR_PAD_LEFT');
  	},
  	
  	/**
  	 * Returns input string padded on the left or right to specified length with pad_string  
  	 * 
  	 * version: 1004.2314
  	 * discuss at: http://phpjs.org/functions/str_pad
  	 * +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  	 * + namespaced by: Michael White (http://getsprink.com)
  	 * +      input by: Marco van Oort
  	 * +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  	 * example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
  	 * returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
  	 * example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
  	 * returns 2: '------Kevin van Zonneveld-----'
  	 */
  	str_pad: function(input, pad_length, pad_string, pad_type) {
  	    
  	    var half = '', pad_to_go;
  	 
  	    var str_pad_repeater = function (s, len) {
  	        var collect = '', i;
  	 
  	        while (collect.length < len) {collect += s;}
  	        collect = collect.substr(0,len);
  	 
  	        return collect;
  	    };
  	 
  	    input += '';
  	    pad_string = pad_string !== undefined ? pad_string : ' ';
  	    
  	    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
  	    if ((pad_to_go = pad_length - input.length) > 0) {
  	        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
  	        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
  	        else if (pad_type == 'STR_PAD_BOTH') {
  	            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
  	            input = half + input + half;
  	            input = input.substr(0, pad_length);
  	        }
  	    }
  	 
  	    return input;
  	}
});
