/*

	cartoonGallery.js - creates a next and previous button
	Nicholas Dobie bushido@black-magnolia.com
	Writen: Nov 17, 2009
	
*/

//Make the variable cartoonNum a global
var numOfCartoon;

//Establish the function buildCartoonList()
function buildCartoonList() {

	//locates and establishes 
	//a variable to the cartoon ul
	var cartoonList = document.getElementById('p7menubar').getElementsByTagName('ul')[0];
	
	//count the number of cartoons
	//in the menu.
	//Subtract 1 from the number for
	//how the graphics are named
	numOfCartoon = cartoonList.getElementsByTagName('li').length - 1;	
}

function prevCartoon() {

	if(curCartoonNum() == 0) {
		//Sets the newCartoon to the first cartoon
		var newCartoonNum = numOfCartoon;
	}
	else {
		//Sets the newCartoon to the prev cartoon
		var newCartoonNum = curCartoonNum() - 1;
	}
	
	//puts back in the zeros
	newCartoonNum = addZero(newCartoonNum);
	
	//creates the src for the image
	var newCartoonSrc = "graphic/" + newCartoonNum + ".jpg";
	
	/* get the <img /> element that is to be changed using the unique id */
    var dynamicImgElement = document.getElementById("dynamicGraphic");
   
    /* Update the src attribute of the <img /> element
    with the new graphic filename */
	dynamicImgElement.setAttribute("src", newCartoonSrc);

}

function nextCartoon() {
	
	if(curCartoonNum() == numOfCartoon) {
		//Sets the newCartoon to the first cartoon
		var newCartoonNum = 0;
	}
	else {
		//Sets the newCartoon to the next cartoon
		var newCartoonNum = curCartoonNum() + 1;
	}
	
	//puts back in the zeros
	newCartoonNum = addZero(newCartoonNum);
		
	//creates the src for the image
	var newCartoonSrc = "graphic/" + newCartoonNum + ".jpg";
	
	
	/* get the <img /> element that is to be changed using the unique id */
    var dynamicImgElement = document.getElementById("dynamicGraphic");
   
    /* Update the src attribute of the <img /> element
    with the new graphic filename */
	dynamicImgElement.setAttribute("src", newCartoonSrc);

}

//gets the current cartoon number
function curCartoonNum() {


	//Makes a varable for the current cartoon
	var curCartoon = document.getElementById('dynamicGraphic');
	
	//Gets the src from #dynamicGraphic
	var curCartoonSrc = curCartoon.src;
	
	//Get the start of the image name
	var curCartoonLoc = curCartoonSrc.indexOf("graphic/") + 8;
	
	//adds the number to the cartoon
	var curCartoonNum = curCartoonSrc.substring(curCartoonLoc, (curCartoonLoc + 5));
	
	//parses the curCartoonNum to an int
	curCartoonNum = parseInt(curCartoonNum, 10);	
	
	//sends back the curCartoonNum
	return curCartoonNum;
	
}

//adds in the zeros
function addZero(num) {

	//makes num into a string
	num = num.toString();
	
	//re-adds in the zeros
	while(num.length < 5)	{
		//the quotes around the zero
		//make it a string
		num = "0" + num;
	}
	
	//sends back the num with zeros
	return num;
}