// NHN USA Hangame Avatar Display Function
// 2005-09-15, Version 0.9, CSH
// All copyrights are reserved to NHN USA Inc.

/////////////////////////////////
// constants
/////////////////////////////////
var _viewUrl = "http://avimages.ijjimax.com/avatar/";


/////////////////////////////////
// global
/////////////////////////////////

// debug
var av_gbDebug = true;

// layer info
var av_garrLayerStructure;

// avatar list
var av_garrAvatarItem = new Array();

// initialization flag
var av_gbInit = false;

// emotion code list
var av_gEmotionsList;
var av_gEmotionsKind;
var av_gEmotionsData;
var av_gbInitEmotions = false;


/////////////////////
// avatar list

// returns unique id
function av_gRegisterAvatarItem(obj) {
	var index = av_garrAvatarItem.length;
	av_garrAvatarItem[index] = obj;
	return index;
}

// find ojb
function av_gFindAvatarItem(id) {
	if ( id < av_garrAvatarItem.length ) return av_garrAvatarItem[id];
	return null;
}

/////////////////////
// layer properties
function av_LPItem(layerHeader,layerPosition,fullCodeType,layerHeadersNotCompatible,emotionType) {
	this.layerHeader = layerHeader;
	if ( layerPosition == null || layerPosition == "" ) {
		this.layerPosition = 0;
	} else {
		this.layerPosition = eval(layerPosition);
	}
	this.fullCodeType = fullCodeType;
	this.layerHeadersNotCompatible = (layerHeadersNotCompatible == null) ? "" : layerHeadersNotCompatible;
	this.emotionType = emotionType;
}


// init global variable av_garrLayerStructure
function av_initVariables()
{
	av_garrLayerStructure = new Array (
		new av_LPItem("BB","","BB%N","",""),
		new av_LPItem("A1","","A1%N","",""),
		new av_LPItem("A2","","A2%N","",""),
		new av_LPItem("A3","","A3%N","",""),
		new av_LPItem("A4","","A4%N","",""),
		new av_LPItem("A5","","A5%N","",""),
		new av_LPItem("A6","","A6%N","",""),
		new av_LPItem("A7","","A7%N","",""),
		new av_LPItem("H1","1","H1%N%P","H?@J?@C?@G?",""),
		new av_LPItem("H2","1","H2%N%P","H?@J?@C?@G?",""),
		new av_LPItem("H3","1","H3%N%P","H?@J?@C?@G?",""),
		new av_LPItem("H4","1","H4%N%P","H?@J?@C?@G?",""),
		new av_LPItem("H5","1","H5%N%P","H?@J?@C?@G?",""),
		new av_LPItem("J1","1","J1%N%P","H?@F1@C?@G?@D4",""),
		new av_LPItem("WM","1","WM%R%N%P-%V","WF@C?@G?","B"),
		new av_LPItem("WF","1","WF%R%N%P-%V","WM@C?@G?","B"),
		new av_LPItem("F1","","F1%R%N","J?@C?@G?",""),
		new av_LPItem("F2","","F2%R%N","FA@C?@G?",""),
		new av_LPItem("F3","","F3%R%N-%V","FA@FE@C?@G?","B"),
		new av_LPItem("F4","","F4%R%N-%V","F5@F6@F7@FA@FE@C?@G?","B"),
		new av_LPItem("F5","","F5%R%N-%V","F4@F6@F7@FA@FE@C?@G?","B"),
		new av_LPItem("F6","","F6%R%N-%V","F4@F5@F7@FA@FE@C?@G?","B"),
		new av_LPItem("F7","","F7%R%N-%V","F4@F5@F6@FA@FE@C?@G?","B"),
		new av_LPItem("FA","","FA%R%N-%V","F?@C?@G?","B"),
		new av_LPItem("D1","","D1%N","C?@G?",""),
		new av_LPItem("D2","","D2%N","C?@G?@D4",""),
		new av_LPItem("H1","2","H1%N%P","H?@J?@C?@G?",""),
		new av_LPItem("H2","2","H2%N%P","H?@J?@C?@G?",""),
		new av_LPItem("H3","2","H3%N%P","H?@J?@C?@G?",""),
		new av_LPItem("H4","2","H4%N%P","H?@J?@C?@G?",""),
		new av_LPItem("H5","2","H5%N%P","H?@J?@C?@G?",""),
		new av_LPItem("J1","2","J1%N%P","H?@F1@C?@G?",""),
		new av_LPItem("D3","","D3%N","C?@G?",""),
		new av_LPItem("D4","","D4%N","C?@G?@D2@J?",""),
		new av_LPItem("D5","","D5%N","C?@G?",""),
		new av_LPItem("WM","2","WM%R%N%P-%V","WF@C?@G?","B"),
		new av_LPItem("WF","2","WF%R%N%P-%V","WM@C?@G?","B"),
		new av_LPItem("FE","","FE%S-%V","F3@F4@F5@F6@F7@D?@C?@G?","A"),
		new av_LPItem("GF","","GF%R%N-%V","H?@J?@W?@F?@D?@C?@G?","B"),
		new av_LPItem("GM","","GM%R%N-%V","H?@J?@W?@F?@D?@C?@G?","B"),
		new av_LPItem("C1","","C1%N-%V","H?@J?@W?@F?@D?@G?","AB"),
		new av_LPItem("C2","","C2%N-%V","CE@H?@J?@W?@F?@D?@G?","B"),
		new av_LPItem("C3","","C3%N-%V","CE@H?@J?@W?@F?@D?@G?","B"),
		new av_LPItem("CE","","CE-%V","C2@C3@H?@J?@W?@F?@D?@G?","A"),
		new av_LPItem("K1","","K1%N","",""),
		new av_LPItem("K2","","K2%N","",""),
		new av_LPItem("K3","","K3%N","",""),
		new av_LPItem("K4","","K4%N","",""),
		new av_LPItem("K5","","K5%N","",""),
		new av_LPItem("K6","","K6%N","",""),
		new av_LPItem("K7","","K7%N","",""),
		new av_LPItem("K8","","K8%N","",""),
		new av_LPItem("K9","","K9%N","","")
		// test
		//,new av_LPItem("TT","","TT%R%N%P-%H%V%G-%F","")
	);

	// to call only one time
	av_gbInit = true;


	// test code
	// WM%R%N%P-%V
	/*
	alert(av_getAttributeFromFullCode("WMA1235-23","%R"));
	alert(av_getAttributeFromFullCode("WMA1235-23","%N"));
	alert(av_getAttributeFromFullCode("WMA1235-23","%P"));
	alert(av_getAttributeFromFullCode("WMA1235-23","%V"));
	*/

}

