var rd = Math.floor( Math.sqrt(2) * r ); 
var mouseX=0;
var mouseY=0;

document.onmousemove = function(e){
	try { mouseX = e.pageX; mouseY = e.pageY; }
	catch(ex){ mouseX = window.event.clientX + document.body.scrollLeft; mouseY = window.event.clientY + document.body.scrollTop; }
}

window.onload = function(){
	var tribecp = dhtmlXColorPickerInput('cp_tribe');
   tribecp.setImagePath("../include/js/dhtmlxColorPicker/imgs/");
   tribecp.setColor("#B40000");
   tribecp.init();

   
   var playcp = dhtmlXColorPickerInput('cp_play');
   playcp.setImagePath("../include/js/dhtmlxColorPicker/imgs/");
   playcp.setColor("#B40000");
   playcp.init();
}

function Tip(el,player,x,y,name,points,ally,totpoints){
	
	if(document.getElementById("popup").style.display=="none")
	{
	
		if( player=="")
			player = "Villaggio Abbandonato"
	
		var map = document.getElementById("map");	
	
		document.getElementById("vil").innerHTML = unescape(name).replace(/\+/g," ") + " ("+x+"|"+y+")";
		document.getElementById("pla").innerHTML = player;
		document.getElementById("trb").innerHTML = unescape(ally).replace(/\+/g," ");
		document.getElementById("pnt").innerHTML = points;
		if(totpoints!="")
			document.getElementById("pnt").innerHTML += "/" + totpoints;
		document.getElementById("popup").style.display = "";
		document.getElementById("popup").style.top = ( mouseY + 5 ) + "px";
		document.getElementById("popup").style.left = ( mouseX + 5 ) + "px";
	}
}

function unTip(){
	document.getElementById("popup").style.display = "none";
}

function shift(type){
	var ff = document.forms['coord'];
	var vil = ff.v.value;
	vil = vil.substring(1,vil.length-1);
	vilxy = vil.split("|");
	
	switch(type) {  
	  case 'nw':
	  ff.v.value = "("+ (parseInt(vilxy[0])-rd) + "|" + (parseInt(vilxy[1])+rd) + ")";
	  break;
	  case 'n':
	  ff.v.value = "("+ parseInt(vilxy[0]) + "|" + (parseInt(vilxy[1])+r) + ")";
	  break;
	  case 'ne':
	  ff.v.value = "("+ (parseInt(vilxy[0])+rd) + "|" + (parseInt(vilxy[1])+rd) + ")";
	  break;
	  case 'w':
	  ff.v.value = "("+ (parseInt(vilxy[0])-r) + "|" + parseInt(vilxy[1]) + ")";
	  break;
	  case 'e':
	  ff.v.value = "("+ (parseInt(vilxy[0])+r) + "|" + parseInt(vilxy[1]) + ")";
	  break;
	  case 'sw':
	  ff.v.value = "("+ (parseInt(vilxy[0])-rd) + "|" + (parseInt(vilxy[1])-rd) + ")";
	  break;
	  case 's':
	  ff.v.value = "("+ parseInt(vilxy[0]) + "|" + (parseInt(vilxy[1])-r) + ")";
	  break;
	  case 'sw':
	  ff.v.value = "("+ (parseInt(vilxy[0])+rd) + "|" + (parseInt(vilxy[1])-rd) + ")";
	  break;
	}
	ff.submit();	
}

function del(id){
	var par = document.getElementById(id).parentNode;
	par.removeChild( document.getElementById(id) );
	checkColors();
}

function addColor(el,mod){
	
	if(mod=='play'){
		var id = "?";
		var name = document.getElementById(mod).value;
	}
	else{
		var id = document.getElementById(mod).value;
		var name = document.getElementById(mod).options[document.getElementById('tribe').selectedIndex].innerHTML;
	}
	var col = document.getElementById('cp_'+mod).value;

	if(col.indexOf("#")==-1){
		alert("Devi prima scegliere un colore!");
		return;
	}
	
	if(name==""){
		alert("Devi prima inserire il nome di un gicatore!");
		return;
	}
	addItem(mod,id,col,name);
}

function addItem(mod,id,col,name){
	var par = document.getElementById('add_'+mod);

	var d = new Date()
	var trid = id+"_"+d.getMilliseconds();
	
	var tr = document.createElement('tr');
	tr.id = trid;
	var td1 = document.createElement('td');
	td1.style.border="1px solid gray";
	td1.style.backgroundColor=col
	td1.innerHTML = "<img src='../images/nodot.gif'>";	
	var td2 = document.createElement('td');
	td2.innerHTML = "<b>"+name+"</b>";	

	var td3 = document.createElement('td');
	td3.style.width="22px";
	var arem = document.createElement('a');
	arem.href="javascript:del('"+trid+"')";
	var rem = document.createElement('img');
	rem.src="../images/rem.gif";
	rem.alt="del";
	rem.title="remove this setting";
	rem.style.border=0;
	arem.appendChild(rem);
	
	var inp = document.createElement('input');
	inp.type = "hidden";
	inp.value = id+","+col+","+name;

	td3.appendChild(arem);
	td3.appendChild(inp);
	tr.appendChild(td3);
	tr.appendChild(td1);
	tr.appendChild(td2);
	par.appendChild(tr);
	
	checkColors();	
	
}

function checkColors(){
	
	var par2 = document.getElementById('add_tribe');
	var ins = par2.getElementsByTagName('input');
	var coltxt="";
	for(var i=0;i<ins.length;i++){
		coltxt += ins[i].value + ";";
	}
	document.forms['coord'].tribecolors.value = coltxt;
	
	var par1 = document.getElementById('add_play');
	var ins = par1.getElementsByTagName('input');
	var coltxt="";
	for(var i=0;i<ins.length;i++){
		coltxt += ins[i].value + ";";
	}
	document.forms['coord'].playcolors.value = coltxt;
}

