/* 
* News Li Scroll (1.3)
* by Stuart Gregg (www.civicasoft.com)
* sgregg at civicasoft dot com
*
* Copyright (c) 2011 Stuart Gregg (www.civicasoft.com)
*
* NOTE: This script requires jQuery
* Download jQuery @ www.jquery.com
* v 1.0   - Initial release
*/

// news li scroll plugin definition
(function ($) {
	$.fn.latestheadlines = function (options) {

		// default settings
		var defaults = {
			next: '.next',
			prev: '.prev',
			duration: '400',
			controls: '.controls',
			links: '.links',
			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_controls = $('span' + defaults.controls, this),
						$s_links = $('div' + defaults.links, this),
						max = $('ul', $s_links).size() - 1 - defaults.max_modifier;

			//$s_controls.css('border', 'solid 1px red');
			// methods
			$.extend(self, {

				// remove style at position i
				removeStyles: function (i) {

					$('a.link:eq(' + i + ')', $s_controls).removeClass('active');
					$('a.link:eq(' + i + ')', $s_controls).addClass('num');
					$('ul:eq(' + i + ')', $s_links).addClass('hide');

				},

				// add styles at position i
				addStyles: function (i) {
					//alert(i);
					$('a.link:eq(' + i + ')', $s_controls).removeClass('num');
					$('a.link:eq(' + i + ')', $s_controls).addClass('active');
					$('ul:eq(' + i + ')', $s_links).removeClass('hide');

				},

				// add control links
				addControls: function () {
					//alert('max: ' + max);

					//$($s_links).css("border", "solid 1px red;");
					var html = '<span>Page: </span>'
					for (i = 0; i <= max; i++) {
						var t = i + 1;
						html += '<a href="javascript: void(0);" class="link num" rel="' + i + '">' + t +'</a>';
					}
					$s_controls.html(html);

				}


			});

			// intialize - remove styles 
			self.addControls();
			// intialize - remove styles 
			self.addStyles(pos);

			// set up the links to scroll to each li			
			$(defaults.controls + ' 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 });
					});
				}
			});


		});
	}
})(jQuery);

