var timestamp= new Array();

window.onload = function(){
	setInterval("check()",1000);
}

function check() {
	PassAjaxResponseToFunction('getJson.php', 'refresh', null);
}


function refresh(response){
	var JSON = eval("({"+response+"})");
	for (var i in JSON.canvas) {
		if (JSON.canvas[i].timestamp != timestamp[i]) {
			timestamp[i] = JSON.canvas[i].timestamp;
			var id = JSON.canvas[i].id;
			var s = new String;
			var colori = new Array();
			s = JSON.canvas[i].ip;
			colori = s.split(".");
			//alert(colori[1]);
			document.getElementById(id).style.backgroundColor = "rgb("+colori[0]+","+colori[2]+","+colori[3]+")";
			document.getElementById(id).style.top = JSON.canvas[i].top;
			document.getElementById(id).style.left = JSON.canvas[i].left;
			
			//alert(id + ":" + JSON.canvas[i].top+ ":" + JSON.canvas[i].left);
		}		
	}
}

if  (document.getElementById){

(function(){

//Stop Opera selecting anything whilst dragging.
if (window.opera){
document.write("<input type='hidden' id='Q' value=' '>");
}

var n = 500;
var dragok = false;
var y,x,d,dy,dx;

function move(e){
if (!e) e = window.event;
 if (dragok){
  d.style.left = dx + e.clientX - x + "px";
  d.style.top  = dy + e.clientY - y + "px";
  return false;
 }
}

function down(e){
if (!e) e = window.event;
var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
if (temp.tagName != "HTML"|"BODY" && temp.className != "dragclass"){
 temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
 }
if (temp.className == "dragclass"){
	 if (window.opera){
	  document.getElementById("Q").focus();
	 }
	 dragok = true;
	 temp.style.zIndex = n++;
	 d = temp;
	 dx = parseInt(temp.style.left+0);
	 dy = parseInt(temp.style.top+0);
	 x = e.clientX;
	 y = e.clientY;
	 document.onmousemove = move;
	 return false;
	 }
}

function up(e){
if (!e) e = window.event;
var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
if (temp.tagName != "HTML"|"BODY" && temp.className != "dragclass"){
 temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
	}
dragok = false;
document.onmousemove = null;


id=temp.id;
var top=temp.style.top;
var left=temp.style.left;

PassAjaxResponseToFunction('putChange.php?id='+id+'&top='+top+'&left='+left, 'empty', null);


}

document.onmousedown = down;
document.onmouseup = up;

})();
}

function empty(response){
}



