var images = new Array();
var thumbs = new Array();
var imageIndicator = "(image)";

//the width of each thumbnail
var thumbWidth = 92;

//the width of the portal to the thumbnails
var captionWidth = 410;

//the index of the currently selected image
var current = 0;

//whether the automatic scrolling is paused or not
var paused = false;
var toload = 0;

var sliderMinLeft = 0;
var sliderMaxLeft = 0;
var initialSliderContent = '';
var eternalMoveDelay='5000';

var item, thumb;

function photoDiv(i) {

	parts = images[i].split('|');
	item = parts[0];
	thumb = parts[1];

	onclickstuff = 'pause(); quickRouteTo(' + i + ');';
	div = '<div class="thumb" id="t' + i + '">';
	if( item.substring(0,imageIndicator.length) == imageIndicator ) {
		imagecode = '<img width="90" height="60" src="' + thumb + '" onclick="' + onclickstuff + '"/>';
		div += imagecode;
	} else {
		div +='<img width="90" height="60" src="textthumb.php?text=' + thumb + '" alt="text" onclick="' + onclickstuff + '"/>';
	}
	div += '</div>';
	//alert( div );
	return div;
}

function fullPhoto(i) {

	parts = images[i].split('|');
	item = parts[0];
	thumb = parts[1];

	if( item.substring(0,imageIndicator.length) == imageIndicator ) {
		div = '<img src="' + item.substring(imageIndicator.length ) + '"/>';
	} else {
		div = '<div class="vert1"><div class="vert2"><div class="vert3">' + item + '</div></div></div>';
	}
	return div;
}


//get the bar ready initially
function gallery_init( showfile ) {

	//create the slide array
	images = showfile.split(':');

	//	document.write('<div style="margin-top: 700px; color: black; background-color: white;">');
	//	for ( i=0; i<images.length; i++ ){
	//		document.write(images[i] + '<br/>');
	//	}
	//	document.write('</div>');

	//put the slider in the right place (0th slide starts as current)
	moveXY('caption-slider', (2 - images.length)*thumbWidth , 0);
//	moveXY('caption-slider', 2*thumbWidth - images.length*thumbWidth - 1 , 0);

	//make sure it cant move too far
	sliderMinLeft = 0- images.length * thumbWidth * 2 + captionWidth/2;
	sliderMaxLeft = 0 - images.length * thumbWidth + captionWidth/2;

	//create the slider, with three copies of the thumbnails
	sliderContent = '<nobr>';
	for ( j=0; j<3; j++ ) {
		for( i=0; i<images.length; i++ ) {
			sliderContent += photoDiv( i );
		}
	}
	sliderContent += '</nobr>';
	initialSliderContent = sliderContent;

	//preload images
/*	for ( i=0; i<images.length; i++ ) if( images[i].substring(0,imageIndicator.length)  == imageIndicator ) {
		parts = images[i].split('|');
		image = parts[0].substring(imageIndicator.length);
		thumb = parts[1];
		//document.getElementById( 'holder' ).innerHTML += '<img src="' + image + '" alt="" onload="ifLoadedStart();"/>'; toload++;
		document.getElementById( 'holder' ).innerHTML += '<img src="' + thumb + '" alt="" onload="ifLoadedStart();"/>'; toload++;
	}*/
	render_gallery();
}

function render_gallery() {
	//set the current image
	changeCurrentImage( 0 );
	document.getElementById( 'caption-slider' ).innerHTML = initialSliderContent;
	eternalMove ( eternalMoveDelay );
}

function ifLoadedStart() {
	toload--;
	if ( toload <= 0 ) {
		render_gallery();
	}
}



function left( noPlaces ) {
	smoothMoveByX( 'caption-slider', thumbWidth*noPlaces, 0, .1, 2, 'readySliderForMoveBy(deltaX);');
	changeCurrentImage( putInScope(current-noPlaces) );
}

function right( noPlaces ) {
	smoothMoveByX( 'caption-slider', 0-thumbWidth*noPlaces, 0, .1, 2, 'readySliderForMoveBy(deltaX);');
	changeCurrentImage( putInScope(current+noPlaces) );
}

function changeCurrentImage( newCurrent ) {
	current = newCurrent;
	document.getElementById('content').innerHTML = fullPhoto(current);
}


function quickRouteTo( imageNo ) {

	//debug('current: ' + current + ' imageNo: '+ imageNo );

	var a;
	for( var l=0; putInScope(current - l) != imageNo; l++ ) a++;
	for( var r=0; putInScope(current + r) != imageNo; r++ ) a++;

	//debuga( 'left: '+l+'<br/>right: '+r);

	if ( l == 0 ) {
		return;
	} else if ( l < r ) {
		left( l );
	} else {
		right( r );
	}


}


// wraps image numbers around
function putInScope ( imageNo ) {
	imageNo %= images.length;
	while ( imageNo < 0 ) imageNo += images.length;
	return imageNo;
}

//gets ready to have the slider moved
function readySliderForMoveBy( delta ) {

	//figure out the would-be new position for the slider
	x = xOf( 'caption-slider' ) + delta;

	//make sure we dont go off the edge
	while ( x < sliderMinLeft ) {
		moveXY( 'caption-slider', xOf('caption-slider') + images.length*thumbWidth, yOf('caption-slider') );
		//recalculate x
		x = xOf( 'caption-slider' ) + delta;
	}


	//make sure we dont go off the edge
	while ( x > sliderMaxLeft ) {
		moveXY( 'caption-slider', xOf('caption-slider') - images.length*thumbWidth, yOf('caption-slider') );
		//recalculate x
		x = xOf( 'caption-slider' ) + delta;
	}
}

function pause() {
	paused = true;
}

function eternalMove( delay ) {
	var timeout = 'if( !paused ) {right(1); eternalMove('+ delay + ');}';
	setTimeout(timeout,delay);

}

/*
function currentToContent() {

//	document.getElementById('content').firstChild.style.width = 538;//document.getElementById('content').style.width;
//	document.getElementById('caption-slider').childNodes[4].getAttribute('class') = 'current';
}



function debug_slider() {
	document.getElementById('content').innerHTML = 'left: ' + document.getElementById('caption-slider').style.left+'<br/>'+(document.getElementById('caption-slider').innerHTML.replace(/</g, '&lt;')).replace(/>/g,'&gt;');
}

*/



