
var n1ItemEquipPos_Weapon	= 1;
var n1ItemEquipPos_Armor	= 2;
var n1ItemEquipPos_Shield   = 3;
var n1ItemEquipPos_Hat		= 4;
var n1ItemEquipPos_Shoes	= 13;
var n1ItemEquipPos_Acc		= 14;
var n1ItemEquipPos_Acc_1	= 41;
var n1ItemEquipPos_Clothes	= 15;
var n1ItemEquipPos_Acc2		= 22;


var n1ItemEquipPos_Hair		= 51;
var n1ItemEquipPos_Body		= 52;


var n4LayerNo_First	= 0;
var n4LayerNo_Weapon = 1;
var n4LayerNo_Armor = 5;
var n4LayerNo_Shield = 33;
var n4LayerNo_Hat = 15;
var n4LayerNo_Shoes = 20;
var n4LayerNo_Acc = 31;
var n4LayerNo_Clothes = 30;
var n4LayerNo_Acc2 = 35;

var n4LayerNo_Hair = 3;
var n4LayerNo_Body = 2;



function LodCharacterRenderer()
{
	this.bNewHeadType = 0;
	
	this.codeSex = 0;
	this.codeDirection = 0;			//1:Àü¹æ 2:¿ìÃø 3:ÈÄ¹æ 4:ÁÂÃø
	this.codeEmotion = 0;
	
	this.n1HeadTile = 0;
	this.n1HairTile = 0;
	this.n1HairColor = 0;
	this.n1RidingAnimal = 0;
	this.isShowHelmet = 0;
	
	this.n1MaxItemEquipPos = 27;
	this.arrItems = new Array( this.n1MaxItemEquipPos+1 );
	
	this.renderingArea = null;
	this.strIViewFromSession = '';
	
	
	// Lod
	this.n4Face			= 0;
	this.n4SecondFace	= 0;
	this.n4Hair			= 0;
	this.n4Body			= 0;
	this.n4SecondBody	= 0;
	this.n1Color		= 0;
	
	this.PutOn = LodCharacterRenderer_PutOn;
	this.TakeOff = LodCharacterRenderer_TakeOff;
	this.WearByProductGUID = LodCharacterRenderer_WearByProductGUID;
	this.ToggleWear = LodCharacterRenderer_ToggleWear;
	this.ClearAll = LodCharacterRenderer_ClearAll;
	this.Restore = LodCharacterRenderer_Restore;
	this.SetDirection = LodCharacterRenderer_SetDirection;
	
	this.DeserializeView = LodCharacterRenderer_DeserializeView;
	this.SerializeView = LodCharacterRenderer_SerializeView;
	
	this.GetRenderString = LodCharacterRenderer_GetRenderString;
	this.Render = LodCharacterRenderer_Render;

	// private
	this.Parse = LodCharacterRenderer_Parse;
	this.ParseECString = LodCharacterRenderer_ParseECString;
	this.ParseCharString = LodCharacterRenderer_ParseCharString;
	this.ParseItemString = LodCharacterRenderer_ParseItemString;
	
	this.RenderBody = LodCharacterRenderer_RenderBody;
	this.RenderItem = LodCharacterRenderer_RenderItem;
	this.RenderFace = LodCharacterRenderer_RenderFace;
	
	
	if ( typeof( g_strImgPath ) == "undefined" ) 
	{
		g_strImgPath = "./image";
	}
}


function LodCharacterRenderer_DeserializeView( strIView )
{
	var strIView_cookie = getCookie("strImageView");
	this.ClearAll();
	
	if ( strIView_cookie != null && strIView_cookie != "") 
	{
		this.Parse( strIView_cookie );
	} else 
	{
		this.Parse( strIView );
		
	}
	
}

function LodCharacterRenderer_SerializeView()
{
	
	var strIView = "LO:"+this.codeDirection+","+this.codeSex+"|393218:"+this.n4Hair+","+this.n1Color+"|393219:";
	
	for ( var itemIdx=0; itemIdx<=this.n1MaxItemEquipPos; itemIdx++ )
	{
		if ( this.arrItems[ itemIdx ] != undefined )
		{
			if ( this.arrItems[ itemIdx ].strName != undefined ) {
			  
				strIView += this.arrItems[itemIdx].n4ItemIndex+","+
					this.arrItems[itemIdx].strName +","+
					this.arrItems[itemIdx].strTileNo + ","+ 
					this.arrItems[itemIdx].strPart +","+
					this.arrItems[itemIdx].n4Tile +"," +
					this.arrItems[itemIdx].strCol +"," +
					this.arrItems[itemIdx].n4ItemGen +";";
			}
		}
	}
	
	return strIView;
}

