// adds 'js' class to jsOn and jsOff classes
function setjsvisibility()
{
	//alert('getting jsOn elements');
	var jsOnElements = getElementsByClassName('jsOn');
	for(var i=0;i<jsOnElements.length;i++) {
		addClass(jsOnElements[i],'Show');
	}
	
	//alert('getting jsOff elements');
	var jsOffElements = getElementsByClassName('jsOff');
	for(var i=0;i<jsOffElements.length;i++) {
		addClass(jsOffElements[i],'Hide');
	}
}

// function courtesy of http://snipplr.com/view/1696/get-elements-by-class-name/
function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	
	for(var i=0,j=els.length; i<j; i++) {
		if(re.test(els[i].className))a.push(els[i]);
	}

	return a;
}

// add a class to an existing object
function addClass(object,classname)
{
	object.className+=object.className?classname:classname;
}

// shift the navigation buttons to be half way down the image
function moveNavButtons(imageSource) {

	// get image height first
	var aImg = new Image();
	aImg.src = imageSource.src;
	var aHeight = aImg.height;

	// set the line height of next + previous buttons to be the same as the image height
	var nextButtons = getElementsByClassName('nextButton');
	for(var i=0;i<nextButtons.length;i++) {
		nextButtons[i].style.lineHeight=aHeight+'px';
		nextButtons[i].style.visibility='visible';
	}

	var prevButtons = getElementsByClassName('prevButton');
	for(var i=0;i<prevButtons.length;i++) {
		prevButtons[i].style.lineHeight=aHeight+'px';
		prevButtons[i].style.visibility='visible';
	}

	// remove any "image loading" graphics
	var imageLoading = getElementsByClassName('imageLoading');
	for(var i=0;i<imageLoading.length;i++) {
		imageLoading[i].style.visibility='hidden';
	}
}

function collapseDiv(divName) {
	// find the divs with that name and collapse them
	var divsToCollapse = getElementsByClassName(divName);
	for(var i=0;i<divsToCollapse.length;i++) {
		divsToCollapse[i].style.display='none';
	}

	// hide the collapse link and show the expand link
	var expandAction = document.getElementById('expandAction');
	expandAction.style.display='block';

	var collapseAction = document.getElementById('collapseAction');
	collapseAction.style.display='none';

	var expansionStatus = document.getElementById('expansionState');
	expansionStatus.value = 'collapsed';
}

function expandDiv(divName) {
	// find the divs with that name and expand them
	var divsToExpand = getElementsByClassName(divName);
	for(var i=0;i<divsToExpand.length;i++) {
		divsToExpand[i].style.display='block';
	}

	// show the collapse link and hide the expand link
	var expandAction = document.getElementById('expandAction');
	expandAction.style.display='none';

	var collapseAction = document.getElementById('collapseAction');
	collapseAction.style.display='block';

	var expansionStatus = document.getElementById('expansionState');
	expansionStatus.value = 'expanded';
}

// allows the left and right arrow keys to be used to navigate between photos
function handleArrowKeys(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
	var nextButtons = getElementsByClassName('nextButton');
	var rightLink;
	for(var i=0;i<nextButtons.length;i++) {
		rightLink = nextButtons[i].firstElementChild.href;
	}
	var prevButtons = getElementsByClassName('prevButton');
	var leftLink;
	for(var i=0;i<prevButtons.length;i++) {
		leftLink = prevButtons[i].firstElementChild.href;
	}
        switch (evt.keyCode) {
            case 37:
		if (typeof leftLink!="undefined") {
	                window.location.href = leftLink;    
			break;
		}
            case 39:
		if (typeof rightLink!="undefined") {
                	window.location.href = rightLink; 
			break;
		}
         }
    }
}

document.onkeyup = handleArrowKeys;

