/*
	Douglas Green / greeninteractive.com
	Copyright 2010-2011 All rights reserved
	v1.3 / 07.06.11
*/

$(document).ready(function(){

	// variables
	
	// imageArray in work document
	var totalImages	= imageArray.length;
	var heightArray	= new Array();
	// start counter at 1 - img & nav IDs start at 1 rather than 0
	var counter 	= 1; // current image
	var maxHeight 	= 0;
	// for animation
	var nextImage 	= "img-nav2";
	var intervalID;
	
	// functions

	function clearNoScript() {
		$("#images").empty();
	}
	
	function addImages() {
		for (k=0; k<totalImages; k++) {
			var image = imageArray[k];
			$("#images").append(image);
		}
	}
	
	// adding 1 so that IDs start with 1 not 0 - following 3 functions
	
	function addImageIDs() {
		for (l=0; l<totalImages; l++) {
			$("#images").children().eq(l).attr("id","img"+(l+1));
		}
	}
	
	function addNav() {
		for (m=0; m<totalImages; m++) {
			var navLink = "<a href=\"#\" id=\"img-nav"+(m+1)+"\">"+(m+1)+"</a>";
			$("#img-nav").append(navLink);
		}
	}

	function addNavFunctions() {
		for (p=0; p<totalImages; p++) {
			$("#img-nav"+(p+1)).click(function() {
				imgNav($(this).attr("id"));
				// stop animation
				window.clearInterval(intervalID);
				return false;
			});
		}
	}
	
	function imgNav(targetImage) {
		// get id number from string; split string at "img-nav"
		var tempArray = targetImage.split("img-nav");
		var tID = parseInt(tempArray[1]);
		// do the animations, except on current item
		if (tID != counter) {
			$("#img"+counter).fadeOut(1000);
			$("#img"+tID).fadeIn(1000);
			// update counter and nav
			counter = tID;
			setStatus(counter);
			// values for animation
			var nxtImgNum = ++tID;
			nextImage = "img-nav"+nxtImgNum;
		}
		return false;
	}

	function setStatus(navItem) {
		// clear current class
		for (n=0; n<$("#img-nav").children().length; n++) {
			$("#img-nav").children().eq(n).removeClass("current");
		}
		setCurrent(navItem);
	}
	
	// set current class; subtract one to account for ID offset 
	
	function setCurrent(navItem) {
		$("#img-nav").children().eq(navItem-1).addClass("current");
	}

	function loopImages() {
		for (i=0; i<totalImages; i++) {
			// populate height array; set height to integer
			heightArray[i] = parseInt($("#images").children().eq(i).attr("height"));
			// hide all but first image
			if (i!=0) {
				$("#images").children().eq(i).hide();
			}
		}
	}
	
	function setMaxHeight() {
		for (j=0; j<heightArray.length; j++) {
			// if current value is greater than maxHeight, set maxHeight to current value
			if (heightArray[j] > maxHeight) {
				maxHeight = heightArray[j];
			}
		}
	}
	
	function setCSSHeight() {
		$("#images").attr("style","height:"+maxHeight+"px;");
	}
	
	clearNoScript();
	addImages();
	addImageIDs();
	addNav();
	addNavFunctions();
	setCurrent(counter);
	loopImages();
	setMaxHeight();
	setCSSHeight();
	
	
	// animation
	
	function animate() {
    	intervalID = window.setInterval(nxt, 4000);
	}
	function nxt() {
		if (counter == totalImages) {
			// stop animation
			window.clearInterval(intervalID);
		} else {
			imgNav(nextImage);
		}
	}
	animate();
	
});