/*private*/ function LodCharacterRenderer_Parse( /*string*/strIView )
{
	var strIViewBundles = strIView.split( rendererConstants.separator_bundle );
	
	for ( var bdIndex=0; bdIndex<strIViewBundles.length; bdIndex++ )
	{
		var strIViewBundle = strIViewBundles[ bdIndex ];
		
		var strIViewHeadBody = strIViewBundle.split( rendererConstants.separator_headerBody );
		
		if ( strIViewHeadBody[0] == 'LO' )
		{
			this.ParseECString( strIViewHeadBody[1] );
		}
		else if ( strIViewHeadBody[0] == '393218' )
		{
			this.ParseCharString( strIViewHeadBody[1] );
		}
		else if ( strIViewHeadBody[0] == '393219' )
		{
			this.ParseItemString( strIViewHeadBody[1] );
		}
		else
		{
			//throw 'IViewHeader Invalid.'+strIViewHeadBody[0];
			//alert( 'IViewHeader Invalid.'+strIViewHeadBody[0] );
			this.ParseItemString( strIViewHeadBody[1] );
		}
	}
}

/*private*/ function LodCharacterRenderer_ParseECString( /*string*/strIViewBody )
{
	var strItems		= strIViewBody.split( rendererConstants.separator_property );
	this.codeDirection	= Number( strItems[0] );
	this.codeSex		= Number( strItems[1] );
}

/*private*/ function LodCharacterRenderer_ParseCharString( /*string*/strIViewBody )
{
	var strItems = strIViewBody.split( rendererConstants.separator_property );
	
	this.n4Hair			= Number( strItems[0] );
	this.n1Color		= Number( strItems[1] );
}

/*private*/ function LodCharacterRenderer_ParseItemString( /*string*/strIViewBody )
{

	var strItems = strIViewBody.split( rendererConstants.separator_item );
	
	for ( var itemIdx=0; itemIdx<strItems.length; itemIdx++ )
	{
		var strItemProperties = strItems[ itemIdx ].split( rendererConstants.separator_property );
		this.arrItems[ Number( strItemProperties[0] ) ] = new ItemInfo( Number(strItemProperties[0]), strItemProperties[1], strItemProperties[2] , strItemProperties[3] , strItemProperties[4], strItemProperties[5], strItemProperties[6]  );
		
	}
}

function LodCharacterRenderer_GetRenderString()
{
	var strRet = "";
	var strImgTag;
	
	this.strRenderingString = '';
	
	this.RenderFace();
	this.RenderBody();
	this.RenderItem();

	return this.strRenderingString;
}

function LodCharacterRenderer_Render()
{
	if ( this.renderingArea != null )
		this.renderingArea.innerHTML = this.GetRenderString();
	
	setCookie("strImageView", this.SerializeView() );
}

function LodCharacterRenderer_RenderFace( )
{
	var strGender;
	var isHairChk = 0;
	
	if ( this.codeSex == "1" )
		strGender = "wh"
	else
		strGender = "mh"
	
	strGender += PadLeft( this.n4Hair, 3, 0 )
	
	// Çì¾î°¡ ÀÖ´Â°æ¿ì ±âº» ¸Ó¸®¸¦ º¸¿©ÁÖÁö ¾Ê´Â´Ù...
	for ( var itemIdx=0; itemIdx<=this.n1MaxItemEquipPos; itemIdx++ )
	{
		if ( this.arrItems[ itemIdx ] == undefined )
			continue;
		
		if ( this.arrItems[ itemIdx ].n4ItemIndex == 4 ) 	
			isHairChk = 1;
	}
	
	if ( isHairChk != 1 ) {
		var strHeadImgTag = "<img src='" + g_strImgPath + "/_H/" + strGender + "_" + PadLeft( this.n1Color, 2, 0 ) +".gif' onError=\"this.src='http://s.nx.com/s2/game/lod/static/game/nexon/Lod/itemImage/bg_blank.gif'\">";
		this.strRenderingString += DivisionCoating( "head", GetLayerNumber( n1ItemEquipPos_Hair , 0 ), strHeadImgTag );
	}
}

function LodCharacterRenderer_RenderBody( )
{
	
	var strGender;
	
	if ( this.codeSex == "1" )
		strGender = "wb001"
	else
		strGender = "mb001"
	var strBodyImgTag = "<img src='" + g_strImgPath + "/_B/"+strGender+".gif' onError=\"this.src='http://s.nx.com/s2/game/lod/static/game/nexon/Lod/itemImage/bg_blank.gif'\">";
	this.strRenderingString += DivisionCoating( "body", GetLayerNumber( n1ItemEquipPos_Body , 0 ), strBodyImgTag );
	
}


