var image = '<img src="gs/toaster_deg_2.gif" />';
var images = new Array();

function get_random_image(){
	images[0] = "";
	images[1] = '<img src="gs/head_1.gif" />';
	images[2] = '<img src="gs/head_2.gif"/>';
	images[3] = '<img src="gs/hamster.gif"/>';
	images[4] = '<img src="gs/dolphinswimgrn.gif"/>';
	images[5] = '<img src="gs/letters/s.gif"/>';
	images[6] = '<img src="gs/letters/b.gif"/>';
	random_image = images[Math.floor(Math.random() * images.length)];
	return random_image;
}

function make_image_div_with_id(id){
	var div = 	"<div id='"+id+"' class='ss_image'>"+
					get_random_image()+
				"</div>";
	return div;
}

var last_mousemove = 0;
var ss_on = false;
var loopInterval;

var mouseMoveWatcher;
var mouseClickWatcher;

var ss_image_array = [];

function ss_initialize(){
	ss_on = false;
	last_mousemove = 0;
	if(loopInterval != null){
		clearInterval(loopInterval);
	}
	loopInterval = setInterval("ss_loop()", 1000);
	ss_mouse_initialize();
}

function ss_mouse_initialize(){
	mouseMoveWatcher = $("body").mousemove(function(e){
		if(e.pageX < 15 && e.pageY < 15){
			show_ss();
		} else {
			last_mousemove = 0;
		}
	});
}

function show_ss(){
	mouseMoveWatcher = null;
	mouseClickWatcher = $("#screensaver").click(function(e){
		hide_ss();
	})
	$("#computer_content").hide();
	$('#screensaver').show();
	ss_on = true;
	clearInterval(loopInterval);
	loopInterval = setInterval("ss_loop()", 100);
}

function hide_ss(){
	ss_initialize();
	ss_image_array = [];
	mouseClickWatcher = null;
	$('#screensaver').hide();
	$("#computer_content").show();
	$("#screensaver").html("");
}

function ss_loop(){
	if(ss_on == true){
		for(var i = 0; i < 20; i++){
			if(ss_image_array[i] == null){
				var start_x = Math.ceil(1000 * Math.random());
				var start_y = (-1 * Math.ceil(500 * Math.random())) + 250;
				$("#screensaver").append(make_image_div_with_id("ssi_"+i));
				ss_image_array[i] = $("#ssi_"+i);
				ss_image_array[i].css("left", start_x+"px");
				ss_image_array[i].css("top", start_y+"px");
			} else {
				var speed = i * .5 + 2;
				var new_x = $("#ssi_"+i).position().left - speed;
				var new_y = $("#ssi_"+i).position().top + speed;
				if(new_x < -50 || new_y > 600){
					new_x = Math.ceil(1000 * Math.random());
					new_y = (-1 * Math.ceil(500 * Math.random()));
				}
				ss_image_array[i].css("left", new_x+"px");
				ss_image_array[i].css("top", new_y+"px");
			}
		}
	} else {
		last_mousemove++;
		if(last_mousemove > 25){
			show_ss();
		}
	}
}

