// Tabbed box: http://www.cssnewbie.com/advanced-jquery-tabbed-box-techniques/
var currentTab = 0; // Set to a different number to start on a different tab. 
var rotateSpeed = 10000;
var numTabs;
var autoRotate;

//carousel
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

 
function openTab(clickedTab) {
	var thisTab = $(".tabbed-box .tabs a").index(clickedTab);
	var lastTab = $(".tabbed-box .tabbed-content:visible").index();

	// visual tab switch by removing top border
	/*
	$(".tabbed-box .tabs li:eq("+lastTab+")").css('border-top','1px solid #b6b1b1');
	$(".tabbed-box .tabs li:eq("+thisTab+")").css('border-top','1px solid #663366');
	*/
	switch (thisTab) {
		case 0:
			$(".tabbed-box").css('background-image','url('+tzd_welcomeImage+')');
			break;
		case 1:
			$(".tabbed-box").css('background-image','url(\'/i/tab/tabbox_today2.jpg\')');
			break;
		case 2:
			// $(".tabbed-box").css('background-image','url(\'stolenfeather.jpeg\')');
			$(".tabbed-box").css('background-image','url(\'/i/tab/gettingstarted.jpeg\')');
			// $(".tabbed-box").css('background','#AA3026');
			break;
		default:		
			$(".tabbed-box").css('background','#fff');
	}

	$(".tabbed-box .tabs li:eq("+lastTab+")").css('background','#fff');
	$(".tabbed-box .tabs li:eq("+thisTab+")").css('background','#b6b1b1');


	$(".tabbed-box .tabs li a").removeClass("active");
	$(".tabbed-box .tabs li a:eq("+thisTab+")").addClass("active");

	// Fade crap apparently causes scroll-to-top issues if you fade to zero
	// (jQuery sets display:none, so page changes size?)
    //$(".tabbed-box .tabbed-content:eq("+thisTab+")").css({ 'opacity' : 0.01 });
	$(".tabbed-box .tabbed-content").hide();
    $(".tabbed-box .tabbed-content:eq("+thisTab+")").show();
	//$(".tabbed-box .tabbed-content:eq("+thisTab+")").fadeTo('slow', 1, function() {
	      // fade in foreground
    //});
 
	currentTab = thisTab;
}

function rotateTabs() {
	var nextTab = (currentTab == (numTabs - 1)) ? 0 : currentTab + 1;
	openTab($(".tabbed-box .tabs li a:eq("+nextTab+")"));
}
 
$(document).ready(function() {
	$(".tabbed-content").equalHeights(100, 300);
   numTabs = $(".tabbed-box .tabs li a").length;
   $(".tabbed-box .tabs li a").click(function() { 
      openTab($(this)); return false; 
   });
   
   // Rotate through all tabs once to prevent it from scrolling to the top on tab viewing
	$('.tabbed-box .tabbed-content').each(function(index) {
		// style them while you're at it
		// background-image:url('stolenfeather.jpeg');background:#E3E4FA;
		// tabbed-box style="background:#E3E4FA;"
		if (index == 2) {
			// Tab colors
			// $(".tabbed-box .tabs li:eq("+index+")").css('background','#E3E4FA');
		}
		rotateTabs();
	});

	// Auto-tab-rotate   
	$(".tabbed-box").mouseover(function(){clearInterval(autoRotate)})
	.mouseout(function(){autoRotate = setInterval("rotateTabs()", rotateSpeed)});

	// Init first tab (click or open?)
   //$(".tabbed-box .tabs li a:eq("+currentTab+")").click()
   openTab($(".tabbed-box .tabs li a:eq(0)"));
   $(".tabbed-box").mouseout();

	//carousel   
   jQuery('#mycarousel').jcarousel({
        // Configuration goes here (auto:5 for auto scrolling)
		wrap: 'circular', scroll: 1, visible:4,itemFallbackDimension:150, initCallback: mycarousel_initCallback
   });

});
