
var toggleTabs = function(theCarousel,thar,type) {
	theCarousel._toggleTabs(thar,type);
};

var customButtonState = function(button,state,thar){
	var buttonDirection = 'next';
	if ( button.toString().match(/prev/) )
		buttonDirection = 'prev';

	$(button).style.display = 'block';

	if ( ( state || ( thar && thar.tabIndex && thar.tabIndex < thar.tabSize ) ) && ! ( button == thar.options.nextElementID && ! state ) )
		$(button).removeClassName( ''+buttonDirection+'-disabled' ).removeClassName( ''+buttonDirection+'-disabled-horizontal' );
	else //if ( ! thar || ( thar && thar.tabIndex == 0 ) ) 
		$(button).addClassName( ''+buttonDirection+'-disabled' ).addClassName( ''+buttonDirection+'-disabled-horizontal' );
};

var discover_initDoneHandler = function(thar) { //passes ITSELF
	var items = thar.carouselList.childElements();

	//init first (centered) item!
	thar.scrollTo(1);

	for ( var x = thar.options.size - 1; x >= 0 ; x-- ) {

		var li = items[x];
 		var cd = x - 1;

		var activeImg = $(li).down('img.pcarousel-scrollto-me');

		if ( activeImg ) {
			activeImg.setStyle({'cursor':'pointer'});

			var cheeseIt = function(e,curDex) {
						//var data = $A(arguments);
            //console.log( data );
            //var firing_activeImg = Event.element(e);
            //if ( $(activeImg) == $(firing_activeImg) )
            if ( thar.currentIndex == curDex )
                return;  //don't try to animate self

						//var curDex = data[1];
						thar.scrollTo( parseInt(curDex) );
					};

			Event.observe( activeImg, 'click', cheeseIt.bindAsEventListener(cheeseIt,cd));
		}

	}

};

var discover_animHandler = function(currentID,context,direction){
// 						console.log(currentID+', '+context+', '+direction);
	var thar = $(currentID)._CAROUSEL;

	var horses = thar.carouselList.childElements();

	var curDex = thar.currentIndex + 1;  //totally assuming 3 visible @ a time
	var nextDex = direction == 'prev' ? curDex - 1 : curDex + 1;

	
	var currentEle = horses[curDex];
	var nextEle = typeof horses[nextDex] != 'undefined' ? horses[nextDex] : null;

	if ( context == 'before' ) {
		new Effect.Move(currentEle, {
			queue: { position:'end', scope:thar.options.queue },
			duration:0.0,
			transition: Effect.Transitions.none,
			afterFinish:function() { 

				if ( currentEle.getWidth() > 145 ) { 

					new Effect.Scale(currentEle, 80, {
							scaleMode: { originalHeight: 138, originalWidth: 181 },
							duration: 0.5
						});
				}

				if ( nextEle ) {

					new Effect.Scale(nextEle, 126, {
							scaleMode: { originalHeight: 110, originalWidth: 145 },
							duration: 0.5
						});

				}
			}
		});
	}

};





/* init */


Event.observe(window, 'load', function() {

		var nextID, prevID, compID;
		// if you end up with a lot of time on your hands one day, condense this into a loop on a config object

		//same options for left/right style carousels
		['1','2'].each(function(i) {
			nextID = 'next-button-'+i;
			prevID = 'prev-button-'+i;
			compID = 'carousel-'+i;
			new Carousel(	compID,  { 
						prevElementID: prevID, 
						nextElementID: nextID, 
						numVisible: 1, 
						scrollInc: 1,
						animParameters: { duration: 0.0 }, 
						queue: compID,
						buttonStateHandler: customButtonState
					});
			});

		//now for center "Discovery" featured carousel 
		var i = 3;
		nextID = 'next-button-'+i;
		prevID = 'prev-button-'+i;
		compID = 'carousel-'+i;
		new Carousel(	compID,  { 
					prevElementID: prevID, 
					nextElementID: nextID, 
					numVisible: 3, 
					scrollInc: 1,
					animParameters: { duration: 0.5 }, 
					queue: compID,
					hasTabs: false,
					initDoneHandler:discover_initDoneHandler,
					animHandler: discover_animHandler
				});
	});