// get layer property, only return first position
function av_getLayerProperty(avCode) {
	for ( var i = 0 ; i < av_garrLayerStructure.length ; i++ ) {
		if ( av_isEqualHeader(avCode, av_garrLayerStructure[i].layerHeader) ) {
			return av_garrLayerStructure[i];
		}
	}
	return null;
}

// get attribute from avatar id property
function av_getAttributeFromAvatarIdProperty(avatarId, attrib) {
	if ( avatarId==null || avatarId.length < 6 ) return "";

	var exp;
	var prop = avatarId.substr(0,6);
	if ( attrib=="%R" ) {
		exp = new RegExp("A([ABCDU])[MFU][AB][A-Z0-9]{2}","g");
	} else if ( attrib=="%S" ) {
		exp = new RegExp("A[ABCDU]([MFU])[AB][A-Z0-9]{2}","g");
	} else if ( attrib=="%W") {
		exp = new RegExp("A[ABCDU][MFU]([AB])[A-Z0-9]{2}","g");
	} else if ( attrib=="%V") {
		exp = new RegExp("A[ABCDU][MFU][AB]([A-Z0-9]{2})","g");
	} else {
		return "";
	}

	var ret = prop.match(exp);
	if ( ret == null ) return "";

	return RegExp.$1;
}

// get attribute from full code
function av_getAttributeFromFullCode(fullCode, attrib) {
	var fullCodeExp = av_getFullCodeRegularExpression(fullCode, attrib);

	var re = new RegExp(fullCodeExp,"g");
	var ret = fullCode.match(re);
	if ( ret == null ) return "";

	return RegExp.$1;
}

// get attribute from item code
function av_getAttributeFromItemCode(itemCode, attrib) {
	var itemCodeExp = av_getItemCodeRegularExpression(itemCode, attrib);

	var re = new RegExp(itemCodeExp,"g");
	var ret = itemCode.match(re);
	if ( ret == null ) return "";

	return RegExp.$1;
}

// returns regular expression
// this logic is subject to system of properties
function av_getFullCodeRegularExpression(avCode, whatToFind) {
	var layerProperty = av_getLayerProperty(avCode);
	if ( layerProperty==null ) return "";
	var fullCodeType = layerProperty.fullCodeType;

	// hyphen (optional)
	fullCodeType = fullCodeType.replace(/\-/g,"-*");

	// %R, skin color
	if ( whatToFind=="%R" ) {
		fullCodeType = fullCodeType.replace(/%R/g,"([ABCDU])");
	} else {
		fullCodeType = fullCodeType.replace(/%R/g,"[ABCDU]");
	}

	// %S, sex
	if ( whatToFind=="%S" ) {
		fullCodeType = fullCodeType.replace(/%S/g,"([MFU])");
	} else {
		fullCodeType = fullCodeType.replace(/%S/g,"[MFU]");
	}

	// %N, serial
	if ( whatToFind=="%N" ) {
		fullCodeType = fullCodeType.replace(/%N/g,"([A-Z|0-9]+)");
	} else {
		fullCodeType = fullCodeType.replace(/%N/g,"[A-Z|0-9]+");
	}

	// %P, position count
	if ( whatToFind=="%P" ) {
		fullCodeType = fullCodeType.replace(/%P/g,"([0-9])");
	} else {
		fullCodeType = fullCodeType.replace(/%P/g,"[0-9]*");
	}

	// %V, variation (emotion or action) - optional
	if ( whatToFind=="%V" ) {
		fullCodeType = fullCodeType.replace(/%V/g,"([A-Z|0-9]*)");
	} else {
		fullCodeType = fullCodeType.replace(/%V/g,"[A-Z|0-9]*");
	}

	return fullCodeType;
}

// returns regular expression of Item Code from code
function av_getItemCodeRegularExpression(avCode, whatToFind) {
	var layerProperty = av_getLayerProperty(avCode);
	if ( layerProperty==null ) return "";
	var fullCodeType = layerProperty.fullCodeType;

	// hyphen (optional)
	fullCodeType = fullCodeType.replace(/\-/g,"-*");

	// %R, skin color
	if ( whatToFind=="%R" ) {
		fullCodeType = fullCodeType.replace(/%R/g,"([ABCDU])");
	} else {
		fullCodeType = fullCodeType.replace(/%R/g,"[ABCDU]");
	}

	// %S, sex
	if ( whatToFind=="%S" ) {
		fullCodeType = fullCodeType.replace(/%S/g,"([MFU])");
	} else {
		fullCodeType = fullCodeType.replace(/%S/g,"[MFU]");
	}

	// %N, serial
	if ( whatToFind=="%N" ) {
		fullCodeType = fullCodeType.replace(/%N/g,"([A-Z|0-9]+)");
	} else {
		fullCodeType = fullCodeType.replace(/%N/g,"[A-Z|0-9]+");
	}

	// %P, position count - remove this...
	fullCodeType = fullCodeType.replace(/%P/g,"");

	// %V, variation (emotion or action) - optional
	if ( whatToFind=="%V" ) {
		fullCodeType = fullCodeType.replace(/%V/g,"([A-Z|0-9]*)");
	} else {
		fullCodeType = fullCodeType.replace(/%V/g,"[A-Z|0-9]*");
	}

	return fullCodeType;
}



// returns regular expression of Type A Code, used in avatar id
function av_getTypeACodeRegularExpression(avCode, whatToFind) {
	var layerProperty = av_getLayerProperty(avCode);
	if ( layerProperty==null ) return "";
	var fullCodeType = layerProperty.fullCodeType;

	// hyphen - remove this
	fullCodeType = fullCodeType.replace(/\-/g,"");

	// %R, skin color - remove this
	fullCodeType = fullCodeType.replace(/%R/g,"");

	// %S, sex - remove this
	fullCodeType = fullCodeType.replace(/%S/g,"");

	// %N, serial
	if ( whatToFind=="%N" ) {
		fullCodeType = fullCodeType.replace(/%N/g,"([A-Z|0-9]+)");
	} else {
		fullCodeType = fullCodeType.replace(/%N/g,"[A-Z|0-9]+");
	}

	// %P, position count - remove this...
	fullCodeType = fullCodeType.replace(/%P/g,"");

	// %V, variation (emotion or action) - remove this
	fullCodeType = fullCodeType.replace(/%V/g,"");

	return fullCodeType;
}


