//	js/functions.js

	var t;
	var clear_menu = 16;		//	remove the menu after x seconds
	
	
	function revealChildNodes(that, deactivate) {
		clearTimeout(t);
		
//		loop through the uls hiding them all
		parent_node = that.parentNode.parentNode;
		nodes = parent_node.getElementsByTagName('ul');

		for ( i = 0; i < nodes.length; i++ ) {
			if ( nodes[i] == that ) 
				window.status = ('that');
			else
				if ( nodes[i].id.indexOf('ul-') == 0 )
					nodes[i].style.display = 'none';
		}

//	turn on the relevant ul	
		if ( that.nextSibling.nextSibling ) 
			that = that.nextSibling.nextSibling;
		else if ( that.nextSibling )
			that = that.nextSibling;
				
		if ( that.style ) {
			that.style.display = "block";
//	fixes funny business with IE6/7 zindex
			that.style.zIndex = 1000;
			document.getElementById('sidebar').style.zIndex = 999;
		}


		t = setTimeout('hideParents("' + that.id + '")', clear_menu * 1000);
	}
	
	
	function hideParents(id) {
		node = document.getElementById(id);
		node.style.display = 'none';
		if ( node.parentNode.parentNode.id ) {
			if ( node.parentNode.parentNode.id != 'sidebar-ul' )
				node.parentNode.parentNode.style.display = 'none';
		}
	}


	var preloaded = new Array();
		
		
	function preload(src) {
		i = preloaded.length + 1;
		preloaded[i] = new Image();
		preloaded[i].src = src;
	}


	function Gallery() {
	
	}
	
	
	function setImage(target, src, title, id) {
	//	alert(target);
		document.getElementById(target).src = src;
		if ( title )
			document.getElementById(target).title = title;
		else 
			document.getElementById(target).title = "";
			
		if  ( id )
			if ( title )
				document.getElementById(id).innerHTML = title;
			else 
				document.getElementById(id).innerHTML = "";
	}

	function setImage2(target, src, index, id, size) {
	//	alert(target);
		document.getElementById(target).src = src;
		// alert(id + index);
		
		for (var i=0;i<size;i++) {
			if (document.getElementById(id + i)) {
				document.getElementById(id + i).className = "project";
			}
		}
		document.getElementById(id + index).className = "currentProject";
	}

/*
	@attr		obj is the obj we clicked on to call this launchSlideshow script
	@attr		links is a container object for the real lightbox links
	@requires	myLightbox is available if lightbox.js has already run
*/
	function launchSlideshow(obj, links) {
		l = links.getElementsByTagName('a');
		for ( i = 0; i < l.length; i++ ) {
			if ( l[i].href == obj.href ) {
				link = l[i];
				break;
			}
		}
		
		if ( link && myLightbox ) {
			myLightbox.start(link);
			return true;
		}
		return false;
	}

	function launchSlideshow2(obj, links) {
		l = links.getElementsByTagName('a');
		for ( i = 0; i < l.length; i++ ) {
			if ( l[i].title == obj.title ) {
				link = l[i];
				break;
			}
		}
		
		if ( link && myLightbox ) {
			myLightbox.start(link);
			return true;
		}
		return false;
	}
	
	function lightbox_callback(status) {
//	actions to fix firefox overflow:auto divs under lightbox
/* in lightbox.js
>>
//	added mr.snow
	start:
		if ( typeof(lightbox_callback) == 'function') {
			lightbox_callback('start');
		}
<<
*/

		nodes = getElementsByClassName('entry','div',document);
		for ( i = 0; i < nodes.length; i++ ) {
			if ( status == 'start' )
				nodes[i].style.overflow = 'hidden';
			else if ( status == 'end' || status == 'stop' )
				nodes[i].style.overflow = 'auto';
		}
	}
			
			
			
			
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function showBox(){
	$('overlay2').show();
    centerBox('textBox', '500', '400');
	$('textBox').style.zIndex = 99;
    return false;
}

function hideBox(){
	    $('textBox').hide();
    $('overlay2').hide();
    return false;
}

function center(element){
    try{
        element = $(element);
    }catch(e){
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }else if ( document.documentElement &&
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body &&
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }
	
	my_height = my_height;

	my_width = my_width;

    element.style.position = 'absolute';
    element.style.zIndex   = 99;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

    // var elementDimensions = element.getDimensions();
	var elementWidth = 1000;
	var elementHeight = 450;
	
    // var setX = ( my_width  - elementDimensions.width  ) / 2;
	var setX = ( my_width  - elementWidth  ) / 2;
    // var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;
	var setY = ( my_height - elementHeight ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}

function centerBox(element, elementWidth, elementHeight){
    try{
        element = $(element);
    }catch(e){
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }else if ( document.documentElement &&
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body &&
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }
	
	my_height = my_height - 100;

	my_width = my_width - 22;

    element.style.position = 'absolute';
    element.style.zIndex   = 1;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

	if ((elementWidth) == "100%") {
		elementWidth = my_width;
	}

    // var setX = ( my_width  - elementDimensions.width  ) / 2;
	var setX = ( my_width  - elementWidth  ) / 2;
    // var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;
	var setY = ( my_height - elementHeight ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}

function shiftThumbsLeft(maxThumbs) {
	// move all thumbs back one
	scrollPos--;
	if (scrollPos < 0) {
		scrollPos = imageMedium.length-1;
	}
	adjustThumbs(maxThumbs);
}

function shiftThumbsRight(maxThumbs) {
	// move all thumbs forward one
	scrollPos++;
	if (scrollPos >= imageMedium.length) {
		scrollPos = 0;
	}
	adjustThumbs(maxThumbs);
}

function adjustThumbs(maxThumbs) {
	var currentPos = scrollPos;
	for (i=0;i<maxThumbs;i++) {
		$("thumbLink"+i).href = imageFull[currentPos];
		$("thumbLink"+i).title = imageTitle[currentPos];
		// $("thumbLink"+i).onmouseover="setImage('gallery_target','"+imageMedium[currentPos]+"','" + imageTitle[currentPos] + "');"
		// $("thumbLink"+i).onmouseover="alert('hello');"
		$("thumbLink"+i).onmouseover = new Function('setMediumImage('+ i +')');
		$("thumbnail"+i).alt = imageTitle[currentPos];
		$("thumbnail"+i).src = imageThumb[currentPos];
		currentPos++;
		if (currentPos == imageThumb.length) {
			currentPos = 0;
		}
	}
}

function setMediumImage(pos) {
	// alert("Setting the Medium Image to: " + pos);
	posInArray = scrollPos + pos;
	if (posInArray >= imageMedium.length) {
		posInArray = posInArray - imageMedium.length;
	}
	setImage('gallery_target',imageMedium[posInArray],imageTitle[posInArray]);
}
