/*------------------------------------------------------------
CLASS       AIT Tip
			Version 1.0
--------------------------------------------------------------
Funktion:   öffnet ein unbewegliches Popup am Curser bei MouseOver
            Benötigt Prototype
--------------------------------------------------------------
Author 		Tobias Arnoldt
			Arnoldt IT Limited
			Industriestrasse 36
			74927 Eschelbronn
Datum 		10. Juli 2008
--------------------------------------------------------------
Copyright:	basiert auf
            jTip
            By Cody Lindley (http://www.codylindley.com)
            umgeschrieben von jquery.js auf prototype.js
--------------------------------------------------------------
Changes:    1.0 umschreiben auf prototype.js
--------------------------------------------------------------*/

function AIT_init(){
    $("a.AITip")
    .hover(function(){AIT_show(this.href,this.id,this.name)},function(){$('#AIT').remove()})
    .click(function(){return false});
}

function AIT_show(url,linkId,title){
	if(title == false)title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
	
	
	
	var queryString = url.replace(/^[^\?]+\??/,'');
	var params = parseQuery( queryString );
	if(params['width'] === undefined){params['width'] = 250};
	if(params['link'] !== undefined){
        $(linkId).observe('click', function(){window.location = params['link']});
    	$(linkId).setStyle({cursor:'pointer'});
    }
	
	if(hasArea>((params['width']*1)+75)){
        //tooltip erstellen und am ende der seite einfügen
        var tooltip = new Element('div').update("<div id='AIT' style='width:"+params['width']*1+"px'><div id='AIT_arrow_left'></div><div id='AIT_close_left'>"+title+"</div><div id='AIT_copy'><div class='AIT_loader'><div></div></div>");
        var bodyElement = $('body');
        Element.insert(bodyElement, {'bottom': tooltip});
		var arrowOffset = getElementWidth(linkId) + 11;
		var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	}else{

        //tooltip erstellen und am ende der seite einfügen
        var tooltip = new Element('div').update("<div id='AIT' style='width:"+params['width']*1+"px'><div id='AIT_arrow_right' style='left:"+((params['width']*1)+1)+"px'></div><div id='AIT_close_right'>"+title+"</div><div id='AIT_copy'><div class='AIT_loader'><div></div></div>");
        var bodyElement = $('body');
        Element.insert(bodyElement, {'bottom': tooltip});
		var clickElementx = getAbsoluteLeft(linkId) - ((params['width']*1) + 15); //set x position
	}
	
	//style
	koord_top=clickElementy+"px";
	koord_left=clickElementx+"px";
	$('AIT').setStyle({top:koord_top,left:koord_left});
	
	$('AIT').show();
	
	new Ajax.Updater('AIT_copy', url);


}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function blockEvents(evt) {
              if(evt.target){
              evt.preventDefault();
              }else{
              evt.returnValue = false;
              }
}

