// copyright 2006 Joris Landman, all rights reserved - copyright notice for http://www.keen.nl/ available at http://www.keen.nl/copyright/

// preferences
var scrollAtBottom = 1; // determines whether items should move up when they're cut off at the bottom
var scrollAtBottomOffset = 51; // if the item is scrolled to the top the title is cut off because it's another div, hence this offset

var expandingSpeed = 1.5;
var collapsingSpeed = 1.5;
if (expandingSpeed <= 1) {
	expandingSpeed = 1.75;
}		
if (collapsingSpeed <= 1) {
	collapsingSpeed = 1.5;
}		

var x = null; // variable that can be anything at any time, reset to null after use
var y = null; // variable that can be anything at any time, reset to null after use

var currentItem = null;
var currentHeight = null;
var newHeight = null;
var arrayAnimate = new Array();
var animating = null;
var thisItem = null;
function expandCollapse(thisItem, thisItemID) {
	if (animating === null) {
		animating = 0;
		currentItem = document.getElementById(thisItem);
		if (document.all && !document.getElementById) { // use document.all for MSIE4
			currentItem = document.all(thisItem);
		}
		thisItem = null;
		// set status flag for items that are declared in index.php
		if (myItems[currentItem.id] !== null) {
			if (myItems[currentItem.id] === 0) {
				myItems[currentItem.id] = 1;
				cookieOn(thisItemID);
				var readItem = 'read' + thisItemID
				cookieOn(readItem);
				readItem = null;
			} else {
				myItems[currentItem.id] = 0;
				cookieOff(thisItemID);
			}
		}
		if (currentItem.className == 'item-collapsed') {
			currentItem.style.height = '0px'; // set height to 0px so animation will start from 0
			currentItem.className = 'item-expanded';
			newHeight = 'auto'; // set newHeight to auto in case browser doesn't understand scrollHeight or offsetHeight
			if (currentItem.offsetHeight >= 1) {
				newHeight = currentItem.offsetHeight;
			} else if (!currentItem.offsetHeight && currentItem.scrollHeight >= 1) {
				newHeight = currentItem.scrollHeight;
			}
			currentHeight = 0;
		} else {
			currentHeight = 'auto'; // set currentHeight to auto in case browser doesn't understand scrollHeight or offsetHeight
			if (currentItem.offsetHeight >= 1) {
				currentHeight = currentItem.offsetHeight;
			} else if (!currentItem.offsetHeight && currentItem.scrollHeight >= 1) {
				currentHeight = currentItem.scrollHeight;
			}
			newHeight = 0;
		}
		// make array, but only if scrollHeight or offsetHeight can be obtained
		if (newHeight === 0 && currentHeight != 'auto') { // collapsing
			y = 0;
			for (x = currentHeight / collapsingSpeed; x > 0; x = Math.floor(x / collapsingSpeed)) {
				arrayAnimate[y] = x;
				y++;
			}
			arrayAnimate[y] = 0; // add zero to completely collapse array
			x = null;
			y = null;
		} else if (currentHeight === 0 && newHeight != 'auto') { // expanding
			y = 0;
			for (x = newHeight; x > 0; x = Math.floor(x / expandingSpeed)) {
				arrayAnimate[y] = x;
				y++;
			}
			arrayAnimate.reverse();
			x = null;
			y = null;
		} else {
			arrayAnimate[0] = newHeight;
		}
		currentHeight = null;
		// animate
		if (arrayAnimate.length == 1 && arrayAnimate[0] == 'auto') {
			currentItem.style.height = arrayAnimate[0];
			currentItem = null;
			newHeight = null;
			arrayAnimate = new Array();
			animating = null;
		} else {
			expandCollapseItem();
		}
	}
}

