/**
 * Adapted from code by AM for hlocator
 *
 * @author Justin Johnson <justin@fryewiles.com>, justin@booleangate.org
 * @author Andrew Murphy <andrew@fryewiles.com>
 * @version 2.0.1 20080317 JJ
 * @version 2.0.0 20080316 JJ
 * @version 1.0.0 20071212 AM
 */


var elLargeViewer      =
	elLargeViewerPrint =
	elPhotoTitle       =
	elPhotoDesc        = null,
	imageData          =
	keyIndexes         = [],
	photoIndex         = 0;

var display_large_from_thumb_url = function(id) {
	elPhotoTitle.innerHTML = imageData[id].title;
	elPhotoDesc.innerHTML  = imageData[id].desc;

	url = imageData[id].img.parentNode.style.backgroundImage.replace(/id=/, 'idl=');
	elLargeViewer.style.backgroundImage = url;

	url = url.substr(4, url.length-5);
	elLargeViewerPrint.src = url;

	photoIndex = keyIndexes.indexOf(id);
	
	return false;
}


var display_next_large_photo = function(el) {
	if ( el && el.blur ) {
		el.blur();
	}

	if ( imageData && imageData.length  && imageData.length > 0 ) {
		photoIndex = (photoIndex + 1) % imageData.length;
	}
	
	return display_large_from_thumb_url( keyIndexes[photoIndex] );
}

var display_prev_large_photo = function(el) {
	if ( el && el.blur ) {
		el.blur();
	}

	if ( imageData && imageData.length  && imageData.length > 0 ) {
		photoIndex = (photoIndex == 0 ? imageData.length : photoIndex) - 1;
	}
	
	return display_large_from_thumb_url( keyIndexes[photoIndex] );
}

var ol = window.onload;
window.onload = function() {
	if ( ol ) ol();

	var nodes = $$('.right-column ul li'), t = {}, id

	for ( var i=0; i<nodes.length; ++i ) {
		id = nodes[i].id ;
		t.img   = nodes[i].getElementsByTagName('a')[0];
		t.title = $(id+'-title').innerHTML;
		t.desc  = $(id+'-description').innerHTML;

		keyIndexes.push(nodes[i].id);
		imageData[id] = t;
		t = {};
	}

	elLargeViewer      = $('current-image');
	elLargeViewerPrint = $('image-viewer-print');
	elPhotoTitle       = $('photo-title');
	elPhotoDesc        = $('photo-description');
}