// get item code from full code
// this logic is subject to system of properties
function av_getItemCodeFromFullCode(fullCode) {
	var layerProperty = av_getLayerProperty(fullCode);
	if ( layerProperty==null ) return fullCode;

	// only collect header, %R, %S, %N, -, %V
	var ret = av_getHeaderFromCode(fullCode);
	var temp = av_getAttributeFromFullCode(fullCode,"%R");
	ret += temp;

	var temp = av_getAttributeFromFullCode(fullCode,"%S");
	ret += temp;

	temp = av_getAttributeFromFullCode(fullCode,"%N");
	ret += temp;

	if ( fullCode.indexOf("-") >= 0 ) {
		ret += "-";
		temp = av_getAttributeFromFullCode(fullCode,"%V");
		ret += temp;
	}

	return ret;
}

// get TYPE A CODE from Item Code
// this logic is subject to system of properties
function av_getTypeACodeFromItemCode(itemCode) {

	// collect %N only
	var temp = av_getAttributeFromItemCode(itemCode,"%N");
	var ret = av_getHeaderFromCode(itemCode) + temp;

	return ret;
}

// get Item Code from TYPE A Code
// the logic depends on the order of properties
function av_getItemCodeFromTypeACode(typeACode, avatarIdProp) {
	var layerProperty = av_getLayerProperty(typeACode);
	if ( layerProperty==null ) return "";
	var fullCodeType = layerProperty.fullCodeType;
	var header = av_getHeaderFromCode(typeACode);

	// order: %R, %S, %N, -, %V
	// Type A : header + serial
	var ret = header;
	if ( fullCodeType.indexOf("%R") >= 0 ) {
		ret += avatarIdProp.skinColor;
	}
	if ( fullCodeType.indexOf("%S") >= 0 ) {
		ret += avatarIdProp.sex;
	}
	if ( fullCodeType.indexOf("%N") >= 0 ) {
		ret += typeACode.substr(2);
	}
	if ( fullCodeType.indexOf("%V") >= 0 ) {
		// check type consistency
		var emotionType = layerProperty.emotionType;
		if ( emotionType == avatarIdProp.emotionType ) {
			if ( avatarIdProp.emotionCode != "00" ) {
				ret += "-" + avatarIdProp.emotionCode;
			}
		}
	}
	return ret;
}

// get code from file name, file name matches exactly with full code
function av_getItemCodeFromFileName(fileName) {
	var n = fileName.indexOf(".");
	if ( n < 0 ) return "";
	var code = fileName.substr(0,n).toUpperCase();
	var itemCode = av_getItemCodeFromFullCode(code);
	return itemCode;
}


////////////////////////////
// avatar id
////////////////////////////


// avatar id object
function av_avatarId() {
	this.avatarId;
	this.propPart;
	this.itemPart;
	this.items = new Array();
	this.wellFormed = false;

	this.parse = av_parseAvatarId;
	this.getItemByHeader = av_getItemByHeader;
}


function av_parseAvatarId(avatarId) {
	if ( avatarId==null ) return;

	var n = avatarId.indexOf("-");
	if ( n < 0 ) return;

	// parse prop
	var prop = avatarId.substr(0,n);
	var propPart = new av_propPart();
	propPart.parse(prop);
	if ( !propPart.wellFormed ) return;
	this.propPart = propPart;

	// parse item part
	var itemPart = avatarId.substr(n+1);
	if ( itemPart.length == 0 ) return;
	this.itemPart = itemPart;

	// split and store
	var items = itemPart.split("_");
	var itemCode = "";
	for ( var i = 0 ; i < items.length ; i++ ) {
		itemCode = av_getItemCodeFromTypeACode(items[i],this.propPart);
		this.items[this.items.length] = itemCode;
	}

	// set
	this.avatarId = avatarId;
	this.wellFormed = true;
}

// get item by whose header
function av_getItemByHeader(header) {
	if ( this.items == null ) return "";
	for ( var i = 0 ; i < this.items.length ; i++ ) {
		if ( av_isEqualHeader(header,this.items[i]) ) return this.items[i];
	}
	return "";
}

// avatar id property object
function av_propPart() {
	this.string = "";
	this.version = "";
	this.skinColor = "";
	this.sex = "";
	this.emotionType = "";
	this.emotionCode = "";
	this.wellFormed = false;

	this.parse = av_parsePropPart;
}

// parse properties of avatar id
function av_parsePropPart(str) {
	if ( str==null || str.length==0 ) return;

	// check consistency
	if ( !av_checkAvatarIdPropertyConsistency(str) ) return;

	// parse
	this.version = str.substr(0,1);
	this.skinColor = str.substr(1,1);
	this.sex = str.substr(2,1);
	this.emotionType = str.substr(3,1);
	this.emotionCode = str.substr(4,2);
	this.wellFormed = true;
}

// returns boolean, checking consistency of avatar id property part
function av_checkAvatarIdPropertyConsistency(prop) {
	var exp = new RegExp("A[ABCDU][MFU][AB][A-Z0-9]{2}","g");
	var ret = prop.match(exp);
	if ( ret == null ) return false;
	return true;
}

// returns src - dest
function av_getDiffItmes(srcAvatarId, destAvatarId ) {
	var src = new av_avatarId();
	src.parse(srcAvatarId);

	var dest = new av_avatarId();
	dest.parse(destAvatarId);

	if ( !src.wellFormed || !dest.wellFormed ) {
		return;
	}

	var result = new Array();
	for ( var i = 0 ; i < src.items.length ; i++ ) {
		var match = false;
		for ( var j = 0 ; j < dest.items.length ; j++ ) {
			if ( src.items[i] == dest.items[j] ) {
				match = true;
				break;
			}
		}
		if ( !match ) {
			result[result.length] = src.items[i];
		}
	}
	return result;
}

/////////////////////////////////
// helpers
/////////////////////////////////

// change itemcode skin
function av_changeItemCodeSkin(avCode, skinCode) {
	if ( avCode.length==6 && ( skinCode=="A" || skinCode=="B" || skinCode=="C" || skinCode=="D") ) {
		return avCode.substr(0,2) + skinCode + avCode.substr(3);
	}
	return avCode;
}

// returns boolean if the two matches, eg. S? matches SA
function av_isEqualHeader(hWild,hDest) {
	if ( hWild == null || hDest == null || hWild.length < 2 || hDest.length < 2)
		return false;

	if ( hWild.substring(1,2) == "?" )
		return ( hWild.substring(0,1) == hDest.substring(0,1) );
	else
		return ( hWild.substring(0,2) == hDest.substring(0,2) );
}

// return boolean if the two codes are equal
function av_isEqualCode(src,dest) {
	if ( src==dest ) return true;
	return false;
}

// returns header part to upper case
function av_getHeaderFromCode(avCode) {
	return avCode.substr(0,2).toUpperCase();
}

