/**
 * ...
 */
var Time = function()
{
    this._d = new Date();
};
Time.prototype = { _d: null };

Time.format = 'HH:ii:ss';

(function() {

	/**
	 * Adds a given method under the given name 
	 * to the Time prototype if it doesn't
	 * currently exist.
	 *
	 * @private
	 */
	function add(name, method) {
		if( !Time.prototype[name] ) {
			Time.prototype[name] = method;
		}
	};

    add('getHours', function()
    {
        return this._d.getHours();
    });

    add('setHours', function(num)
    {
        this._d.setHours(num);
        return this;
    });

	add('addHours', function(num) {
		this.setHours(this.getHours() + num);
		return this;
	});

    add('getMinutes', function()
    {
        return this._d.getMinutes();
    });

    add('setMinutes', function(num)
    {
        this._d.setMinutes(num);
        return this;
    });

	add("addMinutes", function(num) {
		this.setMinutes(this.getMinutes() + num);
		return this;
	});

    add('getSeconds', function()
    {
        return this._d.getSeconds();
    });

    add('setSeconds', function(num)
    {
        this._d.setSeconds(num);
        return this;
    });

	add("addSeconds", function(num) {
		this.setSeconds(this.getSeconds() + num);
		return this;
	});

    add('getTime', function()
    {
        return this._d.getTime();
    });

	add("asString", function(format) {
		var r = format || Time.format;
		return r
		    .split('hh').join(_zeroPad(this.getHours() > 12 ? this.getHours() - 12 : this.getHours()))
            .split('h').join(this.getHours() > 12 ? this.getHours() - 12 : this.getHours())
            .split('HH').join(_zeroPad(this.getHours()))
            .split('H').join(this.getHours())
            .split('ii').join(_zeroPad(this.getMinutes()))
            .split('i').join(this.getMinutes())
            .split('ss').join(_zeroPad(this.getSeconds()))
            .split('s').join(this.getSeconds())
            .split('a').join(this.getHours() < 12 ? 'am' : 'pm')
            .split('A').join(this.getHours() < 12 ? 'AM' : 'PM');
	});
	
	/**
	 * Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
	 * (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
	 *
	 * @example var dtm = Date.fromString("12/01/2008");
	 * dtm.toString();
	 * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
	 * 
	 * @name fromString
	 * @type Date
	 * @cat Plugins/Methods/Date
	 * @author Kelvin Luck
	 */
    Time.fromString = function(s)
	{
		var f = Time.format;
		var t = new Time();
		
        t.setHours(Number(s.substr(f.indexOf('HH'), 2)));
        t.setMinutes(Number(s.substr(f.indexOf('ii'), 2)));
        t.setSeconds(Number(s.substr(f.indexOf('ss'), 2)));

		if (isNaN(t.getTime()))
		{
			return false;
		}

		return t;
	};
	
	// utility method
	var _zeroPad = function(num) {
		var s = '0'+num;
		return s.substring(s.length-2);
	};
	
})();
