//alert("enter datetime.js");
Number.prototype.remain = function ( ) {
	var n = this.valueOf();
	return new Number(n - Math.floor(n));
}
Number.prototype.remainInt = function ( f ) {
	return Math.floor(this.remain() * f);
}
function now(secs) {
	var t = new Date();
	var milli = t.getTime();
	delete t;
	return ( secs ? Math.round(milli / 1000) : milli );
}
function DateTime(milliseconds) {
	var d = new Date();
	d.setTime(milliseconds);
	return d;
}
Date.prototype.unixDate = function ( ) {
	var x;
	var y = this.getFullYear();
	var m = ( x = this.getMonth() + 1 ) < 10 ? ( '0' + x ) : x;
	var d = ( x = this.getDate()	  ) < 10 ? ( '0' + x ) : x;
	return y + "-" + m + "-" + d;
}

Date.prototype.unixTime = function ( ) {
	var x;
	var h = ( x = this.getHours()   ) < 10 ? ( '0' + x ) : x;
	var m = ( x = this.getMinutes() ) < 10 ? ( '0' + x ) : x;
	var s = ( x = this.getSeconds() ) < 10 ? ( '0' + x ) : x;
	return h + ":" + m + ":" + s;
}

Date.prototype.localDate = function ( ) {
	var d = _s('tagesNamen');
	var m = _s('monatsNamen');
	return eval(_s('dateFormat'));// + " / " + this.unixDate();
}

Date.prototype.unixDateTime = function ( ) {
	return this.unixDate() + "T" + this.unixTime();
}
function unixDate ( unix ) {
	var d = new Date();
	return d.setUnixDate(unix);
}
Date.prototype.setUnixDate = function ( unix ) {
	var dd = [];
	var tt = [];
	if ( unix.length > 10 )	{
		var d = unix.substr(0,10);
		var t = unix.substr(unix.length-8);
		dd = d.split("-");
		tt = t.split(":");
		dd[1] = parseInt(dd[1],10) - 1;
	}
	else {
		var x = unix.split("-");
		if ( x.length == 1 ) {
			tt = unix.split(":");
			dd[0] = this.getFullYear();
			dd[1] = this.getMonth();
			dd[2] = this.getDate();
		}
		else {
			dd = x;
			dd[1] = dd[1] - 1;
			tt[0] = this.getHours();
			tt[1] = this.getMinutes();
			tt[2] = this.getSeconds();
		}
	}
	var t = new Date ( parseInt(dd[0],10),parseInt(dd[1],10),parseInt(dd[2],10),
					 parseInt(tt[0],10),parseInt(tt[1],10),parseInt(tt[2],10));
	this.setTime(t.getTime());
	delete t;
	return this;
}

Date.prototype.setUnixOrTimestamp = function ( unixOrTimestamp ) {
	if ( typeof ( unixOrTimestamp ) == "number" )
		this.setTime( unixOrTimestamp * 1000 );
	else
		this.setUnixDate(unixOrTimestamp);
}

Date.prototype.backToMidnight = function ( ) {
	this.setUnixDate(this.unixDate() + "T00:00:00");
}

Date.prototype.phpTime = function ( ) {
	return Math.round ( this.getTime() / 1000 );
}

Date.prototype.plus = function ( seconds ) {
	return new Date ( this.getTime() + seconds * 1000 );
}

Date.prototype.duration = function ( exact ) {
	var ms= this.getUTCMilliseconds();
	var s = this.getUTCSeconds();
	var m = this.getUTCMinutes();
	var h = this.getUTCHours();
	var d = this.getUTCDate() - 1;
	var t = (d > 0 ? d + ':' : '');
	t += (h < 10 ? '0' + h : h) + ':';
	t += (m < 10 ? '0' + m : m) + ':';
	t += (s < 10 ? '0' + s : s);
	if ( exact )
		t += '.' + (ms < 10 ? '00' + s : ms < 100 ? '0' + s : s);
	return t;
}

Date.prototype.add = function ( seconds ) {
	this.setTime ( this.getTime() + seconds * 1000 );
	return this;
}

Date.prototype.subtract = function ( seconds ) {
	this.setTime ( this.getTime() - seconds * 1000 );
	return this;
}

Date.prototype.difference = function ( time, exact ) {
	var time = ( time ? time.getTime() : now() );
	var diff = ( this.getTime() - time ) / 1000;
	if ( exact ) return diff;
	return Math.round ( diff );
}

Date.prototype.age = function ( exact ) {
	var diff = now() - this.getTime();
	if ( exact ) return diff;
	return Math.round ( exact / 1000 );
}

Date.prototype.timestamp = function ( ) {
	return Math.round ( this.getTime() / 1000 );
}

Date.prototype.copy = function ( time ) {
	this.setTime ( time.getTime() );
	return this;
}

function formatTimeString (seconds) {
	var sign = "";
	if ( seconds < 0 ) {
		sign = "-";
		seconds = - seconds;
	}
	var h = Math.floor(seconds / 3600);
	seconds -= h * 3600;
	var m = Math.floor(seconds / 60);
	seconds -= m * 60;

	var hours = ( h < 10 ? ( '0' + h ) : h);
	var minutes = ( m < 10 ? ( '0' + m ) : m);
	seconds = ( seconds < 10 ? ( '0' + seconds ) : seconds);
	
	return sign + hours + ':' + minutes + ':' + seconds;
}
//alert("exit datetime.js");

