/* Utility methods */
Math.sign = function(number) {
	return number / Math.abs(number);
}

/* Namespace */
var InnoWeb = {}

$(document).ready(function() {
	//create navigation links
	$('#teasernav').append(Array($('#teaser .banner').length + 1).join('<li><a>&bull;</a></li>'));
	InnoWeb.teaserControl.init($('#teaser .banner'), $('#teasernav A'));
});

InnoWeb.teaserControl = (function() {
	//private fields
	var $_details;
	var $_thumbnails;
	var $_detailIndex;
	var $_timer;

	//private methods
	var initialize = function(details, thumbnails) {
		$_details = details;
		$_thumbnails = thumbnails;
		$_detailIndex = $_details.length - 1;
		$_timer = null;

		//attach event handlers
		$_thumbnails.click(function () {
			showDetail($(this).parent().index());
		});

		showDetail();
	}

	var showDetail = function(newIndex) {
		clearTimeout($_timer); //stop next transition (if active)

		//hide current banner
		$_details.fadeOut(800);
		$_thumbnails.removeClass('selected');

		//update current bannerIndex value
		$_detailIndex = (newIndex == null) ? (($_detailIndex + 1) % $_details.length) : newIndex;

		//show banner
		$_thumbnails.eq($_detailIndex).addClass('selected');
		$_details.eq($_detailIndex).fadeIn(800);

		//set up next transition
		$_timer = setTimeout(function() { showDetail() }, 3800);
	}

	//public methods
	return { init: initialize }
}());


InnoWeb.referencesControl = (function() {
	var $_details;
	var $_thumbnails;
	var $_index;
	var $_prevBtn;
	var $_nextBtn;
	var $_toggleFunc;

	var initialize = function(details, thumbnails, prevBtn, nextBtn, toggleFunc) {
		$_details = details;
		$_thumbnails = thumbnails;
		$_index = 0;
		$_prevBtn = prevBtn;
		$_nextBtn = nextBtn;
		$_toggleFunc = toggleFunc;
		$_thumbnails.click(function () { showDetail($(this).parent().index()); });
		$_prevBtn.click(function() { showDetail(($_index - 1) % $_details.length); });
		$_nextBtn.click(function() { showDetail(($_index + 1) % $_details.length); });

		//display first detail
		$_details.hide();
		$_details.eq($_index).fadeIn(800);
		$_thumbnails.eq($_index).addClass('selected');
	}

	var showDetail = function(index) {
		$_details.fadeOut(800);
		$_thumbnails.removeClass('selected');

		$_thumbnails.eq(index).addClass('selected');
		$_details.eq(index).fadeIn(800);
		if ($_toggleFunc)
			$_toggleFunc();
		$_index = index;
	}

	return { init: initialize }
}());


InnoWeb.scrollControl = (function() {
	var $_config;
	var $_content;
	var $_scrollLeft;
	var $_scrollRight;
	var $_timer;

	var initialize = function(content, scrollLeft, scrollRight) {
		$_config = {
			scrollOffset: 2, //2 pixels, controls scrolling speed
			scrollInterval: 16 //16 milliseconds, controls scrolling smoothness
		}
		$_content = content;
		$_scrollLeft = scrollLeft;
		$_scrollRight = scrollRight;

		//requirements checks
		if (!$_content || !$_scrollLeft || !$_scrollRight)
			return;
		$_content.scrollLeft(1);
        if ($_content.scrollLeft() == 0) {
            $_scrollLeft.hide();
            $_scrollRight.hide();
            return;
        }
		$_content.scrollLeft(0);

		//attach event handlers
		$_scrollLeft.mouseenter(function() { scrollToPosition(-1 * $_config.scrollOffset) });
		$_scrollLeft.mousedown(function() { scrollToPosition(-1 * $_config.scrollOffset * 4) });
		$_scrollRight.mouseenter(function() { scrollToPosition(1 * $_config.scrollOffset) });
		$_scrollRight.mousedown(function() { scrollToPosition(1 * $_config.scrollOffset * 4) });
		$_scrollLeft.add($_scrollRight).bind('mouseleave mouseup', function() { clearTimeout($_timer); $_timer = null; });
		$_scrollLeft.dblclick(function() { jumpToStart(); clearTimeout($_timer); $_timer = null; });
		$_scrollRight.dblclick(function() { jumpToEnd(); clearTimeout($_timer); $_timer = null; });

		scrollToSelected();
	}

	var scrollToSelected = function() { //scroll to .selected element
		var selectedElement = $_content.find('.selected:first');
		if (selectedElement && !$_timer) {
			var position = ($_content.scrollLeft() + selectedElement.offset().left + selectedElement.width() / 2)
				- ($_content.offset().left + $_content.width() / 2);
			scrollToPosition($_config.scrollOffset * 6, position);
		}
		_checkBorders();
	}

	var jumpToStart = function() { //scroll to start
		$_content.scrollLeft(0);
		$_scrollLeft.addClass('disabled');
		$_scrollRight.removeClass('disabled');
    }

    var jumpToEnd = function() { //scroll to end
        $_content.scrollLeft($_content.width() + 1); //use 100000 if problems :)
        $_scrollRight.addClass('disabled');
		$_scrollLeft.removeClass('disabled');
    }

	var scrollToPosition = function(offset, position) { //scrolls content by offset steps	
		if (!offset)
			return;
        var currentPosition = $_content.scrollLeft();
		var direction = (position == undefined) ? Math.sign(offset) : Math.sign(position - currentPosition);

		$_content.scrollLeft($_content.scrollLeft() + direction * Math.abs(offset));

		if (($_content.scrollLeft() == currentPosition) ||
			(direction == -1) && ($_content.scrollLeft() <= position) ||
			(direction == 1) && ($_content.scrollLeft() >= position)) {
			clearTimeout($_timer); $_timer = null;
			_checkBorders();
            return;
		}

        $_scrollLeft.removeClass('disabled');
        $_scrollRight.removeClass('disabled');
		$_timer = setTimeout(function() { scrollToPosition(offset, position) }, $_config.scrollInterval);
    }

	var _checkBorders = function() {
		//check borders
		var currentOffset = $_content.scrollLeft();
		$_content.scrollLeft($_content.scrollLeft() + 1);
        if ($_content.scrollLeft() == 1) {
            $_content.scrollLeft(0);
            $_scrollLeft.addClass('disabled');
			$_scrollRight.removeClass('disabled');
			return;
		}
		if ($_content.scrollLeft() <= currentOffset) {
			$_scrollRight.addClass('disabled');
			$_scrollLeft.removeClass('disabled');
			return;
		}
		$_scrollLeft.removeClass('disabled');
		$_scrollRight.removeClass('disabled');
		$_content.scrollLeft($_content.scrollLeft() - 1);
	}

	return {
		init: initialize,
		scrollToSelected: scrollToSelected
	}
}());

