/**
 * 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/05/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
 *
 * @id jQuery.fn.scrollTo
 * @param {String|Number|DOMElement} target The target position for the scrolling.
 * @param {Object} settings ( none required )
 *	 @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 ended.
 *
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * 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;
			}
		});
	};
	
	$.scrollTo = function( target, settings ){
		return $('html,body').scrollTo( target, settings );
	};

})( jQuery );
