var KCHTooltip = Class.create({
   linkobj: null,
   visible: false,
   draw:false,
   kchcontent:false,
   id:null,
   loadingimage:null,
   arrowimg:null,
   tipwidth:187,
   uint:'px',
   loadingimgw:16,
   loadingimageh:16,
   initialize: function(option)
	{
		this.linkobj = option.linkobj;
	 	Event.observe(this.linkobj, 'mouseover', this.showdata.bindAsEventListener(this));
	 	Event.observe(this.linkobj, 'mouseout', this.hidedata.bindAsEventListener(this));
		this.kchcontent = new Element('div');
    this.loadingimage = new Element('img', {src: 'fileadmin/templates/images/ajaxload.gif'});	
		this.arrowimg = new Element('div');
		this.arrowimg.appendChild(new Element('img', {src: 'fileadmin/templates/images/tiparrow.gif'}));
		this.arrowimg.addClassName('kchtooltiparrow');
		this.kchcontent._KCHTooltip = this;
		this.id = 'KCHTooltip_' + Element.identify(this.kchcontent);
	},

	showdata: function(event)

	{

		if(!event) event = window.event || {type: 'mouseover', target: this.linkobj};

		var linkobj = Event.element(event);

		if(linkobj.tagName.toLowerCase() != 'a') linkobj= linkobj.parentNode;

		Event.stop(event);

		if(event.type == 'mouseover' && !this.visible && linkobj == this.linkobj)

		{

		    $A(KCHTooltip._alltipitems).each(function(obj){

				if(obj != this)

				{

					obj.hide();

				}

			}.bind(this));

			

			if(!this.draw)

			{

		        Element.setStyle(this.kchcontent,  {'display': 'none'});

				this.kchcontent.appendChild(this.loadingimage);

				this.kchcontent.addClassName('kchtooltipwrapper'); 

				this.kchcontent.addClassName('kchtooltiploading'); 

				Element.setStyle(this.kchcontent, {

						'display': 'block'

			    });		

			    document.getElementsByTagName('body')[0].appendChild(this.kchcontent);					

				this._reposition();

				new Ajax.Request(this.getgslink(this.linkobj.href), 

				{

					 asynchronous: true, 

					 method: 'get',

					 onSuccess:this._getdataonsuccess.bind(this),

					 onFailure:this._getdataonfailure.bind(this)

					 

				}				 

				);

				this.draw = true;

			}

			else

			{

				Element.setStyle(this.kchcontent, {

							'display': 'block'

				});	

			}			

			this._reposition();

			this.visible = true;			



		}

		else

		{

		}

	},

	getgslink:function(link)

	{

		var rlink = "";

		rlink=link.substring(0,link.indexOf('?')+1)+'d=1&'+link.substring(link.indexOf('?')+1);

		

		return rlink;

	}

	,

	hidedata: function(event)

	{

		if(!event) event = window.event || {type: 'mouseout', target: this.linkobj};

		var linkobj = Event.element(event);

		if(linkobj.tagName.toLowerCase() != 'a') linkobj= linkobj.parentNode;

		Event.stop(event);

		if(event.type == 'mouseout' && this.visible && linkobj == this.linkobj)

		{

	          this.hide();			

		}

	},

	hide: function()

	{

		if(this.visible)

		{

			Element.setStyle(this.kchcontent, {

					'display': 'none'

				});								

			this.visible = false;

			Event.stopObserving(window, 'resize', this._reposition.bindAsEventListener(this));

		}

	},

	_reposition: function()

	{

		var pos = Position.cumulativeOffset(this.linkobj);
		var y = pos[1];
		var x = pos[0];

		var ho = this.linkobj.offsetWidth / 2; //center
    
		var vo = this.linkobj.offsetHeight / 2;//middle
      
    if(this.linkobj.offsetHeight > 16) {
      x = pos[0]+15 - this.tipwidth/2;
    } else {
      x = pos[0]+parseInt(this.linkobj.offsetWidth)/2 - this.tipwidth/2;
    }
    y += vo+10;
		var kchcontentStyle = {

			'left' 	: x + this.uint,

			'top'	: y + this.uint,

			'width' : this.tipwidth + this.uint
		}

		

		Element.setStyle(this.kchcontent, kchcontentStyle);

	},

	_getdataonsuccess: function (transport)

	{

		var htmltext = transport.responseText;

		var arrowimgcontainer = new Element('div');

		arrowimgcontainer.appendChild(this.arrowimg);

		this.kchcontent.innerHTML = '<div class="kchtooltipinnerwrapper">'

		+arrowimgcontainer.innerHTML+'<div class="kchtooltipcntmain">'

		+htmltext+'</div></div>';
		
		this.kchcontent.removeClassName('kchtooltiploading'); 

   this._reposition();      

	 this.draw = true;	

	},

	_getdataonfailure:function ()

	{

		alert('network problem!');

		this.draw = false;

	}

});



Object.extend(KCHTooltip,{

   ELEMENT_CLASS_NAME : 'KCHTooltip',

   _alltipitems:[],

   registerClassLinks: function(e) {

		$A(document.getElementsByClassName(KCHTooltip.ELEMENT_CLASS_NAME))

			.each(function(obj){

			

			if(obj && obj.tagName && obj.href && obj.href != '')

			{

			   //obj.onclick = new Function("return false;" ); 

				new KCHTooltip({

					linkobj:obj

				});

			   obj.addClassName('kcheventloaded'); 

			}

		});				

	}



});

addLoadEvent(KCHTooltip.registerClassLinks);


