if (location.href.indexOf(".okcashbag.com") > -1) {
	document.domain = "okcashbag.com";
	var SERVICE = "okcashbag";
	var URL_ROOT = "http://avatar.nategame.okcashbag.com";
} else {
	document.domain = "nate.com";
	var SERVICE = "nate";
	var URL_ROOT = "http://avatar.game.nate.com";
}
var TRANS_URL = URL_ROOT+"/newtrans/transaction.cgi";
var DP_ICON = "http://img.nate.com/game/img/logo_nate_2009/n10001_nate_01.gif";

var AVATAR_WIDTH = 120;		// ¾Æ¹ÙÅ¸ °¡·Î »çÀÌÁî
var AVATAR_HEIGHT = 140;	// ¾Æ¹ÙÅ¸ ¼¼·Î »çÀÌÁî
var MAX_ZORDER = 130;		// ¾Æ¹ÙÅ¸ ·¹ÀÌ¾î ÃÑ ¼ö
var IMG_ROOT = "http://img.nate.com/game/img/avatar/images";	// ¾Æ¹ÙÅ¸ ÀÌ¹ÌÁö ¼­¹ö

var Z_BACKGROUND=0;			// ¹é±×¶ó¿îµå ·¹ÀÌ¾î
var Z_PANTS=20;				// ÇÏÀÇ ·¹ÀÌ¾î
var Z_COAT=30;				// »óÀÇ ·¹ÀÌ¾î
var Z_ONEPIECE=40;			// ÇÑ¹ú ·¹ÀÌ¾î
var Z_HEAD=50;				// ¸Ó¸® ·¹ÀÌ¾î
var Z_HAIR_BACK=15;			// µÞ¸Ó¸® ·¹ÀÌ¾î
var Z_THEME=70;				// Å×¸¶ ·¹ÀÌ¾î

function item_image (code, x_pos, y_pos, zindex) {
	var op = new Array (2);

	op = code.split('-');
	var path = "http://item.avatar.game.nate.com/"+hex(op[0]%256)+"/item";
	if (x_pos==null) return "<img src='"+path+code+".gif' border=0>";
	return "<img src='"+path+code+".gif' border=0 style='position:absolute; left:"+x_pos+"px; top:"+y_pos+"px; z-index:"+zindex+"'>";
}

function view_image (code) {
	var op = new Array (2);

	op = code.split('-');
	var path = "http://item.avatar.game.nate.com/"+hex(op[0]%256)+"/view";
	return "<img src='"+path+code+".gif' width=81 height=81 border=0 style='left:0px; top:0px; position:absolute; z-index:2'>";
}

function head_image (code) {
	var op = new Array (2);

	op = code.split('-');
	var path = "http://item.avatar.game.nate.com/"+hex(op[0]%256)+"/head";
	return "<img src='"+path+code+".gif' border=0>";
}


// ÀÏ¹Ý ÇÔ¼ö

function hex(number) {
	var h = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');

	var lo=number%16;
	var hi=(number-lo)/16;

	return h[hi]+h[lo];
}

function comma(number) {
	number = '' + number;
	if (number.length > 3) {
		var mod = number.length % 3;
		var output = (mod > 0 ? (number.substring(0,mod)) : '');
		for (var i=0 ; i < Math.floor(number.length / 3); i++) {
			if ((mod == 0) && (i == 0))
				output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
			else
				output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
			}
		return (output);
	}
	else return number;
}

function point2han(point) {
	var uk,cho,man,won,ret="";

	point=Number(point);
	man=Math.floor(point/10000);
	uk=Math.floor(man/10000);
	cho=Math.floor(uk/10000);
	man=man%10000;
	uk=uk%10000;
	won=point%10000;

	if (cho) ret+=String(cho).replace("000","Ãµ")+"Á¶";
	if (uk) ret+=String(uk).replace("000","Ãµ")+"¾ï";
	if (man) ret+=String(man).replace("000","Ãµ")+"¸¸";
	if (won) ret+=String(won).replace("000","Ãµ");
	if (ret =='') ret=0;

	return ret;
}


// ÆË¾÷ Ã¢

