var PLABO_PIC_DIR	= "http://upper.under.jp/game/plabo/pic";
var PLABO_IS_ALT	= false;

var x16 = new Array();
x16["0"] = 0;x16["1"] = 1;x16["2"] = 2;x16["3"] = 3;
x16["4"] = 4;x16["5"] = 5;x16["6"] = 6;x16["7"] = 7;
x16["8"] = 8;x16["9"] = 9;x16["a"] = 10;x16["b"] = 11;
x16["c"] = 12;x16["d"] = 13;x16["e"] = 14;x16["f"] = 15;
var xf = new Array("0","1","2","3","4","5","6","7"
				  ,"8","9","a","b","c","d","e","f");

//色クラス
function COLOR(r,g,b){
	this.r = r;
	this.g = g;
	this.b = b;
}
//文字列から色を取得
//ついでに、使用可能な色かどうかのチェック
function getColor(color){
	if(color.length != 6){
		var c = new COLOR(0,0,0);
		return c;
	}
	var flag = false;
	for(var i=0;i<6;i++){
		if(!color.charAt(i).match(/[0-9]|[a-f]/))	flag = true;
	}
	if(flag){
		var c = new COLOR(255,0,0);
		return c;
	}
	//透明色"ff00ff"だったら少し改造
	if(color == "ff00ff"){
		color = "ff01ff";
	}
	
	var r = x16[color.charAt(0)]*16 + x16[color.charAt(1)];
	var g = x16[color.charAt(2)]*16 + x16[color.charAt(3)];
	var b = x16[color.charAt(4)]*16 + x16[color.charAt(5)];
	var c = new COLOR(r,g,b);
	
	return c;
}
//数字３つから色を文字列化
function getColor2(r,g,b){
	var r1 = xf[Math.floor(r/16)];
	var r2 = xf[r%16];
	var g1 = xf[Math.floor(g/16)];
	var g2 = xf[g%16];
	var b1 = xf[Math.floor(b/16)];
	var b2 = xf[b%16];
	
	return r1 + r2 + g1 + g2 + b1 + b2;
}

function drawRobo(head,body,c_c,
				leaf,l_data,l_c,
				flower,f_data,f_c,
				alt,userID){
	//色取得
	//胴体
	var c_color = new COLOR();
	c_color = getColor(c_c);
	//胴体の影
	var r_ = Math.round(c_color.r * 0.9 -16); if(r_ < 0) r_ = 0;
	var g_ = Math.round(c_color.g * 0.9 -16); if(g_ < 0) g_ = 0;
	var b_ = Math.round(c_color.b * 0.9 -16); if(b_ < 0) b_ = 0;
	//alt
	if(alt == undefined || !PLABO_IS_ALT){
		var alts = new Array("","");
	}
	else{
		var alts = alt.split(",");
	}
	//タグ
	var div_css = "test-align:center;width:68;" +
		"margin:5px 10px;padding:5px;border:3px dotted #" +c_c+ ";";
	var body_h_css = "filter:chroma(color=ff00ff);" +
		"position:absolute;top:136;left:10;";
	var LINKROOM = "<a target='_blank' href='http://upper.under.jp/" +
		"game/plabo/other/showroom.cgi?id=" + userID + "'>";
	var plabo_tag = "<div style='width:52;height:160;position:relative;'>" +
		"<a target='_blank' href='http://upper.under.jp/game/plabo/'>" +
		"<img src='http://upper.under.jp/game/plabo/pic/title.gif' " +
		"style='position:absolute;left:4;top:0;border:0' alt='Plabo!'></a>" +
		"<img alt='" +alts[0]+ 
		"' style='" + body_h_css + "' src='" + PLABO_PIC_DIR +
		"/body" + body + "l.gif' style='" +
		"background:rgb(" +c_color.r+ "," +c_color.g+ "," +c_color.b+ ")'>" +
		"<img alt='" +alts[0]+ 
		"' style='" + body_h_css + "' src='" + PLABO_PIC_DIR +
		"/body" + body + "r.gif' style='" +
		"background:rgb(" +r_+ "," +g_+ "," +b_+ ")'>" +
		LINKROOM +
		"<img alt='" +alts[0]+ 
		"' style='position:absolute;top:136;left:10;border:0;'" +
		" src='" + PLABO_PIC_DIR +
		"/body" + body + ".gif'></a>" +
		LINKROOM +
		"<img alt='" +alts[0]+ 
		"' style='position:absolute;top:110;left:10;border:0;'" +
		" src='" + PLABO_PIC_DIR +
		"/head" + head + ".gif'></a>";
	
	//葉っぱ
	var p_height = 120;	//プランターまでの高さ(上からの)
	
	if(leaf != undefined &&
		l_data != undefined &&
		l_c != undefined){
	if((leaf+"").length > 0 &&
		(l_data+"").length > 0 &&
		(l_c+"").length > 0){
		//色
		var l_color = new COLOR();
		l_color = getColor(l_c);
		//本体
		var leaves = l_data.split(",");
		for(var i=0;i<leaves.length;i++){
			var l_height = Math.round(16*leaves[i]);
			var l_width = l_height * 2;
			p_height -= l_height;
			plabo_tag += "<img alt='" +alts[1]+ 
				"' src='" + PLABO_PIC_DIR +
				"/green" + leaf + ".gif' style='" +
				"filter : chroma(color=ff00ff);" +
				"position : absolute;" +
				"background:rgb(" +l_color.r+ "," +l_color.g+ "," +l_color.b+ ");" +
				"top:" + p_height + ";" +
				"left:" + Math.floor(26-l_width/2) + ";" +
				"width:" + l_width + "'>";
		}
	}}
	//花
	if(flower != undefined &&
		f_data != undefined &&
		f_c != undefined){
	if((flower+"").length > 0 &&
		f_data > 0 &&
		(f_c+"").length > 0){
		//花の色
		var f_color = new COLOR();
		f_color = getColor(f_c);
		
		var f_size = Math.floor(32*f_data);
		if(32 < f_size) f_size = 32;
		plabo_tag += "<img alt='" +alts[1]+ 
			"' src='" + PLABO_PIC_DIR +
			"/flower" + flower + ".gif' style='" +
			"filter : chroma(color=ff00ff);" +
			"position : absolute;" +
			"background:rgb(" +f_color.r+ "," +f_color.g+ "," +f_color.b+ ");" +
			"top:" + (p_height-f_size) + ";" +
			"left:" + Math.floor(26-f_size/2) + ";" +
			"width:" + f_size + "'>";
	}}
	
	plabo_tag += "</div>";
	
	document.write("<div ID='plabo_ret' style='" +div_css+ "'></div><br>\n");
	
	document.all.plabo_ret.innerHTML = plabo_tag;
	return "";
}
