/**
 * 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/16/2007
 *
 * @projectDescription Easy element scrolling using jQuery.
 * Compatible with jQuery 1.2, tested on Firefox 2.0.0.7, and IE 6, both on Windows.
 *
 * @author Ariel Flesler
 * @version 1.0.2
 *
 * @id jQuery.fn.scrollTo
 * @param {String|Number|DOMElement} target The target position for scrolling.
 *	  The different options for target are:
 *		- A number position.
 *		- A string position ('4em', '100px', '10%', etc )
 *		- A 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 {Boolean} horizontal Whether the container must be scrolled horizontally.
 *	 @option {Number} speed Length of the animation, passed to the animate function.
 *	 @option {String} easing Easing method, passed to the animate function.
 *	 @option {Function} callback Function to be called after the scrolling ends.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * @example $('div').scrollTo( 340 );
 *
 * @example $('div').scrollTo( '340px', { horizontal:true } );
 *
 * @example $('div').scrollTo( 'p.paragraph:eq(2)', { speed:500, easing:'swing' } );
 *
 * @example var second_child = document.getElementById('container').firstChild.nextSibling;
 *			$('#container').scrollTo( second_child, { speed:500, callback:function(){
 *				alert('scrolled!!');																   
 *			}});
 *
 * Notes:
 *	- The plugin was inspired by this post: http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12
 *  - jQuery.scrollTo will make the whole window scroll, it accepts the same parameters as jQuery.fn.scrollTo.
 *  - This plugin requires jQuery.Dimensions. Tested with Dimensions 1.1.2.
 **/
(function( $ ){
		  
	$.fn.scrollTo = function( target, settings ){
		settings = settings || {}; //no mandatory settings.
		var attr = {},
			pos = settings.horizontal ? 'left' : 'top',
			key = 'scroll' + pos.charAt(0).toUpperCase() + pos.substring(1);//scrollLeft || scrollTop
			
		return this.each(function(){
			switch( typeof target ){
				case 'string':
					if( /(\d|px|em|%)$/.test(target) )
						break;//skip this one.
					target = $(target,this);// relative selector, no break!
				case 'object'://a DOM element
					target = $(target).position()[pos];//get the real position of the element
			}
			
			if( settings.speed ){
				attr[key] = target;
				$(this).animate( attr, settings.speed, settings.easing, settings.callback );
			}else{//if no speed was given, just alter the scroll value
				this[key] = target;
				settings.callback && settings.callback.call(this);
			}
		});
	};
	
	$.scrollTo = function( target, settings ){
		return $('html,body').scrollTo( target, settings );
	};

})( jQuery );