dojo.provide("epages.cartridges.de_epages.calendar.widget.Appointmenttimeitem");
dojo.require("epages.widget.LocalizedWidget");
dojo.require("epages.cartridges.de_epages.calendar.widget.Appointment");
dojo.require("dojo.date.stamp");

dojo.declare(
	"epages.cartridges.de_epages.calendar.widget.Appointmenttimeitem",
	[epages.widget.LocalizedWidget],
	{

		/**
		 * public properties
		 */
		objectId:       '', // CalendarId
		productId:      '',
		StartDate:      '',
		EndDate:        '',
		IsBusiness:			'',
		IsBookingHours: '',
		_StartDateTime: '',
		Appointments:   [],
		url:            '?',
		isinactive:			'0',
		availableQuantity: '',

		/**
		 * dojo widget properties
		 */
		translationName : dojo.moduleUrl('epages.cartridges.de_epages.calendar.widget','templates/translation'),
		templatePath:     dojo.moduleUrl('epages.cartridges.de_epages.calendar.widget',"templates/AppointmentListSmall.html"),
		widgetsInTemplate:true,

		postMixInProperties: function () {
			this.inherited('postMixInProperties',arguments);
			var pad = dojo.string.pad;
			var start = dojo.date.stamp.fromISOString(this.StartDate);
			this._StartDateTime = pad(start.getHours(),2) + ':' + pad(start.getMinutes(),2);
		},

		postCreate: function () {
			// summary: Create instance of an Appointmentitem.
			// description: This method is used to show the list of booked resources for one day when
			// mouse-overed a day in the appointment list. To show the number of booked appointment slots
			// for this day, all booked appontments are traversed and the quantity of booked slots is
			// summed up.
			// Then the name and status of each booked appointment is listed which results in the appropiate
			// dom nodes. A dom node is marked as overbooked if more slots were booked for the day than
			// available (which is set by the stock value for the resource product).
			this.inherited('postCreate',arguments);

			// calculate sum of used slots
			var sumQuantities = 0.0;
			for( var i=0,l=this.Appointments.length ; i<l ; i++ ) {
				sumQuantities += parseFloat(this.Appointments[i].Quantity);
			}

			// create overview list
			for( var i=0,l=this.Appointments.length ; i<l ; i++ ) {
				var d = this.Appointments[i];
				d.productId = this.productId;
				d.url = this.url;
				// if number of used slots is higher than number of available ones, mark as overbooked
				if(sumQuantities > parseFloat(this.availableQuantity)){
					dojo.addClass(this.domNode,'Overbooked');
				}
				var w = new epages.cartridges.de_epages.calendar.widget.Appointment(d);
				// if any appointments for this time, list them
				if(i>0) {
					var s = document.createTextNode(", ");
					$(this.appointmentsNode).appendChild(s);
				}
				$(this.appointmentsNode).appendChild(w.domNode);
			}
			if (this.isinactive == '1') {
				if (this.inactiveNode != undefined){
					this.inactiveNode.style.display = '';
				}
				if (this.activeNode != undefined){
					this.activeNode.style.display = 'none';
				}
			}
			if(this.numberOfDatesNode){
				this.numberOfDatesNode.innerHTML = sumQuantities + ' / ' +this.availableQuantity;
			}
		}
	}
);