var Vevent = Class.create();

var VUID = 0;

Vevent.prototype = {
	dtstart: '',
	dtend: '',
	url: '',
	location: '',
	summary: '',
	home_calendar: null,
	for_home: '',
	work_calendar: null,
	for_work: '',
	id:0,


	addMe: function() {
		
		//this is the initialising bit.
		this.id = VUID++;
		if(!this.dtstart) {
			this.dtstart = new Date();
			this.dtstart = this.dtstart.getFullYear()+'-'+(this.dtstart.getMonth()+1)+'-'+this.dtstart.getDate();
		}
		if(!this.dtend) {
			this.dtend = new Date();
			this.dtend = this.dtend.getFullYear()+'-'+(this.dtend.getMonth()+1)+'-'+this.dtend.getDate();
		}

		if(this.for_work) {
			this.work_calendar.add(this);
		} else {
			this.home_calendar.add(this);
		}		
	},

	deleteMe: function() {
		if(this.for_work) {
			this.work_calendar.remove(this);
		} else {
			this.home_calendar.remove(this);
		}		
	},

	draw: function() {
		var node = document.createElement('span');
		ObjectiveDegradation.copyToNode(this,node);
		node.className = 'Vevent';
		node.id = "vevent"+this.id;
		
		var url = document.createElement('a');
		url.className = 'url';
		url.href=this.url;
		node.appendChild(url);
		
		url.innerHTML = 
		((this.summary)?'<span class="summary">'+this.summary+'</span>:':'')+ 
		'<abbr class="dtstart" title="'+this.dtstart+'">'+this.dtstart+'</abbr>-'+
		'<abbr class="dtend" title="'+this.dtend+'">'+this.dtend+'</abbr>,'+
		((this.location)?'at <span class="location">'+this.location+'</span>':'');
		
		var deleteMe = document.createElement('a');
		deleteMe.className = 'deleteMe';
		deleteMe.href='delete'+this.id;
		deleteMe.innerHTML = 'Delete';
		node.appendChild(deleteMe);
		return node;
	}
};

var Calendar = Class.create();

Calendar.prototype = {
	title:'',
	event_count:'',
	vevent_container: Array(),
	add: function(vevent) {
		var container = document.createElement('div');
		container.appendChild(vevent.draw());
		this._element.appendChild(container);
		this.vevent_container[vevent.id] = container;
		new Effect.Highlight(container);
		this.event_count.increment();
	},

	remove: function(vevent) {
		var container = this.vevent_container[vevent.id];
		this.vevent_container[vevent.id] = null;
		if(container) {
			new Effect.Fade(container, {afterFinish: function() {
				this._element.removeChild(container);
				this.event_count.decrement();
			}.bind(this)});
		}
	}
};

var Counter = Class.create();

Counter.prototype = {
	value: 0,
	increment: function() {
		this.value++;
		this.updateValue();
	},
	decrement: function() {
		this.value--;
		this.updateValue();
	},
	updateValue: function() {
		this._element.firstChild.innerHTML = this.value;
	}
};


ObjectiveDegradation.register({
	'Vevent': Vevent,
		'Calendar': Calendar,
			'Counter': Counter
});
