/*

Quicklist JQuery Plugin
Written by Andrew Smith [andrew.caleb.smith@gmail.com]

*/
;(function($) {

	$.fn.quicklist = function(settings) {
		settings = jQuery.extend({
			items       : 5,
			className   : 'pagelist',
			id          : '',
			position    : 'before',
			nextPrev    : true,
			cycle       : true,
			showList    : true,
			custom      : false,
			autoAdvance : false,
			autoTime    : 5000
		}, settings);
		
		var t = this;
		
		var ql = {
			timer: null,
			start: function() {
				ql.length = t.length;
				ql.pages = Math.ceil(ql.length / ql.items);
				
				ql.list = $('<ul class="'+ql.className+'"></ul>');
				
				// Set up the page list links
				
				for(var i=0;i<ql.pages;i++) {
					ql.list.append('<li><a href="#">'+(i+1)+'</a></li>');
				}
				
				if (ql.nextPrev) {
					ql.list.append('<li class="prev"><a href="#">Previous</a></li><li class="next"><a href="#">Next</a></li>');
				}
				
				ql.list.children(':first-child').addClass('current');
				
				ql.list.find('a').click(ql.change);
			
				// Add the page list
				if (ql.position == 'before') {
					t.filter(':first').before(ql.list);
				} else if (ql.postion == 'after') {
					t.filter(':last').after(ql.list);
				}
				
				if (! ql.showList) {
					ql.list.hide();
				}
				
				// Hide everything but the first X items
				t.filter(':gt('+(ql.items-1)+')').hide();
				ql.after();
				
				if (ql.autoAdvance) {
					ql.timer = setTimeout(s, ql.autoTime);
					
					function s() {
						ql.autoSwitch();	
					}
					if (! ql.cycle) ql.cycle = true;
				}
			},
			change: function(e, index) {
				if (e) e.preventDefault();
				
				if (! index && index != 0) {	
					var e = $(e.target);
					var index = 0;
						
					if (e.parent().attr('class') != 'next' && e.parent().attr('class') != 'prev') {
						ql.list.find('.current').removeClass('current');
						e.parent().addClass('current');
					} else {
						if (e.parent().attr('class') == 'next') {
							ql.list.find('.current').removeClass('current').next().addClass('current');
						} else {
							ql.list.find('.current').removeClass('current').prev().addClass('current');
						}
					}
					
					ql.list.children('li').each(function(i) {
						if ($(this).is('.current')) {
							index = i+1;
						}
					});
				}
				
				if (ql.cycle) {
					if (index <= 0) index = ql.pages;
					if (index > ql.pages) index = 1;
				} else {
					if (index <= 0) index = 1;
					if (index > ql.pages) index = ql.pages;
				}
									
				ql.list.children('.current').removeClass('current');
				ql.list.children('li:eq('+(index-1)+')').addClass('current');
				
				var max = ql.items * index;
				var min = (ql.items * index) - ql.items;
				
				var show = [];
				
				for(var i=0;i<max;i++) {
					if (i >= min) {
						show.push(':eq('+i+')');
					}
				}
				
				if (t.filter(':animated').length == 0) {
					if (ql.custom) {
						ql.custom(t.filter(':visible'), t.filter(show.join(',')), ql.items, index);
						ql.after();
					} else {				
						t.filter(':visible').fadeOut('fast', function() {
							t.filter(show.join(',')).fadeIn('fast');
							ql.after();
						});
					}
				}
				
				if (ql.autoAdvance) {
					clearTimeout(ql.timer);
					ql.timer = setTimeout(s, ql.autoTime);
					
					function s() {
						ql.autoSwitch();	
					}
				}
			},
			autoSwitch: function() {
				var index = 0;
				if (! ql.list.children('.current').next().is(':contains(Next)')) {
					ql.list.children('li').each(function(i) {
						if ($(this).is('.current')) {
							index = i+2;
						}
					});
				}
				ql.change(null, index);
			},
			after: function() {
				t.removeClass('quicklist-last').removeClass('quicklist-first');
				t.filter(':visible:last').addClass('quicklist-last');
				t.filter(':visible:first').addClass('quicklist-first');
			}
		}
		
		ql = jQuery.extend(settings, ql);
		ql.start();
	}

})(jQuery);