//hello.js

//var viable = [4,5,8,9,10,11,14,15,16,17,20,21,22,23,26,27,28,29,32,33];
var loc = Math.floor(Math.random()*35)+1;

var oldWorld = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var newWorld = new Array(40);

var saucer = -10000;
var helloR = false;

function init() {
	setTimeout("genesis()", 1500);
	setTimeout("message()", 5500);
	var listener = setInterval("listen();", 4000);	
	
}
Event.onReady(init);


function genesis() {
	var dotmenu = $('dotmenu');
	new Effect.Opacity(dotmenu, {
		to:0,
		duration:0,
		afterFinish: function () {
			$('dotmenu').style.display = "block";
		}
	});
  	for (var i=1;i<=36;i++){
		$('w'+i).style.color="#000";
		$('w'+i).style.backgroundColor="#fff";  //153595
		$('w'+i).innerHTML = 'world';
			new Effect.Morph($('w'+i), {
			style: 'color: rgb(25,25,25); background-color:#000;',
			duration: 2 + (i/25)
		});		
  	}
}


function message(){
	$("message").style.display="block";
}


function listen(){
	new Ajax.Request("/hello/hello.php?loc=" + loc, {
		method: 'get',
		onSuccess: function(transport) {
		  	oldWorld = newWorld;
			newWorld = transport.responseText.evalJSON();
		  	for (var i=1;i<= newWorld.length;i++){
		  		if(!(oldWorld[i]==newWorld[i])){
		  			var campfire = $('w'+i);
		  			if(newWorld[i]==9999){
		  				campfire.innerHTML = 'hello';
		  				campfire.title = 'You are here';		  					
	  					Event.observe(campfire, 'click', hello);
						campfire.style.cursor="pointer";
		  				new Effect.Morph(campfire, {
							style: 'color: #fff',
							duration: 2.5
						});
	  					campfire.style.cursor="pointer";
	  					
		  			} else if (newWorld[i]==0){
		  				campfire.title = '';
		  				campfire.innerHTML = 'world';
		  				new Effect.Morph($('w'+i), {
							style: 'color: rgb(25,25,25)',
							duration: 1.0
						});
		  			} else {
		  				campfire.innerHTML = 'hello';
		  				campfire.title = newWorld[i] + ' seconds ago';
		  				var r = 200 - Math.floor(newWorld[i]/2);
						if(r < 16){ r = 16; }
		  				new Effect.Morph(campfire, {
							style: 'color: rgb('+r+','+r+','+r+')',
							duration: 3.0
						});
		  			}
		  		}
		  	}
		}
	});
}


function twinkle(){
	var randNum=parseInt(Math.random()*50);
	var randCol=50+parseInt(Math.random()*25);
	var newColor = 'rgb('+randCol+','+randCol+','+randCol+')';	
	if($('star'+randNum)){
		$('star'+randNum).style.color = newColor;
	}

	saucer+=5;
	if(saucer>-40){
		$('flyingsaucer').style.left = saucer;
	}	
}


function hello(){
	var dotmenu = $('dotmenu');
	var thisHello = $('w' + loc);
	if(!(helloR)){
		thisHello.style.color='#fff';
		thisHello.style.backgroundColor = '#101010';
		new Effect.Opacity(dotmenu, {
			duration:0.8,
			from:0,
			to:1.0
		});
		helloR = true;
	} else {
		$('w' + loc).style.color='#fff';
		thisHello.style.backgroundColor = '#000';
		new Effect.Opacity(dotmenu, {
			duration:0.6,
			from:1.0,
			to:0
		});
		helloR = false;
	}
}