﻿	function xmlList()
	{
		this.initList = initList;
		this.initStory = initStory;
		
		this.pageURL = "";
		this.pagePath = "";
		this.xmlStoryFilePattern = "";
		
		this.queryString_keys = new Array();
		this.queryString_values = new Array();
		
		this.npage = 1;
		this.pageterm = 10;
		this.listnum = 10;
		
		this.xmlDoc = null;
		this.xmlSection = null;
		
		this.otherParameter = "";

		this.getParameter = getParameter;
		this.getOtherParameter = getOtherParameter;
		this.syncXMLDocument = syncXMLDocument;
		this.geonlyfileName = geonlyfileName;

		this.getList = getList;
		this.getNavigation = getNavigation;
		
		this.getPrevLink = getPrevLink;
		this.getNextLink = getNextLink;
		this.getPrevTitle = getPrevTitle;
		this.getNextTitle = getNextTitle;

		this.getListLink = getListLink;
		
		this.prevArtURL;
		this.nextArtURL;
		this.ArtPrevTitle;
		this.ArtNextTitle;

	}

	/*********************** story only *****************/
	function initStory( catId, artId, xmlListFileUrl, listnum, pageterm, xmlStoryTplId, formIndex)
	{
		pageHref = self.location.href ;
		if(listnum != null) this.listnum = listnum;
		if(pageterm != null) this.pageterm = pageterm;

		try {
			if ( pageHref.indexOf('?') >=0 )
			{ 
				this.pageURL = pageHref.substring(0, pageHref.indexOf('?'));
			}
			else
			{
				this.pageURL = pageHref;
			}
		}
		catch(e) 
		{
			this.pageURL = '';
		}
		
		var prevArtId = null;
		var nextArtId = null;
		var thisIndex = null;
		
		this.xmlDoc = this.syncXMLDocument(this.geonlyfileName(xmlListFileUrl));
		this.xmlSection = this.xmlDoc.getElementsByTagName('article');

		for (j=0 ; j < this.xmlSection.length ; j++)
		{
			if (this.xmlSection[j].childNodes[1].text == artId )
			{ 
				try{
					prevArtId = this.xmlSection[j-1].childNodes[1].text
				} catch(e) {  
					prevArtId = null;
				}

				try{
					nextArtId = this.xmlSection[j+1].childNodes[1].text
				} catch(e) {  
					nextArtId = null;					
				}

				thisIndex = this.xmlSection[j].childNodes[0].text

				break; 
			}
		}


		// 프리뷰일때랑 퍼블리싱 일때랑 다르게 처리해야 하는데...
		
		
		
		this.xmlStoryFilePattern = pageHref.substring(0, pageHref.lastIndexOf("/"))+"/<!--artId-->_"+xmlStoryTplId+".xml";
		


		if (prevArtId != null) 
		{	
			xmlStoryFileUrl = this.xmlStoryFilePattern.replace("<!--artId-->", prevArtId);
			prevDoc = syncXMLDocument(this.geonlyfileName(xmlStoryFileUrl));
			prevSection = prevDoc.getElementsByTagName('data');
			this.prevArtURL = prevSection[formIndex].text;
			this.ArtPrevTitle = prevSection[0].text;
		}
		
		
		if (nextArtId != null) 
		{
			xmlStoryFileUrl = this.xmlStoryFilePattern.replace("<!--artId-->", nextArtId);
			nextDoc = syncXMLDocument(this.geonlyfileName(xmlStoryFileUrl));
			nextSection = nextDoc.getElementsByTagName('data');
			this.nextArtURL = nextSection[formIndex].text;
			this.ArtNextTitle = nextSection[0].text;
		}
		
		this.npage = parseInt(thisIndex / 10) + 1

	}

	function getPrevLink(prevStr)
	{
		if (this.prevArtURL != null) {
			document.write ("<a href=" + this.prevArtURL + ">"+prevStr+"</a>")  
		} else {
			document.write (prevStr);
		}
	}

	function getPrevTitle(msg)
	{
		if (this.prevArtURL != null) {
			document.write ("<a href=" + this.prevArtURL + ">"+this.ArtPrevTitle+"</a>")  
		} else {
			document.write (msg);
		}
	}

	
	function getNextLink(nextStr)
	{	
		if (this.nextArtURL != null) {
			document.write ("<a href=" + this.nextArtURL + ">"+nextStr+"</a>");
		} else {
			document.write (nextStr);
		}
	}

	function getNextTitle(msg)
	{	
		if (this.nextArtURL != null) {
			document.write ("<a href=" + this.nextArtURL + ">"+this.ArtNextTitle+"</a>");
		} else {
			document.write (msg);
		}
	}

	function getListLink(listStr, listUrl)
	{	
		if (listUrl != null) {
			if(listUrl.indexOf("?") == -1)
				listUrl += "?npage=" + this.npage;
			else
				listUrl += "&npage=" + this.npage;
			
			document.write ("<a href=" + listUrl + ">"+listStr+"</a>");
		}
	}
	
	/*********************** story only *****************/

	
	function initList( catId, xmlListFileUrl, listnum, pageterm, xmlStoryTplId)
	{
		pageHref = self.location.href ;
		if(listnum != null) this.listnum = listnum;
		if(pageterm != null) this.pageterm = pageterm;

		try {
			if ( pageHref.indexOf('?') >=0 )
			{ 
				this.pageURL = pageHref.substring(0, pageHref.indexOf('?'));
			}
			else
			{
				this.pageURL = pageHref;
			}
		}
		catch(e) 
		{
			this.pageURL = '';
		}
		
		// 프리뷰일때랑 퍼블리싱 일때랑 다르게 처리해야 하는데...
		
		
		
		this.xmlStoryFilePattern = pageHref.substring(0, pageHref.lastIndexOf("/"))+"/<!--artId-->_"+xmlStoryTplId+".xml";
		
		
		var query = self.location.search.substring(1);
		var pairs = query.split("&");
		for ( var i = 0; i < pairs.length; i++ )
		{
			var pos = pairs[i].indexOf('=');
			if ( pos >= 0 )
			{
				var argname = pairs[i].substring( 0, pos );
				var value = pairs[i].substring(pos+1);
				this.queryString_keys[this.queryString_keys.length] = argname;
				this.queryString_values[this.queryString_values.length] = value;		
			}
		}

		this.npage = this.getParameter('npage');
		if (this.npage == null) {
			this.npage = 1;
		} else if (this.npage < 1) {
			this.npage = 1;
		}
		
		this.otherParameter = this.getOtherParameter('npage');
		
		this.xmlDoc = this.syncXMLDocument(this.geonlyfileName(xmlListFileUrl));
		this.xmlSection = this.xmlDoc.getElementsByTagName('article');
	}
	
	
	
	function getOtherParameter( key )
	{
		var value = "";
		for ( var i = 0; i < this.queryString_keys.length; i++ )
		{
			if ( this.queryString_keys[i] != key )
			{
				value += this.queryString_keys[i] + "=" + this.queryString_values[i] + "&";
			}
		}
		return value;
	}
	
	function getParameter( key )
	{
		var value = null;
		for ( var i = 0; i < this.queryString_keys.length; i++ )
		{
			if ( this.queryString_keys[i] == key )
			{
				value = this.queryString_values[i];
				break;
			}
		}
		return value;
	}
	

	function syncXMLDocument(filename) {
		if (document.implementation && document.implementation.createDocument) 
		{
			// Mozilla
			private_xmlDoc = document.implementation.createDocument("", "", null);
		}else if (window.ActiveXObject) 
		{
			// MSIE 5.x later
			private_xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}
		private_xmlDoc.async="false"
		private_xmlDoc.load(filename);
		return private_xmlDoc;
	}

	function geonlyfileName( HTTPaddress ) 
	{
		if ( HTTPaddress.toUpperCase().indexOf('HTTP://') > -1 ) 
			filename = HTTPaddress.substring(HTTPaddress.lastIndexOf('/')+1, HTTPaddress.length );
		else
			filename = HTTPaddress;
		
		return filename;
	}

	function getList()
	{
		intNowPage = this.npage;
		listnum = this.listnum; 
		intBlockPage = this.pageterm;
		
		intTotalPage = this.xmlSection.length / listnum;
		intTemp = ((intNowPage - 1) / intBlockPage) * intBlockPage + 1;
		intLoop = (intTemp-1) * listnum;
		
		
		htmlSorce = document.all("xlist").innerHTML;
		headSorce = htmlSorce.substring( 0, htmlSorce.indexOf("<!--xRoofStart-->"));
		bodySorce = htmlSorce.substring( htmlSorce.indexOf("<!--xRoofStart-->")+"<!--xRoofStart-->".length, htmlSorce.indexOf("<!--xRoofEnd-->"));
		bottomSorce = htmlSorce.substring(htmlSorce.indexOf("<!--xRoofEnd-->")+"<!--xRoofEnd-->".length, htmlSorce.length);

		tmpbodySorce = headSorce;
		while ( (intLoop < ((intTemp-1) * listnum) + 10 ) && intLoop < this.xmlSection.length ) 
		{
			childs = this.xmlSection[intLoop].childNodes;
			
			artindex = childs[0].text;
			artindex_desc = this.xmlSection.length - artindex+1;
			artiid = childs[1].text;
			
			xmlStoryFileUrl = this.xmlStoryFilePattern.replace("<!--artId-->", artiid);
			artDoc = syncXMLDocument(this.geonlyfileName(xmlStoryFileUrl));
			artSection = artDoc.getElementsByTagName('data');

			tmpSorce = bodySorce;
			tmpSorce = tmpSorce.replace("<!--xIndex[1]-->",artindex);
			tmpSorce = tmpSorce.replace("<!--xIndex[0]-->",artindex_desc);
			
			for(x=0; x < artSection.length; x++)
			{	
				tmpSorce = tmpSorce.replace("<!--xData["+x+"]-->",artSection[x].text);
			}
			tmpbodySorce += tmpSorce;

			intLoop++;
		}
		tmpbodySorce +=  bottomSorce;
		document.all("xlist").innerHTML = tmpbodySorce;
	}
	
	function getNavigation(prevTermStr, nextTermStr, currColor, sMark, eMark) 
	{
		if(currColor == null)
			currColor = "red"
		
		countPerPage = this.listnum;
		currPageNo = this.npage;
		pagePerScreen = this.pageterm;
		totalCnt = this.xmlSection.length;

		countOfCurrScreen = 0;
		tmpCnt = parseInt((currPageNo - 1 )/ pagePerScreen) * (countPerPage * pagePerScreen);
		if(totalCnt > tmpCnt ){
			tmpCnt2 = countPerPage * pagePerScreen + 1;
			if(totalCnt > tmpCnt2) {
				if((totalCnt - tmpCnt) < (countPerPage * pagePerScreen)){
					countOfCurrScreen = (totalCnt - tmpCnt);
				}else{
					countOfCurrScreen = tmpCnt2;
				}
			}else{
				countOfCurrScreen = totalCnt;
			}
		}

		xscreen = parseInt((currPageNo-1) / pagePerScreen);
		npage = xscreen*pagePerScreen + 1;
		maxpage = parseInt((countOfCurrScreen / countPerPage) + ((countOfCurrScreen % countPerPage) == 0 ? 0 : 1)) ;
		
		if (countOfCurrScreen == 0) 
			maxpage = 0 ;
		
		result = "" ;
		if (xscreen > 0) {
			result += "<a href=" +this.pageURL + "?npage=" + (npage - 1) + "&"+this.otherParameter+ ">"+prevTermStr+"</a>";
			
		}
		
		i = 1;
		while(true){
			if ((i > pagePerScreen) || (i > maxpage)) 
				break ;
			if (npage == currPageNo)
				result += sMark+"<font color="+currColor+"><b>" + npage + "</b></font>"+eMark;
			else
				result += sMark+"<a href=" +this.pageURL + "?npage=" + npage + "&"+this.otherParameter + ">" + npage + "</a>"+eMark;
			npage++ ; 
			i++ ;
		}

		if (countPerPage * pagePerScreen < countOfCurrScreen){
			result += "<a href=" +this.pageURL + "?npage=" + npage + "&"+this.otherParameter + ">"+nextTermStr+"</a>";
		}
		document.write (result);
	}