/* Copyright (c) 2009 Smileweb co., Ltd.  All rights reserved.  www.smileweb.co.kr */
var productCate = new Array();

var productSubCateArr = new Array();	
var productCnt=0;
var ProductRealCnt=0;
var ProductTid;
var c_key;

	
function SetCate(stage, cateCode, cateNum, topNum, totalCateCode, tag) 
{
	productCate[0] = stage;
	productCate[1] = cateCode;
	productCate[2] = cateNum;
	productCate[3] = topNum;
	if(totalCateCode != null && totalCateCode != '')
		productCate[4] = totalCateCode;
	else
		productCate[4] = "000000000000";
		
	ProductCateSearch();
	
}
function ProductCateSearch()
{	
	new ajax.xhr.Request("/common/inc/cate_list.php", "stage="+ProductGetStage(productCate[0])+"&catenum="+productCate[2]+"&mode=getTitle&totalCateCode="+productCate[4]+"&"+c_key.c_addWhere, ProductGetSubCateList, 'GET');			
}

function ProductSetCate(config){	
	c_key = config;	
	SetCate(c_key.c_stage, c_key.c_cateCode, c_key.c_cateNum, c_key.c_topNum, c_key.c_totalCateCode);
}

function SetKeyMake(str)
{
	if(c_key != null && c_key.c_subPage != null && c_key.c_subPage.length > 1)
		document.location.href = c_key.c_subPage+".php?param="+str
	
	var tempKey = new Array();
	tempKey = str.split(',');
	
	var CateConfig = {
		'c_stage' : tempKey[0],
		'c_cateCode': tempKey[1],														
		'c_cateNum': tempKey[2],
		'c_topNum': tempKey[3],
		'c_totalCateCode' : tempKey[4],
		'c_addWhere' : tempKey[5]	
	}
	
	ProductSetCate(CateConfig);
	
	try { shopListConfig[3] = tempKey[4]; }catch(e){}
	
	try { Search();	}catch(e){}
}

function ProductGetSubCateList(xmlDoc)
{	
	
	var code = getNodeValue( xmlDoc.getElementsByTagName( 'code' ) );
	
	if (code == 'success') 
	{	
		var loaddata = eval( "(" + getNodeValue( xmlDoc.getElementsByTagName ( 'data' ) ) + ")" );
		
		productCate[5] = loaddata;
		ProductRealCnt = loaddata.length-1;		
		
		var cateListId = new Array('cate_list_sub1','cate_list_sub2','cate_list_sub3','cate_list_sub4','cate_list_sub5');
		for (var i = 0; i < cateListId.length; i++) {
			ChildNodesDel(cateListId[i]);
		}		
		var rowNum = 0;
		
		for(var i = 0; i < loaddata.length; i++)
		{	
			if(i == 0) {
				try {					
					var cateTitle = eval("(" + getNodeValue(xmlDoc.getElementsByTagName('cateTitle')) + ")");
					var cateTitleHtml = "<a href='javascript:void(0);' onclick='SetKeyMake(\",,,,,"+c_key.c_addWhere+"\"); return false;'>전체</a> > ";
					
					for (var j = 0; j < cateTitle.length; j++) {
						if (j > 0) cateTitleHtml += " > ";
						cateTitleHtml += "<a href='javascript:void(0);' onclick='SetKeyMake(\"" + cateTitle[j].stage +
						"," +
						cateTitle[j].cateCode +
						"," +
						cateTitle[j].cateNum +
						"," +
						cateTitle[j].topNum +
						"," +
						cateTitle[j].totalCateCode +
						"," +
						c_key.c_addWhere +
						"\"); return false;'>" +
						cateTitle[j].cateName +
						"</a>";
					}
					try{$('cateName').innerHTML = cateTitleHtml;}catch(e){}
					try{$('cateName').className ='f_blue2';}catch(e){}
					try{$('shopCnt').innerHTML = loaddata[0].num;}catch(e){}
				}catch(e){
					try{$('cateName').innerHTML = '전체 카테고리';}catch(e){}
					try{$('cateName').className = '';}catch(e){}
					try{$('shopCnt').innerHTML = loaddata[0].num;}catch(e){}
				}
			}
			
			
			if(i > 0)
			{
				if (rowNum % 5 == 0) 
					rowNum = 0;
				
				var obj = $(cateListId[rowNum]);
				
				var topTotalCateCode = ProductGetCateCode(loaddata[i].catecode);
				var h2 = document.createElement("h2");				
				var html = "<a href='javascript:void(0);' onclick='SetKeyMake(\"" + ProductGetStage(productCate[0]) +
						"," + loaddata[i].catecode +
						"," +
						loaddata[i].num +
						"," +
						loaddata[i].topnum +
						"," +
						topTotalCateCode +
						"," +
						c_key.c_addWhere +
						"\"); return false;'><strong>" +
						loaddata[i].catename+"<strong></a>";
				
				h2.innerHTML = html;
				if(i > 4)	h2.className = 'mg_t5';
				obj.appendChild(h2);
				
				if(c_key.c_subPage != null && c_key.c_subPage.length > 1)
				{
	
					new ajax.xhr.Request("/common/inc/shop_menagement.php", "cutMname=" + c_key.c_shopCutName + "&cate1=" + topTotalCateCode + "&pageNum=" + c_key.c_shopCnt + "&"+c_key.c_addWhere, SetShopCateList, 'GET');
				
					
				}
								
				rowNum++;	
				
			}
		}
		
		if (loaddata.length == 1) {			
			var obj = $('cate_list_sub1');				
			var cateTitle = eval( "(" + getNodeValue( xmlDoc.getElementsByTagName ( 'cateTitle' ) ) + ")" );
				
			var h2 = document.createElement("h2");
			
			var html = "<a href='javascript:void(0);' onclick='SetKeyMake(\"" + cateTitle[cateTitle.length - 1].stage +
				"," +
				cateTitle[cateTitle.length - 1].cateCode +
				"," +
				cateTitle[cateTitle.length - 1].cateNum +
				"," +
				cateTitle[cateTitle.length - 1].topNum +
				"," +
				cateTitle[cateTitle.length - 1].totalCateCode +
				"," +
				c_key.c_addWhere +
				"\"); return false;'><strong>" +
				cateTitle[cateTitle.length - 1].cateName+"<strong></a>";
			
			h2.innerHTML = html;			
			obj.appendChild(h2);
		}
		MakeCateShopList();
		
	}
	
	
}