// returns an array with tokenizing
function av_makeArray(src, token) {
	if ( src==null || src.length==0 ) return new Array();
	var ret = src.split(token);
	return ret;
}

// check if the itemInfo is null
function av_isEmptyItem(itemInfo) {
	if ( itemInfo==null || typeof(itemInfo.avCode) != "string" )
		return true;

	return false;
}

// get file name from avcode
function av_getFileNameFromCode(avCode, position) {
	if ( position==null || position == 0 ) return avCode.toLowerCase() + ".gif";
	var n = avCode.indexOf("-");
	if ( n >= 0 ) {
		return avCode.substr(0,n).toLowerCase() + position.toString() + "-" + avCode.substr(n+1).toLowerCase() + ".gif";
	}
	return avCode.toLowerCase() + position.toString() + ".gif";
}

// get folder name from avcode
function av_getFolderNameFromCode(avCode) {
	if ( avCode==null || avCode=="" ) return "";
	return avCode.substr(0,2).toLowerCase()	;
}

// get file url from avcode
function av_getFileUrlFromCode(avCode, position) {
	return _viewUrl + av_getFolderNameFromCode(avCode) + "/" + av_getFileNameFromCode(avCode, position);
}



/////////////////////////////////
// Item Info
// information of specific item
/////////////////////////////////

// constructor
function av_ItemInfo(avCode, name, unitPrice, buyPrice, invCode, shopCode) {
	this.avCode = avCode;
	this.name = name;
	this.unitPrice = unitPrice;
	this.buyPrice = buyPrice;
	this.invCode = invCode;
	this.shopCode = shopCode;
}



/////////////////////////////////
// Avatar Item
// displays avatar
/////////////////////////////////
function av_AVItem(avatarId,displayObject,defaultHumanId,defaultCharacterId, isClipped, clipTop, clipRight, clipBottom, clipLeft, nWidth, nHeight, nMode) {
	// check init of layer info
	if ( !av_gbInit ) av_initVariables();

	// identifier
	this.objectName = "AVItem";
	this.uniqueId = av_gRegisterAvatarItem(this);

	// member variable
	this.avatarId = "";
	this.displayObject = null;
	this.items = null; // indexed array of av_ItemInfo, index means nothing

	this.curVariationTimer = null; // for timer of emotion or action
	this.curVariationOrgAvatarId = "";
	this.curVariationRestoreString = ""; // eval this after restoring

	this.nMode = (nMode==null) ? 0 : nMode; // 0-normal, 1-sample, 2-disabled

	// clipping
	this.top = 0;
	this.left = 0;
	this.width = (nWidth==null) ? 100 : nWidth;
	this.height = (nHeight==null) ? 120 : nHeight;
	this.clipTop = (clipTop==null) ? 0 : clipTop;
	this.clipRight = (clipRight==null) ? 100 : clipRight;
	this.clipBottom = (clipBottom==null) ? 120 : clipBottom;
	this.clipLeft = (clipLeft==null) ? 0 : clipLeft;
	this.isClipped = (isClipped==null) ? false : isClipped;

	// public methods
	this.setAvatarId = av_setAvatarId;
	this.getAvatarId = av_getAvatarId;
	this.evalAvatarId = av_evalAvatarId;

	this.isWearingCode = av_isWearingCode;
	this.autoWearItem = av_autoWearItem;
	this.safeWearItem = av_safeWearItem;
	this.clearItems = av_clearItems;

	this.refreshShow = av_refreshShow;
	this.getHTML = av_getHTML;

	this.guessSkinColor = av_guessSkinColor;
	this.guessSex = av_guessSex;
	this.guessVariationCode = av_guessVariationCode;

	this.getSkinColor = av_getSkinColor;
	this.setSkinColor = av_setSkinColor;
	this.getSex = av_getSex;
	this.getEmotion = av_getEmotion;

	this.wearVariation = av_wearVariation;
	this.isCompositeFace = av_isCompositeFace;
	this.isCharacter = av_isCharacter;
	this.isWholeAvatar = av_isWholeAvatar;

	this.getFaceHairItemInfoList = av_getFaceHairItemInfoList;
	this.getCharacterItemInfoList = av_getCharacterItemInfoList;
	this.getItemInfoByHeader = av_getItemInfoByHeader;

	// private methods
	this.insertItem = av_insertItem;
	this.normalizeAvatarId = av_normalizeAvatarId;
	this.removeNotCompatible = av_removeNotCompatible;
	this.removeItemByCode = av_removeItemByCode;
	this.removeItemByHeader = av_removeItemByHeader;
	this.getEmptyIndex = av_getEmptyIndex;
	this.initItems = av_initItems;
	this.syncAvatarId = av_syncAvatarId;
	this.makeComplete = av_makeComplete;

	// set init values
	this.initItems();
	this.displayObject = displayObject;
	if ( defaultHumanId ==null ) defaultHumanId = "";
	this.defaultHumanIdObj = new av_avatarId();
	this.defaultHumanIdObj.parse(defaultHumanId);

	if ( defaultCharacterId == null ) defaultCharacterId = "";
	this.defaultCharacterIdObj = new av_avatarId();
	this.defaultCharacterIdObj.parse(defaultCharacterId);

	if ( avatarId!=null && avatarId.length > 0 ) {
		this.evalAvatarId(avatarId);
		// refresh show
		this.refreshShow();
	}

}

////////////////////////////////////////////////////
// public
// set avatar id
function av_setAvatarId(avatarId) {
	var normId = this.normalizeAvatarId(avatarId);
	this.avatarId = normId;

}

// get avatar id
function av_getAvatarId() {
	return this.avatarId;
}

// check, parse, wear, sync id, no view sync
function av_evalAvatarId(avatarId, bNoRefresh) {
	if ( avatarId==null || avatarId.length==0 ) return;
	var avId = new av_avatarId();
	avId.parse(avatarId);
	if ( !avId.wellFormed ) return;

	this.clearItems();
	for(var i = 0; i < avId.items.length ; i++ ) {
		this.autoWearItem(new av_ItemInfo(avId.items[i]));
	}

	//
	this.setAvatarId(avatarId);
	if ( bNoRefresh==true ) {
		// nothing
	} else {
		this.refreshShow();
	}
}

// check wearing
function av_isWearingCode(avCode) {
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			if ( av_isEqualCode(this.items[j].avCode,avCode) )
				return true;
		}
	}
	return false;
}

