/* 
* 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
* v 1.1   - Added max_modifier [09/07/2011]
* v 1.2		- bug fixes for no scroll and uses the defaults more... [10/28/2011]
*/

// 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',
            max_modifier: '0'
        };

        // 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 - defaults.max_modifier;

            // 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) {
                        $(defaults.prev, $header).removeClass('disabled');
                    }
                    else if (i == max) {
                        $(defaults.next, $header).removeClass('disabled');
                    }

                },

                // add styles at position i
                addStyles: function (i) {
                    //alert(i);
                    $('a.link:eq(' + i + ')', $header).removeClass('num');
                    $('a.link:eq(' + i + ')', $header).addClass('active');

                    if (i == 0) {
                        $(defaults.prev, $header).addClass('disabled');
                    }
                    if (i == max) {
                        $(defaults.next, $header).addClass('disabled');
                    }

                }

            });

            // intialize - remove styles 
            self.addStyles(pos);

            // set up the links to scroll to each li			
            $(defaults.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 });
                    });
                }
            });

            $(defaults.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 });
                }
            });

            $(defaults.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);

