/* 
 * News Li Scroll (1.3)
 * by Stuart Gregg (www.civicasoft.com)
 * sgregg at civicasoft dot com
 *
 * Copyright (c) 2010 Stuart Gregg (www.civicasoft.com)
 *
 * NOTE: This script requires jQuery and scrollTo to work.  
 * Download jQuery @ www.jquery.com
 * Download scrollTo @ http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 * v 1.0   - Initial release
 *
 */

// news li scroll plugin definition
(function( $ ){
	$.fn.newsliscroll = function( options ){

		// default settings
		var defaults = {
			next					: '.next',
			prev					: '.prev',
			duration			: '400',
			scroll_bucket	: '.scroll-bucket',
			scroll_wrap		: '.scroll-wrap',
			header				: '.header'
		};
		
		// If options exist, lets merge them
		// with our default settings
		if ( options ) { 
			$.extend( defaults, options );
		}
		
		return this.each( function() {
			var self = this,
				$this = $(this),
				pos = 0,
				$s_bucket = $('div' + defaults.scroll_bucket,this),
				$s_wrap = $('div' + defaults.scroll_wrap,this),
				$header = $(defaults.header,this),
				max = $('li', $s_bucket).size()-1;
			
			//alert( max );
			
			// methods
			$.extend(self, {

				// remove style at position i
				removeStyles: function(i) {
					
					$('a.link:eq(' + i + ')',$header).removeClass('active');
					$('a.link:eq(' + i + ')',$header).addClass('num');

					if(i==0)
					{
						$('a.prev',$header).removeClass('disabled');
					}
					else if(i==max)
					{
						$('a.next',$header).removeClass('disabled');
					}

				},

				// add styles at position i
				addStyles: function(i) {
				
					$('a.link:eq(' + i + ')',$header).removeClass('num');
					$('a.link:eq(' + i + ')',$header).addClass('active');
					
					if(i==0)
					{
						$('a.prev',$header).addClass('disabled');
					}
					else if(i==max)
					{
						$('a.next',$header).addClass('disabled');
					}
					
				}
				
			});

			// intialize - remove styles 
			self.addStyles(pos);

			// set up the links to scroll to each li			
			$('.header a.link', this).each(function(index){
				if($(this).is('a[rel]')) {
					$(this).click( function(){
						// set the styles 
						self.removeStyles(pos);
						pos = $(this).attr('rel');
						self.addStyles(pos);
						// start scroll
						$s_wrap.scrollTo( $('li:eq(' + pos + ')', $s_bucket), {duration:defaults.duration} );
					});
				}
			});			

			$('.header a' + defaults.prev, this).click( function(){
				
				if( pos > 0 )
				{
					// set the styles 
					self.removeStyles(pos);
					self.addStyles(--pos);
					// start scroll
					$s_wrap.scrollTo( $('li:eq(' + pos + ')', $s_bucket), {duration:defaults.duration} );
				}
			});

			$('.header a' + defaults.next, this).click( function(){
				
				if( pos < max)
				{
					// set the styles 
					self.removeStyles(pos);
					self.addStyles(++pos);
					// start scroll
					$s_wrap.scrollTo( $('li:eq(' + pos + ')', $s_bucket), {duration:defaults.duration} );
				}
			});
		
		});
	}
})(jQuery);

