/**
 * jQuery.ScrollTo
 * Copyright (c) 2007 Ariel Flesler - aflesler(at)gmail(dot)com
 * Licensed under GPL license (http://www.opensource.org/licenses/gpl-license.php).
 * Date: 10/29/2007
 *
 * @projectDescription Easy element scrolling using jQuery.
 * Compatible with jQuery 1.2.1, tested on Firefox 2.0.0.7, and IE 6, both on Windows.
 *
 * @author Ariel Flesler
 * @version 1.2.3
 *
 * @id jQuery.fn.scrollTo
 * @param {String|Number|DOMElement|jQuery} target Where to scroll the matched elements.
 *	  The different options for target are:
 *		- A number position.
 *		- A string position ('44', '100px', '+=90', etc )
 *		- A jQuery/DOM element ( logically, child of the element to scroll )
 *		- A string selector, that will be relative to the element to scroll ( 'li:eq(2)', etc )
 * @param {Object} settings Hash of settings, optional.
 *	 @option {String} axis Which axis must be scrolled, use 'x', 'y', 'xy' or 'yx'.
 *	 @option {Number} speed The OVERALL length of the animation.
 *	 @option {String} easing The easing method for the animation.
 *	 @option {Boolean} margin If true, the margin of the target element will be deducted from the final position.
 *	 @option {Boolean} queue If true, and both axis are given, the 2nd axis will only be animated after the first one ends.
 *	 @option {Function} onAfter Function to be called after the scrolling ends. 
 *	 @option {Function} onAfterFirst If queuing is activated, this function will be called after the first scrolling ends.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * @example $('div').scrollTo( 340 );
 *
 * @example $('div').scrollTo( '+=340px', { axis:'y' } );
 *
 * @example $('div').scrollTo( 'p.paragraph:eq(2)', { speed:500, easing:'swing', queue:true, axis:'xy' } );
 *
 * @example var second_child = document.getElementById('container').firstChild.nextSibling;
 *			$('#container').scrollTo( second_child, { speed:500, axis:'x', onAfter:function(){
 *				alert('scrolled!!');																   
 *			}});
 *
 * Notes:
 *  - jQuery.scrollTo will make the whole window scroll, it accepts the same parameters as jQuery.fn.scrollTo.
 *  - The plugin no longer requires Dimensions.
 *	- If you are interested in animated "same-page-scrolling" using anchors, check http://jquery.com/plugins/project/LocalScroll.
 *	- The option 'margin' won't be valid, if the target is an absolute position.
 *	- The option 'queue' won't be taken into account, if only 1 axis is given.
 **/
(function( $ ){
		  
	$.scrollTo = function( target, settings ){
		return $('html,body').scrollTo( target, settings );
	};
	
	$.scrollTo.defaults = {
		axis:'y',
		speed:1
	};
	
	$.fn.scrollTo = function( target, settings ){
		settings = $.extend( {}, $.scrollTo.defaults, settings );
		if( settings.axis.length != 2 )
			settings.queue = false;//make sure the settings are given right
		if( settings.queue )
			settings.speed = Math.ceil( settings.speed / 2 );//let's keep the overall speed, the same.
			
		return this.each(function(){
			var $elem = $(this), t = target, toff, eoff, attr = {};
			switch( typeof t ){
				case 'string':
					if( /^([+-]=)?\d+(px)?$/.test(t) )
						break;//skip this one.
					t = $(t,this);// relative selector, no break!
				case 'object'://a DOM element
					toff = $(t).offset();//get the real position of the target 
			}
			$.each( settings.axis.split(''), parse );			
			animate( settings.onAfter );			
			
			function parse( i, axis ){
				var Pos = axis == 'x' ? 'Left' : 'Top',
					pos = Pos.toLowerCase(),
					key  = 'scroll' + Pos;
					
				attr[key] = toff ? toff[pos] + ( $elem.is('html,body') ? 0 : $elem[0][key] - $elem.offset()[pos] ) : t;
				
				if( settings.margin && typeof t == 'object' )//if it's a dom element,reduce the margin
					attr[key] -= parseInt($(t).css('margin'+Pos)) || 0;
				
				if( !i && settings.queue ){//queueing each axis is required					
					if( $elem[0][key] != attr[key] )//don't waste time animating, if there's no need.
						animate( settings.onAfterFirst );//intermediate animation
					delete attr[key];//don't animate this axis again in the next iteration.
				}
			};
			function animate( callback ){
				$elem.animate( attr, settings.speed, settings.easing, function(){
					if( callback )
						callback.call(this, $elem, attr, t );
				});
			};
		});
	};

})( jQuery );