// JavaScript Document
function updateBuilding() {

	var str = document.getElementById('bldgtext').value.toString().toLowerCase();
	var floors=str.split(" ");
	outputbldg= "";
	for (var flr in floors){
		//alert(output);
		outputbldg += "<div class=\"level\">";
		var totalwidth=0;		
		for (var i=0; i < floors[flr].length; i++){			
			totalwidth += Math.abs( Number(ascii_value( floors[flr].charAt(i) )) - 96);
			//alert(totalwidth);
		}
		for (var i=0; i < floors[flr].length; i++){
			ascii = Number(ascii_value( floors[flr].charAt(i))) - 96;
			floorthis = Math.abs(ascii)*100/totalwidth-1;
			pct = floorthis;
			outputbldg += "<div class=\"window\" style=\"width:" + pct.toString() + "%;background-color:" + genHex() + ";\"></div>";
			//totalwidth += Math.max(0,(ascii_value( floors[flr].charAt(i) ) - 96));
			//alert(outputbldg);
		}		
		outputbldg += "</div>";
	}
	
	document.getElementById('facade').innerHTML = outputbldg;
}

function ascii_value (c)
{
	// restrict input to a single character
	c = c . charAt (0);

	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}

function genHex(){
	colors = new Array(14);
	colors[0]="#fefefe";
	colors[1]="#ffffff";
	colors[2]="#fafafa";
	colors[3]="#efefef";
	colors[4]="#efefea";
	colors[5]="#ffefef";
	colors[6]="transparent";
	colors[7]="transparent";
	
/*	colors[0]="0";
	colors[1]="1";
	colors[2]="2";
	colors[3]="3";
	colors[4]="4";
	colors[5]="5";
	colors[5]="6";
	colors[6]="7";
	colors[7]="8";
	colors[8]="9";
	colors[9]="a";
	colors[10]="b";
	colors[11]="c";
	colors[12]="d";
	colors[13]="e";
	colors[14]="f";
*/	
	digit = new Array(5);
	color="";
	c = colors[Math.round(Math.random()*8)];
	for (i=0;i<6;i++){
		//digit[i]=colors[Math.round(Math.random()*14)];
		color = color+c;
	}
	return c;
}