//defined abscrat class by namespace
pandora.util.board.prototype = {
	_targetID : null,
	_super : null,
	_loadId : null,

	initialize : function(){
	},

	_getTargetID : function(){
		return this._targetID;
	},

	setUI : function(html){
		$(this._targetID).update('');
		$(this._targetID).update(html);
	},

	loadingStart : function(id,type){
		this._loadId = id;
		switch(type) {
			case "text" :
					$(this._loadId).addClassName('page_next');
					$(this._loadId).update("<img src='"+variable.getChild("designHost").toString()+"/img/waiting.gif' border='0' width='14px' height='14px'>")
				break;
			case "img" :
					$(this._loadId).src = variable.getChild("designHost").toString()+"/img/waiting.gif";
				break;
		}
	},

	loadingStop : function(){
		$(this._loadId).update('');
	}
};

//defined BoardPage class
var BoardPage = Class.create();
	BoardPage.prototype = {
		_bbsCurrPage : null,
		_bbsPageSize : null,
		_bbsLimit : null,
		_bbsTotal : null,
		_evtObj : null,
		_isEvt : false,
		nowEvt : null,
		prevEvt : null,
		totPage : null,

		setPagingInfo : function(target, currPage, pageSize, limit, obj){
			this._targetID = target;
			this._bbsCurrPage = currPage;
			this._bbsPageSize = pageSize;
			this._bbsLimit = limit;
			this._super = obj;
			this._evtObj = new BoardEvent();
		},

		getID : function(){
			return this._getTargetID();
		},

		getPagingInfo : function(total){
			this._bbsTotal = total;
			this.totPage = Math.ceil(this._bbsTotal / this._bbsLimit);
			
			var totBlock = Math.ceil(this.totPage / this._bbsPageSize);
			var iStart = 1;
			var iEnd = 0;
			var iBlockNumber = 0;
			var sPaging = new pandora.util.StringBuffer();
			var target = this._getTargetID();
			var evArr = new pandora.util.StringBuffer();

			if(this.totPage > this._bbsPageSize) {
				iBlockNumber = Math.floor((this._bbsCurrPage-1) / this._bbsPageSize);
				iStart = (iBlockNumber * this._bbsPageSize) + 1;
			}

			sPaging.append('<table border="0" align="center" cellpadding="0" cellspacing="5"><tr>');
			iEnd = iStart + this._bbsPageSize;
			if(iBlockNumber == 0 && this._bbsPageSize == 1 && iBlockNumber != (totBlock-1)) {
				sPaging.append('<td class="page"><a><img class="icon_left_no" src="'+ variable['skinHost'] +'static/blank.gif"/></a></td>');
			}else if(iBlockNumber > 0) {
				sPaging.append('<td class="page"><a title="previous page" style="cursor:pointer"><img class="icon_left" src="'+ variable['skinHost'] +'static/blank.gif" id="'+target+'_'+(iStart-1)+'" /></a></td>');
				evArr.append(target+"_"+(iStart-1));
			}
			for(var i=iStart;i<iEnd;i++) {
				if(i > this.totPage) break; 
				if(this._bbsPageSize!=1){
					if(i == this._bbsCurrPage) sPaging.append('<td class="page_on">'+i+'</td>');
					else {
						sPaging.append('<td class="page"><a id="'+target+'_'+i+'" class="number">'+i+'</a></td>');
						evArr.append(target+"_"+i);
					}
				}
			}

			if(iBlockNumber == (totBlock-1) && this._bbsPageSize == 1 && iBlockNumber != 0) {
				sPaging.append('<td class="page"><a><img class="icon_right_no" src="'+ variable['skinHost'] +'static/blank.gif"/></a></td>');
			}else if(iBlockNumber < (totBlock-1)) {
				sPaging.append('<td class="page"><a title="next page" style="cursor:pointer"><img class="icon_right" src="'+ variable['skinHost'] +'static/blank.gif" id="'+target+'_'+i+'" /></a></td>');
				evArr.append(target+"_"+i);
			}

			sPaging.append('</tr></table>');

			if(!this._isEvt){ // no event...
				this.setUI(sPaging.toString());
				if(evArr!=""){
					this.nowEvt = evArr.toComma().split(", ");
					this.setEventListener(this.nowEvt, "set");
				}
			}else{
				this.nowEvt = evArr.toComma().split(", ");
				if(this.prevEvt!="") this.setEventListener(this.prevEvt, "remove");
				this.setUI(sPaging.toString());
				if(this.nowEvt!="") this.setEventListener(this.nowEvt, "set");
			}

			this.prevEvt = this.nowEvt;
			sPaging = null;
		},

		eventFunc : function(event){
			var el = Event.element(event);
			switch(el.tagName.toUpperCase()) {
				case "A" : this.loadingStart(el.id,'text');
					break;
				case "IMG" : this.loadingStart(el.id,'img');
					break;
			}
			this._super.doMovePage(el.id.substring(el.id.lastIndexOf("_")+1, el.id.toString().length), this._getTargetID());
		},

		setEventListener : function(arr,flag){
			this._isEvt = true;
			this._evtObj.setEventListener(arr, this, flag);
		}
	};