var movingItem = null;
if (scrollAtBottom == 1) {
	var viewableTop = null;
	var windowHeight = null;
	var currentItemTop = null;
	var currentItemHeight = null;
	var scrollAmount = null;
}
function expandCollapseItem() {
	currentItem.style.height = arrayAnimate[animating] + 'px';
	if (scrollAtBottom == 1 && myItems[currentItem.id] == 1) { // if item is opened // vars for scrolling when item is opening at bottom
		// viewableTop // the amount of pixels this document is scrolled from the top // Safari, Navigator, Firefox - window.pageYOffset // IE6+ - document.documentElement.scrollTop // IE5- - document.body.scrollTop
		if (window.pageYOffset >= 1) {
			viewableTop = window.pageYOffset;
		} else if (!window.pageYOffset && document.documentElement) {
			viewableTop = document.documentElement.scrollTop;
		} else if (!window.pageYOffset && !document.documentElement && document.body) {
			viewableTop = document.body.scrollTop;
		}
		// windowHeight // height of the window // Safari, Navigator, Firefox - window.innerHeight // IE6+ - document.documentElement.clientHeight // IE5- - document.body.clientHeight
		if (window.innerHeight >= 1) {
			windowHeight = window.innerHeight;
		} else if (!window.pageYOffset && document.documentElement) {
			windowHeight = document.documentElement.clientHeight;
		} else if (!window.pageYOffset && !document.documentElement && document.body) {
			windowHeight = document.body.clientHeight;
		}
		currentItemTop = currentItem.offsetTop; // position on page of top of current item
		currentItemHeight = arrayAnimate[animating]; // height of current item
		if (viewableTop === null || windowHeight === null || currentItemTop === null || currentItemHeight === null) {
			scrollAtBottom = 0;
		} else {
			if ((currentItemTop + arrayAnimate[animating]) > (viewableTop + windowHeight)) {
				scrollAmount = (currentItemHeight - arrayAnimate[animating - 1]);
				if (viewableTop > (currentItemTop - scrollAmount - scrollAtBottomOffset)) {
					scrollAmount = currentItemTop - viewableTop - scrollAtBottomOffset;
				}
				if (scrollAmount === null) {
					scrollAmount = 1;
				}
				window.scrollBy(0,scrollAmount);
			}
		}
	}
	animating++;
	if (currentItem.style.height.replace(/px$/i, '') != newHeight) {
		movingItem = setTimeout('expandCollapseItem()', 8);
	} else {
		if (arrayAnimate[arrayAnimate.length - 1] === 0) {
			currentItem.className = 'item-collapsed';
		} else {
			currentItem.style.height = 'auto'; // set to auto, in case the div is encasing other divs, and it needs to expand/collapse when they do
		}
		clearTimeout(movingItem);
		if (scrollAtBottom == 1 && myItems[currentItem.id] == 1) {
			viewableTop = null; // vars for scrolling at bottom
			windowHeight = null;
			currentItemTop = null;
			currentItemHeight = null;
			scrollAmount = null;
		}
		currentItem = null;
		newHeight = null;
		arrayAnimate = new Array();
		movingItem = null;
		animating = null; // turn clickables back on after animation
	}
}

var thisTitle = null;
var thisClassName = null;
var currentTitle = null;
function changeClassName(thisTitle,thisClassName) {
	currentTitle = document.getElementById(thisTitle);
	if (document.all && !document.getElementById) { // use document.all for MSIE4
		currentTitle = document.all(thisTitle);
	}
	if (thisClassName == 'title-div' && isCookie('read' + myIds['id' + thisTitle.substring(8)])) {
		thisClassName = thisClassName + '-read';
	}
	currentTitle.className = thisClassName;
	thisTitle = null;
	thisClassName = null;
	currentTitle = null;
}

var rolItemNum = null;
var rollThumbNum = null;
var rollMaxImages = null;
var rollTotalImages = null;
var rollProjectLink = null;
var rollImageExt = null;
var rollServer = null;
var rollTempItem = null;
var rollCurrent = null;
var rollNext = null;
var rollPrevious = null;
var rollUrl = null;
var rollOn = 0;
function rollThumbs(rolItemNum, rollThumbNum, rollMaxImages, rollTotalImages, rollProjectLink, rollImageExt, rollServer) {
	rollTempItem = 'item' + rolItemNum + 'image' + 1;
	rollCurrent = document.getElementById(rollTempItem);
	if (document.all && !document.getElementById) { // use document.all for MSIE4
		rollCurrent = document.all(rollTempItem);
	}
	rollUrl = 'http://' + rollServer + '/project/' + rollProjectLink + '/images/' + rollThumbNum + '.' + rollImageExt;
	if (rollCurrent.src != rollUrl) {
		rollOn = 1;
	}

	for (n = rollMaxImages; n > 1; n--) {
		rollTempItem = 'item' + rolItemNum + 'image' + n;
		rollNext = document.getElementById(rollTempItem);
		if (document.all && !document.getElementById) { // use document.all for MSIE4
			rollNext = document.all(rollTempItem);
		}
		rollTempItem = 'item' + rolItemNum + 'image' + (n - 1);
		rollPrevious = document.getElementById(rollTempItem);
		if (document.all && !document.getElementById) { // use document.all for MSIE4
			rollPrevious = document.all(rollTempItem);
		}
		if (rollOn) {
			rollNext.src = rollPrevious.src;
		}
	}
	if (rollOn) {
		rollOn = 0;
		rollCurrent.src = rollUrl;
	}
}

var myObject = new Object();
var myMaxImgs = null;
var myThisItem = null;
var nextImg = null;
var imageTemp = null;
var myThisItemnow = null;
function rollImages(myObject, myMaxImgs, myThisItem, myThisItemnow) {
	if (myObject.numberofimages > 1) {
		for (m = myMaxImgs; m > 0; m--) {
			imageTemp = 'item' + myThisItem + 'image' + m;
			imageCurrent = document.getElementById(imageTemp);
			if (document.all && !document.getElementById) { // use document.all for MSIE4
				imageCurrent = document.all(imageTemp);
			}
			if (m > 1) {
				imageTempTemp = 'item' + myThisItem + 'image' + (m - 1);
				imageCurrentTemp = document.getElementById(imageTempTemp);
				if (document.all && !document.getElementById) { // use document.all for MSIE4
					imageCurrentTemp = document.all(imageTempTemp);
				}
				imageCurrent.src = imageCurrentTemp.src;
			} else {
				for (n = 0; n < myObject.numberofimages; n++) {
					if (imageCurrent.src == myObject.images[n]) {
						nextImg = n;
					}
				}
				nextImg++;
				if (nextImg >= myObject.numberofimages) {
					nextImg = 0;
				}
				imageCurrent.src = myObject.images[nextImg];
			}
		}
	}
}

