DTWCalendar = Class.create();

DTWCalendar.prototype.initialize = function(_element){
	this.element = $(_element);
	this.toolTip = $A(this.element.getElementsByClassName("tooltip"))[0];
	

	Event.observe(this.element.ownerDocument, 'mousemove', this.mouseMove.bindAsEventListener(this), false);
	
	$$('div.cal-event a').each(function(_eventElement) {
		Event.observe(_eventElement, 'mouseover', this.onEnterEvent.bindAsEventListener(this), false);
		Event.observe(_eventElement, 'mouseout', this.onLeaveEvent.bindAsEventListener(this), false);
	}.bind(this));

}

DTWCalendar.prototype.onEnterEvent = function(_event){
	this.toolTip.style.visibility = "visible";
	sender = $(Event.findElement(_event, "div"));
	
	contentsDiv = $A( sender.getElementsByClassName("tooltipContent") )[0];
	
	this.toolTip.update(contentsDiv.innerHTML);
	classes = $A(contentsDiv.classNames());

	this.toolTip.removeClassName("red");
	this.toolTip.removeClassName("green");
	this.toolTip.removeClassName("blue");
	this.toolTip.removeClassName("orange");
	this.toolTip.removeClassName("yellow");
	this.toolTip.removeClassName("grey");
	
	this.toolTip.addClassName(classes[1]);

}

DTWCalendar.prototype.onLeaveEvent = function(_event){
	this.toolTip.style.visibility = "hidden";
}


DTWCalendar.prototype.mouseMove = function(_event){

	x = Event.pointerX(_event)-Position.cumulativeOffset(this.element)[0];
	y = Event.pointerY(_event)-Position.cumulativeOffset(this.element)[1];


	x -= 20;
	y += 20;


	//bound the tooltip to the calendar
	if (x > this.element.getWidth() - this.toolTip.getWidth()){
            x = this.element.getWidth() - this.toolTip.getWidth();
	}

	if (x < 0){
            x = 0;
	}

        //bound the tooltip to the calendar
	if (y > this.element.getHeight() - this.toolTip.getHeight()){
            y = y - this.toolTip.getHeight() - 45;
	}

	//position the tooltip
	this.toolTip.style.left = x + "px";
	this.toolTip.style.top = y + "px";

	//this.toolTip.style.left = (x + Position.cumulativeOffset(this.element)[0]) + "px";
	//this.toolTip.style.top = (y + Position.cumulativeOffset(this.element)[1]) + "px";

}



function initDTWCalendars(_event) {
	$$('div.calendar').each(function(_DTWCalendar) {
		if (_DTWCalendar.controller == undefined){
			_DTWCalendar.controller = new DTWCalendar(_DTWCalendar);
		}
	});
}

Event.observe(window, 'load', initDTWCalendars.bindAsEventListener(this), false);
	
