/*
 * FadeScroll - jQuery plugin
 * written by eighty4	
 * http://broken8.se/fadescroll
 *
 * Dual licensed under the MIT 
 * and GPL licenses.
 *
 *
 * Usage:
 *
 * Initialize the script.
 ***********************************
   
 $(document).ready(function() {
 	$( 'header' ).fadeScroll({});
 });
 
 
 *
 * No idea if it works on any browser except FF3.5 on OS X 10.6 
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

(function($) {

	$.fn.fadeScroll = function(options){
	  
		// default configuration properties
		var defaults = {			
			begin:			100,
			full:			200,
			move:			false,
			height:			200,
			opacity:		0, /* By standard we want the element to be completly hidden */
			bottom:			false,
			that:			false,
			
			hide:			true,
			minheight:		null, /* If not set by input this will be based on default css */
			diff:			null, /* Just to keep track of the difference between begin and full */
			nochange:		false  /* By standard we want to run doit when initializing. */
		}; 
		
/*
		if( $.browser.msie && $.browser.version.substring(0,1) < 8 ) {
			return false;
		}
*/

		function doit()
		{
			// Up or down?
			if( options.bottom ) {
				currentScroll = $( '#wrapper' ).innerHeight() - $(window).height() - $( window ).scrollTop();
			} else {
				currentScroll = $( window ).scrollTop();
			}
			
			if( currentScroll < options.begin ) {
				options.nochange = false;
				$( options.that ).css( 'display', 'block' );	
			}
			
			if( !options.nochange ) {
				currentScroll = currentScroll - options.full;
				if( currentScroll < 0 ) currentScroll = 0;
				opacity = (options.diff - currentScroll) / options.diff;
				
				if( opacity <= 0 ) {
					options.nochange = true;
				}
				
				if( options.move ) {
					if( opacity < 0 ) opacity = 0;
					height = opacity * options.height;
					if( height < options.minheight ) height = options.minheight;
					$( options.that ).height( height );
				}			
				
				
				if( opacity < options.opacity ) opacity = options.opacity;
				$( options.that ).css( 'opacity', opacity );
				if( options.hide && opacity <= 0 ) $( options.that ).css( 'display', 'none' );

			}
		}
		
		var options = $.extend( defaults, options );  

		

		options.that = this;
		options.diff = options.begin - options.full;


		if( options.minheight == null ) {
			options.minheight = $( this ).height();
		}
		doit();


		$( window ).scroll( function(){	
			doit();
		});

}})(jQuery);
