jQuery(document).ready(function(){
	jQuery('a.jTip').JT({delay: 200});
});

jQuery.fn.JT = function(options){
	var defaults = {
		delay: 300
	};
	
	var options = jQuery.extend(defaults, options);
	
	makeTipHTML = function(){
		var TipHTML = 
			"<div id='JT'>"+
				"<div id='JT_title'></div>"+
				"<div id='JT_copy'>"+
					"<div class='JT_loader'></div>"+
				"</div>"+
			"</div>";
		return TipHTML;
	}
	jQuery("body").prepend(makeTipHTML()); 
	
	jQuery(this).each(function(){
		var $this = jQuery(this);
		
		var tip = jQuery('#JT');
		var tTitle = this.title;
		this.name='';
		

		var offset = $this.offset();
		var tLeft = offset.left;
		
		
		/* Mouse over and out functions*/
		$this.hover(
			function() {	
				var url = $this.attr('href');
				var linkId = $this.attr('id');
				$this.attr('title','');
				
				jQuery('#JT_title').html(tTitle);
				setTip(tLeft,linkId);
				
				ajaxCall(url,linkId);
				setTimer();
			}, 
			function() {
				stopTimer();
				tip.hide();
				
			}
		).click(function(){
			var splitedURL = $this.attr('href').split('?');
			window.location.href = urlpath+lang+"/"+splitedURL[1];	
			return false;
		});
		
		
		ajaxCall = function(url,linkId){
			jQuery('#JT_copy').load(url);
			
			var queryString = url.replace(/^[^\?]+\??/,'');
			var params = parseQuery( queryString );
			
			if(params['link'] !== undefined){
				jQuery('#' + linkId).bind('click',function(){window.location = params['link']});
				jQuery('#' + linkId).css('cursor','pointer');
			}
		}
		
		
		setTimer = function() {
			$this.showTipTimer = setInterval("showTip()", defaults.delay);
		}
		
		stopTimer = function() {
			clearInterval($this.showTipTimer);
		}

		setTip = function (left,ids){
			stopTimer();
			
			var xTip = (left-220)+"px";  
			var yTip = (getAbsoluteTop(ids)+35)+"px";  
			tip.css({'top' : yTip, 'left' : xTip,'width':225});
		} 
		
		showTip = function(){
			stopTimer();
			tip.slideDown();
			
		}

		parseQuery = function ( 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;
		}
	});
	
	getAbsoluteTop = function(objectId) {
		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
	}


};
