var oMainTopic = new pandora.util.StringBuffer();
	oMainTopic.append('<div style="padding:10px; font-weight:bold" class="text_link text_13"> Hot Topics <img src="'+variable.getChild("designHost")+'img/icon_hottopic.gif" width="4" height="7" align="absmiddle">#{topics}</div>');
	html.appendData({mainTopic:oMainTopic});
	oMainTopic = null;

var oMainTopicContent = new pandora.util.StringBuffer();
	oMainTopicContent.append('<img src="'+variable.getChild("designHost")+'img/space.gif" width="10" height="2"><a id="topic_#{category_code}">#{category_codename}</a>');
	html.appendData({mainTopicContent:oMainTopicContent});
	oMainTopicContent = null;

Object.extend(pandora.main.category.prototype, pandora.util.model.prototype);

var Category = Class.create();
	Category.prototype = {
		_toggle : false,
		_super : null,
		_data : null,
		oCategory : null,

		//set initialization
		set : function(){
		},

		//set category list
		setCategoryList : function(menu, obj){
			this._super = obj;
			var divCategoty = $('category');
			var divCateTitle = $('cateTitle');

			switch(menu) {
				case "main" :
						this.oCategory = new cCategory(this, $("dCategory"), menu);
						this.setData(menu);
					break;
				case "video" :
						this.oCategory = new cCategory(this, $("dCategory"), menu);
						this.setData(menu);
					break;

				case "channel" :
						this.oCategory = new cCategory(this, $("dCategory"), menu);
/*
						var tmp = new pandora.util.StringBuffer();
						tmp.append(html.getChild('mainCategoryA'));
						var cont = html.getChild('mainCategory').toString();
						var img = null;
						var obj = oCode.get('chInclination','channel');

						for(var i=0; i<obj.length; i++){
							img = "";
							tmp.append(cont.replace(/#\{code\}/gi, obj[i][0]).replace(/#\{title\}/gi, obj[i][1]).replace(/#\{trgImg\}/gi, img));
						}
						tmp.append(html.getChild('mainCategoryB'));
						divCategoty.update(tmp.toString());
						divCategoty.setStyle({left:'130px'});
						divCateTitle.update('CHANNEL');
						tmp, cont = null;
*/
					break;
			}
		},

		//set sub category via upper category
		setData : function(menu){
//			this.viewTopic(menu);
			var url = "/data/main_json/main" + chInfoJson.langCode + ".js";
			new Ajax.Request(url,{
				method : 'get',
				onSuccess : function(res){
					this._super.setJson(res.responseText.evalJSON());
					this._super.setFeaturedVideo();
				}.bind(this),

				onException : function(){
					//throw new Error('main js fail');
				}
			});
		},

/*
		viewTopic : function(menu){
			var url = "/main_json/main_hot" + chInfoJson.langCode + ".js";
			new Ajax.Request(url,{
				method : 'get',
				onSuccess : function(res){
					var json = res.responseText.evalJSON();
					var topic = json.hotTopics;
					var tmp = new pandora.util.StringBuffer();
					var tmpl = new Template(html.getChild('mainTopicContent').toString());

					for(var i=0; i<topic.length; i++) tmp.append(tmpl.evaluate(topic[i]));

					$('cateTopic').update(html.getChild('mainTopic').toString().replace(/#\{topics\}/gi, tmp.toString()));
					topic, tmp = null;
					var category = cMainCategoryMgr.getInstance();
					category.setEventListener('topic');
				}.bind(this),

				onException : function(){
					//throw new Error('main js fail');
				}
			});
		},*/



		topicFunc : function(evt){
			var el = Event.element(evt);
			var cate_1 = el.id.toString()
			if(cate_1) {
				cate_1 = cate_1.split("_")[1];
				cate_1 = cate_1.substring(0,1);
				cate_1 = cate_1 + "0000";
				//if(el.id!="") document.location.href = "/video.ptv?c1="+cate_1+"&c2="+el.id.split("_")[1];
				if(el.id!="") document.location.href = "/video.ptv?c1="+el.id.split("_")[1];
			}
		}/*,

		setEventListener : function(tag){
			switch(tag) {
				case "topic" :
						Event.observe($('cateTopic'),'click', this.topicFunc);
					break;
			}
		}*/
	}

