var tooltips = new Array();

var _tooltip = {};
_tooltip = function(){};
_tooltip.prototype = {};

gTooltip = Class.create();
gTooltip.prototype = Object.extend(new _tooltip(), {
	initialize: function(options) {
		this.options = {
			className: '',
			fade: true,
			delayin: 200,
			delayout: 500,
			autohidedelay: 5000
		};
		Object.extend(this.options,options || { });
		this.arrowheadimg = ['arrow-left.gif','arrow-right.gif'];
		for (var i=0;i<this.arrowheadimg.length;i++){
			var img = new Image();
			img.src = this.arrowheadimg[i];
		};
		var i = 0;
		var frameId = 'overlay_iframe_' + i;
		while (document.getElementById(frameId)) {
			i++;
			frameId = 'overlay_iframe_' + i;
		};
		var iframe = null;
		if (window.ActiveXObject) {
			iframe = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" scrolling="no" />');
		} else {
			iframe = document.createElement('iframe');
			iframe.setAttribute('id',frameId);
			iframe.setAttribute('name',frameId);
		};
		iframe.setAttribute('frameBorder','0');
		iframe.setAttribute('scrollbar','0');
		document.body.appendChild(iframe);
		new Fade(iframe).setStyle('opacity',0);
		iframe.style.position = 'absolute';
		iframe.style.display = 'none';
		lyrtooltip = document.createElement("div");
		lyrtext = document.createElement("div");
		imgobj = document.createElement("img");
		lyrtext.onmouseout = this.delayHide.bind(this);
		lyrtext.className = this.options.className;
		lyrtext.style.position = 'absolute';
		imgobj.style.position = 'absolute';
		lyrtooltip.style.position = 'absolute';
		lyrtext.style.top = '0px';
		imgobj.style.top = '0px';
		lyrtooltip.style.left = '-1000px';
		lyrtooltip.style.top = '-1000px';
		lyrtooltip.overflow = 'hidden';
		iframe.style.zIndex = 2000;
		lyrtooltip.style.zIndex = 2001;
		lyrtooltip.appendChild(lyrtext);
		lyrtooltip.appendChild(imgobj);
		document.body.appendChild(lyrtooltip);
		this.fade = new Fade(lyrtooltip);
		this.lyrtext = new Element(lyrtext);
		this.imgobj = imgobj;
		this.iframe = iframe;
		this.id = tooltips.length;
		tooltips[this.id] = this;
		$(document.body).addEvent('click',function(){
			for (var i=0;i<tooltips.length;i++){
				tooltips[i].hide();
			};
		});
		this.req = new GAjax();
	},

	setArrow: function(img1,img2) {
		this.arrowheadimg[0] = img1;
		var img = new Image();
		img.src = img1;
		this.arrowheadimg[1] = img2;
		var img = new Image();
		img.src = img2;
	},

	showAjax: function(elem,url,query) {
		if(typeof this.req._xhr != 'undefined') this.req.abort();
		temp = this;
		this.req.send(url,query,function(xhr){
			data = xhr.responseText;
			if (temp.iframe.style.display != 'none') temp.show(elem,data)
			else this.delayin = window.setTimeout(function(){temp.show(elem,data)},temp.options.delayin);
		});
		el = $(elem).elem;
		var old_onmouseout = el.onmouseout;
		req = this.req;
		el.onmouseout = function(){
			req.abort();
			window.clearTimeout( temp.delayin );
			window.clearTimeout( temp.delayout );
			window.clearTimeout( temp.timeautohidedelay );
			el.onmouseout = old_onmouseout;
			temp.delayout = window.setTimeout(function(){temp.hide.call(temp)},temp.options.delayout);
		};
	},

	show: function(elem,val) {
		var body = document.getElementsByTagName('body')[0];
		var temp = document.createElement('div');
		temp.className = this.options.className;
		temp.style.position = 'absolute';
		temp.style.visibility = 'hidden';
		body.appendChild(temp);
		temp.innerHTML = val;
		var lyrtextHeight = temp.offsetHeight;
		var lyrtextWidth = temp.offsetWidth;
		body.removeChild(temp);
		temp = null;
		this.cancleHideDelay();
		this.fade.Element.style.display = 'block';
		this.lyrtext.elem.innerHTML = val;
		var elems = this.lyrtext.elem.getElementsByTagName('*');
		for (var i = 0 ; i < elems.length ; i++ ) elems[i].onmouseover = this.cancleHideDelay.bind(this);
		el = $(elem);
		elemPos = el.viewportOffset();
		elemHeight = el.getHeight();
		elemWidth = el.getWidth();
		clientHeight = document.viewport.getHeight();
		clientWidth = document.viewport.getWidth();
		scrollTop = document.viewport.getscrollTop();
		scrollLeft = document.viewport.getscrollLeft();
		arrowTop = ((lyrtextHeight + elemHeight) / 2);
		t = elemPos.top - (lyrtextHeight / 2) + (elemHeight / 2);
		arrowTop = (lyrtextHeight / 2) - (elemHeight / 2);
		if (t < scrollTop) {
			arrowTop = scrollTop < elemPos.top ? arrowTop - (scrollTop - t) : 0;
			t = scrollTop < elemPos.top ? scrollTop : elemPos.top;
		} else if ((t + lyrtextHeight) > (scrollTop + clientHeight)) {
			ot = t;
			t = (scrollTop + clientHeight) - lyrtextHeight;
			if ((t + lyrtextHeight) < (elemPos.top + elemHeight)) {
				t = (elemPos.top + elemHeight) - lyrtextHeight;
			};
			arrowTop = arrowTop + (ot - t);
		};
		bw = this.lyrtext.getStyle('borderLeftWidth');
		bw = bw == 'medium' ? 0 : parseFloat( bw );
		
		l = elemPos.left + elemWidth + 5;
		if ( l + lyrtextWidth < clientWidth ) {
			this.imgobj.src = this.arrowheadimg[0];
			w = this.imgobj.width || 13;
			this.lyrtext.elem.style.left = (w - bw) + 'px';
			this.imgobj.style.marginLeft = '0px';
		} else {
			this.imgobj.src = this.arrowheadimg[1];
			var w = this.imgobj.width || 13;
			l = elemPos.left - lyrtextWidth - w;
			this.lyrtext.elem.style.left = '0px';
			this.imgobj.style.marginLeft = (lyrtextWidth - bw) + 'px';
		};
		this.fade.Element.style.left = l + 'px';
		this.iframe.style.left = l + 'px';
		
		this.imgobj.style.top = arrowTop + 'px';
		this.fade.Element.style.top  = t + 'px';
		this.fade.Element.style.height = lyrtextHeight + 'px';
		this.fade.Element.style.width = (lyrtextWidth + w) + 'px';
		this.iframe.style.top  = t + 'px';
		this.iframe.style.height = lyrtextHeight + 'px';
		this.iframe.style.width = (lyrtextWidth + w - bw) + 'px';
	
		var temp = this;
		for (var i=0;i<tooltips.length;i++){
			if (i != temp.id) tooltips[i].hide();
		};
		if (this.options.fade && (this.iframe.style.display == 'none' || this.fade.playing)) {
			this.fade.play({'from':0,'to':100,'iframe':this.iframe,'onComplete':function(el){
				if (el.name == 'fadeOut') {
					el.Element.style.display = 'none';
					el.options.iframe.style.display = 'none';
				} else temp.timeautohidedelay = window.setTimeout(temp.hide.bind(temp),temp.options.autohidedelay);
			}});
		} else this.timeautohidedelay = window.setTimeout(this.hide.bind(this),this.options.autohidedelay);
		this.fade.Element.style.display = '';
		this.iframe.style.display = '';
	},

	delayHide: function() {
		this.timedelayhide = window.setTimeout(this.hide.bind(this),this.options.autohidedelay);
	},

	hide: function() {
		if (this.options.fade) {
			this.fade.play({'from':100,'to':0});
		} else {
			this.fade.Element.style.display = 'none';
			this.iframe.style.display = 'none';
		};
	},

	cancleHideDelay: function() {
		if(typeof this.req._xhr == 'undefined') this.req.abort();
		window.clearTimeout(this.timeautohidedelay);
		window.clearTimeout(this.timedelayhide);
		window.clearTimeout(this.delayout);
	}
});
