/*
 * ============================================================================
 * 
 * Copyright (c) Zennaware GmbH, 2010
 * All Rights Reserved.
 * 
 * ============================================================================
 */

/*
 * ============================================================================
 * 
 * Hides the element in the style of a popover for the target element
 *  
 * ============================================================================
 */
 
(function($) {
	$.fn.hidePopover = function(settings){
	
		settings = $.extend({
		
			duration:100,
			side:'bottom',
			startOffset:30,
			
		}, settings);
					
		var obj = $(this);
		var pos = obj.position();
		
		obj.stop(false, true);
		
		if(settings.side == 'bottom')
			end = pos.top + settings.startOffset;
		else if(settings.side == 'top')
			end = pos.top - settings.startOffset;
		
		obj.animate({'opacity':0.0, 'top':end}, settings.duration, function(){
		
			obj.css('display', 'none');
		
		});
	}			
})(jQuery);

/*
 * ============================================================================
 * 
 * Displays the element in the style of a popover for the target element
 *  
 * ============================================================================
 */
 
(function($) {
	$.fn.showPopover = function(settings){
	
		settings = $.extend({
		
			target:null,
			duration:100,
			position:'middle',
			side:'bottom',
			cornerOffset:20,
			startOffset:30,
			endOffset:0,
			
			
		}, settings);
		
		if(settings.target == null)
			return;
		
		var obj = $(this);	
		var width = obj.width();
		var targetWidth = settings.target.width();
		var targetHeight = settings.target.height();
		var targetPos = settings.target.offset();
		
		obj.stop(false, true);
		
		obj.css({'opacity':0, 'display':'block'});
		
		var start = 0;
		var left = 0;
		
		if(settings.side == 'bottom')
			start = targetPos.top + targetHeight + settings.startOffset;
		else if(settings.side == 'top')
			start = targetPos.top - obj.height() - settings.startOffset;
			
		if(settings.position == 'middle')
			left = targetPos.left + targetWidth / 2 - width / 2;
		else if(settings.position == 'left')
			left = targetPos.left - settings.cornerOffset;
		else if(settings.position == 'right')
			left = targetPos.left - width + targetWidth + settings.cornerOffset;
				
		obj.offset({'top':start, 'left':left});
		
		var end = 0;	
		var pos = obj.position();
		
		if(settings.side == 'bottom')
			end = pos.top - settings.startOffset + settings.endOffset;
		else if(settings.side == 'top')
			end = pos.top + settings.startOffset - settings.endOffset;
		
		obj.animate({'opacity':1.0, 'top':end}, settings.duration);
	};

})(jQuery);

/*
 * ============================================================================
 * 
 * Toggles the visibility of the element by adjusting
 * the opacity from 0 to 1 and vice-versa.
 *  
 * ============================================================================
 */	
 
(function($) {
	$.fn.toggleVisibility = function(settings){
	
		settings = $.extend({
		
			duration:250,
			
		}, settings);
		
		var obj = $(this);
		
		//Force the current animation to complete to ensure we
		//have a valid end state to compare against
		
		obj.stop(false, true);
 		
 		if(obj.css('opacity') == 1)
 			obj.animate({'opacity': 0}, 150);
 		else
 			obj.animate({'opacity': 1}, 150);
	};

})(jQuery);

/*
 * ============================================================================
 * 
 * Scales and reduces the opacity of the element resulting in a
 * radar-style blip animation. The original dimenstions of the div
 * are restored after completion.The operation is performed directly
 * on the div and thus could result in layout changes and clipping.
 *  
 * ============================================================================
 */
 
(function($) {
	$.fn.blip = function(settings, callback){
	
		settings = $.extend({
		
			percentage:3,
			blipDuration:150,
			returnDuration:150,
			startOpacity:1,
			endOpacity:0
			
		}, settings);
		
		var obj = $(this);
		var data = obj.data('blip-data');
		
		if(data == null)
		{
			data = {
				'width':parseInt(obj.css('width')),
				'height':parseInt(obj.css('height')),
				'top':parseInt(obj.css('top')),
				'left':parseInt(obj.css('left'))
			};
			
			obj.data('blip-data', data);	
		}
			
		var v = (settings.percentage * data.height) / 2;
		var h = (settings.percentage * data.width) / 2; 
	
		obj.stop(false, true)
		obj.animate({
			'width':data.width + (h * 2),
			'height':data.height + (v * 2),
			'left':data.left - h,
			'top':data.top - v,
			 opacity:settings.endOpacity
			},
			settings.blipDuration,
			function(){
			
				obj.css({
					'width':data.width,
					'height':data.height,
					'top':data.top,
					'left':data.left
					});
				
				obj.animate({'opacity': settings.startOpacity}, settings.returnDuration, function(){
				
					callback();
				
				});
			
			});
	};
	
})(jQuery);

/*
 * ============================================================================
 * 
 * Returns the current bounding rectangle  of an element relative
 * to the document. The object contains the properties left, top,
 * right, bottom 
 *  
 * ============================================================================
 */
 
(function($){
  
  $.fn.offsetRect = function(){
  
  	var ele = $(this);  
  
  	var left = ele.offset().left;
		var top = ele.offset().top;
		var right = left  + ele.width();
		var bottom = top + ele.height();

		return {left:left, top:top, right:right, bottom:bottom};
  };  
  
})(jQuery);

/*
 * ============================================================================
 * 
 * Returns the intersecting rectangle of the two elements relative
 * to the document. The object contains the properties left, top,
 * right, bottom
 *
 * ============================================================================
 */
(function($){
  
  $.fn.offsetRectIntersection = function(o){
  
  	var e = $(this);
  	var r1 = e.offsetRect();
  	var r2 = o.offsetRect();
  	
		var left = Math.max(r1.left, r2.left);
		var top = Math.max(r1.top, r2.top);
		var right = Math.min(r1.right, r2.right);
		var bottom = Math.min(r1.bottom, r2.bottom);
		
		return {left:left, top:top, right:right, bottom:bottom};
		
  };  
})(jQuery);
