(function($) {
	$.fn.contentPager = function(options)
	{
		// Extend our default options with those provided.
		var opts = $.extend({}, $.fn.contentPager.defaults, options);

		return this.each(function() {
			$this = $(this);
			var locked = false;

			// build element specific options
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

			$divs = $this.children('.content');
			$divs.addClass('dynamic');

			if ($divs.length > 1)
			{
				$divs.hide();
				$current = $divs.first();
				if (o.fadefirst)
				{
					$current.fadeIn(o.speed);
				}
				else
				{
					$current.show();
				}
				/*console.log($this);
				if (o.rotate)
				{
					var ref = jQuery.fjTimer({
						interval: o.delay,
						repeat: true,
						tick: function(counter, timerId) {
							console.log($this);
							var cur = $this.find('.content-pager-button.active');
							console.log(cur);
							var next = cur.next();
							console.log(next);
							if ( next == null )
							{
								next = $(this).find('.content-pager-button:first-child');
							}
							console.log(next);

							next.click();
						}
					});
				}*/

				// create menu
				var container = $('<div class="content-pager-buttons" />');
				$divs.each(function(i){
					container.append(
						$('<a class="content-pager-button" href="#"><span>'+(i+1)+'</span></a>')
						.click(function(e){

							// sanity check
							if ( !locked )
							{
								locked = true;

								// toggle active class
								$(this).parent().find('.active').removeClass('active');
								$(this).addClass('active');

								// swap content
								$current.fadeOut(o.speed, function(){ $current.fadeIn(o.speed, function(){locked=false}); });
								$current = $($divs[i]);
							}

							// prevent selection line
							this.blur();

							// prevent default action
							return false;
						})
					);
				});
				container.children('.content-pager-button').first().addClass('active');
				
				$this.prepend(container);
			}
		});
	};
	function debug($message){
		if (window.console && window.console.log)
		{
			window.console.log($message);
		}
	};

	$.fn.contentPager.defaults = {
		fadefirst: false,
		//rotate: true,
		//delay: 2000,
		speed: 'slow'
	};
})(jQuery);