Object.extend(Category.prototype, pandora.main.category.prototype);

var cMainCategoryMgr = Class.create();
	cMainCategoryMgr._instance_ = null;
	cMainCategoryMgr.getInstance = function(tag){
			if(this._instance_==null) this._instance_ = new Category();
			else {
					if(tag=='new') this._instance_ = new Category();
				}
			return this._instance_;
	}







var cCategory = Class.create();
cCategory.prototype = {
	_parent : null,
	area : null,
	active : null,
	tbl : null,
	arrSubDiv : new Array(),
	btnAllCategory : null,
	areaAllCategory : null,
	dpType : 1,
	lang : "gb",
	langCode : "40000",
	arrSubPos : {
		"gb":[68, 195, 275, 379, 464, 563],
		"en":[68, 195, 275, 379, 464, 563],
		"ko":[48, 173, 246, 319, 405, 491],
		"jp":[73, 219, 305, 399, 494, 567],
		"cn":[61, 134, 207, 280, 353, 426]
	},

	menu : "main",

	initialize : function(parent, area, menu) {
		//this.area = el;
		this._parent = parent;
		this.lang = chInfoJson.clientLang ? chInfoJson.clientLang : "gb";
		this.langCode = chInfoJson.langCode ? chInfoJson.langCode : "40000";
		this.arrSubDiv[0] = null;

		this.menu = menu;
		switch(this.menu) {
			case "main" :
			case "video" :	this.setVideo(area);
				break;
			case "channel" :	this.setChannel(area);
				break;
		}
	},
/*
	setChannel : function(area) {
		var url = "/channel.ptv";
		var chType = oCode.get("chInclination");
		var html = "<div class=\"title\">CHANNEL</div>" +
		           "<div style=\"position:absolute; left:120px; bottom:-2px;\">" +
		           "	<table height=\"26px\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">" +
		           "		<tr>" +
		           "			<td class=\"top_cate\"><a href=\"" + url + "\" class=\"top_cate_link link_none\">전체보기</a> </td>";
		var cssName = new Array();
		for(var i=0;i<chType.length;i++) {
			if(chInfoJson.c1 == chType[i][0]) {
				cssName[0] = "top_cate_on_channel";
				cssName[1] = "";
			} else {
				cssName[0] = "top_cate" ;
				cssName[1] = "top_cate_link link_none";
			}
			html += "			<td width=\"3px\">&nbsp;</td>" +
		          "			<td class=\"" + cssName[0] + "\"><a href=\"" + url + "?c1=" + chType[i][0] + "\" class=\"" + cssName[1] + "\">" + chType[i][1] + "</a></td>";
		}

		html += "		</tr>" +
		        "	</table>" +
		        "</div>";

		area.update(html);
	} ,
*/
	setVideo : function(area) {
//		var currCategory = chInfoJson.c1.substr(0, 1) + "0000";
		var tCode = "";
		var html = "";
		var shtml = "";

		var img = "";
		var split = "		<td width=\"3px\">&nbsp;</td>\n";
/*
		if(this.menu != "main") {
			html = "<div class=\"title\" style=\"width:120px;\" id=\"cateTitle\">VIDEO</div>\n" +
			       "<div style=\"position:absolute; left:120px; bottom:-2px;z-index:2;\">\n";
		} else {
			html = "<div style=\"position:absolute; left:5px; bottom:-2px;z-index:2;\">\n";
		}
*/
		html = "<div style=\"position:absolute; left:5px; bottom:-2px;z-index:2;\">\n";
		html += "<table height=\"26px\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n" +
		        "	<tr>\n";

		var arrCategory = oVodCateg.getMenuLevelA();
		var arrSubCategory  = null;
		var cnt = arrCategory .length;
		var cssName = new Array();
		for(var i=0; i<cnt; i++){
			if(arrCategory[i][0] != '' && arrCategory[i][0] != '0') img = "&nbsp;<img src=\"" + variable.getChild("designHost") + "/img/topmenu_icon.gif\" width=\"9\" height=\"5\" align=\"absmiddle\">";
			else img="";
			if(i == cnt-1)	split = "";
/*
			if(currCategory == arrCategory[i][0]) {
				cssName[0] = "top_cate_on_channel";
				cssName[1] = "";
			} else {
				cssName[0] = "top_cate" ;
				cssName[1] = "top_cate_link link_none";
			}
			html += "		<td class=\"" + cssName[0] + "\" code=\"" + arrCategory[i][0] + "\"><a class=\"" + cssName[1] + "\">" + arrCategory[i][1] + "</a>" + img + "</td>\n" + split;
*/
			tCode = (arrCategory[i][0] != '' && arrCategory[i][0] != '0') ? arrCategory[i][0]+"0000" : arrCategory[i][0];

			if(this.lang == 'jp') {
				if(Prototype.Browser.Gecko) {
					var jpsize="style=\"font-size:12px;letter-spacing:-1\"";
					//74, 238, 324, 423, 522, 595
					this.arrSubPos[this.lang][0] = "71";
					this.arrSubPos[this.lang][1] = "225";
					this.arrSubPos[this.lang][2] = "307";
					this.arrSubPos[this.lang][3] = "401";
					this.arrSubPos[this.lang][4] = "495";
					this.arrSubPos[this.lang][5] = "565";
				} else {
					var jpsize = "";
				}
			} else var jpsize = "";

			// 홈 탭 숨김 2008.01.25 edina.kim
			if(i==0) var tabshow=" style=\"display:none;\"";
			else var tabshow=null;

			html += "		<td class=\"top_cate\" code=\"" + tCode + "\" " + jpsize + tabshow + "><a class=\"top_cate_link link_none\">" + arrCategory[i][1] + "</a>" + img + "</td>\n" + split;
//서브메뉴 마우스오버표시안함 (& line370)
/*			shtml += "	<div style=\"position:absolute; left:" + this.arrSubPos[this.lang][i] + "px; top:26px;display:none;\" class=\"cate_sub_table\">\n" +
			         "		<table width=\"200\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">\n";

			arrSubCategory = oVodCateg.getLevelB(i);
			for(var j=1;j<arrSubCategory.length;j++) {
				tCode = arrCategory[i+1][0] + arrSubCategory[j][0];
				shtml += "			<tr><td  class=\"cate_sub_link\" code=\"" + tCode + "\"><a>" + arrSubCategory[j][1] + "</a></td></tr>\n";
			}
			shtml  += "		</table>\n	</div>\n";
*/
//서브메뉴 새로운 타입
			//alert(tCode);
		}

		html += "</tr></table>" + shtml + "</div>\n";
		html += "<div style=\"z-index:1;\"><!-- all category area --></div>\n";
		// 2008.01.18 edina.kim 카테고리 전체보기 숨김
		html += "<div style=\"display:none;\" id=\"top_allcate_link\" class=\"allcate_bg\"><span style=\"float:left\"><a>" + oLang.get("allCategory") + "</a></span><span id=\"top_allcate_class\" class=\"allcate_dw\" ></span></div>\n";
	/*	html += "<div style=\"position:absolute; right:0px; bottom:0px; background:url(" + variable.getChild("designHost") + "/img/cate_top_rightbg.gif); background-position:top right;\">\n" +
						"	<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
						"		<tr>\n" +
						"			<td><img src=\"" + variable.getChild("designHost") + "/img/cate_top_right.gif\"></td>\n" +
						"			<td style=\"padding:4px 10px 0px 5px; font-weight:bold\"><a class=\"top_cate_link link_none\">" + oLang.get("allCategory") + "&nbsp;<img src=\"" + variable.getChild("designHost") + "/img/cate_top_down.gif\" border=\"0\" align=\"absmiddle\"></a></td>\n" +
						"		</tr>\n" +
						"	</table>\n" +
						"</div>\n";*/

		area.update(html);
	//	this.area = (this.menu != "main") ? area.down(0).next(0) : area.down(0);
		this.area =  area.down(0);

		this.setAllCategory();

		this.tbl = this.area.down(0);
		for(var i=1;i<cnt;i++) {
			this.arrSubDiv[i] = this.tbl.next(i-1);
		}
//		this.btnAllCategory = this.area.next(1).down(3).next(0).down(0);
		this.btnAllCategory = $('top_allcate_link');
		this.setEvent();
	},

	setAllCategory : function() {
		var tCode = "";
		var ahtml = "";
		ahtml  = "<div style=\"position:absolute; left:0px; top:24px;width:100%;display:none;\">\n" +
		         "	<div class=\"allcate\">\n" +
		         "		<table width=\"948\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n" +
		         "			<tr>\n";

		var arrCategory = oVodCateg.getLevelA();
		var arrSubCategory  = null;

		for(var i=1; i<arrCategory.length; i++){
			tCode = arrCategory[i][0]+"0000";
			ahtml += "				<td width=\"158\" valign=\"top\">\n" +
			         "					<div class=\"allcate_link\" style=\"padding:10px\">\n" +
//			         "						<div style=\"font-weight:bold; padding-bottom:5px\">" + arrCategory[i][1] + " <a code=\"" + tCode + "\"><img src=\"" + variable.getChild("designHost") + "/img/icon_rss.gif\" border=\"0\" align=\"absmiddle\"></a></div>\n";
				     "						<div style=\"font-weight:bold; padding-bottom:5px\">" + arrCategory[i][1] + " <a href=\"/data/main_json/VIDEO/" + arrCategory[i][0] + "/sub_hit_4_"+ this.langCode +".xml\" target=\"_rss\"><img src=\"" + variable.getChild("designHost") + "/img/icon_rss.gif\" border=\"0\" align=\"absmiddle\"></a></div>\n";
			arrSubCategory = oVodCateg.getLevelB(i-1);
			for(var j=1;j<arrSubCategory.length;j++) {
				tCode = arrCategory[i][0] + arrSubCategory[j][0];
				ahtml += "						<a code=\"" + tCode + "\">" + arrSubCategory[j][1] + "</a>" + (j+1 < (arrSubCategory.length) ? "<br>\n" : "\n");
			}
			ahtml += "					</div>\n				</td>\n";
		}
		ahtml  += "			</tr>\n" +
		          "		</table>\n" +
		          "	</div>\n" +
		          "</div>\n";

		var div = this.area.next(0) ;
		div.update(ahtml);
		this.areaAllCategory = div.down(0);
		Event.observe(this.areaAllCategory, "click", this.gotoVideo.bind(this, "all"));
		Event.observe(this.areaAllCategory, "mouseover", this.activeAllCategory.bind(this, "on"));
		Event.observe(this.areaAllCategory, "mouseout", this.activeAllCategory.bind(this, "off"));
	},

	setEvent : function() {
		var oTR =  this.tbl.rows[0];
		var objThis = this;
		var code = null;
		//var func = this.active;
		Event.observe(oTR.cells[0].down(0), "click", this.gotoHome.bind(this));	// 홈
		for(var i=2;i<oTR.cells.length;i=i+2) {
			code = oTR.cells[i].getAttribute("code");
			if(code == "0") {
				Event.observe(oTR.cells[i].down(0), "click", this.gotoVideo.bind(this, "cate"));	// 비디오 카테고리 전체
			} else {
				var obj = oTR.cells[i];//.down(0);
//				Event.observe(obj, "mouseover", this.active.bind(this, "inline"));					// 2008.01.18 edina.kim
//				Event.observe(obj, "mouseout", this.active.bind(this, "none"));					// 2008.01.18 edina.kim
				Event.observe(obj.down(0), "click", this.gotoVideo.bind(this, "cate"));

				var objSub = this.arrSubDiv[i/2];
//				Event.observe(objSub, "mouseover", this.activeSub.bind(this, objSub, "inline"));	// 2008.01.18 edina.kim
//				Event.observe(objSub, "mouseout", this.activeSub.bind(this, objSub, "none"));		// 2008.01.18 edina.kim
//				Event.observe(objSub.down(0), "click", this.gotoVideo.bind(this, "cate"));			///서브메뉴 마우스오버표시안함
			}
		}
//		Event.observe(this.btnAllCategory, "click", this.showAllCategory.bind(this));					// 2008.01.18 edina.kim
	},

	active : function(flag, evt) {
		var el = Event.element(evt);
		if(el.tagName.toUpperCase() != "TD") el = el.up(0);

		var code = el.getAttribute("code").substring(0,2);
		for(var i=1;i<this.arrSubDiv.length;i++) {
			var oTable = this.arrSubDiv[i].down(0);
			var subCode = oTable.rows[0].cells[0].getAttribute("code").substring(0,2);
			if(code == subCode) {
				this.arrSubDiv[i].style.display = flag;
				break;
			}
		}
	},

	activeSub : function(obj, flag, evt) {
		obj.style.display = flag;

		var el = Event.element(evt);
		if(el.tagName.toUpperCase() == "A") {
			el = el.up(0);
			switch(flag) {
				case "inline" : el.className = "cate_sub_link_on";	break;
				default :	el.className = "cate_sub_link";
			}
		}
	},

	activeAllCategory  : function(flag, evt) {
		var el = Event.element(evt);
		var oTbl = this.areaAllCategory.down(1);
		for(var i=0;i<oTbl.rows[0].cells.length;i++) {
			var cell = oTbl.rows[0].cells[i];
			if(el == cell || el.descendantOf(cell)) {
				switch(flag) {
					case "on" : cell.className = "allcate_link_on";	break;
					default :	cell.className = "";
				}
				break;
			}
		}
	} ,

	showAllCategory : function(evt) {
		alert('1');
		var el = Event.element(evt);
		el = (el.tagName.toUpperCase() == "IMG") ? el : el.down(0);
		if(this.areaAllCategory.style.display == "inline") {
			this.areaAllCategory.style.display  = "none";
			$('top_allcate_class').removeClassName("allcate_up");
			$('top_allcate_class').addClassName("allcate_dw");
//			el.src = variable.getChild("designHost") + "/img/cate_top_down.gif";
		} else {
			this.areaAllCategory.style.display  = "inline";
			$('top_allcate_class').removeClassName("allcate_dw");
			$('top_allcate_class').addClassName("allcate_up");
//			el.src = variable.getChild("designHost") + "/img/cate_top_up.gif";
		}
	},

	gotoHome : function() {
		location.href = "/";
	},

	gotoVideo : function(type, evt) {
		var url = "/video.ptv";
		var el = Event.element(evt);
		switch(type) {
			case "all" :
				if(el.tagName.toUpperCase() == "A") {
					var sUrl = url + "?c1=" + el.getAttribute("code") + "&dpType=" + this.dpType;
					location.href = sUrl;
				}
				break;
//			case "rss" :
//				break;
			default :	// cate
				if(el.tagName.toUpperCase() == "A") {
					el = el.up(0);
					var sUrl = url + "?c1=" + el.getAttribute("code") + "&dpType=" + this.dpType;
					location.href = sUrl;
				}
		}
	}
}

