<PROPERTY NAME="localpic"/>
<PROPERTY NAME="userid"/>
<PROPERTY NAME="cookies"/>
<PROPERTY NAME="ImagePath"/>
<PROPERTY NAME="usergender"/>
<PROPERTY NAME="oriusergender"/>
<METHOD NAME="InitTable"/>
<METHOD NAME="InitDressList"/>
<METHOD NAME="clickDress"/>
<METHOD NAME="ShowIMG"/>
<METHOD NAME="DisplayTakeOnRecord"/>
<METHOD NAME="GetAvatar"/>
<METHOD NAME="PutAvatar"/>
<EVENT NAME="setCookies" ID="UpdateCookies"/>
<ATTACH EVENT="ondocumentready" HANDLER="ShowIMG"/>

<script language="jscript">
function ShowIMG() {
	var w=element.width;
	var h=element.height;
	var srcHTML=element.innerHTML;
	var s="";
	var x=0;
	var y=0;
	
	cookies=eval(cookies);
	s+="<DIV style='padding:0;position:relative;top:0;left:0;width:"+w+";height:"+h+"'>";		
	//1	背景1
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:1;'>";
	//2	背景2
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:2;'>";
	//3	右身后
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:3;'>";
	//4	左身后
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:4;'>";
	//5	后背
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:5;'>";
	//6	后帽子
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:6;'>";
	//7	※后头发
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:7;'>";
	//8	※下身
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:8;'>";
	//9	※上身
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:9;'>";
	//10	项饰
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:10;'>";
	//11	※脸型
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:11;'>";
	//12	脸纹
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:12;'>";
	//13	※口鼻
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:13;'>";
	//14	※眼眉
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:14;'>";
	//15	耳饰
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:15;'>";
	//16	胡子
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:16;'>";
	//17	面纱
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:17;'>";
	//18	※前头发
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:18;'>";
	//19	耳罩
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:19;'>";
	//20	发饰
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:20;'>";
	//21	眼镜
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:21;'>";
	//22	右身前
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:22;'>";
	//23	左身前
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:23;'>";
	//24	前景1
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:24;'>";
	//25	前景2
	s+="<IMG style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:none;z-index:25;'>";
	//50	
	s+="<IMG src";
	s+="="+baseName+" style='padding:0;position:absolute;top:"+y+";left:"+x+";width:"+w+";height:"+h+";display:inline;z-index:50;'>";
	s+="</DIV>";
	s+="<DIV style='display:inline'></DIV>";
	element.innerHTML=s;
	takeonrecordTr.DressOnShow = new Array;
	var ret = InitShow();
	if(ret == -1) {
		InitIMG();
	}
	if(userid == '0')
		InitDressList(getCookie('MyAvatarRec_' + usergender + '_' + userid));
	else
		InitDressList(getCookie('MyAvatarRec_' + userid));
	isLoaded = 1;
}

function clickDress(mode) {
	if(mode == null)
		var t = window.event.srcElement;
	else
		var t = window.document.getElementById('AvatarFrame').contentWindow.event.srcElement;
	if(t.tagName=="IMG"||(t.tagName=="INPUT"&&t.value=="试用")) {
		if(!t.itemno) return;
		var itemno=t.itemno;
		var layerno=t.layerno;
		var itemgender=t.itemgender;
		
		if(usergender != '2') {
			if ((itemgender != '2') && (itemgender)) {
				if(itemgender != usergender) {
					alert("对不起，性别不符，您无法试用");
					return;
				}
			}
		}
		var nPos=layerno.indexOf('_');
		if(nPos<0) {
			t = GetPart(layerno);
			if((!t.preFileNo)||(t.preFileNo != itemno+'.'+layerno)) {
				TakeOnSingle(layerno, itemno);
			} else {
				TakeOffSingle(layerno);
			}
		} else {
			t = GetPart(layerno.substring(0, nPos));
			if((!t.preFileNo)||(t.preFileNo != itemno+'.'+layerno)) {
				TakeOnMulti(itemno, layerno);
			} else {
				TakeOffMulti(layerno);
			}
		}
	}
}

function GetPart(layerno) {
	t=element.children[0].children[layerno-1];
	return t;
}

function TakeOnSingle(layerno, itemno) {
		var t = GetPart(layerno);

		if(!t.preFileNo) {
			TakeLayerIMG(layerno, itemno);
			return 0;
		}
			
		var nPos = t.preFileNo.indexOf('_');

		if (nPos < 0) {
			TakeLayerIMG(layerno, itemno);
			return 0;
		} else {
			var layerArray = t.preFileNo.split('.');		

			TakeOffMulti(layerArray[1]);
			TakeLayerIMG(layerno, itemno);
			return 0;
		}
}

function TakeOffSingle(layerno) {
	SetGenderIMG(layerno);
}

