// define global needed vars
var tooltip_timer;

/**
 * this function resizes a given object according to the given min-width and max-width
 */
jQuery.fn.resize = function( minWidth , maxWidth ) {
	
	this.each(function() {
		$this = $(this);
		var oldWidth = $this.width();		
		
		var newWidth;
		if(  oldWidth < minWidth ) {
			newWidth = minWidth;
		} else if ( oldWidth > maxWidth ) {
			newWidth = maxWidth;
		} else {
			newWidth = oldWidth;
		}
		$this.width(newWidth);
	});	

	// Don't break the chain
	return this;
}

jQuery.fn.tooltip = function( html , options ) {
		
	var $tooltip;	
	
	options = options || {};
	
	options.tooltipClass = options.tooltipClass || 'tooltip';
	// string with position or offset-coords
	options.pos = options.position || [ 10 , 20 ];
	options.time = options.time || 500 ;
	options.fadeInTime = options.fadeInTime || 500;
	options.fadeOutTime = options.fadeOutTime || 500;
	options.positionOffset = options.positionOffset || 10;

	
	this.each(function() {
		$me = $(this);
			
		$me.mouseover( function(e) {
			clearTimeout( tooltip_timer );
			createTooltip( e , html );
		});
		
		$me.mouseout( function(e) {
			tooltip_timer = setTimeout(function() {
				if( typeof $tooltip == 'undefined' ) return;
				$tooltip.fadeOut(options.fadeOutTime ,function() {
					$tooltip.remove();
				});
			}, options.time );		
		});		
	});	
	
	function createTooltip(e , html) {	
		
		if( typeof $tooltip == 'undefined' ) {			
			$("body").append("<div id='"+options.tooltipClass+"'>"+ html+ "</div>");
			$tooltip = $('#'+ options.tooltipClass );					
		}	

		if( $tooltip.length != 0 ) {			
			$('div#'+options.tooltipClass ).remove();			
		}				
		$("body").append("<div id='"+options.tooltipClass+"'>"+ html+ "</div>");
		$tooltip = $('#'+ options.tooltipClass );
		
		if (typeof options.pos == 'object' ) {
// 			console.log('xPos: '+e.pageX);
//			console.log('yPos: '+e.pageY);
			$tooltip.setPosition(  e.pageY - options.pos[0] , e.pageX + options.pos[1] );
//			console.log('xPos (tool): '+ (e.pageX + options.pos[1]) );
//			console.log('yPos (tool): '+ (e.pageY - options.pos[0]) );			
		} else {			
			var obj = e.target;
			var pos = getAbsPosition( obj );
			switch( options.pos ) {
			case "top":				
				$tooltip.setPosition(  pos.y - $tooltip.height() - options.positionOffset,  pos.x );				
				break;
			case "bottom":
				$tooltip.setPosition(  pos.y + obj.offsetHeight + options.positionOffset,  pos.x );				
				break;
			case "left":
				$tooltip.setPosition(  pos.y ,  pos.x - $tooltip.width() - options.positionOffset );
				break;
			case "right":				
				$tooltip.setPosition(  pos.y ,  pos.x + obj.offsetWidth  + options.positionOffset );
				break;
			default: 
			
			}				
		}
		$tooltip
			.fadeIn(options.fadeInTime)				
			.mouseover( function() {				
				clearTimeout( tooltip_timer );	
			})				
			.mouseout(function ( ) {
				tooltip_timer = setTimeout(function() {
					$tooltip.fadeOut(options.fadeOutTime ,function() {
						$tooltip.remove();
					});
				}, options.time );
			});	
	}	
	return this;
}

jQuery.fn.setPosition = function( top, left) {
	
	this.each( function() {
		$me = $(this);
		$me.css('top', top)
			.css('left', left);

	});		
	return this;
}

jQuery.fn.messureMouseover = function( link, text  ) {
	
	var timeIn;
	
	this.each(function() {
		$me = $(this);
		
		$me.mouseover( function(e) {			
			timeIn = new Date();
		});		

		$me.mouseout( function(e) {
			var timeOut = new Date();
			if( typeof timeIn == 'undefined') return;
			timeOver = parseInt( timeOut.getTime() - timeIn.getTime() );
			// the 500 is the fadeIn time, after 500ms, the tooltip ist completely shown
			if( timeOver >= 500 ) submit( link, text, timeOver);
		});		
	});
	
	function submit(link, text, timeOver) {
		var text = URLEncode( text );		
		var link = URLEncode( link );
		sajax_do_call('woogleAjaxRedLinkVisit', [link, text, timeOver], function(){return true;});	
	}
}


/**
 * @param obj	a dom-object
 * @return the absolut x/y-positions of the obj.
 */
function getAbsPosition( obj ) {
	var curleft = obj.offsetLeft || 0;
	var curtop = obj.offsetTop || 0;
	while (obj = obj.offsetParent) {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
	}
	return {y:curtop,x:curleft};	
}

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

function cutSymbols( string, symbols) {
	for( var i = 0; i < symbols.length; i++) {		
		string = str_replace( symbols[i], '', string );
		//console.log( symbols[i] );
	}
	// console.log( string );
	return string;
}

