function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value + ";path=/";
}

function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	  x=x.replace(/^\s+|\s+$/g,"");
	  if (x==c_name)
	    {
	    return unescape(y);
	    }
	  }
}


$(document).ready(function() {
	var i = 0;
	var timeOuts= new Array();  
	$(".tooltipp").mouseover(function(e) {
		e.preventDefault();
	});
	
	$(".tooltipp").mouseenter(function(e) {
		tooltippId = $(this).attr('tooltipp');
		if(typeof tooltippId == 'undefined'){
			i++;
			tooltippId = i;
			$(this).attr('tooltipp', tooltippId);
			
			var element = $(this);
			var offset = element.offset();
			var attrTitel = $(this).attr('title');
			
			
			/*cookie auslesen*/
			var md5 = $().crypt({method:"md5",source:attrTitel});
			
			var cookiewert = getCookie(md5);
			/*ende*/
			
			if("tooltipp" != cookiewert){
                            	$('body').append('<div id="tooltipp'+tooltippId+'" class="speechbubble" style="left:-100px; top: -100px"></div>');
                        	$('div#tooltipp'+tooltippId).html('<img id="tooltipp'+tooltippId+'" class="closeTooltipp" src="http://www.ahgz.de/images/icon_closetooltip.png" style="cursor: pointer; float: right;margin: -10px -5px 0 0;position: relative;" /><p>'+attrTitel+'</p><img src="http://www.ahgz.de/images/icon_tooltiparrow.png" style="left:30px;position:relative;top:9px;" /></div>');


				$(this).attr('title','');
				
				var heightTooltipp = $('div#tooltipp'+tooltippId).height();
		
				var posTop = offset.top - heightTooltipp - 25;
				var posLeft = offset.left - 50;
				
				
				$('div#tooltipp'+tooltippId).css("top", posTop);
				$('div#tooltipp'+tooltippId).css("left", posLeft);
			}
			
			if ($(this).hasClass('showdaily')) {
				setCookie("tooltipphash"+tooltippId, md5, 1); // Morgen wieder anzeigen
				$('div#tooltipp'+tooltippId).addClass('showdaily');
			}
			
			$('div#tooltipp'+tooltippId +' img.closeTooltipp').click(function() {
				$('div#tooltipp'+tooltippId).fadeOut();
				$('div#tooltipp'+tooltippId).remove();
				if (!$(this).hasClass('showdaily')) {
					setCookie(md5, "tooltipp", 365); // 1 Jahr lang nicht anzeigen
				}
			});
			
		}
                
                $('div#tooltipp'+tooltippId).show();
	});
	
	$(".tooltipp.showonstart").mouseenter();

	$(".tooltipp").mousemove(function() {
		tooltippId = $(this).attr('tooltipp');
		
		tooltipptime = $(this).attr('class');
		var Ausdruck = / (\d+) /;
		tooltipptime = Ausdruck.exec(tooltipptime);
		
		if(tooltipptime == null){
			tooltipptime = 2000;
		}else{
			tooltipptime = tooltipptime[1] * 1000;
		}
		clearTimeout(timeOuts[tooltippId]);  
		timeOuts[tooltippId] = window.setTimeout("$('div#tooltipp'+tooltippId).fadeOut();", tooltipptime);
	});
});
