// this file contains javascript functions concerned with changing the 
// image displayed by the screen area at the top right of the page

var currentTVrotation;
var rotation_index;
var rotation_timer = 0;
var rotation_images;


function setTV(filename, opacity)
{
	if (document.body)
	{
		var obj = document.getElementById("screen_image");
		
		if (filename != "") {
			obj.style.backgroundImage = "url('tv/"+filename+".jpg')";
		}
		obj.style.opacity = opacity;
	}
}



function rotation_timer_callback()
{
	var next_index = (rotation_index + 1) % currentTVrotation.length;

	// check if the next image has finished preloading
	if (rotation_images[next_index].complete || rotation_images[next_index].complete==null)
	{
		rotation_index = next_index;
		next_index = (rotation_index + 1) % currentTVrotation.length;
	
		setTV(currentTVrotation[rotation_index], 0.1);
		setTimeout("setTV('',1.0)" , 300);
	
	
	
	
		if (next_index >= rotation_images.length)
		{
			rotation_images.push(new Image);
			rotation_images[next_index].src = "tv/"+currentTVrotation[next_index]+".jpg";
		}
	}
}


// provide a list of images which the TV screen will show in sequence, perhaps with static and line-sync effects
function setTVRotation(filename)
{
	if (arguments.length > 0)
	{
		currentTVrotation = new Array();
		rotation_images = new Array();
		
		for (var i=0; i < arguments.length; i++)
		{
			currentTVrotation.push(arguments[i]);
		}

		if (!rotation_timer)
		{
			rotation_timer = setInterval( "rotation_timer_callback()", 4000);
		}

		rotation_index = 0;
		
		setTV(currentTVrotation[0], 1.0);
		rotation_images.push(new Image);
		rotation_images[0].src = "tv/"+currentTVrotation[0]+".jpg";
		rotation_images.push(new Image);
		rotation_images[1].src = "tv/"+currentTVrotation[1]+".jpg";
		
	}
}

// specify a single image to be shown until further notice
function setTVImage(filename)
{
	if (rotation_timer)
	{
		clearInterval(rotation_timer);
		rotation_timer = 0;
	}

	setTV(filename, 1.0);
}

// futher notice at the end of setTVImage, go back to rotating images
function TVResume()
{
	if (currentTVrotation.length > 0)
	{
		if (!rotation_timer)
		{
			rotation_timer = setInterval( "rotation_timer_callback()", 4000);
		}		
		setTV(currentTVrotation[rotation_index], 1.0);	
	}
}
