﻿/// <reference path="../Edentity.Global.js" />
/// <reference path="../jquery-1.3.2-vsdoc.js" />

Edentity.RegisterNamespace("TSO.Modules.FeatureBox");

(function(FeatureBox, $) {

	var defaults = {
		Container: {},
		AutoPlay: false,
		RotationSpeed: 2500
	};

	var self = this;
	var container = null;
	var tabs = null;
	var items = null;
	var currentIndex = 0;
	var timerCallback = null;

	FeatureBox.Init = function(p) {

		defaults = $.extend({}, defaults, p || {});

		container = defaults.Container;
		tabs = container.find('.Tabs .Tab');
		items = container.find('.Items .Item');
		initControls();
		updateSlide(0);
	}

	function initControls() {

		tabs.filter(':last').css('margin-right', '0px');
		tabs.each(function(index) {
			$(this).bind('click', function() {
				clearTimeout(timerCallback);  // when user clicks on tab, stop
				updateSlide(index);
			});
			$(this).css('visibility', 'visible');
		});

		if (defaults.AutoPlay) {
			timerCallback = setInterval(function() { nextSlide() }, defaults.RotationSpeed);
		}
	}

	function nextSlide() {

		currentIndex = (currentIndex == 2) ? 0 : currentIndex + 1;
		updateSlide(currentIndex);
	}

	function updateSlide(n) {

		currentIndex = n;

		// Update Tab State
		tabs.each(function(i) {
			if (i == currentIndex) {
				$(this).addClass('TabSel');
			}
			else {
				$(this).removeClass('TabSel');
			}
		});

		// Show Slide
		items.each(function(i) {
			if (i == currentIndex) {
				$(this).fadeIn('slow');
			} else {
				$(this).hide();
			}
		});

	}

} (TSO.Modules.FeatureBox, jQuery));