/*
 *
 * Based on newsTicker by Sam Collett:
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
 
(function($) {
/*
 * A basic news pager.
 *
 * @name     newspager
 * @param    delay      Delay (in milliseconds) between iterations. Default 4 seconds (4000ms)
 * @author   Sam Collett (http://www.texotela.co.uk), modified by Anton Andreasson, 2010
 * @example  $("#news").newspager();
 *
 */
$.fn.newspager = function(delay) {
	delay = delay || 4000;
	initPager = function(el) {
		stopPager(el);
		origWidth = $(el).width();
		el.items = $(".m", el);
		$(el).wrapInner('<div class="pager-wrap"/>').children().css('width', function() {
			return el.items.width() * el.items.size();
		}).parent().after('<div class="pager-paging"/>');
		$.each(el.items, function(i) {
			$('<span>•</span>').click(function() {
				$(el.items[i]).slideMe();
				updatePagePaging(i);
			}).hover(function() {
				pausePager(el);
			},function(){
				resumePager(el);
			}).appendTo('.pager-paging');
		}).width(origWidth);
		$(window).load(function(){
			$('.pager-paging').css({
				'position': 'absolute',
				'top': $(el).height() + $(el).position().top,
				'left': $(el).position().left,
				'width': origWidth
			});
		});
		// current item
		el.currentitem = 0;
		startPager(el);
	};
	startPager = function(el) {
		el.Pagefn = setInterval(function() { doPage(el); }, delay);
		updatePagePaging(el.currentitem);
	};
	stopPager = function(el) {
		clearInterval(el.Pagefn);
	};
	pausePager = function(el) {
		el.pause = true;
	};
	resumePager = function(el) {
		el.pause = false;
	};
	// slideMe function
	$.fn.extend( {
		slideMe: function() {
			return this.each(function() {
				var left = this.offsetLeft - this.parentNode.offsetLeft;
				$(this).closest('.pager').animate( {scrollLeft: left + 'px'}, 500, 'linear');
				// revert to original image (see jquery.lazyload)
				if ($('img[original]', this).size()) {
					$('img[original]').attr("src", function() {
						return $(this).attr("original");
					}).removeAttr('original');
				}
			});
		}
	});
	doPage = function(el) {
		// don't run if paused
		if(el.pause) return;
		// pause until animation has finished
		el.pause = true;
		// move to next item and center
		el.currentitem = ++el.currentitem % (el.items.size());
		$(el.items[el.currentitem]).slideMe();
		updatePagePaging(el.currentitem);
		el.pause = false;
	};
	updatePagePaging = function(i) {
		$('.pager-paging span').removeClass('sel').filter(':eq('+i+')').addClass('sel');
	};
	this.each(
		function() {
			initPager(this);
		}
	)
	// Add ARIA attributes
	.attr("aria-live", "polite") // Make the newsPager a live region
	.hover(
		function() {
			// pause if hovered over
			pausePager(this);
		},
		function() {
			// resume when not hovered over
			resumePager(this);
		}
	);
	return this;
};

})(jQuery);

$(document).ready(function() {
	/* Initialize the news Pager */
	$(".pager").newspager(5000);
});
