/** SCROLL TO PLUGIN **/

/**
 * 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/24/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.1.1
 *
 * @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 ('44', '100px', '+=90', 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} onafter Function to be called after the scrolling ends.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * @example jQuery('div').scrollTo( 340 );
 *
 * @example jQuery('div').scrollTo( '+=340px', { horizontal:true } );
 *
 * @example jQuery('div').scrollTo( 'p.paragraph:eq(2)', { speed:500, easing:'swing' } );
 *
 * @example var second_child = document.getElementById('container').firstChild.nextSibling;
 *			jQuery('#container').scrollTo( second_child, { speed:500, onafter: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.
 *  - The plugin no longer requires Dimensions, and after a hacky check, they should coexist peacefully.
 **/


		  
	jQuery.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
			parts;
			
		return this.each(function(){
			switch( typeof target ){
				case 'string':
					if( parts = /^([+-])?(=)?\d+(px)?jQuery/.exec(target) ){
						if( !settings.speed && (parts[1]||parts[2]||parts[3]) )//advanced animation requested
							settings.speed = 1;
						break;//skip this one.
					}
					target = jQuery(target,this);// relative selector, no break!
				case 'object'://a DOM element
					target = jQuery(target).offset()[pos]; //+ this[key];//get the real position of the element
					if( !jQuery(this).is('html,body') ) //if not checked, will fail if dimensions is included, grrr!
						 target -= jQuery(this).offset()[pos]
			}
			
			
			if( settings.speed ){//animation is required
				attr[key] = target;
				jQuery(this).animate( attr, settings.speed, settings.easing, settings.onafter );
			}else{//if no speed was given, just alter the scroll value
				this[key] = target;
				if( settings.onafter )
					settings.onafter.call(this);
			}
		});
	};
	
	jQuery.scrollTo = function( target, settings ){
		return jQuery('html,body').scrollTo( target, settings );
	};