Tooltip = Class.create();

Tooltip.prototype = {

	divId: 'hint_container',
	
	initialize: function() {
		this.createDiv();
		var self = this;
		
		$A($$('.hint')).each(function(item) {
			self.createHint(item);
		});
	},
	
	createDiv: function() {
		var div = document.createElement('div');
		div.className = 'hint';
		
		div.id = this.divId;
		this.divId = div.id;
		
		div.style.position = 'absolute';
		div.style.zIndex = '+999';
		
		$('main').appendChild(div);

		$(this.divId).hide();
	},
	
	createHint: function(elem) {
		var self = this;
		
		Event.observe(elem, 'mouseover', function(e) {
			if($('hint_container').visible()) {
				return ;
			}
			
			if(Prototype.Browser.Gecko) {
				event = e;
			}

			var div = $('hint_container');
			
			div.innerHTML = elem.title;
			
			var pageSize = getPageSize();
			
			var x = Event.pointerX(event) + 5;
			var y = Event.pointerY(event) + 5;
			
			var w = div.getWidth();
			
			if(w + x > pageSize[0]) {
				x = x - w - 5;
			}
			if(x < 0) {
				var freeSpace = pageSize[0] - w;
				x = freeSpace / 2;
			}
			
			div.style.left = (x) + "px";
			div.style.top = (y) + "px";
			
			elem.title = '';
			
			div.show();
			if(Prototype.Browser.IE || Prototype.Browser.Gecko) {
				self.attachBorders();
			}
			//new Effect.Appear($(div), {duration: 0.3});
		});
		
		Event.observe(elem, 'mouseout', function() {
			$('hint_container').hide();
			//new Effect.Fade($('hint_container'), {duration: 0.2});
			elem.title = $('hint_container').innerHTML;
			
			$A($$('.hint_border')).each(function(item) {
				item.remove();
			});
		
		});
	},
	
	attachBorders: function() {
		$A($$('.hint_border')).each(function(item) {
			item.remove();
		});
		var borderWidth = 3;
		
		var div = $('hint_container');
		
		if(div.getHeight() < 100) {
			borderWidth = 1;
		}
		
		div.style.borderBottom = (1+borderWidth) + "px solid #CBCBCB";
		
		// right border
		var border = document.createElement('div');
		
		border.style.position = 'absolute';
		border.style.zIndex = '+999';
		border.style.width = borderWidth + 'px';
		border.style.height = div.getHeight()-borderWidth + "px";
		border.style.background = "#CBCBCB";
		border.className = 'hint_border';

		border.style.left = (parseInt(div.style.left) + div.getWidth()) + "px";
		border.style.top = (parseInt(div.style.top) + borderWidth) + "px";
		document.body.appendChild(border);
	}
}

Event.observe(window, 'load', function() {
	new Tooltip();
});


function getPageSize() {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}