// wear or unwear item, if bForceUnwear is true, then it always unwear.
function av_autoWearItem(itemInfo, bForceUnwear) {
	if ( av_isEmptyItem(itemInfo) ) return;

	if ( this.isWearingCode(itemInfo.avCode) || bForceUnwear ) {
		// except for face,eye, mouth, ear, body, hair
		// except for character body,mouth,eye
		var header1 = itemInfo.avCode.substr(0,1);
		if ( header1=="F" || header1=="W" || header1=="H" || header1=="G" || header1=="J" ) return;
		if ( header1=="C" ) return;

		this.removeItemByCode(itemInfo.avCode); // unwear
	} else {
		this.insertItem(itemInfo); // wear
	}

	// sync
	this.syncAvatarId();
}

// wear safe to completeness
function av_safeWearItem(itemInfo, bForceUnwear) {
	this.autoWearItem(itemInfo,bForceUnwear);
	this.makeComplete();
	this.syncAvatarId();
	this.refreshShow();
}

// clear all the items
function av_clearItems() {
	for (var j = 0; j < this.items.length; j++) {
		this.items[j] = null;
	}
	this.initItems();
	// sync
	this.syncAvatarId();
}

// refresh show
function av_refreshShow() {
	if ( this.displayObject !=null && typeof(this.displayObject)=="object" ) {
		this.displayObject.innerHTML = this.getHTML();
	} else {
		setTimeout("av_gRefreshShow(" + this.uniqueId + ");",200);
	}
}

// global
function av_gRefreshShow(id) {
	var avatarItem = av_gFindAvatarItem(id);
	if ( avatarItem == null ) return;

	// restore to original avatar id, and refresh
	avatarItem.refreshShow();
}

// get html representation
function av_getHTML() {
	var s = "";

	for ( var i = 0 ; i < av_garrLayerStructure.length ; i++ ) {
		for (var j = 0; j < this.items.length; j++) {
			if ( ! av_isEmptyItem(this.items[j]) )  {
				if ( av_isEqualHeader(this.items[j].avCode, av_garrLayerStructure[i].layerHeader) ) {
					s += "<div id='layer' style='position:absolute; top:" + this.top + "px; left:" + this.left + "px; width:" + this.width + "px; height:" + this.height + "px; ";
					if ( this.isClipped ) {
						s += " overflow:hidden; clip:rect(" + this.clipTop + " " + this.clipRight + " " + this.clipBottom + " " + this.clipLeft + "); ";
					}
					if ( this.nMode == 1 || this.nMode == 2 ) {
						s += " filter:progid:DXImageTransform.Microsoft.BasicImage(grayScale=1); ";
					}
					s += " '>\n";
					s += "<img src='" + av_getFileUrlFromCode(this.items[j].avCode, av_garrLayerStructure[i].layerPosition) + "' width='100' height='120' border='0'>\n";
					s += "</div>\n";
				}
			}
		}
	}
	if ( this.nMode == 1 ) { // display "SAMPLE"
		s += "<div id='layer' style='position:absolute; top:" + this.top + "px; left:" + this.left + "px; width:" + this.width + "px; height:" + this.height + "px; ";
		if ( this.isClipped ) {
			s += " overflow:hidden; clip:rect(" + this.clipTop + " " + this.clipRight + " " + this.clipBottom + " " + this.clipLeft + "); ";
		}
		s += " filter:progid:DXImageTransform.Microsoft.Alpha(opacity=60); ";
		s += " ' >\n";
		s += "<div style='position:relative; top:" + (((this.clipBottom-this.clipTop) / 2 )-10) + "px; font:bold 16pt Verdana; font-style:italic; color:#ff4444;'>SAMPLE</div>\n";
		s += "</div>\n";
	}
	return s;
}

// guess skin color from face shape(F2) or whole face(FA), if they don't exist, returns "U"
// and also guess from GF, GM
function av_guessSkinColor() {
	var skinColor = "U";
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			if ( av_isEqualHeader("F2",this.items[j].avCode) ) {
				skinColor = av_getAttributeFromFullCode(this.items[j].avCode,"%R");
				if ( skinColor == "" ) skinColor = "U";
			}
			if ( av_isEqualHeader("G?",this.items[j].avCode) ) {
				skinColor = av_getAttributeFromFullCode(this.items[j].avCode,"%R");
				if ( skinColor == "" ) skinColor = "U";
			}
		}
	}
	return skinColor;
}

// guess sex from body (WF,WM) or FE or (GF,GM)
function av_guessSex() {
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			if ( av_isEqualHeader("WM",this.items[j].avCode) ) {
				return "M";
			}
			if ( av_isEqualHeader("WF",this.items[j].avCode) ) {
				return "F";
			}
			if ( av_isEqualHeader("GF",this.items[j].avCode) ) {
				return "F";
			}
			if ( av_isEqualHeader("GM",this.items[j].avCode) ) {
				return "M";
			}
			if ( av_isEqualHeader("FE",this.items[j].avCode) ) {
				var sex = av_getAttributeFromFullCode(this.items[j].avCode,"%S");
				return sex;
			}
		}
	}
	return "U";
}

// guess variation from a layer that has variation (%V)
function av_guessVariationCode() {
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			var avCode = this.items[j].avCode;
			var layerProperty = av_getLayerProperty(avCode);
			if ( layerProperty==null ) break;
			var layerEmotionType = layerProperty.emotionType;

			if ( layerEmotionType !=null && layerEmotionType != "" ) {
				var emotion = av_getAttributeFromFullCode(avCode,"%V");
				if ( emotion != null && emotion != "" )
					return emotion;
			}
		}
	}
	return "00";
}



// get skin color from avatar id
function av_getSkinColor() {
	var avatarId = this.getAvatarId();
	if ( avatarId==null || avatarId.length==0 ) return "U";

	var skinColor = av_getAttributeFromAvatarIdProperty(avatarId,"%R");
	return skinColor;
}

// set skin color and refresh
function av_setSkinColor(skinCode) {
	var avatarId = this.getAvatarId();
	if ( avatarId==null || avatarId.length==0 ) return;
	if ( !this.isCompositeFace() && !this.isWholeAvatar() ) return;
	if ( skinCode!="A" && skinCode!="B" && skinCode !="C" && skinCode!="D" ) return;

	var skinColor = avatarId.substr(1,1);
	avatarId = avatarId.substr(0,1) + skinCode + avatarId.substr(2);
	this.evalAvatarId(avatarId);
}


// get sex from avatar id
function av_getSex() {
	var avatarId = this.getAvatarId();
	if ( avatarId==null || avatarId.length==0 ) return "U";

	var sex = av_getAttributeFromAvatarIdProperty(avatarId,"%S");
	return sex;
}

