// =============================
// = Utilitaire pour les dates =
// =============================


function Duration() {
	this.years = 0;
	this.months = 0;
	this.weeks = 0;
	this.milliseconds = 0;
}


Number.prototype.pad = function( length ) {
	var str = this.toString();
	
	while( str.length < length )
	{
		str = "0" + str;
	}
	return str;	
}

Number.prototype.seconds = function() {
	return this * 1000;
}

Number.prototype.minutes = function() {
	return this * 60000;
}

Number.prototype.hours = function() {
	return this * 3600000;
}

Number.prototype.days = function() {
	return this * 86400000; // Nombre de millisecondes dans un jour.
}

Number.prototype.weeks = function() {
	return this * 604800000;
}

Number.prototype.months = function() {
	d =  new Duration();
	d.months = this;
	return d;
}

Number.prototype.years = function() {
	d = new Duration();
	d.years = this;
	return d;
}

function MonthDuration( number )
{
	this.number = number;
}

function YearDuration( number )
{
	this.number = number;
}

Date.prototype.format = function( format_string ) {

	if( format_string == undefined ) 
		format_string = "yyyy-MM-dd HH:mm:SS";

	format_string = format_string.replace("dd", this.getDate().pad( 2 ) );
	format_string = format_string.replace( "d", this.getDate() );

	format_string = format_string.replace( "MM", (this.getMonth() + 1 ).pad( 2 ) );
	format_string = format_string.replace( "M", (this.getMonth() ).pad( 2 ) );
	format_string = format_string.replace( "yyyy", this.getFullYear() );

	format_string = format_string.replace( "HH", this.getHours().pad( 2 ) );
	format_string = format_string.replace( "H", this.getHours() );

	format_string = format_string.replace( "mm", this.getMinutes().pad( 2 ) );
	format_string = format_string.replace( "m", this.getMinutes() );

	format_string = format_string.replace( "SS", this.getSeconds().pad( 2 ) );

	return format_string;
};

Date.prototype.ddMMyyyy = function() {
	return this.format( "dd/MM/yyyy" );
}

Date.prototype.yyyyMMdd = function() {
	return this.format( "yyyy-MM-dd" );
}

Date.prototype.add = function( duration ) {
	var type = typeof( duration );
	var new_date = new Date( this.getTime() );
	
	if( type == "number" ) {
		new_date.setTime( this.getTime() + duration );
	} else 	{
		if( duration.months != undefined )
		{
			var m = (this.getMonth() + duration.months);
			if( m > 11 ) {
				duration.years = Math.floor( m / 12 );
			}
			new_date.setMonth( m % 12 );
		}
		if( duration.years != undefined )
			new_date.setYear( this.getFullYear() + duration.years );
	}
	
	return new_date;
}

Date.prototype.substract = function( duration ) {
	var type = typeof( duration );
	var new_date = new Date( this.getTime() );
	
	if( type == "number" ) {
		new_date.add( this.getTime() + duration );
	} else 	{
		if( duration.months != undefined )
		{
			duration.years = Math.floor( duration.months / 12 );
			var m = ( this.getMonth() - duration.months % 12 );
			
			if( m < 0 )
				duration.years ++;
			
			new_date.setMonth( Math.abs( m ) );
		}
		if( duration.years != undefined )
			new_date.setYear( this.getFullYear() - duration.years );
	}
	
	return new_date;
}

Date.prototype.tomorrow = function() {
	return this.add( (1).days() );
}

Date.prototype.yesterday = function() {
	return this.substract( (1).days() );	
}

Date.prototype.beginningOfWeek = function() {
	var diff = (this.getDay() + 6 ) % 7; // On commence le lundi
	var only_date = this.beginningOfDay();
	
	
	return only_date.add( -1 * diff.days() );
}

Date.prototype.endOfWeek = function() {
	return this.beginningOfWeek().add( (7).days() - 1 ); // 1 milliseconde avant la fin de la semaine :p
}

Date.prototype.nextWeek = function() {
	return this.add( (7).days() );
}

Date.prototype.lastWeek = function() {
	return this.substract( (7).days() );
}

Date.prototype.midnight = 
Date.prototype.beginningOfDay = function() {
	return new Date( this.getFullYear(), this.getMonth(), this.getDate() );
}

Date.lastDayOnEarth = Date.apocalypse = function() { return new Date( 2012, 11, 21 ) };
