width = 200;	//box width, in px
speed = 30;		//arbitrary figure. For instant refresh, change to 1000.

refactor = 0;
limit = 10;
noBoxes = 0;
cBox = 0;

pause = 5000;	//millisecond delay between movements

currentBox = null;
firstBox = null;
lastBox = null;

paused = 0;
allowMove = 1;

direction = 1;
RTL = 1;
LTR = 0;

thread = 0;
animating = 2;
animator = 0;
sem = 0;

help = 0;

maxSnips = 0;
posScroller = null;
posRefactor = 0;

function SnippetBox( snippetID )
{
	this.snippetID = snippetID;
	this.next = null;
	this.prev = null;
	
	this.writerName;
	this.writerID;
	this.rating;
	this.datetime;
	this.rowNumber;
	
	this.toString = function toString()
	{
		return "snippetBox" + snippetID;
	}
}

function initSnippets( totalSnippets, snippetIDs ) 
{	
	image1 = new Image();
	image2 = new Image(); 
	image3 = new Image(); 
	image4 = new Image(); 
	image5 = new Image(); 
	image6 = new Image(); 
	image7 = new Image();
	image8 = new Image();
	image9 = new Image();
	image10 = new Image();
	image11 = new Image();
	image12 = new Image();
	image13 = new Image();
	image14 = new Image();
	image15 = new Image();
	image16 = new Image();
	
	image1.src = '/images/RHS/icons/one.gif'; 	
	image2.src = '/images/RHS/icons/two.gif'; 	
	image3.src = '/images/RHS/icons/three.gif'; 	
	image4.src = '/images/RHS/icons/four.gif'; 	
	image5.src = '/images/RHS/icons/five.gif'; 	
	image6.src = '/images/RHS/icons/forward.gif'; 	
	image7.src = '/images/RHS/icons/forward_on.gif'; 	
	image8.src = '/images/RHS/icons/pause.gif'; 	
	image9.src = '/images/RHS/icons/pause_on.gif'; 	
	image10.src = '/images/RHS/icons/skipback.gif'; 	
	image11.src = '/images/RHS/icons/skipback_on.gif'; 	
	image12.src = '/images/RHS/icons/skipforward.gif'; 	
	image13.src = '/images/RHS/icons/skipforward_on.gif'; 	
	image14.src = '/images/RHS/icons/help.gif';
	image15.src = '/images/RHS/icons/increase_speed.gif';
	image16.src = '/images/RHS/icons/decrease_speed.gif';
	
	maxSnips = totalSnippets;
	
	posScroller = document.getElementById( "posScroller" );
	posRefactor = 210.0 / maxSnips;

	if( posRefactor == 0 )
		posRefactor = 0.2;
	posScroller.style.width = posRefactor + "px";
	
	for( i = 0; i < snippetIDs.length; i++ )
		addSnippet( snippetIDs[ i ] );
		
	posScroller.style.left = ( ( firstBox.rowNumber * posRefactor ) - ( posRefactor ) ) + "px";
	
	playForwards();
}

function addSnippet( snippetID )
{
	thisBox = new SnippetBox( snippetID );
	thisBox.prev = lastBox;
	
	initSnipData( thisBox );
	
	if( lastBox != null )
		lastBox.next = thisBox;
	
	lastBox = thisBox;
	
	if( firstBox == null )
	{
		firstBox = thisBox;
		noMoreBox = new SnippetBox( 0 );
		noMoreBox.next = firstBox;
		noMoreBox.prev = noMoreBox;
		firstBox.prev = noMoreBox;
		currentBox = firstBox;
	}
	
	lastBox.next = firstBox;
	
	noBoxes++;
}

function removeSnippetFront()
{
	document.getElementById( 'animbox' ).removeChild( document.getElementById( firstBox ) );
	temp = firstBox.prev;
	firstBox = firstBox.next;
	firstBox.prev = temp;
	lastBox.next = firstBox;
	
	noBoxes--;
}