// get sex from avatar id
function av_getEmotion() {
	var avatarId = this.getAvatarId();
	if ( avatarId==null || avatarId.length==0 ) return "U";

	var emotionType = av_getAttributeFromAvatarIdProperty(avatarId,"%W");
	var emotionCode = av_getAttributeFromAvatarIdProperty(avatarId,"%V");
	return emotionType + emotionCode;
}


// show emotion or action, delay: milli second
// you can only check whether the vairation exists by setting checkOnly true
function av_wearVariation(emotionType, emotionCode, restoreString, delay, checkOnly) {
	if ( delay==null || delay==0 ) {
		delay = 2000;
	}
	if ( checkOnly==null || checkOnly=="") checkOnly = false;

	// save current avatar id
	if ( !checkOnly ) this.curVariationOrgAvatarId = this.getAvatarId();

	var hasEmotion = true;

	if ( emotionType=="A" ) { // total change, exaggeration

		// if character, use CE. if composite face, use FE

		if ( this.isCharacter() ) {
			if ( checkOnly ) return true;

			var avCode = "CE" + "-" + emotionCode;
			var avItem = new av_ItemInfo(avCode);
			// wear emotion
			this.autoWearItem(avItem);

		} else {
			// check FE layer is possible
			if ( ! this.isCompositeFace() ) {
				if ( checkOnly ) return false;

				hasEmotion = false;
			} else {
				if ( checkOnly ) return true;

				var sex = this.getSex();
				if ( sex == "U" ) sex = "M";
				var avCode = "FE" + sex + "-" + emotionCode;
				var avItem = new av_ItemInfo(avCode);
				// wear emotion
				this.autoWearItem(avItem);
			}
		}

	} else if ( emotionType=="B") {
		var avItems = new Array();

		for (var j = 0; j < this.items.length; j++) {
			if ( ! av_isEmptyItem(this.items[j]) )  {
				var layerProperty = av_getLayerProperty(this.items[j].avCode);
				if ( layerProperty==null ) break;
				var orgCode = this.items[j].avCode;

				if ( orgCode.indexOf("-") >= 0 ) continue; // if already has variations.

				var layerEmotionType = layerProperty.emotionType;

				if ( layerEmotionType.indexOf("B") >= 0 ) {
					if ( av_checkHasEmotion(this.items[j].avCode, emotionCode) ) {
						if ( checkOnly ) return true;
						// wear emotion
						var avCode = orgCode + "-" + emotionCode;
						// save for later wearing
						avItems[avItems.length] = new av_ItemInfo(avCode);
					}
				}
			}
		}

		// wear actually
		if ( avItems.length==0 ) {
			if ( checkOnly ) return false;

			hasEmotion = false;
		} else {
			for ( var i = 0 ; i < avItems.length ; i++ ) {
				this.autoWearItem(avItems[i]);
			}
		}
	}

	if ( hasEmotion ) {
		// post job
		this.curVariationRestoreString = restoreString;

		// save timer id
		this.curVariationTimer = setTimeout("av_gRestoreVariation(" + this.uniqueId + ");",delay);
	} else {
		if ( av_gbDebug ) alert("There is no corresponding variation.");
	}

}

// restore to orginal emotion or action
function av_gRestoreVariation(id) {
	var avatarItem = av_gFindAvatarItem(id);
	if ( avatarItem == null ) return;

	// restore to original avatar id, and refresh
	avatarItem.evalAvatarId(avatarItem.curVariationOrgAvatarId);

	// post job string
	var restore = avatarItem.curVariationRestoreString;

	// init
	this.curVariationTimer = null;
	this.curVariationOrgAvatarId = "";
	this.curVariationRestoreString = "";

	// do post job
	if ( restore !=null ) eval(restore);

}

// composite face composed of F1,F2,F3,F4,FA etc...
// it allows FE layer
function av_isCompositeFace() {
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			if ( av_isEqualHeader("C?", this.items[j].avCode)
				|| av_isEqualHeader("G?", this.items[j].avCode) )
				return false;
		}
	}
	return true;
}

// character is composed of C1,C2,C3,CE etc...
function av_isCharacter() {
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			if ( av_isEqualHeader("C?", this.items[j].avCode) )
				return true;
		}
	}
	return false;
}

// check if it is G?
function av_isWholeAvatar() {
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			if ( av_isEqualHeader("G?", this.items[j].avCode) )
				return true;
		}
	}
	return false;
}

// returns face,eye, hair, mouth, ear
// temporarily return also body..-.-
function av_getFaceHairItemInfoList() {
	var items = new Array();
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			if ( av_isEqualHeader("F?", this.items[j].avCode) || av_isEqualHeader("H?", this.items[j].avCode) || av_isEqualHeader("W?", this.items[j].avCode) ) {
				items[items.length] = this.items[j];
			}
		}
	}
	return items;
}

// returns C? layers
function av_getCharacterItemInfoList() {
	var items = new Array();
	for (var j = 0; j < this.items.length; j++) {
		if ( ! av_isEmptyItem(this.items[j]) )  {
			if ( av_isEqualHeader("C?", this.items[j].avCode)  ) {
				items[items.length] = this.items[j];
			}
		}
	}
	return items;
}

// return av_ItemInfo
function av_getItemInfoByHeader(avCode) {
	for ( var i=0; i < this.items.length; i++ ) {
		if ( !av_isEmptyItem(this.items[i]) && av_isEqualHeader(avCode, this.items[i].avCode) ) {
			return this.items[i];
		}
		return null;
	}
}


////////////////////////////////////////////////////
// private

// wear an item
function av_insertItem(itemInfo) {
	// first, remove incompatible layers
	this.removeNotCompatible(itemInfo);

	// insert
	this.items[this.getEmptyIndex()] = itemInfo;

}


// normalize avatar Id
function av_normalizeAvatarId(avatarId) {
	var ret = unescape(avatarId);
	return ret;
}

function av_removeNotCompatible(itemInfo) {
	var avCode = itemInfo.avCode;
	// remove not compatibles
	var layerProperty = av_getLayerProperty(avCode);
	if ( layerProperty==null ) return;
	var incompats = av_makeArray(layerProperty.layerHeadersNotCompatible,"@");
	if ( incompats!=null && incompats.length>0 ) {
		for (var i=0; i < incompats.length; i++) {
			this.removeItemByHeader(incompats[i]);
		}
	}
	// remove same header
	this.removeItemByHeader(avCode);

}

// removes same item code
function av_removeItemByCode(avCode) {
	for ( var i=0; i < this.items.length; i++ ) {
		if ( !av_isEmptyItem(this.items[i]) && av_isEqualCode(avCode, this.items[i].avCode) ) {
			this.items[i] = null;
			// trigger to restore, will be implemented afterwards
		}
	}
}