//defined concreate class extended abstract class
Object.extend(BoardPage.prototype, new pandora.util.board());

var cBoardPageMgr = Class.create();
	cBoardPageMgr._instance_ = null;
	cBoardPageMgr.getInstance = function(tag){
			if(this._instance_==null) this._instance_ = new BoardPage();
			else { 
					if(tag=='new') this._instance_ = new BoardPage();
				}
			return this._instance_;
	};

var BoardList = Class.create();
	BoardList.prototype = {
		_col : null,
		_row : null,
		_UI : null,
		_data : null,
		_backData : null,
		_dest : null,
		_evtObj : null,
		_isEvt : null,
		_execFunc : null,
		_evtArr : null,
		_uqId : null,
		nowEvt : null,
		oneJSON : null,
		_evt : null,
		isBR : null,
		BRName : null,

		setListType : function(target, col, row, UI, dest, func, evtArr, uqid, obj){
			this._targetID = target;
			this._col = col;
			this._row = row;
			this._UI = UI;
			this._dest = dest;
			this._super = obj;
			this._evtArr = evtArr;
			this._uqId = uqid;
			this._execFunc = func;
			this._evtObj = new BoardEvent();
			this.isBR = arguments[9];
			this.BRName = arguments[10];

			Object.extend(Template.prototype, {
				evaluate : function(object, dest, size, flag){
				 return this.template.gsub(this.pattern, function(match) {
			      var before = match[1];
			      if (before == '\\') return match[2];
				  if(dest!=undefined) {
					//if(match[3] == "title" || match[3] == "contents" || match[3] == "categ_name") {
					if(match[3] == "title" || match[3] == "categ_name") {
						var str = before + String.interpret(object[match[3]]);
						if(flag==undefined) return str.truncate(size);
						else return str.wordBreak(size);
					}
					else return before + String.interpret(object[match[3]]);
				 }
			      else return before + String.interpret(object[match[3]]);
			    });
			}})
		},

		getListType : function(data, callback){
//alert(Object.toJSON(data));
			this._data = data;
			var count = 0;
			var tmp = "";
			var oList = new pandora.util.StringBuffer();
			var evArr = new pandora.util.StringBuffer();
			var tmpStr = "";
			var tmp_row = 0;
			var tmp_col = 0;
			var t_row = (this._data['total']) ?  parseInt(this._data['total'].toString(), 10) : ((this._data['prg_tot']) ? parseInt(this._data['prg_tot']) : ((this._data['ch_tot']) ? parseInt(this._data['ch_tot'])  :  parseInt(this._data['tot'].toString(), 10)));
			var rlyChk = false;
			var xwidth = 0;

			var chkBox = "";

			if(arguments[2]=="search") oList.append("<div><table border='0' cellspacing='0' cellpadding='0'>");
			else if(arguments[2]=="search2") oList.append("<div style='position: relative; width:100%'><table width='100%' border='0' cellspacing='0' cellpadding='0' style='margin-top:15px'>");
			else oList.append("<div><table border='0' cellspacing='0' cellpadding='0' width='100%'>");

			if(this._row > t_row ) tmp_row = t_row;
			else tmp_row = this._row;

			if(this._row > this._data[this._dest].length) tmp_row = this._data[this._dest].length;


			if(this._col >this._data[this._dest].length) tmp_col = this._data[this._dest].length;
			else tmp_col = this._col;

			var xwidth = Math.ceil(100 / tmp_col);

			var mod = this._data[this._dest].length%this._col;
			var mfl = Math.ceil(this._data[this._dest].length/this._col);

			if(mfl==1) tmp_row = 1;
			else tmp_row = mfl;

			for(var tr=0; tr<tmp_row; tr++){
				oList.append("<tr>");

				if(tmp_row-1==tr && mod>0) tmp_col = mod;

				for(var td=0; td<tmp_col; td++){
					count = td + tr * this._col;

					if(count==0 && typeof(this._super._isStart)!="undefined" &&!this._super._isStart) {
						this._super.setClickPrgId(this._data[this._dest][count][this._uqId]);
					}

					try{
						if(this.isBR!=undefined || this.isBR!=null) {
							if(!this.isBR) this._data[this._dest][count][this.BRName] = this._data[this._dest][count][this.BRName].toString().replace(/<br>/gi,"&nbsp;");
						}

						try{
							if(this._data[this._dest][count]['regdate'].toString().indexOf("/")>-1) this._data[this._dest][count]['regdate'] = cGMT.getGMT(this._data[this._dest][count]['regdate']);

						}catch(e){}

						if(this._uqId=="ch_userid"){
							this._data[this._dest][count]['ch_thumb'] = getChThumbnail(this._data[this._dest][count]['ch_userid'],'s');
							this._data[this._dest][count]['seq'] = ((this._super.page._bbsCurrPage-1)*(this._row*this._col))+(count+1);
						}

						// 체크 박스 활성 비 활성
						if (this._uqId == "prg_id" && this._super._clickCategory != "cate_all") {
							chkBox = "chkBox"; //_" + this._data[this._dest][count]["prg_id"].toString();
							this._data[this._dest][count][chkBox] = "";
							if (chInfoJson['mychannel'] == 1) {
								this._data[this._dest][count][chkBox] = '<input id="chk_' + this._data[this._dest][count]["prg_id"].toString() + '" type="checkbox" style="vertical-align:middle" value=""> ';
							}
						}

						if(this._uqId=="ment_id") tmpStr = this._UI.evaluate(this._data[this._dest][count],'contents',45,"wordbreak");
						else if(this._uqId=="prgID") tmpStr = this._UI.evaluate(this._data[this._dest][count],'title',15,"wordbreak");
						else if(this._uqId=="categ_id") tmpStr = this._UI.evaluate(this._data[this._dest][count],'categ_name',15,"wordbreak");
						else if(this._uqId=="iic_prgID") tmpStr = this._UI.evaluate(this._data[this._dest][count],'title',15,"wordbreak");
						else tmpStr = this._UI.evaluate(this._data[this._dest][count]);

						if(this._uqId=="ment_id"){
							rlyChk = false;
							var img = '&nbsp;<a><img id="rlyDel_'+this._data[this._dest][count][this._uqId]+'" src="'+variable.getChild('chImg').toString()+'/btn_x.gif" width="12" height="12" border="0" align="absmiddle"></a>';

							if(this._super.login_userid=="null"){
								tmpStr = tmpStr.replace(/rlyDImg/g,img);
								rlyChk = true;
							}else{
								if(this._super.login_userid ==this._super.chInfo.getInfo('ch_userid').toString()){
									tmpStr = tmpStr.replace(/rlyDImg/g,img);
									rlyChk = true;
								}else{
									if(this._super.login_userid==this._data[this._dest][count]['writer']){
										tmpStr = tmpStr.replace(/rlyDImg/g,img);
										rlyChk = true;
									}else{
										tmpStr = tmpStr.replace(/rlyDImg/g,"");
										rlyChk = false;
									}
								}
							}
						}
/*
						if (this._uqId == "prg_id") {
							// 타이틀 변경
							if (this._data[this._dest][count]["status"].toString() == "30010") { // 스크랩일 경우 타이틀 앞에 [펌] 이라고 붙여 준다.
								this._data[this._dest][count]["title"] = oLang.get('video_title_scrap') + "펌" + this._data[this._dest][count]["title"];
//								this._data[this._dest][count]["title"] = "[펌] " + this._data[this._dest][count]["title"];
							}
						}
*/

					}catch(e){
						tmpStr = "&nbsp;"
					}
					if(arguments[2]=="search2") oList.append("<td valign='top' width='"+xwidth+"%' align='center'>"+tmpStr+"</td>");
					else oList.append("<td valign='top'>"+tmpStr+"</td>");

					if(this._uqId=="ment_id"){
						for(var i=0; i<this._evtArr.length; i++){
							if(rlyChk){
								evArr.append(this._evtArr[i].toString()+"_"+this._data[this._dest][count][this._uqId]);
							}else{
								if(i==0) evArr.append(this._evtArr[i].toString()+"_"+this._data[this._dest][count][this._uqId]);
							}
						}
					}else{
						for(var i=0; i<this._evtArr.length; i++){
//							if (this._evtArr[i].indexOf("over_") > -1) {
//								evArr.append(this._evtArr[i].toString().replace("over_", "")+"_"+this._data[this._dest][count][this._uqId]);
//							} else if (this._evtArr[i].indexOf("out_") > -1) {
//								evArr.append(this._evtArr[i].toString().replace("out_", "")+"_"+this._data[this._dest][count][this._uqId]);
//							} else {
								evArr.append(this._evtArr[i].toString()+"_"+this._data[this._dest][count][this._uqId]);
//							}
						}
					}
				}
				oList.append("</tr>");
			}
			oList.append("</table></div>");

			//if(this._targetID!="reply_content") this._super.json = data;
			if(this._targetID=="ch_program_list") this._super.json = data; 
			if(typeof(this._super._isStart)!="undefined" && !this._super._isStart) this._super._isStart = true;

			this.nowEvt = evArr.toComma().split(", ");

			// no event...
			if(this._isEvt && this.prevEvt!="") this.setEventListener(this.prevEvt, "remove");

			this.setUI(oList.toString());

			if(this.nowEvt!="") this.setEventListener(this.nowEvt, "set");

			this.prevEvt = this.nowEvt;

			oList = null;
			if(callback!=undefined) callback(this._data[this._dest]);
		},

		doPlay : function(prg_id, idStr, evt){
			this._evt = evt;
			this._execFunc(prg_id, idStr);
		},

		getOneJSON : function(target, prg_id){
			var ret = null;
			for(var i=0; i<this._data[this._dest].length; i++){
				if(this._data[this._dest][i][target] == prg_id) ret = this._data[this._dest][i];
			}
			return ret;
		},

		eventFunc : function(event){
			var el = Event.element(event);
			if (event.type == "click") {
				this.doPlay(el.id.substring(el.id.indexOf("_")+1, el.id.toString().length), el.id.substring(0, el.id.indexOf("_")),event);
			} else {
				//alert(event.type);
			}
		},

		setEventListener : function(arr, flag){
			this._isEvt = true;
			try{this._evtObj.setEventListener(arr, this, flag);}catch(e){}
		}

	};