function removeSnippetBack()
{
	document.getElementById( 'animbox' ).removeChild( document.getElementById( lastBox ) );
	lastBox = lastBox.prev;
	lastBox.next = firstBox;

	noBoxes--;
}

function initSnipData( snippetBox )
{
	snippetInfo = document.getElementById( 'snippetInfo' + thisBox.snippetID ).innerHTML;
	
	snippetBox.rating = snippetInfo.substring( 0, snippetInfo.indexOf( '#' ) );
	snippetInfo = snippetInfo.substring( snippetInfo.indexOf( '#' ) + 1 );
	snippetBox.writerID = snippetInfo.substring( 0, snippetInfo.indexOf( '#' ) );
	snippetInfo = snippetInfo.substring( snippetInfo.indexOf( '#' ) + 1 );
	snippetBox.writerName = snippetInfo.substring( 0, snippetInfo.indexOf( '#' ) );
	snippetInfo = snippetInfo.substring( snippetInfo.indexOf( '#' ) + 1 );
	snippetBox.datetime = snippetInfo.substring( 0, snippetInfo.indexOf( '#' ) );
	snippetBox.rowNumber = snippetInfo.substring( snippetInfo.indexOf( '#' ) + 1 );
}

function toggleHelp()
{
	if( waitForAnimation( "toggleHelp()" ) )
		return;
	
	if( help = !help )
	{
		document.getElementById( "animbox" ).style.overflowY = "auto";
		document.getElementById( currentBox ).style.display = 'none';
		document.getElementById( "helpBox" ).style.display = '';
	}
	else 
	{
		document.getElementById( "helpBox" ).style.display = 'none';
		document.getElementById( currentBox ).style.display = '';
		document.getElementById( 'animbox' ).scrollTop = 0

		playForwards();
	}
}

function setBoxes(boxCount) 
{
	noBoxes = boxCount;
}

function increaseSpeed()
{
	if( pause > 1000 )
	{
		pause -= 2000;
		
		updateSpeed();
	}
}

function decreaseSpeed()
{
	if( pause < 9000 )
	{
		pause += 2000;
		
		updateSpeed();
	}
}

function updateSpeed()
{
	switch( pause )
	{
		case 9000:
			changeImage( 'speedIndicator', '/images/RHS/icons/five.gif'  ); 
			break;
		case 7000:
			changeImage( 'speedIndicator', '/images/RHS/icons/four.gif' ); 
			break;
		case 5000:
			changeImage( 'speedIndicator', '/images/RHS/icons/three.gif'  ); 
			break;
		case 3000:
			changeImage( 'speedIndicator', '/images/RHS/icons/two.gif'  ); 
			break;
		case 1000:
			changeImage( 'speedIndicator', '/images/RHS/icons/one.gif'  ); 
			break;
	}
}

function playForwards()
{
	if( help )
		return;
		
	if( waitForAnimation( "playForwards()" ) )
		return;
		
	changeImage( 'forwardButton', '/images/RHS/icons/forward_on.gif'  ); 
	changeImage( 'pauseButton', '/images/RHS/icons/pause.gif'  ); 

	direction = RTL;
	
	paused = 0;

	prepareNext();
}

function playPause()
{
	if( help )
		return;
		
	if( paused == 0 )
	{
		if( waitForAnimation( "playPause()" ) )
			return;
		
		paused = 1;
	
		changeImage( 'pauseButton', '/images/RHS/icons/pause_on.gif'  ); 
		changeImage( 'forwardButton', '/images/RHS/icons/forward.gif'  ); 
		
		hold();	
	}
}

function skipBack()
{
	if( help )
		return;
		
	paused = 1;
	allowMove = 1;
	
	if( waitForAnimation( "skipBack()" ) )
		return;
	
	changeImage( 'pauseButton', '/images/RHS/icons/pause_on.gif'  ); 
	changeImage( 'forwardButton', '/images/RHS/icons/forward.gif'  ); 
	
	direction = LTR;
	
	prepareNext();
}

