
/*

*/
var G_CAROUSEL_ANIMATION_PENDING = false;
var G_CAROUSEL_ELEMENTS = ['1','2','3'];

// set up the menu and items for the carousel animation
function initCarousel() {
	//attach mouse click handlers
	$('#carousel_menu_container .carousel_menu_item').bind('click', function() {	
		selectCarouselItem(this);
		});
		
	// set up carousel items to just show the first 3 items
	$('#carousel_container_home .slider_item').each(function (index, el) { 
		if (index > 2) { $(el).css('display', 'none'); }
	});		
}
		

// handle scrolling the carousel menu up/down
function carouselMenuScroll(scrollUp) {
	
	if (G_CAROUSEL_ANIMATION_PENDING) { }	// one at a time
	else {
		// [currentTop] is the relative position of the menu scroller within it's container.
		var currentTop = $('#carousel_menu_container').offset().top - $('#carousel_menu_scroll_container').offset().top;
		var currentHeight = $('#carousel_menu_container').height();
		var menuContainerHeight = $('#carousel_menu_scroll_container').height();

		if (G_DEBUG_ON) {
			$('#debugTxt').text('currentTop: ' + currentTop + ', currentHeight: ' + currentHeight +  ', menuContainerHeight: ' + menuContainerHeight);
		}
		
		
		if (scrollUp==0) {
			// scroll up 
			if ((currentTop + currentHeight) > (menuContainerHeight+25)) {
				G_CAROUSEL_ANIMATION_PENDING = true;
				$('#carousel_menu_container').animate( { top: '-=25' }, 222, function() { G_CAROUSEL_ANIMATION_PENDING = false;});
			}
		}
		else {
			// scroll down
			if (currentTop < 0) { 
				G_CAROUSEL_ANIMATION_PENDING = true;
				$('#carousel_menu_container').animate( { top: '+=25' }, 222, function() { G_CAROUSEL_ANIMATION_PENDING = false;});
			}
		}
	}	
}

function selectCarouselItem(el) {

	if (G_CAROUSEL_ANIMATION_PENDING) { }	// one at a time
	else {
	
		var numericId = (el.id).replace("menu_item_","");
		
		var foundId = false;
		for (var i=0; i<G_CAROUSEL_ELEMENTS.length; i++) {
			if (G_CAROUSEL_ELEMENTS[i] == numericId) { foundId = true; }
		}
		
		if (foundId == false) {
			G_CAROUSEL_ANIMATION_PENDING = true;	// set wait flag
		
			var pushed = G_CAROUSEL_ELEMENTS.push(numericId);	// add the clicked item to the end of the current array.
			var shifted = G_CAROUSEL_ELEMENTS.shift();		// return the newly removed 1st item of the array.
			
			var showMenuId = '#menu_item_' + numericId;
			var hideMenuId = '#menu_item_' + shifted;

			var showSliderId = '#slider_item_' + numericId;
			var hideSliderId = '#slider_item_' + shifted;
			
			// toggle the menu items
			$(hideMenuId).switchClass( " grey_menu", "black_menu", 200 );
			$(showMenuId).switchClass( " black_menu", "grey_menu", 250 );
			
			// toggle the shown elements
			var options = {};
			$(hideSliderId).hide('slide', options, 1000, function() {
				//phase 1 animation finished, start phase 2
				$('#carousel_slider').prepend($(showSliderId));	// move the element to be shown to the end (RHS) of the list
				$(showSliderId).show('slide', options, 1000, function() { 
					//both animations finished, reset wait flag
					G_CAROUSEL_ANIMATION_PENDING = false;
					//DEBUG:: $(showSliderId).css('background-color', '#0cc');
					
					} );
			} );
			
			if(G_DEBUG_ON) {
				$('#debugTxt').text('showMenuId: ' + showMenuId + ', hideMenuId: ' + hideMenuId +  ', showSliderId: ' + showSliderId + ', hideSliderId: ' + hideSliderId + ', current Array: [' + shownElements.toString() + ']');
			}
		}
		else {
			G_CAROUSEL_ANIMATION_PENDING = false;
			if(G_DEBUG_ON) { $('#debugTxt').text('nothing to do...'); }
		}
	}
}