function SetShopCateList(xmlDoc)
{
	productSubCateArr[productSubCateArr.length] = xmlDoc	
}

function MakeCateShopList()
{
	ProductTid = setInterval('MakeCateShopListCheck()',50);
}

function MakeCateShopListCheck()
{
	if (ProductRealCnt == productSubCateArr.length) {
		clearInterval(ProductTid);		
		
		MakeCateShopList2(0)
	}	
}

function MakeCateShopList2(i)
{
	if(i > productSubCateArr.length-1) return;
	var xmlDoc = productSubCateArr[i];
	var code = getNodeValue( xmlDoc.getElementsByTagName( 'code' ) );

	if (code == 'success') 
	{
		var loaddata = eval( "(" + getNodeValue( xmlDoc.getElementsByTagName ( 'data' ) ) + ")" );
		

		if (loaddata.length > 0) {
			var j;
			var d1 = loaddata[0].CateCode.substring(0,3);
			for(k=1; k < productCate[5].length; k++ )
			{
				
				var d2 = productCate[5][k].catecode.substring(0,3);
				if(d1 == d2)
				{
					j=k;
					break;
				}
			}
			
			var topTotalCateCode = ProductGetCateCode(productCate[5][j].catecode);
			var html2 = "SetKeyMake(\"" + ProductGetStage(productCate[0]) +
			"," +
			productCate[5][j].catecode +
			"," +
			productCate[5][j].num +
			"," +
			productCate[5][j].topnum +
			"," +
			topTotalCateCode +
			"," +
			c_key.c_addWhere +
			"\")";
			var html3 = c_key.c_shopBaseForm.replaceAll(':CATENAME:', productCate[5][j].catename).replaceAll(":CATELINK:", html2).replaceAll(":NUM:", i);
			
			$(c_key.c_shopBaseFormId).innerHTML = $(c_key.c_shopBaseFormId).innerHTML + html3;
			
			new MakeForm(loaddata, 'goodList' + (i), c_key.c_shopListForm, MakeCateShopList2, i + 1);
		}else{
			MakeCateShopList2(i+1);
		}
	}
}

function SetShopCateListCheck(str)
{
	if(str)	shopCateFlag = true;
}

function ProductGetStage(stage)
{
	switch(stage)
	{
		case "L":	return "M"; break;	
		case "M":	return "S"; break;
		case "S":	return "SS"; break;		
		default : return "L"; break;
	}
}

function ProductGetCateCode(cateCode)
{
	var ProductCateCode = new Array(4);
	ProductCateCode[0] = productCate[4].substring(0,3);
	ProductCateCode[1] = productCate[4].substring(3,6);
	ProductCateCode[2] = productCate[4].substring(6,9);
	ProductCateCode[3] = productCate[4].substring(9,12);
	
	
	for(var i=0; i < ProductCateCode.length; i++){
		
		if (parseInt(ProductCateCode[i]) == 0) {
			ProductCateCode[i] = cateCode;
			break;	
		}
	}
	
	var returnValue = "";
	for(var i = 0; i < ProductCateCode.length; i++)
	{		
			returnValue += ProductCateCode[i].toString();
	}
	
	return returnValue;
}


var svChangeBeforNum = 1;
function SVChange(num){
	$('svimg'+svChangeBeforNum).src = "/images/product/tab_shopping_special"+svChangeBeforNum+"_off.gif";	
		
	$('svimg'+num).src = "/images/product/tab_shopping_special"+num+"_on.gif";
	
	$('svList'+svChangeBeforNum).style.display='none';
		
	$('svList'+num).style.display='';
	
	svChangeBeforNum = num;
	
}