function skipForward()
{
	if( help )
		return;
		
	paused = 1;
	allowMove = 1;
	
	if( waitForAnimation( "skipForward()" ) )
		return;
	
	changeImage( 'pauseButton', '/images/RHS/icons/pause_on.gif'  ); 
	changeImage( 'forwardButton', '/images/RHS/icons/forward.gif'  ); 

	direction = RTL;
	
	prepareNext();
}

function clearAndCallBack( callBack )
{
	sem = 0;
	eval( callBack );
}

function waitForAnimation( callBack )
{	
	if( sem != 0 )
	{
		return 2;
	}
		
	if( animating == 1 ) 
	{
		sem = 1;
		thread = clearTimeout( thread );		
		thread = setTimeout( "clearAndCallBack( " + callBack + ")", 100 );

		return 1;
	}
	
	animator = clearTimeout( animator );
	
	return 0;
}

function getNextBox()
{
	saveCurrentBox();
	
	if( direction == RTL )
	{
		return currentBox.next;
	}
	
	cBox--;
	if( currentBox.prev == lastBox )
		cBox = noBoxes;
	return currentBox.prev;
}

function prepareNext()
{	
	if( paused )
		timeout = 0;
	else
		timeout = pause;
	
	if( direction == RTL )
	{
		cBox++;
		if( currentBox.next == firstBox )
			cBox = 0;
	}
	else
	{
		cBox--;
		if( currentBox.prev == lastBox )
			cBox = noBoxes;
	}
	
	if( direction == RTL && cBox % limit == 1 )		
		loadMoreSnippets();
	
	animating = 2;
	
	if( paused )
	{
		if( direction == RTL )
			changeImage( 'skipForwardButton', '/images/RHS/icons/skipforward_on.gif'  ); 
		else
			changeImage( 'skipBackButton', '/images/RHS/icons/skipback_on.gif'  ); 
	}

	animator = clearTimeout( animator );
	animator = setTimeout( "animate( " + width + ")", timeout );	
}

function saveCurrentBox()
{
	document.cookie = "LASTSNIPPET = " + currentBox.datetime + ";expires=1; path=/";
}

function animate( remainingWidth ) 
{
	if( !allowMove )
		return;

	first = 0;

	origBox = currentBox;
	newBox = getNextBox();
	
	animating = 1;

	if( direction == RTL )
		document.getElementById( newBox ).style.left = width + 'px';
	else
		document.getElementById( newBox ).style.left = -width + 'px';
	
	if( document.getElementById( newBox ).style.display != '' ) 
		document.getElementById( newBox ).style.display = '';
	
	if( document.getElementById( "animbox" ).style.overflowY != "hidden" )
	{
		document.getElementById( "animbox" ).style.overflowY = "hidden";
		document.getElementById( 'animbox' ).scrollTop = 0
	}
	
	posScroller.style.left = ( ( newBox.rowNumber * posRefactor ) - ( posRefactor ) ) + "px";
	
	cSpeed = 1 + remainingWidth / width * speed;
		
	start = remainingWidth - parseInt( cSpeed );
	
	if( cSpeed == 1 )
		start = 0;
	
	if( direction == RTL )
	{
		boxA = start - width;
		boxB = start;
	}
	else
	{
		boxA = width - start;
		boxB = 0 - start;
	}
	
	document.getElementById( origBox ).style.left = boxA + 'px';
	document.getElementById( newBox ).style.left = boxB + 'px';
	
	if( start > 0 )
		animator = setTimeout( "animate( " + start + ")", 5 );
	else 
	{	
		if( currentBox.prev != currentBox )
			document.getElementById( origBox ).style.display = 'none';
		
		currentBox = getNextBox();
		
		if( !paused )
			prepareNext();
		else
		{
			if( direction == RTL )
				changeImage( 'skipForwardButton', '/images/RHS/icons/skipforward.gif'  ); 
			else
				changeImage( 'skipBackButton', '/images/RHS/icons/skipback.gif'  ); 
		}
		
		fixHeight( newBox );
		
		animating = 0;
	}
}