function item_buy (buy_items, buy_div, gmcode) {
	window.open(TRANS_URL+"?command=buy&item=" + buy_items + "&buy_div=" + buy_div +"&gmcode="+ gmcode, "", 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=435,height=493');
}

function item_buy_cody (buy_items, game_code) {
	window.open(TRANS_URL+"?command=buy_cody&item=" + buy_items + "&game_code=" + game_code, "", 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=435,height=493');
}

function item_buy_member (sn) {
	window.open(TRANS_URL+"?command=buy_member&sn=" + sn, "", 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=435,height=493');
}

function item_present (buy_items, buy_div, gmcode) {
	window.open(TRANS_URL+"?command=present&item=" + buy_items + "&buy_div=" + buy_div +"&gmcode="+ gmcode, "", 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=435,height=493');
}

function item_present_cody (buy_items, game_code) {
	window.open(TRANS_URL+"?command=present_cody&item=" + buy_items + "&game_code=" + game_code, "", 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=435,height=493');
}

function item_present_member (sn) {
	window.open(TRANS_URL+"?command=present_member&sn=" + sn, "", 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=435,height=493');
}

function item_info (item, buy_div, gmcode) {
	window.open(URL_ROOT+"/avatar/view_item_info.cgi?item=" + item + "&buy_div=" + buy_div +"&gmcode="+ gmcode,"",'width=359,height=307,top=10,left=300');
}

function item_mark (item) {
	window.open(URL_ROOT+"/avatar/mark.cgi?command=mark_form&item=" + item, "", 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=350,height=329');
}

function user_info(uid){
	window.open("http://game.nate.com/myinfo/profile.html?id="+uid,"","width=494,height=514,scrollbars=no,top=10,left=10");
}

function buy_items (items, game_code) {
	var op = new Array ();
	var sp = new Array ();
	var buy_items = "";
	var c=0;

	for (i = 0; i <=MAX_ZORDER; i++) {
		if (items[i] != 0 && i != Z_HAIR_BACK) {
			op = items[i].split(':');
			sp = op[0].split('-');
			if (sp[0] > 4) {
				buy_items += sp[0]+":"+op[1]+"|";
				c++;
			}
		}
	}
	if (c==0) {
		alert("Âø¿ëÇÑ ¾ÆÀÌÅÛÀÌ ¾ø½À´Ï´Ù.");
		return;
	}
	item_buy_cody (buy_items, game_code);
}

function present_items (items, game_code) {
	var op = new Array ();
	var sp = new Array ();
	var buy_items = "";
	var c=0;

	for (i = 0; i <=MAX_ZORDER; i++) {
		if (items[i] != 0 && i != Z_HAIR_BACK) {
			op = items[i].split(':');
			sp = op[0].split('-');
			if (sp[0] > 4) {
				buy_items += sp[0]+":"+op[1]+"|";
				c++;
			}
		}
	}
	if (c==0) {
		alert("Âø¿ëÇÑ ¾ÆÀÌÅÛÀÌ ¾ø½À´Ï´Ù.");
		return;
	}
	item_present_cody (buy_items, game_code);
}

// ¾Æ¹ÙÅ¸ Ç¥Ãâ °ü·Ã

function avatarDraw(items, x, y, w, h) {
	var op = new Array (4);

	if (x==null) {
		x=0;
		y=0;
		w=AVATAR_WIDTH;
		h=AVATAR_HEIGHT;
	}

	var str = "<table width='"+w+"' height='"+h+"' cellspacing='0' cellpadding='0' border='0'>";
	str += "<tr><td width='"+w+"' height='"+h+"' valign='top' bgcolor='FFFFFF'>";
	str += "<div style='width:"+w+"px; height:"+h+"px; clip:rect(0 "+w+" "+h+" 0); position:absolute'>";
	for (var i = 0; i < MAX_ZORDER; i++) {
		if (items[i]!=0 && items[i] != null) {
			op = items[i].split(':');
			op[2]-=x;
			op[3]-=y;
			str += item_image(op[0],op[2],op[3],i);
		}
	}
	str += "</div></td></tr></table>";
	return str;
}

function draw_avatar (items)
{
	var op = new Array (3);
	var temp;

	if (items[0]==-1) return "<img src=http://img.nate.com/game/img/avatar/avatar_notice.gif>";

	var str = "<table width='122' height='142' cellspacing='1' cellpadding='0' border='0' bgcolor='D6D6D6'>";
	str += "<tr><td width='120' height='140' valign='top' bgcolor='FFFFFF'>";
	str += "<div style='width:120; height:140; clip:rect(0 120 140 0); position:absolute'>";

	for (i = 0; i <= MAX_ZORDER; i++) {
		if (items[i]!=0 && items[i] != null) {
			op = items[i].split(':');
			str += "<div style='top:"+op[3]+"; left:"+op[2]+"; position:absolute; z-index:"+i+";'>";
			if (i==Z_THEME) {
				temp = op[0].split('-');
				str += item_image(temp[0]+'-'+get_face());
			} else str += item_image(op[0]);
			str += "</div>";
			if (i==Z_HEAD) {
				str += "<div style='top:"+op[3]+"; left:"+op[2]+"; position:absolute; z-index:"+Z_HAIR_BACK+";'>";
				temp = op[0].split('-');
				str += item_image(temp[0]+'-b');
				str += "</div>";
			}
		}
	}

	str += "</div>";
	str += "</td></tr></table>";

	return str;
}

function update_avatar (avatar_items) {
	var i;
	var op = new Array ();
	var new_items = new Array ();

	for (i=0; i<=MAX_ZORDER; i++) {
		items[i]=0;
		default_items[i]=0;
	}

	new_items = avatar_items.split('|');
	default_head=new_items[0]+":0:0:0";
	for (i=1; i< new_items.length; i++) {
		op = new_items[i].split(':');
		if (op[0] != Z_HAIR_BACK) {
			default_items[op[0]]=op[1]+":0:"+op[2]+":"+op[3];
			items[op[0]]=default_items[op[0]];
		}
	}
	show_avatar(items);
}

function show_avatar (items) {
	document.getElementById('avatar').innerHTML = draw_avatar (items);
}

function set_clear (start) {
	for (var i=start; i<=MAX_ZORDER; i++) {
		items[i]=0;
		default_items[i]=0;
	}
}

function set_default (start) {
	for (var i=start; i<=MAX_ZORDER; i++) items[i]=default_items[i];
}

function item_putting (item_code,zindex) {
	if (items[Z_THEME]!=Z_BACKGROUND && zindex != Z_THEME) { // Å×¸¶
		if (zindex >= Z_PANTS && zindex < Z_THEME) {
			if (default_items[Z_THEME] == 0) {
//				set_default(1);
				for (var i=Z_PANTS; i<=Z_THEME; i++) items[i]=default_items[i];
			} else {
				items[Z_THEME] = 0;
				items[Z_PANTS] = default_pants;
				items[Z_COAT] = default_body;
				items[Z_HEAD] = default_head;
			}
		}
	}
	if (zindex == Z_COAT) { // »óÀÇ
		if (items[zindex] == item_code) {
			items[zindex] = default_body;
		} else {
			items[zindex] = item_code;
			if (items[Z_ONEPIECE]) {
				items[Z_PANTS]=default_items[Z_PANTS];
			}
			items[Z_ONEPIECE] = 0;
		}
	} else if (zindex == Z_PANTS) { // ÇÏÀÇ
		if (items[zindex] == item_code) {
			items[zindex] = default_pants;
		} else {
			items[zindex] = item_code;
			if (items[Z_ONEPIECE]) {
				items[Z_COAT]=default_items[Z_COAT];
			}
			items[Z_ONEPIECE] = 0;
		}
	} else if (zindex == Z_ONEPIECE) { // ¿øÇÇ½º
		if (items[zindex] == item_code) {
			if (default_items[zindex] == item_code) {
				items[zindex] = 0;
				items[Z_PANTS] = default_pants;
				items[Z_COAT] = default_body;
			} else {
				items[zindex] = default_items[zindex];
				items[Z_PANTS] = default_items[Z_PANTS];
				items[Z_COAT] = default_items[Z_COAT];
			}
		} else {
			items[zindex] = item_code;
			items[Z_PANTS] = 0;
			items[Z_COAT] = 0;
		}
	} else if (zindex == Z_HEAD) { // ¸Ó¸®
		if (items[zindex] == item_code) {
			items[zindex] = default_head;
		} else {
			var item = item_code.split(':');
			items[zindex] = item[0]+'-'+get_face()+':'+item[1]+':'+item[2]+':'+item[3];
		}
	} else if (zindex == Z_THEME) { // Å×¸¶
		if (items[zindex] == item_code) {
			if (default_items[zindex] == item_code) {
				items[zindex] = 0;
				items[Z_PANTS] = default_pants;
				items[Z_COAT] = default_body;
				items[Z_HEAD] = default_head;
			} else {
//				set_default(1);
				for (var i=Z_PANTS; i<=Z_THEME; i++) items[i]=default_items[i];
			}
		} else {
			for (i=Z_PANTS; i<=Z_THEME; i++) {
				items[i]=0;
			}
			items[zindex] = item_code;
		}
	} else {
		if (items[zindex] == item_code) {
			items[zindex] = 0;
		} else {
			items[zindex] = item_code;
		}
	}
	if (items[Z_ONEPIECE]==0 && items[Z_THEME]==0) {
		if (items[Z_PANTS]== 0) items[Z_PANTS] = default_pants;
		if (items[Z_COAT] == 0) items[Z_COAT] = default_body;
	}

	show_avatar(items);
}

function get_face() {
	var op = default_head.split(':');
	var sp = op[0].split('-');
	if (sp[1] == undefined) return 0;
	return sp[1];
}
