// FIXME: edge case: if the user navigates to the team grid and to a slide past the first, 
// refreshing or returning to the page will cause the prev and next buttons to vanish without fade when scrolling to the end
// FIXME: next button fades at end of case studies. It should remain at 100% opacity. 
// When scrolling to last slide, check for existence of Main continueLink. If it is there, don't hide next

$(document).ready(function(){

// Show and hide the previous and next links. 
// Links should only show when users mouse over the slideshow.
//// UPDATE Jed Oct. 29 - testing it to show both (where appropriate) no matter which half is moused over
// The links should only show if they are not disabled. 
var slideViewer = $("#main");
slideViewer.hover(MAIN.mouseIn,MAIN.mouseOut);

// initially show arrows, fade them out
MAIN.next = $("#main .next")
MAIN.prev = $("#main .prev");
MAIN.prevShowing = false;
MAIN.nextShowing = false;




});

var MAIN = {
	mouseIsOver:false,
	scrollable:{},
	prev:{},
	prevShowing:{},
	next:{},
	nextShowing:{},
	previousIndex:0,
	initRevealID:0,
	isCaseStudy:false,
	reveal:function(){
		var size = MAIN.scrollable.getSize();
		var index =  MAIN.scrollable.getIndex();
		if( size > 1){
			if(index < size - 1){
				MAIN.next.removeClass('disabled');
			}
			if(index > 0){
				MAIN.prev.removeClass('disabled');
			}
		}
		
		MAIN.initRevealID = setTimeout(function(){
			if(MAIN.prev.hasClass('disabled')){
				MAIN.prev.css('opacity', 0);
			}
			else
			{
				// MAIN.prev.css('opacity', 1);
				MAIN.prev.fadeOut(1000);
			}
			if(MAIN.next.hasClass('disabled')){
				MAIN.next.css('opacity', 0);
			}
			else
			{
				// MAIN.next.css('opacity', 1);
				MAIN.next.fadeOut(1000);
			}

		}, 2000);
	},
	slideUpdate:function(){
		// if this is the first slide, dissable 'prev'
		if(MAIN.scrollable.getIndex() == 0){
			MAIN.prevShowing = false;
			MAIN.prev.fadeTo(600, 0)
			// MAIN.tryHideNav();
		}
		// if this is the last slide, dissable 'next'
		if((MAIN.scrollable.getIndex() + 1 == MAIN.scrollable.getSize()) && !MAIN.isCaseStudy){
			MAIN.nextShowing = false;
			//despite Seth's unwillingness to perservere, I went it alone and got this working. :)
			MAIN.next.fadeTo(600, 0)
			// MAIN.tryShowNav();
		} else if(MAIN.mouseIsOver)//trigger the prev/nav to appear on 2nd/2nd-to-last screens
		{
			MAIN.tryShowNav();
		}

	},
	mouseOut:function(){
		MAIN.mouseIsOver = false;
		MAIN.tryHideNav();
	},
	mouseIn:function(){
		MAIN.mouseIsOver = true;
		MAIN.tryShowNav();
	},
	tryShowNav:function(){
		//account for the initial fadeout and stop it if necessary
		if (MAIN.initRevealID > 0)
		{
			clearTimeout(MAIN.initRevealID);
			MAIN.initRevealID = 0;
		}
//		console.log("---- TRY SHOW NAV")		
		if(!MAIN.prevShowing && !MAIN.prev.hasClass('disabled')){
		//	console.log("SHOWING PREV");
			MAIN.prev.stop(true).fadeTo(400, 1);
			MAIN.prevShowing = true;
		//	console.log("prevShowing? " + MAIN.prevShowing);
		}
		if(!MAIN.nextShowing && !MAIN.next.hasClass('disabled')){
			// console.log("SHOWING NEXT")
			MAIN.next.stop(true).fadeTo(400, 1);
			MAIN.nextShowing = true;
		}
		},
	tryHideNav:function(){
	//	console.log("try hide nav");
	//	console.log("prevShowing: " + MAIN.prevShowing);
		if(MAIN.prevShowing){
	//		console.log("hide prev");
			// console.log("HIDING PREV")
			MAIN.prev.stop(true).fadeTo(600, 0);
			MAIN.prevShowing = false;
		}
		if(MAIN.nextShowing){
//			console.log("HIDING NEXT")
			MAIN.next.stop(true).fadeTo(600, 0);
			MAIN.nextShowing = false;
		}
		}
}
