var KCHTooltip = Class.create({
   linkobj: null,
   visible: false,
   draw:false,
   kchcontent:false,
   id:null,
   loadingimage:null,
   arrowimg:null,
   contentlineheight:16,
   linecharcount:40,
   tipwidth:236,
   tipheight:90,
   addheight:50,
   uint:'px',
   loadingimgw:16,
   loadingimageh:16,
   container: null,    
   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);
    this.container = $('col2_content');
	},

	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];

    if(this.linkobj.offsetHeight > 16) {
		var ho = 30; //center
		} else {
		var ho = this.linkobj.offsetWidth / 2; //center
		}
    
		var vo = this.linkobj.offsetHeight / 2;//middle

		

		vo = -3;//set  top



		if(this.kchcontent.hasClassName('kchtooltiploading'))

		{

			x += ho-this.loadingimgw/2;

			y += vo-this.loadingimageh;			  

		}

		else

		{

			x += ho-this.tipwidth/2;

			y += vo-this.tipheight;	

			//overwrite

	        if(this.container)

			{	
        
        var poscontainer = Position.cumulativeOffset(this.container);

        var pos = Position.cumulativeOffset(this.linkobj);
        
        if(this.linkobj.offsetHeight > 16) {
        x = pos[0]+15 - this.tipwidth/2;
        } else {
        x = pos[0]+parseInt(this.linkobj.offsetWidth)/2 - this.tipwidth/2;
        }
			}
		}

		



		var kchcontentStyle = {

			'left' 	: x + this.uint,

			'top'	: y + this.uint,

			'width' : this.tipwidth + this.uint,

			'height': this.tipheight + this.uint

		}

		

		Element.setStyle(this.kchcontent, kchcontentStyle);

	},

	_getdataonsuccess: function (transport)

	{

		var htmltext = transport.responseText;

		

		var aa=htmltext.length/this.linecharcount;

		var num = this.countH2(htmltext,'h2');

		var tmpheight = (aa+num)*this.contentlineheight+this.addheight;

		

		this.tipheight = this.tipheight > tmpheight?this.tipheight:tmpheight;

		

		

	   //for container 		

	    if(this.container)

	    {	

			var poscontainer = Position.cumulativeOffset(this.container);

			var pos = Position.cumulativeOffset(this.linkobj);

			//var arrowimgleft = pos[0]-poscontainer[0]+parseInt(this.linkobj.offsetWidth)/2-9;
			var arrowimgleft = this.tipwidth/2 - 11;

			var arrowimgtop = this.tipheight - 10;

			Element.setStyle(this.arrowimg, {

					'left': arrowimgleft+this.uint,

					'top': arrowimgtop+this.uint

			});	



		}

		var arrowimgcontainer = new Element('div');

		arrowimgcontainer.appendChild(this.arrowimg);

		this.kchcontent.innerHTML = '<div class="kchtooltipinnerwrapper" style="height:'+

		this.tipheight+'px;">'
		+'<div class="kchtooltiptopwrapper"><div class="kchtooltipleftwrap"><div class="kchtooltiprightwrap">'

		+arrowimgcontainer.innerHTML+' <div class="kchtooltipcntbottombg" style="width:'+

		this.tipwidth+'px;height:8px;top:'+(this.tipheight - 10)+this.uint+

		'"></div><div class="kchtooltipcntmain" style="height:'+(this.tipheight - 10)+

		'px">'

		+htmltext+'</div></div></div></div></div>';	

		this.kchcontent.removeClassName('kchtooltiploading'); 

	

   		this._reposition();      

	    this.draw = true;	

	},

	_getdataonfailure:function ()

	{

		alert('network problem!');

		this.draw = false;

	},

	

	countH2:function (mainStr, subStr) {

    var count = 0;

    var offset = 0;

    do

    {

      offset = mainStr.indexOf(subStr, offset);

      if(offset != -1) {

        count++;

        offset += subStr.length;

      }

    }while(offset != -1)

    return count;

  }



});



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'); 

			}

		});				

	}



});



Event.observe(window, 'load', KCHTooltip.registerClassLinks);
