var usesPreviousButton = 1;	// 0 for no, 1, for yes
var usesNextButton = 1;		// 0 for no, 1, for yes
var navButtonOnStyle = '#144D95'.toLowerCase(); // nav li a:hover {color: #144D95;}

// Note that the Previous and Next buttons are li's in the same group of li's as the anchors
// The style changes the color of the anchors, not the li's. So in the code use firstChild. This makes it less generalized.
//--------------------------------------------------------------------
//No more configuration below here, except configuring the carousel
//--------------------------------------------------------------------

var usesPreviousAndNext = usesPreviousButton + usesNextButton;

// Firefox converts #144D95 to rgb(20, 77, 149), but hexToRgb() converts it to rgb(20,77,149), no spaces
// So make a regular expresion out of it, and use search() in the code instead of indexOf()

var navButtonOnStyleRGBRegEx = navButtonOnStyle.hexToRgb();
navButtonOnStyleRGBRegEx = navButtonOnStyleRGBRegEx.replace(/,/gi, ", ?");
navButtonOnStyleRGBRegEx = navButtonOnStyleRGBRegEx.replace(/\(/gi, "\\(");
navButtonOnStyleRGBRegEx = navButtonOnStyleRGBRegEx.replace(/\)/gi, "\\)");
// it's now rgb\(20, ?77, ?149\)

var theseNavGroups = [];
var theseNavButtons = []; // might or might not be good enough when there are multiple nav groups on the page

window.addEvent("domready", function() {
	var Carousel = new iCarousel("carousel_content", {
		idPrevious: "carousel_previous",
		idNext: "carousel_next",
		idToggle: "undefined",
		item: {
			klass: "carousel_item",
			size: 556
		},
		animation: {
			type: "scroll",
			duration: 1500,
			amount: 1
		}
	});
	
	// Highlight each carousel's first navigation button.
	theseNavGroups = document.getElements('ul#nav');
	for (var i=0;i<theseNavGroups.length;++i) {
		theseNavButtons = theseNavGroups[i].getElementsByTagName('li');
		theseNavButtons[usesPreviousButton].firstChild.style.color = navButtonOnStyle;
		// Generate these with this script, so it accomodates carousels on different pages, of different lengths
		// Every time the item is clicked, the function is run FROM HERE! A breakpoint demonstrated it to me. That is why the eval is used.
		for (var i=usesPreviousButton;i<theseNavButtons.length-usesNextButton;++i) {
			eval('$(theseNavButtons['+i+'].firstChild).addEvent("click", function(event){new Event(event).stop();Carousel.goTo('+(i-usesPreviousButton)+');swapNavButton('+i+');});');
		}
	}
	$("carousel_previous").addEvent("click", function(event){new Event(event).stop();handlePreviousClick()});
	$("carousel_next").addEvent("click", function(event){new Event(event).stop();handleNextClick()});
	//$("carousel_previous").addEvent("click", handlePreviousClick());
	//$("carousel_next").addEvent("click", handleNextClick());
	
	/*
	theseNavButtons[i].firstChild.addEvent("click", function(event){new Event(event).stop();Carousel.goTo(i-usesPreviousButton);swapNavButton(i);});
	$(theseNavButtons[1].firstChild).addEvent("click", function(event){new Event(event).stop();Carousel.goTo(0);swapNavButton(1);});
	$(theseNavButtons[2].firstChild).addEvent("click", function(event){new Event(event).stop();Carousel.goTo(1);swapNavButton(2);});
	$("link-0").addEvent("click", function(event){new Event(event).stop();Carousel.goTo(0);swapNavButton(1)});
	$("link-1").addEvent("click", function(event){new Event(event).stop();Carousel.goTo(1);swapNavButton(2)});
	*/

}); // end of domready

function getCurrentNavButtonIndex() {
	var currentNavButtonIndex = 0;
	for (var i=usesPreviousButton;i<theseNavButtons.length-usesNextButton;i++) { // skip previous and next buttons if used
		// Find the currently highlighted button
		if (theseNavButtons[i].firstChild.style.color.indexOf(navButtonOnStyle) > -1) { // This is the Internet Explorer check
			currentNavButtonIndex = i;
			break;
		}
		//else if(theseNavButtons[i].firstChild.style.color.indexOf(navButtonOnStyleRGB) > -1) { // This is the insufficient Firefox check
		else if(theseNavButtons[i].firstChild.style.color.search(navButtonOnStyleRGBRegEx) > -1) { // This is the Firefox check
			currentNavButtonIndex = i;
			break;
		}
	}
	return currentNavButtonIndex;
}

function handlePreviousClick() {
	var currentNavButtonIndex = getCurrentNavButtonIndex() - 1; // the -1 moves it to the previous slide
	if (currentNavButtonIndex < usesPreviousButton) {
		currentNavButtonIndex = theseNavButtons.length-usesPreviousAndNext;
	}
	swapNavButton(currentNavButtonIndex);
}

function handleNextClick() {
	var currentNavButtonIndex = getCurrentNavButtonIndex() + 1; // the +1 moves it to the next slide
	if (currentNavButtonIndex > theseNavButtons.length-usesPreviousAndNext) {
		currentNavButtonIndex = usesPreviousButton; // 0 if no Previous button, 1, if there is
	}
	swapNavButton(currentNavButtonIndex);
}

function swapNavButton(currentNavButtonIndex) {
	for (var i=usesPreviousButton;i<theseNavButtons.length-usesNextButton;++i) { // skip previous and next buttons if used
		theseNavButtons[i].firstChild.style.color = "";
	}
	theseNavButtons[currentNavButtonIndex].firstChild.style.color = navButtonOnStyle;
}
