// Copyright 2008 ProjectWizards GmbH. All rights reserved.

//console.log("MEDuration.js");

var MEDuration = WRLObject.extend({
	constructor: function(json){
		this.base();
		this.amount = json.a == null || typeof(json.a) == "undefined" ? null : json.a;
		this.unit = json.u == null || typeof(json.u) == "undefined" ? null : json.u;
		this.isFloating = json.f == null || typeof(json.f) == "undefined" ? null : json.f;
		this.relativeError = json.e == null || typeof(json.e) == "undefined" ? null : json.e;
		this.updateSeconds();		
	},
	
	className: "MEDuration",
	
	updateSeconds: function(manDayHours, manWeekHours){
		var Unit = MEDuration.DurationUnit;
		if(this.isFloating || !manDayHours || !manWeekHours){ 
			switch(this.unit) {
				case Unit.MESeconds:
					this.seconds = this.amount; break;
				case Unit.MEMinutes:
					this.seconds = this.amount * 60.0; break;
				case Unit.MEHours:
					this.seconds = this.amount * 3600.0; break;
				default:
				case Unit.MEDays:
					this.seconds = this.amount * 3600.0 * 24.0; break;
				case Unit.MECalendarWeeks:
				case Unit.MEWeeks:
					this.seconds = this.amount * 3600.0 * 24.0 * 7.0; break;
				case Unit.MEMonths:
					this.seconds = this.amount * 3600.0 * 24.0 * 28.0; break;
				case Unit.MEYears:
					this.seconds = this.amount * 3600.0 * 24.0 * 28.0 * 12.0; break;
				case Unit.MEFraction:
				case Unit.MEMaterialUnit:
					this.seconds = 0; break;
			}
		}else{
			switch(this.unit) {
				case Unit.MESeconds:
					this.seconds = this.amount; break;
				case Unit.MEMinutes:
					this.seconds = this.amount * 60.0; break;
				case Unit.MEHours:
					this.seconds = this.amount * 3600.0; break;
				default:
				case Unit.MEDays:
					this.seconds = this.amount * 3600.0 * manDayHours; break;
				case Unit.MECalendarWeeks:
				case Unit.MEWeeks:
					this.seconds = this.amount * 3600.0 * manWeekHours; break;
				case Unit.MEMonths:
					this.seconds = this.amount * 3600.0 * manWeekHours * 4.0; break;
				case Unit.MEYears:
					this.seconds = this.amount * 3600.0 * manWeekHours * 52; break;
				case Unit.MEFraction:
				case Unit.MEMaterialUnit:
					this.seconds = 0; break;
			}
		}		
	},
	
	sortValue: function() {
		return this.seconds;
	},

    toJSON: function() {
        return '{"a":' + this.amount + ', "u":' + this.unit + ', "f":' + this.isFloating +', "e":' + this.relativeError + '}';
    }
	
},{	// Class interface:
    formatter: function(environment) {
        return new DurationFormatter(environment);
    },
	
	sharedFormatter: function(environment) {
		if(!MEDuration._sharedFormatter)
			MEDuration._sharedFormatter = MEDuration.formatter(environment);
		return MEDuration._sharedFormatter;
	},
	
	DurationUnit: {	MESeconds:0, 
					MEMinutes:1, 
					MEHours:2, 
					MEDays:3, 
					MEWeeks:4, 
					MECalendarWeeks:5, 
					MEMonths:6, 
					MEQuarterYears:7, 
					MEYears:8, 
					MENoUnit:19, 
					MEFraction:20, 
					MEMaterialUnit:21, 
					MESpecialCompareUnit:22},
	
	durationWithAmountUnitIsFloatingRelativeError: function(amount, unit, floating, relativeError) {
		return new MEDuration({ a:amount, u:unit, f:floating, e:relativeError });
	}	
	
});
