var galNumber = 0;
var galNumber = 0;
var buttonsExist = false;
var afterFadeIn_moving;
var afterFaceFadeIn_moving;
var moveBeforeAfterMouth_moving;
var animation;
var movement;
var moveBefore;
var moveAfter;

/*function hideCases() {
	styleText = "ul.gallery { display: none; }";
	styleTextNode = document.createTextNode(styleText);
	styleTag = document.createElement("style");
	styleTag.setAttribute("type","text/css");
	styleTag.appendChild(styleTextNode);
	headTag = document.getElementsByTagName("head");
	headTag[0].appendChild(styleTag);
}
hideCases();*/

function init(){
	prepareGallery(galNumber);
}

function prepareGallery(galNumber) {
	if (moveBefore) {
		clearTimeout(moveBefore);
		}
	if (moveAfter) {
		clearTimeout(moveAfter);
		}
	if (animation) {
		clearTimeout(animation);
		}
	if (movement) {
		clearTimeout(movement);
		}
	if (afterFadeIn_moving) {
		clearTimeout(afterFadeIn_moving);
		}
	if (afterFaceFadeIn_moving) {
		clearTimeout(afterFaceFadeIn_moving);
		}
	if (moveBeforeAfterMouth_moving) {
		clearTimeout(moveBeforeAfterMouth_moving);
		}
	
	// Find all lists labled "gallery"
	var allULs = document.getElementsByTagName("ul");
	var allGals = new Array();
	for (x = 0; x < allULs.length; x++) {
		if (allULs[x].className == "gallery") {
			allGals.push(allULs[x]);
			}// end if
		}// end for	
	
	if(allGals.length > 0) {
		// Hide all galleries except the active one
		for (x = 0; x < allGals.length; x++) {
			allGals[x].style.display = "none";
			}
		allGals[galNumber].style.display = "block";
	
		// Check to see if the buttons exist and if not....
		var allDIVs = document.getElementsByTagName("div");
		for (x = 0; x < allDIVs.length; x++) {
			if (allDIVs[x].getAttribute("id") == "buttonWrap") {
				buttonsExist = true;
				}// end if
			}// end for
		
		if (buttonsExist == false) {
			
			// Create and insert buttons before cases
			var buttonWrap = document.createElement("div");
			buttonWrap.setAttribute("id","buttonWrap");
			
			// Create case number
			var caseNum = document.createElement("div");
			caseNum.setAttribute("id","caseNum");
			buttonWrap.appendChild(caseNum);
			
			var caseSpan = document.createElement("span");
			caseNum.appendChild(caseSpan);
			
			var caseSpanText = document.createTextNode(galNumber + 1);
			caseSpan.appendChild(caseSpanText);
			
			var caseText = document.createTextNode(" / " + allGals.length);
			caseNum.appendChild(caseText);
			
			var prevButton = document.createElement("a");
			prevButton.setAttribute("id","prevButton");
			prevGal = galNumber - 1;
			prevButton.setAttribute("href","#");
			prevButton.onclick = function(){
									prepareGallery(prevGal);
									return false;
									}
			/*prevButton.setAttribute("onClick","prepareGallery(" + prevGal + "); return false");*/
			buttonWrap.appendChild(prevButton);
			
			var prevButton_txt = document.createTextNode("Previous");
			prevButton.appendChild(prevButton_txt);
			
			var divider = document.createTextNode(" | ");
			buttonWrap.appendChild(divider);
			
			var nextButton = document.createElement("a");
			nextButton.setAttribute("id","nextButton");
			nextGal = galNumber + 1;
			nextButton.setAttribute("href","#");
			nextButton.onclick = function(){
									prepareGallery(nextGal);
									return false;
									}
			/*nextButton.setAttribute("onClick","prepareGallery(" + nextGal + "); return false");*/
			buttonWrap.appendChild(nextButton);
			
			var nextButton_txt = document.createTextNode("Next");
			nextButton.appendChild(nextButton_txt);
			
			if(allGals.length) {
				allGals[0].parentNode.insertBefore(buttonWrap,allGals[0]);
			};
		} else {
			
			caseNum = document.getElementById("caseNum");
			caseSpan = caseNum.getElementsByTagName("span");
			caseSpanText = caseSpan[0].childNodes;
			newCaseSpanText = document.createTextNode(galNumber + 1);
			caseSpan[0].replaceChild(newCaseSpanText,caseSpanText[0]);
			
			prevGal = galNumber - 1;
			prevButton = document.getElementById("prevButton");
			prevButton.setAttribute("href","#");
			prevButton.onclick = function(){
									prepareGallery(prevGal);
									return false;
									}
			/*prevButton.setAttribute("onClick","prepareGallery(" + prevGal + ");");*/
			
			nextGal = galNumber + 1;
			nextButton = document.getElementById("nextButton");
			nextButton.setAttribute("href","#");
			nextButton.onclick = function(){
									prepareGallery(nextGal);
									return false;
									}
			/*nextButton.setAttribute("onClick","prepareGallery(" + nextGal + "); return false");*/
		}// end if
		
		if (galNumber == 0) {
			prevButton.className = "disabled";
			prevButton.onclick = "";
		} else if (galNumber == (allGals.length - 1)) {
			nextButton.className = "disabled";
			nextButton.onclick = "";
		} else {
			prevButton.className = "";
			nextButton.className = "";
			}
		
		/* Find cases and caption */
		var allLIs = allGals[galNumber].getElementsByTagName("li");
		
		var beforeMouth = new Array();
		var afterMouth = new Array();
		var afterFace = new Array();
		var caption = new Array();
		for (x = 0; x < allLIs.length; x++) {
			
			// Find before mouth
			if (allLIs[x].className == "before") {
				beforeMouth = allLIs[x];
				}// end if
				
			// Find after mouth
			if (allLIs[x].className == "after") {
				afterMouth = allLIs[x];
				}// end if
				
			// Find after face
			if (allLIs[x].className == "afterface") {
				afterFace = allLIs[x];
				}// end if
				
			// Find caption
			if (allLIs[x].className == "caption") {
				caption.push(allLIs[x]);
				}// end if
			}// end for
		// BA
		if (beforeMouth.nodeType && afterMouth.nodeType && !afterFace.nodeType) {
			casesBA(galNumber,beforeMouth,afterMouth,caption);
			}
			
		// BAF
		if (beforeMouth.nodeType && afterMouth.nodeType && afterFace.nodeType) {
			allGals[galNumber].style.height = "415px";
			casesBAF(galNumber,beforeMouth,afterMouth,afterFace,caption);
			}
	};
	
}// end function

