// VERSION: 1.0 LAST UPDATE: 2011-05-25
/*
 * THIS IS FREE SCRIPT BUT LEAVE THIS COMMENT IF
 * YOU WANT USE THIS CODE ON YOUR SITE
 * 
 * Made by Otis Richardson (Oz), ozone1979@gmail.com, Augusta, GA, 05.2011
 * http://progsbyoz.com
 * 
 */
/*
Description:
This plugin allows one to move elements to another, specified location
using relative positioning, so that position is maintained in the event of 
the window resizing.


Notices:
Include script after including main jQuery. Whole plugin uses jQuery
namespace and should be compatible with older version (unchecked). 

Usage:
jQuery(element).relPosTo({target: anotherElement})


Returns:
Element in new position.

Parameters:
{
	target:jQuery element
}
jQuery(imgElement).rotateAnimation

Examples:
	$('#sourceElement').relPosTo('#targetElement');

*/

(function ($) {
	
	var topInc = 0;
	var leftInc = 0;	
	/*	
	if($.client.browser == "Safari") {
		topInc = 0;
	} else {
		topInc = 0;
	}
	*/
	var newLeft; 	
	var newTop;	
	
	$.fn.relPosTo = function( options, callbackFnk ) {
		
			var settings = {
				'position' : 'relative'				
			};
			
		return this.each(function(i){
			$this = $(this);			
			
			if ( options ) {
				$.extend( settings, options );
			}		
			
			if (settings.target) {
				var oTop = $this.offset().top;
				var oLeft = $this.offset().left;
				var tTop = settings.target.offset().top;
				var tLeft = settings.target.offset().left;
				
				// set newTop
				if (oTop > tTop) {
					newTop = -(oTop - tTop);
				} else if (oTop < tTop) {
					newTop = tTop - oTop;
				} else {
					newTop = parseInt($this.css('top'));
				}
				
				// set newLeft
				if (oLeft > tLeft) {
					newLeft = -(oLeft - tLeft);
				} else if (oLeft < tLeft) {
					newLeft = tLeft - oLeft;
				} else {
					newLeft = parseInt($this.css('left'));
				}
				
				$this.css({
					'position' : settings.position,
					'top' : newTop,
					'left' : newLeft,
					'margin-bottom' : -($this.height())
				});
				
			} else {
				alert('hello');
			}		
			/*
			console.log(  '\n' +
				'Source Top:' + $this.offset().top + '\n' +
				'Target Top:' + tTop + '\n' +
				'New Top:' + newTop + '\n' +
				'Source Left' + $this.offset().left + '\n' +
				'Target Left' + tLeft + '\n' +
				'New Left:' + newLeft
			);
			*/
			if($.isFunction(callbackFnk)){
				callbackFnk.call();
			}
			
		});
		
		
	};
})(jQuery);


