/********************************************************************************
	ajax¸¦ ÀÌ¿ëÇÑ json data¸¦ parsingÇØ¼­ tableÀ» ±×¸®´Â class
	ÀÛ¼ºÀÚ : ÀÌÁ¤ÈÆ (ljhoon@gabia.com)
	ÀÛ¼ºÀÏ : 2009.01.13
	¼öÁ¤ÀÏ : 2009.01.15 - paging ±â´É ¿Ï·á
********************************************************************************/

//var tableXML = Common.getRoot() + "/getxml/table";

/********************************************************************************
 stCell : td¸¦ ±¸¼ºÇÏ±â À§ÇÑ ±âº» Á¤º¸¸¦ °®°Ô µÇ´Â ±¸Á¶Ã¼ ¼º°ÝÀÇ Class
     ¸¸¾à td¿¡ Á¤ÀÇ ÇØ¾ß ÇÒ ¼Ó¼ºÀÌ ´Ã¾î³ª¸é xml¿¡ ^^ ±¸ºÐÀÚ¸¦ ÀÌ¿ëÇØ¼­
     Ãß°¡¸¦ ÇÏ°í stCell class¿¡µµ ¼Ó¼ºÀ» Ãß°¡ ÇØ ÁÖ¸é µÈ´Ù.
********************************************************************************/
var stCell = Class.create();
stCell.prototype = {
	cssName : null,
	cellData : null,
	cellBuffer : null,
	cellTemplate : null,

	initialize : function(pValue, pCss)
	{
		this.cssName = (pCss == "")? "" : pCss;
		this.cellTemplate = (pValue == "")? "" : pValue;

		this.cellBuffer = this.cellTemplate;
	},

	replaceData : function (pIndex, pValue)
	{
		var delimiter = new RegExp("#" + pIndex + "#", "ig");
		this.cellBuffer = this.cellBuffer.replace(delimiter, pValue);
	},

	parseData : function()
	{
		this.cellData = this.cellBuffer;
		this.cellBuffer = this.cellTemplate;

		return;
	},

	getCss : function()
	{
		return this.cssName;
	},

	getDataTemplate : function()
	{
		return this.cellTemplate;
	},

	getData : function()
	{
		return this.cellData;
	},

	setData : function(pValue)
	{
		this.cellData = pValue;
	},

	setNullData : function()
	{
		this.cssName = "";
		this.cellData = "";
	}
};

/********************************************************************************
 stExCell : jsonÀ» ÀÌ¿ëÇØ conditionÀ» ÁÖ¾î »óÅÂ¿¡ µû¸¥ °ªÀ» ÃëÇÏ±â À§ÇÑ °´Ã¼.
		keyValue : ±âº»°ª. indexValue°¡ ÀÖ¾îµµ keyValue°ªÀ» °¡Á®¿Â´Ù.
		indexValue : conditionÀ» ÁÙ¶§¿¡ Å°°¡ µÇ´Â °ª.
			indexValueÀÇ °ª°ú µ¿ÀÏÇÑ indexÀÇ °ªÀ» °¡Á®¿Â´Ù.
********************************************************************************/
var stExCell = Class.create();
stExCell.prototype = Object.extend (new stCell, {
	cssTemplate : null,
	cssBuffer : null,

	initialize : function(pValue, pCss)
	{
		this.cssTemplate = (pCss == "")? "" : pCss;
		this.cellTemplate = (pValue == "")? "" : pValue;

		this.cssBuffer = this.cssTemplate;
		this.cellBuffer = this.cellTemplate;
	},

	replaceData : function (pIndex, pValue)
	{
		var delimiter = new RegExp("#" + pIndex + "#", "ig");
		this.cellBuffer = this.cellBuffer.replace(delimiter, pValue);
		this.cssBuffer = this.cssBuffer.replace(delimiter, pValue);
	},

	parseData : function()
	{
		try
		{
			tempCss = eval("(" + this.cssBuffer + ")");
			tempData = eval("(" + this.cellBuffer + ")");

			this.cssName = (tempCss.keyValue == null)? tempCss[tempCss.keyIndex] : tempCss.keyValue;
			this.cellData = (tempData.keyValue == null)? tempData[tempData.keyIndex] : tempData.keyValue;

			this.cssBuffer = this.cssTemplate;
			this.cellBuffer = this.cellTemplate;
		}
		catch(E)
		{
			alert("ExCell json parsing Error : " + E);
		}
	}
});

