﻿$(
	function () {
		BannerSlideshow('home-feature');
	}
);

function BannerSlideshow(containerId, pause) {

	if (!containerId) containerId = 'banner-container';
	if (!pause) pause = 12000;
	var _items = $('#' + containerId + ' .items li');
	var _links = $('#' + containerId + ' .links li');
	var _index = 0;

	Init();

	function Init() {
		var buttons = $('#' + containerId + ' .prev, #' + containerId + ' .next');
		if (_items.length > 1) {
			buttons.fadeTo(1, 0);
			buttons.hover(
				function () {
					$(this).stop(true, true).fadeTo('fast', 1);
				},
				function () {
					$(this).stop(true, true).fadeTo('fast', 0);
				}
			);
			buttons.first().click(
				function () {
					Move(true);
				}
			);
			buttons.last().click(
				function () {
					Move();
				}
			);
			_links.fadeTo(1, 0.6);
			_links.click(
				function () {
					MoveTo(_links.index(this));
				}
			);
			_links.first().addClass('selected').fadeTo(1, 1);
			_items.hide();
			_items.first().addClass('selected').show();
			Move(false, true);
		} 
		else {
			buttons.hide();
			_links.hide();
		}
	}

	function Move(back, auto) {
		var index = (_index + (back ? -1 : 1));
		if (index < 0) index = (_items.length - 1);
		if (index >= _items.length) index = 0;
		MoveTo(index, auto);
	}

	function MoveTo(index, auto) {
		if (_index != index) {
			var item = _items.eq(index);
			if (auto) item.delay(12000);
			else _items.stop(true, true);
			_items.removeClass('selected');
			item.addClass('selected');
			item.queue(
				function (next) {
					_index = index;
					_links.removeClass('selected').fadeTo(1, 0.6);
					_links.eq(_index).addClass('selected').fadeTo('fast', 1);
					next();
				}
			)
			item.fadeIn(
				'slow',
				function () {
					_items.not(item).hide();
					if (auto) Move(false, true);
				}
			);
		}
	}

}