function TakeOnMulti(itemno, layers) {
	var layerArray = layers.split('_');
	var layerArrayLen = layerArray.length;	
	
	for(i=0; i < layerArrayLen; i++) {
		var genderItemNo = GetGenderItemNo(layerArray[i]);
		TakeOnSingle(layerArray[i], genderItemNo);
	}
	for(i=0; i < layerArrayLen; i++) {
		TakeOnSingle(layerArray[i], itemno);
		t = GetPart(layerArray[i]);
		if(!t.preFileNo) {
			TakeLayerIMG(layerno, itemno);
			return 0;
		}
		t.preFileNo = itemno + "." + layers;
	}
}

function TakeOffMulti(layers) {
	var layerArray = layers.split('_');
	var layerArrayLen = layerArray.length;
	
	for(i=0; i < layerArrayLen; i++) {
		TakeOffSingle(layerArray[i]);
	}
}

function TakeLayerIMG(nType,itemNo) {
	var t;

	t = GetPart(nType);
	if(itemNo == -1) {
		t.style.display	= "none";
		t.preFileNo = '';
		t.src = '';
	} else {
		var fileN = GetFileName(nType, itemNo);

		t.preFileNo = itemNo + '.' + nType;
		t.src = fileN;
		t.style.display	= "inline";
	}

	if(oriusergender == usergender) {
		SaveCookieShow();
		SaveRec(userid,usergender,nType,itemNo);
	}
}

function SetGenderIMG(nType) {
	var itemNo = GetGenderItemNo(nType);
		
	TakeLayerIMG(nType, itemNo);
}


function GetGenderItemNo(nType) {
	var itemNo;
	
	if(usergender == '1') {
		switch(nType) {
			case '7':
				itemNo = 14;
				break;
			case '8':
				itemNo = 13;
				break;
			case '9':
				itemNo = 12;
				break;
			case '11':
				itemNo = 11;
				break;
			case '13':
				itemNo = 10;
				break;
			case '14':
				itemNo = 9;
				break;
			case '18':
				itemNo = 8;
				break;
			default:
				return -1;
		}
	} else {
		switch(nType) {
			case '7':
				itemNo = 7;
				break;
			case '8':
				itemNo = 6;
				break;
			case '9':
				itemNo = 5;
				break;
			case '11':
				itemNo = 4;
				break;
			case '13':
				itemNo = 3;
				break;
			case '14':
				itemNo = 2;
				break;
			case '18':
				itemNo = 1;
				break;
			default:
				return -1;
		}
	}
	return itemNo;
}

function InitIMG() {
	if(!enableLayers)	return;
	
	var layerArray = enableLayers.split('_');		
	var layerArrayLen = layerArray.length;
	
	for(var i=0;i<layerArrayLen;i++) {
		eval("var layerFileNo = layer"+layerArray[i]+";");

		var tmpArray = layerFileNo.split('.');	

		TakeLayerIMG(layerArray[i], tmpArray[0]);
		t = GetPart(layerArray[i]);
		t.preFileNo = layerFileNo;
	}
}

function InitShow() {
	var t, j, x;
	var layers, count;

	if(userid == '0')
		var itemnos = getCookie('MyAvatar_' + usergender + '_' + userid);
	else
		var itemnos = getCookie('MyAvatar_' + userid);

	if(itemnos == '') return -1;

	var girlshow = '||||||7|6|5||4||3|2||||1|||||||';
	var boyshow = '||||||14|13|12||11||10|9||||8|||||||';
	
	if(usergender != oriusergender) 
		itemnos = (usergender=='0' ? girlshow : boyshow);

	var showArray = itemnos.split('|');
	var ret = CheckIllegal(showArray);
	if(ret != 0) return -1;
	for (var i=0; i<25; i++) {
		j = i + 1;
		t = GetPart(j);
		if(t.preFileNo) TakeOffSingle(j);
		if(showArray[i] != '')
		{
			TakeLayerIMG(j, showArray[i]);
			layers = '';
			count=0;
			for(x=0; x<25; x++) {
				if(showArray[x] == showArray[i]) {
					if(count == 0)
						layers = (x+1);
					else
						layers = layers + '_' + (x+1);
					count ++;
				}
			}
			t.preFileNo = showArray[i] + '.' + layers;
		}
	}
	return 0;
}

function CheckIllegal(showArray) {
	if((showArray[6] != '')
		&& (showArray[7] != '')
		&& (showArray[8] != '')
		&& (showArray[10] != '')
		&& (showArray[12] != '')
		&& (showArray[13] != '')
		&& (showArray[17] != ''))
		return 0;
	else
		return -1;
}