// return true, if something was really removed
// remove same layer code
function av_removeItemByHeader(avCode) {
	for ( var i=0; i < this.items.length; i++ ) {
		if ( !av_isEmptyItem(this.items[i]) && av_isEqualHeader(avCode, this.items[i].avCode) ) {
			this.items[i] = null;
			// trigger to restore, will be implemented afterwards
		}
	}
}

function av_getEmptyIndex() {
	for ( var i=0; i < this.items.length; i++ ) {
		if ( this.items[i]==null ) {
			return i;
		}
	}
	return this.items.length;
}

// init items
function av_initItems() {
	this.items = new Array();
}

// sync avatar id
function av_syncAvatarId() {
	var version = "A";
	var skinColor = "U";
	var sex = "U";
	var emotionType = "A";
	var emotion = "00";

	// guess skin color
	skinColor = this.guessSkinColor();
	// guess sex
	sex = this.guessSex();
	// guess variation
	emotion = this.guessVariationCode();

	// property part
	var props = version + skinColor + sex + emotionType + emotion;

	// item part
	var s = "";
	for ( var i = 0 ; i < av_garrLayerStructure.length ; i++ ) {
		for (var j = 0; j < this.items.length; j++) {
			if ( ! av_isEmptyItem(this.items[j]) ) {
				if ( av_isEqualHeader(this.items[j].avCode, av_garrLayerStructure[i].layerHeader) ) {
					if ( av_garrLayerStructure[i].layerPosition <= 1 ) { // collect only first layer
						var layerProperty = av_getLayerProperty(this.items[j].avCode);
						if ( layerProperty==null ) break;
						var fullCodeType = layerProperty.fullCodeType;

						if ( s.length > 0 ) s += "_";
						s += av_getTypeACodeFromItemCode(this.items[j].avCode);
					}
				}
			}
		}
	}

	var avatarId = props + "-" + s;

	this.setAvatarId(avatarId);
}

// make complete in human or character
function av_makeComplete() {
	if ( this.isCharacter() ) {
		var obj = this.defaultCharacterIdObj;
		if ( obj.avatarId == null || obj.avatarId=="" ) return;
		var c1 = obj.getItemByHeader("C1");
		var c2 = obj.getItemByHeader("C2");
		var c3 = obj.getItemByHeader("C3");

		var avIdObj = new av_avatarId();
		avIdObj.parse(this.avatarId);
		var d1 = avIdObj.getItemByHeader("C1");
		var d2 = avIdObj.getItemByHeader("C2");
		var d3 = avIdObj.getItemByHeader("C3");

		if ( d1=="" && c1!="" ) this.insertItem(new av_ItemInfo(c1));
		if ( d2=="" && c2!="" ) this.insertItem(new av_ItemInfo(c2));
		if ( d3=="" && c3!="" ) this.insertItem(new av_ItemInfo(c3));

	} else if ( ! this.isWholeAvatar() ) {
		var avIdObj = new av_avatarId();
		avIdObj.parse(this.avatarId);
		var tf1 = avIdObj.getItemByHeader("F1");
		var tf2 = avIdObj.getItemByHeader("F2");
		var tf3 = avIdObj.getItemByHeader("F3");
		var tf4 = avIdObj.getItemByHeader("F4");
		var tf5 = avIdObj.getItemByHeader("F5");
		var tf6 = avIdObj.getItemByHeader("F6");
		var tf7 = avIdObj.getItemByHeader("F7");
		var twm = avIdObj.getItemByHeader("WM");
		var twf = avIdObj.getItemByHeader("WF");
		var th1 = avIdObj.getItemByHeader("H1");
		var th2 = avIdObj.getItemByHeader("H2");
		var th3 = avIdObj.getItemByHeader("H3");
		var th4 = avIdObj.getItemByHeader("H4");
		var th5 = avIdObj.getItemByHeader("H5");
		var tj1 = avIdObj.getItemByHeader("J1");
		var skinCode = avIdObj.propPart.skinColor;

		var obj = this.defaultHumanIdObj;
		if ( obj.avatarId == null || obj.avatarId=="" ) return;
		var f1 = av_changeItemCodeSkin(obj.getItemByHeader("F1"),skinCode);
		var f2 = av_changeItemCodeSkin(obj.getItemByHeader("F2"),skinCode);
		var f3 = av_changeItemCodeSkin(obj.getItemByHeader("F3"),skinCode);
		var f4 = av_changeItemCodeSkin(obj.getItemByHeader("F4"),skinCode);
		var f5 = av_changeItemCodeSkin(obj.getItemByHeader("F5"),skinCode);
		var f6 = av_changeItemCodeSkin(obj.getItemByHeader("F6"),skinCode);
		var f7 = av_changeItemCodeSkin(obj.getItemByHeader("F7"),skinCode);
		var wm = av_changeItemCodeSkin(obj.getItemByHeader("WM"),skinCode);
		var wf = av_changeItemCodeSkin(obj.getItemByHeader("WF"),skinCode);
		var h1 = obj.getItemByHeader("H1");
		var h2 = obj.getItemByHeader("H2");
		var h3 = obj.getItemByHeader("H3");
		var h4 = obj.getItemByHeader("H4");
		var h5 = obj.getItemByHeader("H5");
		var j1 = obj.getItemByHeader("J1");


		if ( tf1=="" && f1!="" && tj1=="" ) this.insertItem(new av_ItemInfo(f1));
		if ( tf2=="" && f2!="" ) this.insertItem(new av_ItemInfo(f2));
		if ( tf3=="" && f3!="" ) this.insertItem(new av_ItemInfo(f3));
		if ( tf4=="" && tf5=="" && tf6=="" && tf7=="" ) {
			if ( f4!="" ) this.insertItem(new av_ItemInfo(f4));
			if ( f5!="" ) this.insertItem(new av_ItemInfo(f5));
			if ( f6!="" ) this.insertItem(new av_ItemInfo(f6));
			if ( f7!="" ) this.insertItem(new av_ItemInfo(f7));
		}

		if ( twm=="" && twf=="" && wm!="" ) this.insertItem(new av_ItemInfo(wm));
		if ( twm=="" && twf=="" && wf!="" ) this.insertItem(new av_ItemInfo(wf));
		if ( th1=="" && th2=="" && th3=="" && th4=="" && th5=="" && tj1=="" ) {
			if ( h1!="" ) this.insertItem(new av_ItemInfo(h1));
			if ( h2!="" ) this.insertItem(new av_ItemInfo(h2));
			if ( h3!="" ) this.insertItem(new av_ItemInfo(h3));
			if ( h4!="" ) this.insertItem(new av_ItemInfo(h4));
			if ( h5!="" ) this.insertItem(new av_ItemInfo(h5));
			if ( j1!="" ) this.insertItem(new av_ItemInfo(j1));
		}
	}
}