/********************************************************************************
 stPaging : paging cellÀÇ ³»¿ëÀ» À§ÇÑ ±¸Á¶Ã¼ ¼º°ÝÀÇ class
     ±âº»ÀûÀÎ stCellº¸´Ù µðÀÚÀÎÀû ¿ä¼Ò°¡ ¸¹±â ¶§¹®¿¡ »ó¼Ó¹Þ¾Æ ±¸Çö.
     paging node¸¦ ÀÎÀÚ·Î ¹Þ´Â´Ù.
     ´ÙÁß »ó¼ÓÀ» À§ÇØ ÀÓ½Ã °´Ã¼¸¦ ÇÏ³ª ´õ ¸¸µç´Ù.
     paging¶ÇÇÑ td(cell)ÀÌ±â ¶§¹®¿¡ stCell°´Ã¼¸¦ È®ÀåÇÑ´Ù.
********************************************************************************/
var stTemp = Class.create();
stTemp.prototype = Object.extend (new xmlParser, new stCell);

var stPaging = Class.create();
stPaging.prototype = Object.extend (new stTemp, {
	m_oFirst : null,
	m_oPrev : null,
	m_oNumber : null,
	m_oCurrent : null,
	m_oNext : null,
	m_oLast : null,
	m_oFunction : null,

	initialize : function(pData)
	{
		this.getPagingTemplate(pData);
	},

	getPagingTemplate : function(pData)
	{
		if (pData == null)
			return ("paging row Info is null");
		else
		{
			try
			{
				this.cssName = this.getValue(this.getNode(pData, "pagingCss"));
				this.m_oFirst = this.getValue(this.getNode(pData, "firstPage"));
				this.m_oPrev = this.getValue(this.getNode(pData, "prevPage"));
				this.m_oCurrent = this.getValue(this.getNode(pData, "currentPage"));
				this.m_oNumber = this.getValue(this.getNode(pData, "otherPage"));
				this.m_oNext = this.getValue(this.getNode(pData, "nextPage"));
				this.m_oLast = this.getValue(this.getNode(pData, "lastPage"));
				this.m_oFunction = this.getValue(this.getNode(pData, "pageFunction"));
			}
			catch(E)
			{
				alert("Paging Node select fail : " + E);
				return;
			}
		}
	},

	setData : function (pPage, pTotal, pListCount, pPagingCount)
	{
		//alert(pPage + " - " + pTotal + " - " + pListCount + " - " + pPagingCount);
		var firstPage = 1;
		var lastPage = Math.ceil(pTotal / pListCount);

		var half = parseInt(pPagingCount/2, 10);

		var startPage = (pPage > half)?(pPage - half) : 1;
		var endPage = startPage + pPagingCount - 1;

		if (pPagingCount < lastPage)
		{
			endPage = (endPage > lastPage)?lastPage : endPage;
			startPage = ( (endPage - pPagingCount) < startPage )?(endPage - pPagingCount + 1) : startPage;
		}
		else
		{
			endPage = lastPage;
			startPage = firstPage;
		}

		prevPage = ((pPage -1) <= 0)?1:pPage-1;
		nextPage = ((pPage+1) > endPage)?pPage : (pPage +1);

		var pagingBuffer = "<center><table cellpadding=\"0\" cellspacing=\"0\" class=\"pagingCell\">\n<tr>\n";

		//alert(half + " - " + startPage + " - " + endPage + " - " + lastPage);
/*
	¸Ç ³¡ ÆäÀÌÁö³ª ¸Ç ¸¶Áö¸· ÆäÀÌÁöÀÇ ¸µÅ©¸¦ ¸·±â À§ÇØ ÀÌ·± ±¸Á¶¸¦ ÃëÇÔ.
*/
		var regPage = new RegExp("#PAGE#", "ig");
		var overCursor = " onmouseover=\"this.style.cursor='pointer';\"";

		if (pPage == firstPage)
		{
			pagingBuffer += this.m_oFirst.replace(/#FUNCTION#/ig, "");
			pagingBuffer += this.m_oPrev.replace(/#FUNCTION#/ig, "");
		}
		else
		{
			pagingBuffer += (this.m_oFirst.replace("#FUNCTION#", this.m_oFunction + overCursor)).replace(regPage, firstPage);
			pagingBuffer += (this.m_oPrev.replace("#FUNCTION#", this.m_oFunction + overCursor)).replace(regPage, prevPage);
		}

		for (var i=startPage ; i<=endPage ; ++i)
			pagingBuffer += (i == pPage)? this.m_oCurrent.replace(regPage, i) : (this.m_oNumber.replace("#FUNCTION#", this.m_oFunction + overCursor)).replace(regPage, i);

		if (pPage == lastPage)
		{
			pagingBuffer += (this.m_oNext.replace("#FUNCTION#", "")).replace(regPage, nextPage);
			pagingBuffer += (this.m_oLast.replace("#FUNCTION#", "")).replace(regPage, lastPage);
		}
		else
		{
			pagingBuffer += (this.m_oNext.replace("#FUNCTION#", this.m_oFunction + overCursor)).replace(regPage, nextPage);
			pagingBuffer += (this.m_oLast.replace("#FUNCTION#", this.m_oFunction + overCursor)).replace(regPage, lastPage);
		}

		this.cellData = pagingBuffer + "</tr></table></center>";
	}
});

/********************************************************************************
 stRow : td ¹è¿­À» °®°Ô µÇ´Â row ±¸Á¶Ã¼ class
     rowº° ¹è°æ»öÀÌ ´Ù¸¥ °æ¿ì¸¦ À§ÇØ cssÀÌ¸§ ¼Ó¼ºÀ» °®´Â´Ù.
********************************************************************************/
var stRow = Class.create();
stRow.prototype = {
	m_stCells : null,
	m_oCss : null,
	length : 0,

	initialize : function()
	{
		this.m_oCss = new Array();
		this.m_stCells = new Array();
	},

	insert : function(pCell)
	{
		this.m_stCells[this.m_stCells.length] = pCell;
		this.length++;
	},

	setCss : function(pCss)
	{
		if (typeof(pCss) != "Object")
			this.m_oCss[this.m_oCss.length] = pCss;
		else if (pCss.length > 1)
		{
			var cssCount = pCss.length;
			for (var i=0 ; i<cssCount ; ++i)
				this.m_oCss[cssCount + i] = pCss[i];
		}
	},

	getCell : function(pIndex)
	{
		if (this.m_stCells.length >= pIndex)
		{
			return this.m_stCells[pIndex];
		}
	},

	getColumnCount : function()
	{
		return this.m_stCells.length;
	},

	getCssCount : function()
	{
		return this.m_oCss.length;
	},

	getCss : function(pIndex)
	{
		if (this.m_oCss.length > pIndex)
			return this.m_oCss[pIndex];
		else
			return this.m_oCss[0];
	}
};

/********************************************************************************
 TableTemplate : Table Template XMLÁ¤º¸¸¦ °¡Á®¿Â´Ù.
 (Table class »ý¼º¸¶´Ù xmlÀ» ÀÐ¾î¿À°Ô µÇ¾î ÀÖ¾î¼­ µû·Î ºÐ¸®)
********************************************************************************/
var TableTemplate = Class.create();
TableTemplate.prototype = Object.extend (new xmlParser, {
	xmlTemplate : null,
	mtableXML : '',

	initialize : function( xmlpath )
	{
		this.mtableXML = xmlpath;
		this.loadXML();
	},

/********************************************************************************
 table ±¸Á¶ xmlÀ» ÀÐ¾î ¿À´Â ºÎºÐ. µ¿±â·Î Ã³¸® ÇßÀ½.
********************************************************************************/
	loadXML : function()
	{
		//requestHeaders: [ "Connection", 'close' ],
		new Ajax.Request(this.mtableXML, {
			asynchronous: false,
			method: "get",
			onSuccess: function(xmlHttp)
			{
				try
				{
					xmlTemplate = xmlHttp.responseXML;
					return true;
				}
				catch(E)
				{
					xmlTemplate = null;
					alert("XML Load Fail : " + E);

					return false;
				}
			},
			onFailure : function (request)
			{
				xmlTemplate = null;
				alert("Table Template XML Request Fail");
				return false;
			}
		});
	},

	getTemplate : function()
	{
		return xmlTemplate;
	}
});

/********************************************************************************
 Table : stRow, stCellÀ» ÀÌ¿ëÇØ ½ÇÁ¦ Table row¸¦ ±×¸°´Ù.
********************************************************************************/
var Table = Class.create();
Table.prototype = Object.extend (new xmlParser, {
	m_oData : null,
	m_oTableID : null,
	m_oRowTemplate : null,
	m_oDefaultRowCss : null,
	xmlBuffer : null,
	m_oRows : null,
	m_nHeaderCount : 0,
	m_bPaging : true,
	m_nTotal : 0,
	m_nPagingCount :0,
	m_nListCount : 10,
	m_nPage : 1,
	m_oMessage : null,
	m_oPagingID : null,

/********************************************************************************
 pData : json data
 pTableID : target table id
 pHeaderCount : 0ÀÌ¸é Å×ÀÌºíÀÇ ¸ðµç row¸¦ Áö¿ì°í 0º¸´Ù Å©¸é ±× ¼ýÀÚ¸¸Å­ÀÇ
 			row¸¦ ³²±ä´Ù.
********************************************************************************/
	initialize : function(pTemplate, pTableID, pHeaderCount, pPaging, pListCount, pPagingCount , pPagingID)
	{
		try
		{
			this.m_oTableID = pTableID;
			this.m_nHeaderCount = pHeaderCount;
			this.m_bPaging = pPaging;
			this.m_nListCount = pListCount;
			this.m_nPagingCount = pPagingCount;
			this.m_nPage = pPaging;
		}
		catch(E)
		{
			alert("Table Init Error : " + E);
			return;
		}
	},

	hasRow : function()
	{
		return ( this.m_oData != null && this.m_oData.length != 0 );
	},

/********************************************************************************
 xml¿¡ Á¤ÀÇµÈ °¢ tdº° ¼³Á¤À» ÀÐ¾î¿Í rowTemplate °´Ã¼¸¦ ¸¸µç´Ù.
 rowº° cssÁ¤º¸µµ ¿©±â¼­ ¼³Á¤ÇÑ´Ù. css°¡ ¸í½ÃµÈ ¼ö ¸¸Å­ Àû¿ëµÈ´Ù.
 row type¿¡ µû¸¥ »ý¼ºÀ» ÇÑ´Ù.
 interface´Â new -> replaceData -> parseData ¼øÀÌ´Ù.
********************************************************************************/
	getRowTemplate : function()
	{
		if (this.xmlBuffer == null)
			return ("table row Info is null");
		else
		{
			try
			{
				this.m_oRows = null;
				this.m_oRows = new stRow();

				var objectNode = this.getNode(this.xmlBuffer, this.m_oTableID);
				var cssTemp = this.getNodeAll(objectNode, "rowCss");

				for(var i=0 ; i<cssTemp.length ; ++i)
					this.m_oRows.setCss(this.getValue(cssTemp[i]));

				var rowTemp = this.getNodeAll(objectNode, "row");

				for(var i=0 ; i<rowTemp.length ; ++i)
				{
					t = this.getAttr(rowTemp[i], "type");

					if (t == null || t == "")
						this.m_oRows.insert(new stCell(this.getValue(rowTemp[i]), this.getAttr(rowTemp[i], "css")));
					else if (t == "json")
					{
						var templateValue = this.getValue(rowTemp[i]);
						this.m_oRows.insert(new stExCell(this.getValue(rowTemp[i]), this.getAttr(rowTemp[i], "css")));
					}
				}
			}
			catch(E)
			{
				alert("Row Template Node select fail : " + E);
				return;
			}
		}
	},

/********************************************************************************
 tableÀÇ ¸ðµç row¸¦ Áö¿î´Ù.
********************************************************************************/
	clearTable : function()
	{
		if ($(this.m_oTableID).rows.length > 0)
		{
			if (this.m_nHeaderCount == 0)
			{
				for(var i=($(this.m_oTableID).rows.length-1) ; i>0 ; --i)
					$(this.m_oTableID).deleteRow(i);
			}
			else
			{
				for(var i=($(this.m_oTableID).rows.length-1) ; i>=this.m_nHeaderCount ; --i)
					$(this.m_oTableID).deleteRow(i);
			}
		}
	},

/********************************************************************************
 ½ÇÁ¦·Î table row¸¦ »ý¼º ÇÏ´Â ºÎºÐ
 row¸¦ Ãß°¡ ÇÏ±â Àü¿¡ ±âÁ¸¿¡ ±×·ÁÁø ¸ðµç row¸¦ Áö¿î´Ù.
 jsonÀÇ ÀÎµ¦½º ¸íÀº xml¿¡¼­ ¸í½ÃÇÑ delimiter¸í°ú °°¾Æ¾ß ÇÑ´Ù.
********************************************************************************/
	insertRow : function(pData)
	{
		if ($(this.m_oTableID) == null)
		{
			alert("Base Table is null.");
			return;
		}

		try
		{
			if(pData != null)
			{
				this.m_nRow = pData.result.length;
				this.m_oData = pData.result;
				//this.m_nListCount = pData.listcount;
				//this.m_nPagingCount = parseInt(pData.pagingcount, 10);
				this.m_nTotal = parseInt(pData.total, 10);
				this.m_oParam = pData.param;
				this.m_nPage = parseInt(pData.page, 10);

				this.xmlBuffer = TemplateInfo.getTemplate();
			}
			this.m_oMessage = pData.message;
		}
		catch(E)
		{
			alert("Table Draw Error : " + E);
			return;
		}

		this.clearTable();
		this.getRowTemplate();

		try
		{
			if (!this.hasRow())
			{
				var row = $(this.m_oTableID).insertRow(-1);
				var c = row.insertCell(-1);

				c.className = "nodata";
				c.colSpan = $(this.m_oTableID).rows[0].cells.length;
				c.align = 'center';
				c.innerHTML = pData.message;
				return;
			}
			for(var i=0 ; i< this.m_oData.length ; ++i)
			{
				var nLoopCount = 0;
				var row = $(this.m_oTableID).insertRow(-1);
				var rowCss = this.m_oRows.getCss( (i+1)%this.m_oRows.getCssCount() );

				if (rowCss != "") row.className = rowCss;

				for (var j=0 ; j<this.m_oRows.length ; ++j)
				{
					var c = row.insertCell(-1);
					var tempCell = this.m_oRows.getCell(nLoopCount);

					for(var tempIndex in this.m_oData[i])
						tempCell.replaceData(tempIndex, this.m_oData[i][tempIndex]);

					tempCell.parseData();

					c.innerHTML = tempCell.getData();
					c.className = tempCell.getCss();
					nLoopCount++;
				}
			}
		}
		catch(E)
		{
			alert("draw table Error : " + E);
			return;
		}
		// page Ç¥½Ã°¡ trueÀÌ¸é Ç¥½Ã ÇÑ´Ù.
		if (this.m_bPaging) this.paging();
	},

/********************************************************************************
 table pagin Ã³¸®
********************************************************************************/
	paging : function()
	{
		var pagingCell = new stPaging(this.getPagingTemplate());

		//alert(this.m_nPage + " - " + this.m_nTotal + " - " + this.m_nListCount + " - " + this.m_nPagingCount);

		// ±âº» ÆäÀÌÂ¡ Å©±âº¸´Ù ÀÛÀ¸¸é Ç¥½ÃÇÏÁö ¾Ê´Â´Ù.
		if (this.m_nTotal <= this.m_nListCount) return;
		pagingCell.setData(this.m_nPage, this.m_nTotal, this.m_nListCount, this.m_nPagingCount);

		if( this.m_oPagingID != null ) {
			$( this.m_oPagingID ).update( pagingCell.getData() );
		}
		else
		{
			var row = $(this.m_oTableID).insertRow(-1);
			var c = row.insertCell(-1);

			c.colSpan = this.m_oRows.getColumnCount();
			c.className = pagingCell.getCss();
			c.innerHTML = pagingCell.getData();
		}
	},

	getPagingTemplate : function()
	{
		if (this.xmlBuffer == null)
			return ("paging row Info is null");
		else
		{
			try
			{
				var objectNode = this.getNode(this.xmlBuffer, this.m_oTableID);
				return this.getNode(objectNode, "paging");
			}
			catch(E)
			{
				alert("Paging Node select fail : " + E);
				return;
			}
		}
	}
});