function LodCharacterRenderer_RenderItem()
{
	for ( var itemIdx=0; itemIdx<=this.n1MaxItemEquipPos; itemIdx++ )
	{
		if ( this.arrItems[ itemIdx ] == undefined )
			continue;
			
		if ( this.arrItems[ itemIdx ].strName == undefined )
			continue;
		
		var n4ItemIndex		= this.arrItems[ itemIdx ].n4ItemIndex;
		var strTileNo		= this.arrItems[ itemIdx ].n4Tile;
		var strItemCol		= this.arrItems[ itemIdx ].strCol;
		var n4ItemGender	= this.arrItems[ itemIdx ].n4ItemGen;
		var strItemNo		= this.arrItems[ itemIdx ].strTileNo;
		
		var strGender;
		
		//alert( n4ItemIndex +":"+ strTileNo +":"+ strItemCol +":"+ n4ItemGender +":"+ strItemNo +":" );
		
		if ( n4ItemGender == 2 || n4ItemIndex == 1) 
		{
			strGender = "m"
		} else 
		{
			if ( this.codeSex == "1" )
				strGender = "w"
			else
				strGender = "m"
		}
		
		if ( ( n4ItemIndex == 5 ) || ( n4ItemIndex == 6 ) || ( n4ItemIndex == 7 ) || ( n4ItemIndex == 8 ) ||
				( n4ItemIndex == 9 ) || ( n4ItemIndex == 10 ) || ( n4ItemIndex == 11 ) || ( n4ItemIndex == 12 ) )
			continue;
		
		if ( n4ItemIndex == 1 ) 
		{
			var temp = strItemCol;
			strTileNo = parseInt(temp, 16);
			//strTileNo = strItemCol;
		}
		else
		{
			strTileNo = strItemCol;
		}
			
		var strImgFolderName = GetFolderName( n4ItemIndex );
		
		if ( strImgFolderName != undefined ) {
			
			var strItemImgTag;
/*			
		if ( ( n4ItemIndex == 4 )  || ( n4ItemIndex == 13 )  || ( n4ItemIndex == 14 )) {
			if ( ( strItemCol == 0 ) && ( n4ItemIndex == 14 ) ) {
				strItemImgTag = "<img src='" + g_strImgPath + "/" + strImgFolderName + strGender + GetItemName(n4ItemIndex ) + PadLeft( strTileNo, 3, 0 ) + ".gif' onError=\"this.src='http://static.nexon.co.kr/game/nexon/Lod/itemImage/bg_blank.gif';\">";
//					strItemImgTag = "<img src='" + g_strImgPath + "/" + strImgFolderName + strGender + GetItemName(n4ItemIndex ) + PadLeft( strTileNo, 3, 0 ) + ".gif' onError=this.src='http://static.nexon.co.kr/game/nexon/Lod/itemImage/bg_blank.gif'>";
			} else {
				strItemImgTag = "<img src='" + g_strImgPath + "/" + strImgFolderName + strGender + GetItemName(n4ItemIndex ) + PadLeft( strTileNo, 3, 0 ) + "_" + PadLeft( strItemCol, 2, 0 ) + ".gif' onError=\"this.src='" + g_strImgPath + "/" + strImgFolderName + strGender + GetItemName(n4ItemIndex ) + PadLeft( strTileNo, 3, 0 ) + ".gif'\">";
//					strItemImgTag = "<img src='" + g_strImgPath + "/" + strImgFolderName + strGender + GetItemName(n4ItemIndex ) + PadLeft( strTileNo, 3, 0 ) + "_" + PadLeft( strItemCol, 2, 0 ) + ".gif' onError=this.src='http://static.nexon.co.kr/game/nexon/Lod/itemImage/bg_blank.gif'>";
			}
		}
*/
		if ( ( n4ItemIndex == 4 )  || ( n4ItemIndex == 13 ) )
		{
			_strItemImgTag_plus = g_strImgPath + "/" + strImgFolderName + strGender + GetItemName( n4ItemIndex ) + PadLeft( strTileNo, 3, 0 ) + "_" + PadLeft( strItemCol, 2, 0 ) + ".gif";
			_strItemImgTag = g_strImgPath + "/" + strImgFolderName + strGender + GetItemName( n4ItemIndex ) + PadLeft( strTileNo, 3, 0 ) + ".gif";
			
			strItemImgTag = "<img src='" + _strItemImgTag_plus + "' onError='onImageError(this, \"" + _strItemImgTag + "\")' style='display:none' onload='this.style.display=\"\";'>";
		}
		else  
			strItemImgTag = "<img src='" + g_strImgPath + "/" + strImgFolderName + strGender + GetItemName( n4ItemIndex ) + PadLeft( strTileNo, 3, 0 )  +".gif' onError=\"this.src='http://s.nx.com/s2/game/lod/static/game/nexon/Lod/itemImage/bg_blank.gif'\">";
					
			//alert (n4ItemIndex +"|" + strItemImgTag );
			//window.open(g_strImgPath + "/" + strImgFolderName + strGender + GetItemName( n4ItemIndex ) + PadLeft( strTileNo, 3, 0 )  +".gif");
			
			if ( chkAcc(strItemNo) == "2" ) 
			{
				this.strRenderingString += DivisionCoating( "weapon", GetLayerNumber( n4ItemIndex , 0, this.codeDirection ), strItemImgTag );
			} else if ( chkAcc(strItemNo) == "3" ) 
			{
				this.strRenderingString += DivisionCoating( "weapon", GetLayerNumber( n1ItemEquipPos_Hair , 0, this.codeDirection ), strItemImgTag );
			} else
			{
				this.strRenderingString += DivisionCoating( "weapon", GetLayerNumber( n1ItemEquipPos_Acc_1 , 0, this.codeDirection ), strItemImgTag );
			}
			
//			alert(this.strRenderingString);
//			var testArea  = window.open();
//			testArea.document.write( this.strRenderingString );
		}
	}
}

