/*
 * JQuery BannerSlider plugin
 * author: Eren Ezgü  ( erenezgu [at] gmail.com )
 * 
*/
(function($)
{
	$.fn.extend({
		bannerslider: function(options)
		{
			var defaults = {
				showNumbers: true,
				showArrows: true,
				subtitleAttr: false,
				itemAnimation: 'fade',
				autoAdvance: false,
				animationPeriod: 500,
				bottomOpacity: 0.3,
				nextButton: '&raquo;',
				prevButton: '&laquo;'
			}
			var options = $.extend(defaults, options);
			return this.each(function()
			{
				var o = options;
				var $container = $(this);
				$container.addClass("jquery_bannerslider");
				$container.append('<div class="pager"></div><div class="bottom_group"><span class="subtitle"></span><span class="bottom"></span></div>');
				$container.find(".bottom").fadeTo(10, o.bottomOpacity);
				var $items = $container.find(".items > *");
				var $first_item = $items.eq(0);
				var $pager = $container.find(".pager");
				var $bottom = $container.find(".bottom");
				var $bottomGroup = $container.find(".bottom_group");
				var $subtitle = $container.find(".subtitle");
				var n = $items.length;
				var current = 0;
				var anim_t = parseInt(o.animationPeriod);
				var timeoutId = null;
				var lastClickTime = 0;
				anim_t = anim_t < 50 ? 50 : anim_t;

				$items.not($first_item).hide();

				if (!(n > 1))
				{
					return;
				}

				var allow_click = function()
				{
					var currentTime = new Date().getTime();
					if ((currentTime - lastClickTime) < anim_t * 3)
					{
						return false;
					}
					lastClickTime = currentTime;
					return true;
				}


				if (o.subtitleAttr != false)
				{
					var subtitle = $items.eq(0).attr(o.subtitleAttr);
					if (subtitle != '' && subtitle != undefined)
					{
						$bottomGroup.css("top", "179px");
						$bottomGroup.css("background-color", "#0053A5");
					}
				}
				var bottom_show = function(callback)
				{
					if (callback == undefined)
					{
						$bottomGroup.animate({ "top": "179px" }, anim_t);
					}
					else
					{
						$bottomGroup.animate({ "top": "179px" }, anim_t, callback);
					}
				}
				var bottom_hide = function(callback)
				{
					if (callback == undefined)
					{
						$bottomGroup.animate({ "top": "204px" }, anim_t);
					}
					else
					{
						$bottomGroup.animate({ "top": "204px" }, anim_t, 'linear', callback);
					}
				}

				var item_animate = function(i, callback)
				{
					if (callback == undefined)
					{
						callback = function() { };
					}
					$items.stop(true, true);
					switch (o.itemAnimation)
					{
						case 'fade':
							$items.not($items.eq(i)).fadeOut(anim_t);
							$items.eq(i).fadeIn(anim_t, callback);
							break;
						default:
							$items.not($items.eq(i)).hide();
							$items.eq(i).show();
					}
				};


				var next_trigger = function() { };
				if (o.autoAdvance)
				{
					next_trigger = function()
					{
						clearTimeout(timeoutId);
						timeoutId = setTimeout(function() { advance(); }, o.autoAdvance);
					};
				}

				var select = function(i)
				{
					current = i;
					var use_subtitle = false;
					if (o.subtitleAttr != false)
					{
						var subtitle = $items.eq(i).attr(o.subtitleAttr);
						if (subtitle != '' && subtitle != undefined)
						{
							use_subtitle = true;
						}
					}
					var si = i > 4 ? 5 : i;
					//alert(si);
					var $pager_item = $pager.find(".page").eq(si);
					$pager.find(".page").not($pager_item).removeClass("active");
					$pager_item.addClass("active");

					bottom_hide(function()
					{
						item_animate(i, function()
						{
							$subtitle.html(subtitle);
							if (use_subtitle)
							{
								bottom_show(next_trigger);
							} else
							{
								next_trigger();
							}
						});
					});
				}

				var advance = function(step)
				{
					if (step == undefined)
					{
						step = 1;
					}
					current = (current + n + step) % n;
					select(current);
				};

				if (o.showArrows)
				{
					$pager.append("<span unselectable='on' class='prev' >" + o.prevButton + "</span>");
				}
				if (o.subtitleAttr != false)
				{
					var subtitle = $items.eq(0).attr(o.subtitleAttr);
					$subtitle.html(subtitle);
				}
				if (o.showNumbers)
				{
					var sn = n > 5 ? 5 : n;
					for (var i = 1; i <= sn; i++)
					{
						$pager.append("<span unselectable='on' id='bannerpage" + i + "' class='button page" + (i == 1 ? ' active' : '') + "' >" + i + "</span>");
					}
					if (n > 5)
					{
						$pager.append("<span unselectable='on' id='bannerpage" + i + "' class='button page' >..</span>");
					}
				}

				if (o.showArrows)
				{
					$pager.append("<span unselectable='on' class='button next' >" + o.nextButton + "</span>");
				}
				$pager.find(".page").click(function()
				{
					if (!allow_click()) return false;
					clearTimeout(timeoutId);
					$bottomGroup.stop(true, true);
					var i = $(".page", $container).index(this);
					current = i;
					select(i);
				});
				$pager.find(".next").click(function()
				{
					if (!allow_click()) return false;
					clearTimeout(timeoutId);
					$bottomGroup.stop(true, true);
					advance(1);
				});
				$pager.find(".prev").click(function()
				{
					if (!allow_click()) return false;
					clearTimeout(timeoutId);
					$bottomGroup.stop(true, true);
					advance(-1);
				});

				o.autoAdvance = parseInt(o.autoAdvance);
				if (o.autoAdvance > 0)
				{
					next_trigger();
				}
				else
				{
					o.autoAdvance = false;
				}

			});
		}
	});
})(jQuery);

