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

function get_random_image(){
	images[0] = '<img src="gs/toaster_deg_2.gif" />';
	images[1] = '<img src="gs/toast.gif" height="25" width="25"/>';
	random_image = images[Math.floor(Math.random() + .4)];
	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(){
	if(screensaver_on == true){
		mouseMoveWatcher = null;
		mouseClickWatcher = $("#screensaver").click(function(e){
			hide_ss();
		})
		$('#screensaver').show();
		ss_on = true;
		clearInterval(loopInterval);
		loopInterval = setInterval("ss_loop()", 100);
	}
}

function hide_ss(){
	ss_initialize();
	for(var i = 0; i < 10; i++){
		ss_image_array[i] = null;
	}
	mouseClickWatcher = null;
	$('#screensaver').hide();
	$("#screensaver").html("<img src='gs/close_ss.gif' class='close_img'/>");
}

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(2000 * 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).offset().left - speed;
				var new_y = $("#ssi_"+i).offset().top + speed;
				if(new_x < -50 || new_y > $(document).width + 50){
					new_x = Math.ceil(2000 * Math.random());
					new_y = (-1 * Math.ceil(100 * 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 > 90){
			show_ss();
		}
	}
}


