var n_image_title = null, image_title;
var n_slide_container = null;
var nodes = [], n_current_index = 0, n_slide_index;
var intv, intv_delay = 1, px_hop = 50;

function gallery_init()
{
	var imgs = document.getElementById("slide_container").getElementsByTagName("IMG");
	var i, len = imgs.length;
	
	n_slide_container = document.getElementById("slide_container");
	
	for (i = 0; i < len; i++)
	{
		if ( imgs[i].className == "slide_node" )
		{
			imgs[i].style.left = "0px";
			nodes.push( imgs[i] );
		}
	}
	
	n_image_title = document.getElementById("archive_title");
	image_title = n_image_title.innerHTML;

	if ( (n_select = document.getElementById( nodes[0].getAttribute("id") +"_thumb") ) )
	{
		n_select.className = "thumb_slide_node_selected";
		arr = n_select.getAttribute("src").split("/");
		n_image_title.innerHTML = arr[arr.length - 1].substring(0, arr[arr.length - 1].length - 4);
	}
}

function prev_slide_index()
{
	if ( n_current_index == 0 )
		return( nodes.length - 1 );
	else
		return( n_current_index - 1 );
}

function next_slide_index()
{
	if ( n_current_index == nodes.length - 1 )
		return( 0 );
	else
		return( n_current_index + 1 );
}

function slide_node(n_index, hard_dir)
{
	if ( intv ) return;			// for rapid click
	
	// position node and set increment for slide
	
	px_hop = Math.abs(px_hop);
	
	if ( hard_dir )
	{
		if ( hard_dir == 2 )
		{
			px_hop = -(px_hop);
			nodes[n_index].style.left = n_slide_container.offsetWidth + 1 +"px";			// right of container
		} else
			nodes[n_index].style.left = -(nodes[n_index].offsetWidth + 1) +"px";			// left of container
	} else if ( n_index > n_current_index ) {
			px_hop = -(px_hop);
			nodes[n_index].style.left = n_slide_container.offsetWidth + 1 +"px";			// right of container
	} else if ( n_index < n_current_index )
		nodes[n_index].style.left = -(nodes[n_index].offsetWidth + 1) +"px";				// left of container
	
	nodes[n_index].style.visibility = "visible";
	
	n_slide_index = n_index;
	intv = setInterval(slide, intv_delay);
}

function slide()
{
	var left = parseInt( nodes[n_slide_index].style.left );
	
	if ( left != 0 )
	{
		left += px_hop;
		
		// make sure we don't slide too far
		if ( ( px_hop < 0 && left < 0 ) || ( px_hop > 0 && left > 0 ) )
			left = 0;
		
		nodes[n_slide_index].style.left = left +"px";
		nodes[n_current_index].style.left = (parseInt(nodes[n_current_index].style.left) + px_hop) +"px";
	} else {
		var n_select, arr = [];
		
		clearInterval(intv);
		intv = null;
		
		if ( (n_select = document.getElementById( nodes[n_current_index].getAttribute("id") +"_thumb") ) )
			n_select.className = "thumb_slide_node"
		
		if ( (n_select = document.getElementById( nodes[n_slide_index].getAttribute("id") +"_thumb") ) )
		{
			n_select.className = "thumb_slide_node_selected";
			arr = n_select.getAttribute("src").split("/");
			n_image_title.innerHTML = arr[arr.length - 1].substring(0, arr[arr.length - 1].length - 4);
		} else
			n_image_title.innerHTML = image_title;
		
		n_current_index = n_slide_index;
		n_slide_index = null;
	}
}

if ( window.attachEvent )
	window.attachEvent("onload", gallery_init);
else
	window.addEventListener("load", gallery_init, false);