/**
 * Todo: when you go to last tab, it shouldnt go backwards (append 1st and last element to give an illusion of continuity)
 * requires jquery
*/
var VozCarousel = {
	container: null,
	tabsContainer1: null,
	tabsContainer2: null,
	tabs: Array(),
	tabIndex: 0,
	maxIndex: 0,
	init: function(container) {
		VozCarousel.container = $(container);
		VozCarousel.tabsContainer1 = $('.tabs', VozCarousel.container);
		VozCarousel.tabsContainer2 = VozCarousel.tabsContainer1.children('div');

		var maxHeight = 0;
		VozCarousel.tabsContainer2.children('div').each(
			function (i, element) {
				element = $(element);
				element.width(VozCarousel.tabsContainer1.width());

				var id = element.attr('id');
				var anchor = id.substring(2);
				var tab = {
					element: element,
					id: id,
					anchor: anchor,
					index: i
				}
				VozCarousel.tabs[i] = tab;
				VozCarousel.maxIndex = i;

				if (element.height() > maxHeight) maxHeight = element.height();
			}
		)

		/* VozCarousel.tabsContainer1.height(maxHeight); bug in IE */
		VozCarousel.initActions();
		//VozCarousel.showTab(0);
	},
	initActions: function() {
		$('a.previous', VozCarousel.container).each(
			function (i, element) {
				$(element).click(function() {VozCarousel.showPreviousTab(); return false;})
			}
		)
		$('a.next', VozCarousel.container).each(
			function (i, element) {
				$(element).click(function() {VozCarousel.showNextTab(); return false;})
			}
		)
	},
	showPreviousTab: function() {
		if (VozCarousel.tabIndex - 1 < 0) {
			VozCarousel.showTab(VozCarousel.maxIndex);
		}
		else {
			VozCarousel.showTab(VozCarousel.tabIndex - 1);
		}
	},
	showNextTab: function() {
		if (VozCarousel.tabIndex + 1 > VozCarousel.maxIndex) {
			VozCarousel.showTab(0);
		}
		else {
			VozCarousel.showTab(VozCarousel.tabIndex + 1);
		}
	},
	getTabPosition: function(index) {
		var tab = VozCarousel.tabs[index];
		var tabWidth = VozCarousel.tabs[0].element.width();
		var position = tabWidth * index;

		return position;
	},
	showTabByAnchor: function(anchor) {
		if (anchor == '') return;
		for(var i in VozCarousel.tabs) {
			if (VozCarousel.tabs[i].anchor == anchor) var tab = VozCarousel.tabs[i];
		}
		if (!tab) return;
		VozCarousel.showTab(tab.index);
	},
	showTab: function(index) {
		var distance = VozCarousel.getTabPosition(index);
		VozCarousel.tabsContainer2.animate({left:-distance},"slow");
		VozCarousel.tabIndex = index;
	}
}



$(document).ready(function(){
	VozCarousel.init($('.vozcarousel'));

	$.historyInit(function(hash) {
		//alert('hash is ' + hash);
		VozCarousel.showTabByAnchor(hash);
	});
	// set onlick event for buttons
	/*
	$("a[rel='history']").click(function(){
			//
		var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			// moves to a new page.
			// pageload is called at once.
			$.historyLoad(hash);
			return false;
		});
	*/
});
/*
function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		$("#load").load(hash + ".html");
	} else {
		// start page
		$("#load").empty();
	}
}
*/