
var pixel_number=0;

function getMousePosition(e) {
  var _x
  var _y
  if (!isIE) {
    _x = e.pageX
    _y = e.pageY
  }
  if (isIE) {
    _x = event.clientX + document.body.scrollLeft
    _y = event.clientY + document.body.scrollTop
  }

	//galaxy.mouse_movement(_x,_y)

  return true;
}


function star(initial_x, initial_y, this_number){

	function draw(){
		if(this.size > 0){
			this.pixels[0].move_to(this.x+0, this.y+0)
		}
		if(this.size > 1){
			this.pixels[1].move_to(this.x+1, this.y+0)
			this.pixels[2].move_to(this.x+0, this.y+1)
			if(this.size == 2)
				this.pixels[3].move_to(this.x+1, this.y+1)
			if(this.size == 3)
				this.pixels[3].move_to(this.x+0, this.y-1)
		}
		if(this.size > 2){
			this.pixels[4].move_to(this.x-1, this.y+0)
		}
	}
	function nudge(){
		this.vx += Math.random()*16-8;
		this.vy += Math.random()*16-8;
	}
	function update_state_helper(){
		this.x += this.vx
		this.y += this.vy
		distance_x = this.target_x-this.x
		distance_y = this.target_y-this.y
		distance = Math.sqrt(Math.pow(distance_x, 2) + Math.pow(distance_y, 2))
		force = distance*0.02
		this.vx += force * distance_x / distance
		this.vy += force * distance_y / distance
		this.vx *= this.friction_factor;
		this.vy *= this.friction_factor;
		this.draw()
//		if(Math.sqrt(Math.pow(this.vx, 2) + Math.pow(this.vy, 2)) > 0.1)
		if(this.update_countdown > 0){
			this.update_countdown = this.update_countdown-1;
			function_string = "stars["+this.number+"].update_state_helper()";
			setTimeout(function_string, 20)
		}
		
	}
	function update_state(i){
		if(this.update_countdown == 0){
			this.update_countdown = i;
			this.update_state_helper();
		}
		else{
			this.update_countdown = i;
		}
	}

	// I think friction_factor should have range [0.5,0.92]
	this.friction_factor = 0.22*Math.random()+0.6;
	size = Math.floor(4*Math.random())+1;
	switch(size){
		case 1:
			size = 1;
			num_pixels = 1;
			break;
		case 2:
			size = 1;
			num_pixels = 1;
			break;
		case 3:
			size = 2;
			num_pixels = 4;
			break;
		case 4:
			size = 3;
			num_pixels = 5;
			break;
		default:
			//alert("where did that size come from?");
			break;
	}
	this.size = size;
	this.number = this_number;
	this.x = initial_x
	this.y = initial_y
	this.target_x = 100
	this.target_y = 100
	this.vx = 0
	this.vy = 0
	this.update_countdown = 0;

	this.pixels = []
	for(i_pixel=0; i_pixel<num_pixels; i_pixel++){
		this.pixels[i_pixel] = new pixel(this.x, this.y, "#BF9F6F")
	}

	// function declarations
	this.draw = draw
	this.nudge = nudge
	this.update_state = update_state
	this.update_state_helper = update_state_helper

}

function galaxy(min_x, min_y, max_x, max_y, num_stars){

	stars = []
	
	this.min_x = min_x
	this.min_y = min_y
	this.max_x = max_x
	this.max_y = max_y
	
	function create_stars(){
		for(i_star=0; i_star<num_stars; i_star++){
			x = Math.random()*(this.max_x-this.min_x)+this.min_x
			y = Math.random()*(this.max_y-this.min_y)+this.min_y
			//document.write("x="+x+", y="+y+"<br />")
			stars[i_star] = new star(x,y,i_star);
			stars[i_star].target_x = x;
			stars[i_star].target_y = y;
			//stars[i_star].x = x;
			//stars[i_star].y = y;
			//stars[i_star].nudge();
			//stars[i_star].update_state(150);
			stars[i_star].draw();
		}
	}
	
	function shuffle(shuffle_all){
		if(shuffle_all){
			fraction_to_shuffle = 1;
		}
		else{
			fraction_to_shuffle = Math.random()*0.5;
		}
		for(i_star=0; i_star<num_stars; i_star++){
			rand = Math.random();
			if(rand < fraction_to_shuffle){
				x = Math.random()*(this.max_x-this.min_x)+this.min_x
				y = Math.random()*(this.max_y-this.min_y)+this.min_y
				stars[i_star].nudge();
				stars[i_star].target_x = x;
				stars[i_star].target_y = y;
				stars[i_star].update_state(150);
			}
		}
	}
	
	function mouse_movement(the_x, the_y){
		//document.write()
		stars[0].target_x = the_x
		stars[0].target_y = the_y
		//stars[0].update_state(SOME NUMBER)
	}
	
	function swap(){
		star1_num = Math.floor(Math.random()*num_stars);
		do{
			star2_num = Math.floor(Math.random()*num_stars);
		}while(star2_num == star1_num)
		stars[star1_num].nudge();
		stars[star2_num].nudge();
		
		stars[star2_num].target_x = stars[star1_num].x
		stars[star2_num].target_y = stars[star1_num].y
		stars[star1_num].target_x = stars[star2_num].x
		stars[star1_num].target_y = stars[star2_num].y
		
		stars[star1_num].update_state(150);
		stars[star2_num].update_state(150);
	}
	
	function do_something(){
		rand = Math.random();
		if(rand < 0.9){
			this.swap();
		}
		else{
			this.shuffle(false);
		}
		setTimeout("galaxy.do_something()", 5000);
	}
	
	this.mouse_movement = mouse_movement;
	this.swap = swap;
	this.do_something = do_something;
	this.create_stars = create_stars;
	this.shuffle = shuffle;
	this.create_stars();
	
}


function pixel(new_x, new_y, new_color){
	this.x = new_x;
	this.y = new_y;
	this.color = new_color;
	this.number = pixel_number;
	pixel_number++;

	function get_draw_string(){
		return "<div id=\"pixel_"+this.number+"\" style=\"position: absolute; left: "+Math.round(this.x)+"px; top: "+Math.round(this.y)+"px; width: 1px; height: 1px; background-color: "+this.color+"; font-size:1pt; line-height:1pt; font-family:Verdana; font-weight:bold;\">|</div>";
	}
	function move_to(new_x, new_y){
		this.x = new_x;
		this.y = new_y;
		document.getElementById("pixel_"+this.number).style.left = ""+Math.round(this.x)+"px";
		document.getElementById("pixel_"+this.number).style.top = ""+Math.round(this.y)+"px";
	}
	this.get_draw_string = get_draw_string;
	this.move_to = move_to;
	document.getElementById("pixel_space").innerHTML += this.get_draw_string();
}


var isIE = document.all?true:false;
if (!isIE) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMousePosition;

galaxy = new galaxy(330,0,830,40,50);




//galaxy.do_something();