function SaveCookieShow() {
	var tFileNo;
	var tmpArray;
	var itemnos;

	var t = GetPart(1);
	if(t.preFileNo) {
		tmpArray = t.preFileNo.split('.');
		itemnos = tmpArray[0];
	} else {
		itemnos='';
	}
	for (var i=2; i<26; i++) {
		t = GetPart(i);
		if(t.preFileNo) {
			tmpArray = t.preFileNo.split('.');
			itemnos = itemnos + '|' + tmpArray[0];
		} else {
			itemnos = itemnos + '|';
		}
	}
	if(userid == '0')
		cookies	= "MyAvatar_" + usergender + "_" + userid + "=" + itemnos;
	else
		cookies	= "MyAvatar_" + userid + "=" + itemnos;
	UpdateCookies.fire();
}

function GetFileNo(itemno, layerno) {		
	var fileNo;
	var nPos=layerno.indexOf('_');
	if(nPos<0) {
		fileNo = itemno + "_" + layerno;
		return(fileNo);
	} else {
		var layerNoArray = layerno.split('_');
		var layerNoArrayLen = layerNoArray.length;

		for(var i=0; i<layerNoArray; i++) {
			fileNo[i] = layerNoArray[i] + "";
		}
	}
}

function GetFileName(nType, itemNo) {
	var fileN;

	if(localpic!=0)
		fileN = ImagePath + nType + "/" + itemNo + ".GIF";
	else
		fileN = ImagePath + itemNo + "/" + nType + "/00/";
	return fileN;
}

/******************************************************************************
* Cookie functions.																													 *
******************************************************************************/

function getCookie(name) {
	var search;
	if(!cookies||cookies=="") return "";
	search = name + "=";
	offset = cookies.indexOf(search);
	if (offset != -1) {
		offset += search.length ;
		end = cookies.indexOf(";", offset) ;
		if (end == -1) end = cookies.length;
		return unescape(cookies.substring(offset, end));
	} else
		return "";
}

/******************************************************************************
* End of cookie functions.																										*
******************************************************************************/

function GetObj(child,name,startIndex) {
	for(var i=startIndex;i<child.length;i++) {
		if(child[i].name==name) {
			return i;
		}
	}
	return -1;
}

function InitDressList(MyAvatarRec) {
	if(RecFlag == 1) {
		InitRecShow(MyAvatarRec);
		return;
	}

	if(userid == '0')
		var itemList = getCookie('MyAvatar_' + usergender + '_' + userid);
	else
		var itemList = getCookie('MyAvatar_' + userid);
	var parent = takeonrecordTr;
	var childSet = parent.all;
	var child;
	
	parent.leftIndex=0;
	if(itemList != "") {
		var tFileNo;
		var itemnoArray = new Array;
		parent.itemList = new Array;
		var itemno;
		var tmpArray;
		var itemnos='';
		var j, t;
		var count=0;
		var flag;
		for (var i=1; i<26; i++) {
			flag = 0;
			t = GetPart(i);
			if(!t.preFileNo) continue;
			tFileNo = t.preFileNo;
			tmpArray = tFileNo.split('.');
			if(tmpArray[0] < 15) continue;
			itemno = tFileNo;	
			for(j=0;j<i;j++) {
				if(!itemnoArray[j]) continue;
				if(itemnoArray[j] == itemno) {
					flag = 1;
					break;
				}
			}
			if(flag == 0) {
				itemnoArray[count] = itemno;
				if (count != 0) itemnos = itemnos + '|';
				itemnos = itemnos + itemno;
				count ++;
			}
		}
		parent.itemList = itemnoArray;
	
		var index,count;
				
		index = GetObj(childSet,"takeonrecord",0);
		if(index>=0) {
			child = childSet[index];
			child = child.all;
				
			var i,t;

			index=-1;
			for(i=0;i<parent.itemList.length;i++) {
				index=GetObj(child,"IMG",index+1);
				if(index>=0) {
					t=child[index];
					takeonrecordTr.DressOnShow[i]=t;
					t.onclick = clickDress;
				} else {
					count=i;
					break;
				}
			}
		}
		if(parent.itemList.length>count) {
			parent.viewCount = count;
			index = GetObj(childSet,"btnMoveLeft",0);
			if(index!=null) {
				child = childSet[index];
				child.onclick = LeftMove;
				child=child.children; 
				child[0].style.display = "inline";
			}
			index = GetObj(childSet,"btnMoveRight",0);
			if(index!=null) {
				child = childSet[index];
				child.onclick = RightMove;
				child=child.children;
				child[0].style.display = "inline";
			}
		} else {
			parent.viewCount = parent.itemList.length;
		}
		refreshShowIMG();
	} else {
		parent.itemList = "";
		parent.viewCount = 0;
	}
}

function RightMove() {
	var parent=takeonrecordTr;

	if(parent.viewCount>=parent.itemList.length) return;
	parent.leftIndex++;
	parent.leftIndex %= parent.itemList.length;
	refreshShowIMG();
}