function fixHeight( cid )
{
	if( document.getElementById( cid ).offsetHeight > 138 )
	{
		document.getElementById( "animbox" ).style.overflowY = "auto";
	}
	else if( document.getElementById( cid ).offsetHeight < 138 )
		document.getElementById( cid ).style.height = 138 + "px";
}

function hold()
{	
	if( waitForAnimation( "hold()" ) )
		return;

	changeImage( 'pauseButton', '/images/RHS/icons/pause_on.gif'  ); 
		
	allowMove = 0;	
}

function vote()
{	
	if( waitForAnimation( "vote()" ) )
		return;
	
	full = Math.floor( currentBox.rating / 2 );
	half = currentBox.rating % 2;
	
	voteBlock = document.getElementById( 'voteBlock' + currentBox.snippetID );
	
	if( voteBlock.innerHTML == '' )
	{
		voteBlock.innerHTML = 'written by <a href="/profile.cfm?uID=' + currentBox.writerID + '">' + currentBox.writerName + '</a><br /><br />';
		voteBlock.innerHTML += '<img src="/images/rate_this.gif" alt="Rating: " align="absbottom" /><br />'
		
		orig = '';
		
		eval( "stars" + currentBox.snippetID + "0 = new Array( 5 )" );
		
		for( i = 1; i <= 5; i++ )
		{
			if( i <= full )
				imgSrc = '/images/staronsel.gif';
			else if ( half != 0 && i == full + 1 )
				imgSrc = '/images/starhalf.gif';
			else
				imgSrc = '/images/staroffsel.gif';
			
			eval( "stars" + currentBox.snippetID + "0[ i ] = '" + imgSrc + "'" );
			
			voteBlock.innerHTML += '<span><img src="' + imgSrc + '" width="20" height="20" onmouseover="selectStars( ' + i + ', ' + currentBox.snippetID + '0 );" onmouseout="resetTimeout( ' + currentBox.snippetID + '0 );" onClick="doRating( 3, ' + currentBox.snippetID + ', ' + i + ',0 );" id="star' + currentBox.snippetID + '0' + i + '" alt="" align="absbottom" /></span>';
			
			orig += "'" + imgSrc + "', ";
		}
		
		loadResult( currentBox );
	}
	
	document.getElementById( 'vote' + currentBox.snippetID ).style.display = "";
	document.getElementById( 'animbox' ).scrollTop = 0;
}

function resume()
{
	if( help )
		return;
	
	if( waitForAnimation( "resume()" ) )
		return;
	
	animator = clearTimeout( animator );
	
	if( paused == 0 )
	{
		allowMove = 1;
		animator = setTimeout( "animate( " + width + ")", pause );
		
		changeImage( 'pauseButton', '/images/RHS/icons/pause.gif'  ); 
	}
	
	
	if( document.getElementById( 'vote' + currentBox.snippetID ) != null )
		document.getElementById( 'vote' + currentBox.snippetID ).style.display = "none";
}

function loadResult( currentBox )
{
	document.getElementById( 'result' + currentBox.snippetID ).innerHTML = '<div id="ratingNew' + currentBox.snippetID + '0" style="display:none;padding:18px 0 0 0;"><span style="color:#f20909;display:block;">Thank you for your rating,</span>Your vote has been counted and is reflected above.</div><div id="ratingOld' + currentBox.snippetID + '0" style="display:none;padding:18px 0 0 0;"><span style="color:#f20909;display:block;">You have already rated this,</span>Thank you for your support.</div>';
}

function loadMoreSnippets()
{
	fileload = '/custom/snippetLoader.cfm?lastdt=' + lastBox.datetime + '&direction=' + direction + '&id=' + new Date().getTime();

	ajaxManager( fileload );
}
