

window.onload = function(){
	
	init_gallery();
	
}

init_gallery = function(){
	var gallery = document.getElementById("gallery");
	if(gallery){
		document.getElementById("gallery-next").onclick = gallery_event;
		document.getElementById("gallery-prev").onclick = gallery_event;

		document.gallery = new Array();
		var galleryList = document.getElementById("gallery-list");
		for(i = 0 ; i<galleryList.childNodes.length;i++){
			if(galleryList.childNodes[i].tagName){
				document.gallery[document.gallery.length]= galleryList.childNodes[i];
			}
		}
	}
}

gallery_event = function (){
	
	var container = this.parentNode;
	
	var prev = container.childNodes[0];
	var left = container.childNodes[1];
	var right = container.childNodes[2];
	var next = container.childNodes[3];
	var list = container.childNodes[4];
	
	if(this.id == "gallery-next"){
		var left_index = parseInt(this.getAttribute("index"));
		var right_index = left_index + 1 >= document.gallery.length ? 0 : left_index + 1;
	}
	else{
		var right_index = parseInt(this.getAttribute("index"));
		var left_index = right_index -1 < 0 ?  document.gallery.length : right_index - 1;
	}
	
	var next_index = -1;
	var prev_index = -1;
	
	for(i = right_index + 1 ; i < document.gallery.length && next_index == -1 ; i++){
		if(document.gallery[i].tagName){
			next_index = i;
		}
	}
	if(next_index == -1){
		for(i = 0; i < left_index && next_index == -1; i++){
			if(document.gallery[i].tagName){
				next_index = i;
			}
		}
	}
	
	for(i = left_index - 1; i >= 0 && prev_index == -1 ; i--){
		if(document.gallery[i].tagName){
			prev_index = i;
		}
	}
	if(prev_index == -1){
		for(i = document.gallery.length - 1; i > right_index && prev_index == -1 ;i--){
			if(document.gallery[i].tagName){
				prev_index = i;
			}
		}
	}

	var next_image_index = -1;
	var prev_image_index = -1;
	
	for(i = right_index + 1 ; i < document.gallery.length && next_image_index == -1 ; i++){
		if(document.gallery[i].tagName.toLowerCase() == "img"){
			next_image_index = i;
		}
	}
	if(next_image_index == -1){
		for(i = 0; i < left_index && next_image_index == -1; i++){
			if(document.gallery[i].tagName.toLowerCase() == "img"){
				next_image_index = i;
			}
		}
	}
	
	for(i = left_index - 1; i >= 0 && prev_image_index == -1 ; i--){
		if(document.gallery[i].tagName.toLowerCase() == "img"){
			prev_image_index = i;
		}
	}
	if(prev_image_index == -1){
		for(i = document.gallery.length - 1; i > right_index && prev_image_index == -1 ;i--){
			if(document.gallery[i].tagName.toLowerCase() == "img"){
				prev_image_index = i;
			}
		}
	}
	
	gallery_switch(prev,  prev_image_index,  prev_index);
	gallery_switch(left,  left_index, null, true);
	gallery_switch(right, right_index, null, true);
	gallery_switch(next,  next_image_index,  next_index);

}

gallery_switch = function(destinationNode, sourceIndex, targetIndex, withCaption){
	destinationNode.setAttribute("index", targetIndex);
	sourceNode = document.gallery[sourceIndex];
	destinationNode.innerHTML = null;
	newNode = sourceNode.cloneNode(true);
	destinationNode.appendChild(newNode);
	
	if(withCaption && sourceNode.tagName.toLowerCase() == "img"){
		var caption = document.createElement("caption");
		caption.innerHTML = sourceNode.alt;
		destinationNode.appendChild(caption);
	}	
}