function onImageError(obj, strReplaceImageUrl )
{
	if ( obj.src == strReplaceImageUrl )
		obj.src="http://s.nx.com/s2/game/lod/static/game/nexon/Lod/itemImage/bg_blank.gif";
	else
		obj.src=strReplaceImageUrl;
}

function chkAcc ( strItemNo ) 
{
	if ( strItemNo == 8101 || strItemNo == 8103 || strItemNo == 8100 || strItemNo == 7992 || strItemNo == 7991  || strItemNo == 7990 )
		return 1;
	else if ( strItemNo == 8033 )
		return 3;
	else
		return 2;
}

function GetFolderName ( n4ItemIndex ) 
{
	if ( n4ItemIndex == 1 ) {
		return "_W/";
	} 
	if ( n4ItemIndex == 2 ) {
		return "_U/";
	} 
	if ( n4ItemIndex == 3 ) {
		return "_S/";
	} 
	if ( n4ItemIndex == 4 ) {
		return "_H/";
	} 
	if ( n4ItemIndex == 13 ) {
		return "_L/";
	} 
	if ( n4ItemIndex == 14 ) {
		return "_C/";
	} 
	if ( n4ItemIndex == 15 ) {
		return "_I/";
	} 
	if ( n4ItemIndex == 22 ) {
		return "_G/";
	} 
}

function GetItemName ( n4ItemIndex ) 
{
	if ( n4ItemIndex == 1 ) {
		return "w";
	} 
	if ( n4ItemIndex == 2 ) {
		return "u";
	} 
	if ( n4ItemIndex == 3 ) {
		return "s";
	} 
	if ( n4ItemIndex == 4 ) {
		return "h";
	} 
	if ( n4ItemIndex == 13 ) {
		return "l";
	} 
	if ( n4ItemIndex == 14 ) {
		return "c";
	} 
	if ( n4ItemIndex == 15 ) {
		return "i";
	} 
	if ( n4ItemIndex == 22 ) {
		return "g";
	} 
}



function _BCharViewCore_GetImgTag_Body( strIndex, strDirection, strColorset, strEmotion, strWeapon )
{
	if ( strEmotion == "00" && strWeapon != "" )
	{
		// EmotionÀÌ StandÀÌ°í WeaponÀ» »ç¿ëÇÏ´Â °æ¿ì¿¡´Â ´Ù¸¥ ÀÌ¹ÌÁö¸¦ »ç¿ëÇÏ°Ô µÈ´Ù.
		strWeapon = "_" + strWeapon;
	}
	else
	{
		strWeapon = "";	
	}
	
	return "<img src='" + g_strImgPath + "/body/" + strIndex + "/" + strDirection + "_" + strColorset + "_" + strEmotion + strWeapon +"_0.gif'>";
}