function cookieOn(cookieName) {
 	var today = new Date();
 	var expire = new Date();
 	var cookieValue = true;
 	var nDays = 1;
 	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
 	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/;";
}
function cookieOff(cookieName) {
 	var today = new Date();
 	var expire = new Date();
	var cookieValue = false;
	var nDays = -10;
 	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/;";
}
function cookieSelect(cookieName, cookieValue) {
 	var today = new Date();
 	var expire = new Date();
 	var nDays = 1;
 	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
 	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/;";
}

function goToAnchor(toAnchor) {
	myTo = 'content' + toAnchor;
	myToItem = document.getElementById(myTo);
	if (document.all && !document.getElementById) { // use document.all for MSIE4
		myToItem = document.all(myTo);
	}
	myTo = null;
	
	document.location.href = '#project' + toAnchor;
// anchor
//	setTimeout('window.scrollBy(0,-27);', 75);
	if (myItems[myToItem.id] === 0) {
		myId = 'id' + toAnchor;
		expandCollapse('content' + toAnchor, myIds[myId]);
	}

	myToTitle = 'titlediv' + toAnchor;
	myCurrentToTitle = document.getElementById(myToTitle);
	if (document.all && !document.getElementById) { // use document.all for MSIE4
		myCurrentToTitle = document.all(myToTitle);
	}
	myToTitle = null;

	myCurrentToTitle.className = 'title-div-expanded';
}

function openAllProjects(totalProjects, toAnchor) {
	for (n = 1; n <= totalProjects; n++) {
		myProject = 'content' + n;
		myCurrentItem = document.getElementById(myProject);
		if (document.all && !document.getElementById) { // use document.all for MSIE4
			myCurrentItem = document.all(myProject);
		}
		myProject = null;

		myCurrentItem.className = 'item-expanded';
		myCurrentItem.style.height = 'auto'; // set height to 0px so animation will start from 0

		myTitle = 'titlediv' + n;
		myCurrentTitle = document.getElementById(myTitle);
		if (document.all && !document.getElementById) { // use document.all for MSIE4
			myCurrentTitle = document.all(myTitle);
		}
		myTitle = null;

		myCurrentTitle.className = 'title-div-expanded';

		// set status flag for items that are declared in index.php
		myId = 'id' + n;
		myItems[myCurrentItem.id] = 1;
		cookieOn(myIds[myId]);
		cookieOn('read' + myIds[myId]);
	}
	document.location.href = '#project' + toAnchor;
// anchor
//	setTimeout('window.scrollBy(0,-27);', 75);
}

function closeAllProjects(totalProjects, toAnchor) {
	for (n = 1; n <= totalProjects; n++) {
		myProject = 'content' + n;
		myCurrentItem = document.getElementById(myProject);
		if (document.all && !document.getElementById) { // use document.all for MSIE4
			myCurrentItem = document.all(myProject);
		}
		myProject = null;

		myCurrentItem.style.height = '0px'; // set height to 0px so animation will start from 0
		myCurrentItem.className = 'item-collapsed';

		myTitle = 'titlediv' + n;
		myCurrentTitle = document.getElementById(myTitle);
		if (document.all && !document.getElementById) { // use document.all for MSIE4
			myCurrentTitle = document.all(myTitle);
		}
		myTitle = null;

		newClassName = 'title-div';
//		if (isCookie('read' + myIds['id' + myCurrentItem.id.substring(7)])) {
		if (isCookie('read' + myIds['id' + n])) {
			newClassName = newClassName + '-read';
		}

		myCurrentTitle.className = newClassName;

		// set status flag for items that are declared in index.php
		myId = 'id' + n;
		myItems[myCurrentItem.id] = 0;

		cookieOff(myIds[myId]);
	}
	document.location.href = '#top';
}

function moveDownIfAnchor() {
	var myLoc = document.location + '';
	var myLocSplit = myLoc.split('#');
	var match = /^project/.test(myLocSplit[1]);
	if (match) {
		myToAnchor = myLocSplit[1].replace(/^project/i, '');
		goToAnchor(myToAnchor);
// anchor
//		setTimeout('window.scrollBy(0,-27);', 75);
	}
}

function isCookie(NameOfCookie) {
	if (document.cookie.length > 0) {
		checkIfCookie = document.cookie.indexOf(NameOfCookie + '=');
	}
	if (checkIfCookie != -1) {
		return true;
	} else {
		return null;
	}
}
