/*
 *
 * 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)
 * @param    prevnext      Displays Next and Previous arrows. Set to true or false. Default false
 * @param    paging        Displays a paging with numbers. Set to true or false. Default true
 * @param    pagingevent   Event to trigger the paging. Set to e.g. "click" or "mouseover". Default "click"
 * @param    remotepaging  Other element to trigger paging (no of items must match the no of slides). Requires paging set to true. Default null
 * @param    effect        Transition effect between slides. Set to "slide", "fadeslide", "fadeflash" or "dissolve". Default "dissolve"
 * @author   Sam Collett (http://www.texotela.co.uk), partly rewritten and extended by Anton Andreasson, 2010
 * @example  $("#news").newspager();
 *
 */
$.fn.newspager = function(options) {
	options = $.extend({
		delay: 1000,
		prevnext: false,
		paging: true,
		pagingevent: "click",
		remotepaging: null,
		effect: "dissolve"
	}, options || {});
	initPager = function(el) {
		elm = $(el);
		stopPager(el);
		origWidth = elm.width();
		slides = elm.children(".m");
		if (options.effect == "slide") {
			elm.wrapInner('<div class="pager-wrap"/>').children().css('width', function() {
				return origWidth * slides.size();
			});
		} else if ((options.effect == "fadeslide") || (options.effect == "fade")) {
			elm.css('height', function(){
				return slides.height();
			});
			slides.css({
				'position': 'absolute',
				'top': function(){ return $(this).parent().css('padding-top'); },
				'left': function(){ return $(this).parent().css('padding-left'); }
			});
		} else {
			elm.css('height', function(){
				return slides.height() - 5;
			});
		}
		slides.css('width', origWidth);
		elmWrap = elm.parent().css('position','relative');

		// current item
		el.currentitem = el.currentitem ? el.currentitem : 0;
		
		if(options.prevnext) {
			$('<a class="page-prev" href="#"/><a class="page-next" href="#"/>').css({
				'top': function(){
					return (elm.height() / 2) - 22 /* <-- TODO: calculate prevnext item height here */;
				},
				'position': 'absolute'
			}).hover(function() {
				pausePager(el);
			},function(){
				resumePager(el);
			}).click(function(){
				resumePager(el);
				if($(this).is('.page-prev')) {
					doPage(el, "prev");
				} else {
					doPage(el);
				}
				pausePager(el);
				return false;
			}).appendTo(elmWrap);
		}

		if(options.paging) {
			if (!options.remotepaging) {
				$('<div class="pager-paging"/>').css({
					'position': 'absolute',
					'top': elm.height() + 20 /* offset of choice */,
					'left': 0 /* offset of choice */,
					'z-index': '1'
				}).appendTo(elmWrap);
			}
			slides.each(function(i) {
				if (options.remotepaging) {
					var page = $(options.remotepaging[i]);
				} else {
					var page = $('<span>'+(i+1)+'</span>');
				}
				page.bind(options.pagingevent, function() {
					$(slides[i]).animateSlide(options.effect);
					updatePagePaging(i);
					el.currentitem = i;
					if (options.pagingevent == "click") {
						return false;
					}
				}).hover(function() {
					pausePager(el);
				},function(){
					resumePager(el);
				});
				if (!options.remotepaging) {
					page.appendTo('.pager-paging');
				}
			});
		}

		startPager(el);
	};
	startPager = function(el) {
		el.Pagefn = setInterval(function() { doPage(el); }, options.delay);
		if(options.paging) {
			updatePagePaging(el.currentitem);
		}
	};
	stopPager = function(el) {
		clearInterval(el.Pagefn);
	};
	pausePager = function(el) {
		el.pause = true;
	};
	resumePager = function(el) {
		el.pause = false;
	};
	// animateSlide function
	$.fn.extend( {
		animateSlide: function() {
			if (options.effect == "slide") {
				return this.each(function() {
					var left = this.offsetLeft - this.parentNode.offsetLeft;
					elm.animate( {scrollLeft: left + 'px'}, 800, 'swing');
				});
			} else if (options.effect == "fadeslide") {
				return this.each(function() {
					$(this).siblings(':visible').hide(600);
					$(this).fadeIn(600);
				});
			} else if (options.effect == "fadeflash") {
				return this.each(function() {
					$(this).siblings(':visible').hide();
					$(this).fadeIn(600);
				});
			} else if (options.effect == "dissolve") {
				return this.each(function() {
					$(this).siblings(':visible').css({
						'position': 'absolute'
					}).fadeOut(600);
					$(this).css({
						'position': 'absolute'
					}).fadeIn(600);
				});
			}
		}
	});
	doPage = function(el, dir) {
		// don't run if paused
		if(el.pause) {
			return;
		} else {
			// pause until animation has finished
			el.pause = true;
			// move to next item and center
			if (dir == "prev") {
				el.currentitem = (slides.size() + (--el.currentitem % slides.size())) % slides.size();
			} else {
				el.currentitem = ++el.currentitem % slides.size();
			}
			$(slides[el.currentitem]).animateSlide(options.effect);
			if(options.paging) {
				updatePagePaging(el.currentitem);
			}
			el.pause = false;
		}
	};
	updatePagePaging = function(i) {
		if (options.remotepaging) {
			options.remotepaging.removeClass('sel').filter(':eq('+i+')').addClass('sel');
		} else {
			$('.pager-paging span').removeClass('sel').filter(':eq('+i+')').addClass('sel');
		}
	};
	this.each(
		function() {
			initPager(this);
		}
	)
	// Add ARIA attributes
	.attr("aria-live", "polite") // make the pager a live region
	.hover(
		function() {
			// pause if hovered over
			pausePager(this);
		},
		function() {
			// resume when not hovered over
			resumePager(this);
		}
	);
	return this;
};

})(jQuery);