Object.extend(BoardList.prototype, new pandora.util.board());

var cBoardListMgr = Class.create();
	cBoardListMgr._instance_ = null;
	cBoardListMgr.getInstance = function(tag){
			if(this._instance_==null) this._instance_ = new BoardList();
			else {
					if(tag=='new') this._instance_ = new BoardList();
				}
			return this._instance_;
	};


var BoardEvent = Class.create();
	BoardEvent.prototype = {
		_super : null,

		initialize : function(){
		},

		setEventListener : function(arr,obj,flag){
			this._super = obj;
			this._super.evt = this._super.eventFunc.bindAsEventListener(this._super);
			var eventStatus = "click";
			for(var i=0; i<arr.length; i++){
				switch(true) {
					case (arr[i].indexOf("over_") > -1) :
							eventStatus = "mouseover";
							arr[i] = arr[i].replace("over_", "")
							//this._super.evt = this._super.eventFunc.bindAsEventListener(this._super);
						break;
					case (arr[i].indexOf("out_") > -1) :
							eventStatus = "mouseout";
							arr[i] = arr[i].replace("out_", "")
							//this._super.evt = this._super.eventFunc.bindAsEventListener(this._super);
						break;
					default :	eventStatus = "click";
				}
				
				switch(flag) {
					case "set" : Event.observe(arr[i], eventStatus, this._super.evt);
						break;
					case "remove" : Event.stopObserving(arr[i], eventStatus, this._super.evt);
						break;
				}
			}
		}
	};