////////////////////////////
// emotions, global
////////////////////////////

function av_initEmotions() {
	if ( av_gbInitEmotions ) return;
	av_gbInitEmotions = true;

	av_gEmotionsList = new Array();
	av_gEmotionsKind = new Array();
	av_gEmotionsData = new Array(
		/*
		new av_emotionItem("WMA001","20"),
		new av_emotionItem("WMA002","20"),
		new av_emotionItem("WMA003","20")
		*/
	);

	for ( var i = 0 ; i < av_gEmotionsData.length ; i++ ) {
		var em = av_gEmotionsData[i];
		for ( var j = 0 ; j < em.emotionCodeList.length ; j++ ) {
			var emotionCode = em.emotionCodeList[j];
			var list = av_gEmotionsList[emotionCode];
			if ( list==null ) {
				av_gEmotionsList[emotionCode] = new Array();
				list = av_gEmotionsList[emotionCode];
				av_gEmotionsKind[av_gEmotionsKind.length] = emotionCode;
			}
			list[list.length] = em.itemCode;
		}
	}

	// debug
	/*
	alert(av_gEmotionsKind.length);
	for ( var i = 0 ; i < av_gEmotionsKind.length ; i++ ) {
		var emotion = av_gEmotionsKind[i];
		var arr = av_gEmotionsList[emotion];
		var s = emotion + " : ";
		if ( arr!=null ) {
			for ( var j = 0 ; j < arr.length ; j++ ) {
				s += arr[j] + " ";
			}
			alert(s);
		}
	}
	*/
}

function av_emotionItem(itemCode, emotionCodeList) {
	this.itemCode = itemCode;
	if ( emotionCodeList==null || emotionCodeList.length==0 ) {
		this.emotionCodeList = new Array();
	} else {
		this.emotionCodeList = emotionCodeList.split("@");
	}
	this.hasEmotionCode = av_EIHasEmotionCode;
}

function av_EIHasEmotionCode(emotionCode) {
	for ( var i = 0; i < this.emotionCodeList.length; i++ ) {
		if ( emotionCode == this.emotionCodeList[i] ) return true;
	}
	return false;
}


// returns true if itemCode has emotionCode
function av_checkHasEmotion(itemCode, emotionCode) {
	// disable
	if ( itemCode.substr(0,1)=="W" || itemCode.substr(0,1)=="G" || itemCode.substr(0,2)=="C1" ) {
		return true;
	}

	if ( !av_gbInitEmotions ) av_initEmotions();


	for ( var i = 0 ; i < av_gEmotionsData.length ; i++ ) {
		var data = av_gEmotionsData[i];
		if ( av_isEqualCode(data.itemCode,itemCode) ) {
			return data.hasEmotionCode(emotionCode);
		}
	}
	return false;
}

/////////////////////
// shop, global

// get current shop avatar id
function av_gGetCurShopAvatarId() {
	if ( typeof(getCookie)=="function" ) return getCookie("curShopAvatarId");
	return "";
}

// set current shop avatar id
function av_gSetCurShopAvatarId(curShopAvatarId) {
	if ( typeof(setCookie)=="function" ) {
		setCookie("curShopAvatarId",curShopAvatarId);
	}
}


////////////////////
// show, global

var _avatar = null;
function showAvatar(avatarId, avAreaId, defaultHumanId, defaultCharacterId, isClipped, clipTop, clipRight, clipBottom, clipLeft, width, height) {
	var avArea = document.getElementById(avAreaId);
	if ( avArea ) {
		_avatar = new av_AVItem(avatarId,avArea,defaultHumanId,defaultCharacterId, isClipped, clipTop, clipRight, clipBottom, clipLeft, width, height);
	}
}

function showSampleAvatar(avatarId, avAreaId, defaultHumanId, defaultCharacterId) {
	var avArea = document.getElementById(avAreaId);
	if ( avArea ) {
		// av_AVItem(avatarId,displayObject,defaultHumanId,defaultCharacterId, isClipped, clipTop, clipRight, clipBottom, clipLeft, nWidth, nHeight, showingMode) {
		_avatar = new av_AVItem(avatarId,avArea,defaultHumanId,defaultCharacterId, null, null, null, null, null, null, null, 1);
	}
}

function showAvatarDisabled(avatarId, avAreaId, defaultHumanId, defaultCharacterId) {
	var avArea = document.getElementById(avAreaId);
	if ( avArea ) {
		// av_AVItem(avatarId,displayObject,defaultHumanId,defaultCharacterId, isClipped, clipTop, clipRight, clipBottom, clipLeft, nWidth, nHeight, showingMode) {
		_avatar = new av_AVItem(avatarId,avArea,defaultHumanId,defaultCharacterId, null, null, null, null, null, null, null, 2);
	}
}

function showNoBackAvatar(avatarId, avAreaId, defaultHumanId, defaultCharacterId, isClipped, clipTop, clipRight, clipBottom, clipLeft, width, height) {
	var a1 = new av_AVItem(avatarId,null);
	a1.removeItemByHeader("BB");
	a1.syncAvatarId();
	var newAvatarId = a1.getAvatarId();

	var a2 = new av_AVItem(defaultHumanId,null);
	a2.removeItemByHeader("BB");
	a2.syncAvatarId();
	var newDefaultHumanId = a2.getAvatarId();

	var a3 = new av_AVItem(defaultCharacterId,null);
	a3.removeItemByHeader("BB");
	a3.syncAvatarId();
	var newDefaultCharacterId = a3.getAvatarId();

	showAvatar(newAvatarId, avAreaId, newDefaultHumanId, newDefaultCharacterId, isClipped, clipTop, clipRight, clipBottom, clipLeft, width, height);
}

function showClippedAvatar(avatarId, avAreaId, nWidth, nHeight) {
	var width = (nWidth==null) ? 84 : nWidth;
	var height = (nHeight==null) ? 120 : nHeight;
	var wClip = 100 - width;
	var nClipLeft = Math.floor(wClip/2);
	var nClipRight = 100 - nClipLeft;
	var hClip = 120 - height;
	var nClipTop = Math.floor(hClip/2);
	var nClipBottom = 120 - nClipTop;

	showNoBackAvatar(avatarId, avAreaId, "", "", true, nClipTop, nClipRight, nClipBottom, nClipLeft, width, height);
}

