dojo.addOnLoad(init);

function init() {
	var links = document.getElementsByTagName('a');
	for (var i=0; i<links.length; i++) {
		if (matches = links[i].id.match(/^event(\d*)$/)) {
			links[i].eventId = matches[1];
			links[i].onclick = toggleEvent;
			links[i].nowShowing = false;
		}
	}
}

function toggleEvent() {
	if (this.extraInfo == null) {
		// do some initialization
		this.parentDiv = dojo.html.getFirstAncestorByTag(this,'div');
		this.statusNode = document.getElementById('status'+this.eventId);
		dojo.dom.removeChildren(this.statusNode);
/*		var nodes = dojo.html.createNodesFromText('<img src="/calendar/images/spinner.gif" style="vertical-align: middle;" />');
		for (var i=0; i<nodes.length; i++) {
			this.statusNode.appendChild(nodes[i]);
		}*/
		
		dojo.io.bind({
			url: '/calendar/ajax.php',
			content: {
				id: this.eventId
			},
			load: dojo.lang.hitch(this,_initMoreInfo)
		});
		return false;
	}

	var eventDiv = this.parentDiv;
	var extraInfoDiv = this.extraInfo;
	this.nowShowing = !this.nowShowing;
	var borderColor = '#fff';
	if (this.nowShowing) {
		dojo.dom.removeChildren(this.statusNode);
		this.statusNode.appendChild(dojo.html.createNodesFromText('&laquo;')[0]);

		var startRgb = new dojo.graphics.color.Color('#7ad3f7').toRgb();
		var endRgb = new dojo.graphics.color.Color(borderColor).toRgb();
		var anim = new dojo.animation.Animation(
			new dojo.math.curves.Line(startRgb, endRgb),
			500, 0);
		dojo.event.connect(anim, "onAnimate", function(e) {
			eventDiv.style.borderColor = "rgb(" + e.coordsAsInts().join(",") + ")";
		});
		dojo.event.connect(anim, "onEnd", function(e) {
			dojo.fx.html.wipeShow(extraInfoDiv,300,function() {
				scrollToShowEvent(eventDiv);
			});
		});
		anim.play(true);
	}
	else {
		dojo.dom.removeChildren(this.statusNode);
		this.statusNode.appendChild(dojo.html.createNodesFromText('&raquo;')[0]);

		dojo.fx.html.wipeHide(this.extraInfo,300,function() {
			var startRgb = new dojo.graphics.color.Color(borderColor).toRgb();
			var endRgb = new dojo.graphics.color.Color('#7ad3f7').toRgb();
			var anim = new dojo.animation.Animation(
				new dojo.math.curves.Line(startRgb, endRgb),
				500, 0);
			dojo.event.connect(anim, "onAnimate", function(e) {
				eventDiv.style.borderColor = "rgb(" + e.coordsAsInts().join(",") + ")";
			});
			anim.play(true);
		});
	}
	return false;
}

function _initMoreInfo(type, data, evt) {
	var newDiv = document.createElement('div');
	newDiv.innerHTML = data;
	dojo.html.getFirstAncestorByTag(this,'div').appendChild(newDiv);
	this.extraInfo = newDiv;
	this.extraInfo.style.display = 'none';
	this.toggleEvent = toggleEvent;
	this.toggleEvent();
}

function scrollToShowEvent(eventDiv) {
	if (dojo.style.totalOffsetTop(eventDiv)+eventDiv.offsetHeight > 
		dojo.html.getViewportHeight()+dojo.html.getScrollTop()) {

		var curve = new dojo.math.curves.Line([0,dojo.html.getScrollTop()],
[0,dojo.style.totalOffsetTop(eventDiv)-50]);
		var anim = new dojo.animation.Animation(curve,500);
		dojo.event.connect(anim, 'onAnimate', function(e) {
			scrollTo(0,e.y);
		});
		anim.play();
	}
}