function fadeOut() {
	i--;
	if (i>=0) {
		whichElem.style.opacity = i/100;
		whichElem.style.filter = "alpha(opacity=" + i + ")";
		animation = setTimeout("fadeOut()",speed);
	} else {
		clearTimeout(animation);
		}	
}
function fadeIn() {
	i++;
	if (i<=100) {
		whichElem.style.opacity = i/100;
		whichElem.style.filter = "alpha(opacity=" + i + ")";
		animation = setTimeout("fadeIn()",speed);
	} else {
		clearTimeout(animation);
		}	
}

function moveElement(elem,final_x,final_y,interval) {
	//alert(interval);
	var xpos = parseInt(elem.style.marginLeft);
	var ypos = parseInt(elem.style.marginTop);
	if (xpos == final_x && ypos == final_y) {return true;}
	if (xpos < final_x) {
		var dist = Math.ceil((final_x - xpos)/10);
		xpos = xpos + dist;
		}
	if (xpos > final_x) {
		var dist = Math.ceil((xpos - final_x)/10);
		xpos = xpos - dist;
		}
	if (ypos < final_y) {
		var dist = Math.ceil((final_y - ypos)/10);
		ypos = ypos + dist;
		}
	if (ypos > final_y) {
		var dist = Math.ceil((ypos - final_y)/10);
		ypos = ypos - dist;
		}
	//alert("xpos = "+xpos+", ypos = "+ypos+", elem = "+elem);
	elem.style.marginLeft = xpos + "px";
	elem.style.marginTop = ypos + "px";
	var repeat = function() {moveElement(elem,final_x,final_y,interval);};
	/*var repeat = "moveElement('" + elem + "'," + final_x + "," + final_y + "," + interval + ")";*/
	movement = setTimeout(repeat,interval);
	}

function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
		} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
		}
	}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
		} else {
		window.onload = function() {
			oldonload();
			func();
			}
		}
	}
addLoadEvent(init);