function LeftMove() {
	var parent=takeonrecordTr;
	
	if(parent.viewCount>=parent.itemList.length) return;
	parent.leftIndex--;
	(parent.leftIndex<0)?parent.leftIndex+=parent.itemList.length:0;
	refreshShowIMG();
}

function refreshShowIMG()
{
	var	parent = takeonrecordTr;
	var childSet = parent.all;
	var child;
	var index;
		
	index = GetObj(childSet,"takeonrecord",0);
	if(index>=0) {
		child = childSet[index];
		child = child.all;
		index=-1;
		for(var i=0;i<2;i++) {
			index=GetObj(child,"IMG",index+1);
			if(index>=0) child[index].src="Dv_Plus/Avatar/Pic/blank.gif";
		}
	}

	var child = parent.DressOnShow;
	var baseIndex = parent.leftIndex;
	var nCount = parent.itemList.length;
	var nIndex,tFileNo,nAct,fileN,t, tmpArray;
	var	itemno, layerno;

	for(var i=0;i<parent.viewCount;i++) {
		nIndex	=(baseIndex+i)%nCount;
		tFileNo = parent.itemList[nIndex];
		tmpArray = tFileNo.split('.');
		itemno = tmpArray[0];
		layerno = tmpArray[1];
		
		var fileN;

		if(localpic!=0)	fileN = ImagePath + "0/" + itemno + ".GIF";
		else fileN = ImagePath + itemno + "/" + "00" + "/00/";
		if(child[i]!=null) {
			child[i].itemno = itemno;
			child[i].layerno = layerno;
			child[i].src=fileN;
			child[i].style.display	="inline";
		}
	}
}

function InitTable(parent) {
	parent.onclick = clickDress;
}

function InitRecShow(MyAvatarRec) {
	if(usergender == oriusergender)
		var itemList = MyAvatarRec;
	else
		var itemList = "";
	var parent = takeonrecordTr;
	var childSet = parent.all;
	var child;

	takeonrecordTr.DressOnShow = new Array;
	parent.leftIndex = 0;
	if(itemList != "") {
		var itemnoArray = new Array;

		parent.itemList = new Array;
		itemnoArray = itemList.split('|');
		parent.itemList = itemnoArray;

		var index;
		var count;
		
		index = GetObj(childSet,"takeonrecord",0);
		if(index>=0) {
			child = childSet[index];
			child = child.all;
				
			var i,t;
			
			index=-1;
			for(i=0;i<parent.itemList.length;i++) {
				index=GetObj(child,"IMG",index+1);
				if(index>=0) {
					t=child[index];
					takeonrecordTr.DressOnShow[i] = t;
					t.onclick = clickDress;
				} else {
					count=i;
					break;
				}
			}
		}
		if(parent.itemList.length>count) {
			parent.viewCount = count;
			index = GetObj(childSet,"btnMoveLeft",0);
			if(index!=null) {
				child = childSet[index];
				child.onclick = LeftMove;
				child = child.children; 
				child[0].style.display = "inline";
			}
			index = GetObj(childSet,"btnMoveRight",0);
			if(index!=null) {
				child = childSet[index];
				child.onclick = RightMove;
				child = child.children;
				child[0].style.display = "inline";
			}
		} else {
			parent.viewCount = parent.itemList.length;
		}
		refreshShowIMG();
	} else {
		parent.itemList	= "";
		parent.viewCount = 0;
	}
}

function GetAvatar() {
	var tFileNo;
	var tmpArray;
	var itemnos;

	var t = GetPart(1);
	if(t.preFileNo) {
		tmpArray = t.preFileNo.split('.');
		itemnos = tmpArray[0];
	} else {
		itemnos='';
	}
	for (var i=2; i<26; i++) {
		t = GetPart(i);
		if(t.preFileNo) {
			tmpArray = t.preFileNo.split('.');
			itemnos = itemnos + '|' + tmpArray[0];
		} else {
			itemnos = itemnos + '|';
		}
	}
	return itemnos;
}

function PutAvatar(itemnos) {
	var t, j, x;
	var layers, count;
	var showArray = itemnos.split('|');
	var ret = CheckIllegal(showArray);

	if(ret != 0) return -1;
	for (var i=0; i<25; i++) {
		j = i + 1;
		t = GetPart(j);
		if(t.preFileNo) TakeOffSingle(j);
		if(showArray[i] != '')
		{
			TakeLayerIMG(j, showArray[i]);
			layers = '';
			count=0;
			for(x=0; x<25; x++) {
				if(showArray[x] == showArray[i]) {
					if(count == 0)
						layers = (x+1);
					else
						layers = layers + '_' + (x+1);
					count ++;
				}
			}
			t.preFileNo = showArray[i] + '.' + layers;
		}
	}
	InitDressList(itemnos);
	return 0;
}
</script>