function GetLayerNumber( /*tinyint*/n4ItemIndex, /*int*/n4TileNum, codeDirection )
{
	var nLayer = 0;
	
	switch ( n4ItemIndex )
	{
		case n1ItemEquipPos_Hair :					
			nLayer = n4LayerNo_Hair;
			break;	
		case n1ItemEquipPos_Body :					
			nLayer = n4LayerNo_Body;
			break;	
		case n1ItemEquipPos_Weapon :					
			nLayer = n4LayerNo_Weapon;
			break;	
		case n1ItemEquipPos_Armor :					
			nLayer = n4LayerNo_Armor;
			break;
		case n1ItemEquipPos_Shield :					
			nLayer = n4LayerNo_Shield;
			break;	
		case n1ItemEquipPos_Hat :					
			nLayer = n4LayerNo_Hat;
			break;	
		case n1ItemEquipPos_Shoes :					
			nLayer = n4LayerNo_Shoes;
			break;	
		case n1ItemEquipPos_Acc :					
			nLayer = n4LayerNo_Acc;
			break;	
		case n1ItemEquipPos_Clothes :					
			nLayer = n4LayerNo_Clothes;
			break;	
		case n1ItemEquipPos_Acc2 :					
			nLayer = n4LayerNo_Acc2;
			break;	
		case n1ItemEquipPos_Acc_1 :					
			nLayer = n4LayerNo_First;
			break;		
	}
	
	return nLayer; 
}

function DivisionCoating( strId, nLayer, strImgTag )
{
	if ( strImgTag != "" )
	{
		return "<div id=" + strId + " style='position:absolute; z-index:" + nLayer + "'>" + strImgTag + "</div>";
	}
	else
	{
		return "";
	}
}

function LodCharacterRenderer_PutOn( oidItem )
{
	this.arrItems[ arrItemInfos[ oidItem ].n4ItemIndex ] = arrItemInfos[ oidItem ];
	this.Render();
}

function LodCharacterRenderer_TakeOff( oidItem )
{
	this.arrItems[ arrItemInfos[ oidItem ].n4ItemIndex ] = undefined;
	this.Render();
}

// ÀÌ¹ÌÁö¸¦ À§ÇØ ÄíÅ° ÀÐ¾î¿À´Â ÇÔ¼ö
function getCookie(name)
{
	var first; 
	var str = name + "=";
	if(document.cookie.length > 0) {
		find = document.cookie.indexOf(str);
		if(find == -1) return null; 
		first = find + str.length;
		end = document.cookie.indexOf(";", first);
		if(end == -1) 
			end = document.cookie.length;
		return unescape(document.cookie.substring(first, end));
	}
}

// ÀÌ¹ÌÁö¸¦ ÀÔ·ÂÇÏ±â À§ÇÑ ÄíÅ°¼³Á¤
function setCookie(name, value) 
{ 
	//ÄíÅ°¼³Á¤
	document.cookie = name + "=" + escape( value ) + "; path=/; domain=nexon.com"; 
}

function LodCharacterRenderer_WearByProductGUID ( n4ProductGUID, n4ProductNo )
{
	if ( arrGoods[ n4ProductGUID ] == undefined )
		return; 
		
	var objSelectedGoods = arrGoods[ n4ProductGUID ];
	
	for ( var itemIdx=0; itemIdx<objSelectedGoods.length; itemIdx++ )
	{
		this.PutOn( objSelectedGoods[ itemIdx ] );
		
	}
}

function LodCharacterRenderer_ToggleWear( oidItem )
{
	if ( arrItems[ arrItemInfos[ oidItem ].n4ItemIndex ] == undefined )
	{
		this.PutOn( oidItem );
	}
	else
	{
		this.TakeOff( n1EquipPos );
	}
	this.Render();
}

function LodCharacterRenderer_ClearAll( )
{
	for ( var itemIdx=0; itemIdx<=this.n1MaxItemEquipPos; itemIdx++ )
	{
		this.arrItems[ itemIdx ] = undefined;
	}
	this.Render();
}

function LodCharacterRenderer_Restore()
{
	setCookie( "strImageView", "" );
	
	this.DeserializeView( this.strIViewFromSession );
	this.SetDirection( 1 );
	this.Render();
}

function LodCharacterRenderer_SetDirection( codeDirection )
{
	if ( codeDirection == 1 ) {
		this.codeDirection = 2;
	} else if ( codeDirection == 2 ) {
		this.codeDirection = 1;
	} else if ( codeDirection == 3 ) {
		this.codeDirection = 0;
	} else if ( codeDirection == 4 ) {
		this.codeDirection = 3;
	}	
	return 0;
}

