var xmlHttp;
var xmlFile;
var startDay = "20061204";

var rk_site = getParameterJS("site");
var rk_scode = getParameterJS("scode");
var rk_date = getParameterJS("date");
var rk_tot = getParameterJS("tot");
var rk_len = getParameterJS("len");
var rk_contlen = getParameterJS("contlen");

//OBJECTS

//objects inside the RSS2Item object
function RSS2Enclosure(encElement)
{
	if (encElement == null)
	{
		this.url = null;
		this.length = null;
		this.type = null;
	}
	else
	{
		this.url = encElement.getAttribute("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
}

function RSS2Guid(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(souElement)
{
	if (souElement == null)
	{
		this.url = null;
		this.value = null;
	}
	else
	{
		this.url = souElement.getAttribute("url");
		this.value = souElement.childNodes[0].nodeValue;
	}
}

//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//optional vars
	this.author;
	this.comments;
	this.pubDate;
	this.category;

	//optional objects
	this.rsscategory;
	this.enclosure;
	this.guid;
	this.source;

	var properties = new Array("title", "link", "description", "author", "comments", "pubDate", "category");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null && tmpElement.childNodes[0] != null) {
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
		}
	}

	this.rsscategory = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
}

//objects inside the RSS2Channel object
function RSS2Category(catElement)
{
	if (catElement == null)
	{
		this.domain = null;
		this.value = null;
	}
	else
	{
		this.domain = catElement.getAttribute("domain");
		this.value = catElement.childNodes[0].nodeValue;
	}
}

//object containing RSS image tag info
function RSS2Image(imgElement)
{
	if (imgElement == null)
	{
	this.url = null;
	this.link = null;
	this.width = null;
	this.height = null;
	this.description = null;
	}
	else
	{
		imgAttribs = new Array("url","title","link","width","height","description");
		for (var i=0; i<imgAttribs.length; i++)
			if (imgElement.getAttribute(imgAttribs[i]) != null)
				eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
	}
}

//object containing the parsed RSS 2.0 channel
function RSS2Channel(rssxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//array of RSS2Item objects
	this.items = new Array();

	//optional vars
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	//optional objects
	this.rsscategory;
	this.image;

	var chanElement = rssxml.getElementsByTagName("channel")[0];
	var itemElements = rssxml.getElementsByTagName("item");

	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);
		//chanElement.removeChild(itemElements[i]);
	}

	var properties = new Array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = chanElement.getElementsByTagName(properties[i])[0];
		if (tmpElement != null && tmpElement.childNodes[0] != null) {
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
		}
	}

	this.rsscategory = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
	this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function getRSS()
{
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xmlHttp = new XMLHttpRequest();
	else {
		//alert("not supported");
		return;
	}

	//prepare the xmlhttprequest object
	xmlFile = getFile ();
	xmlHttp.open("GET", xmlFile, true);
	xmlHttp.setRequestHeader("Cache-Control", "no-cache");
	xmlHttp.setRequestHeader("Pragma", "no-cache");
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				if (xmlHttp.responseText != null)
					processRSS(xmlHttp.responseXML);
				else
				{
					//alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else {
				//alert("Error code " + xmlHttp.status + " received: " + xmlHttp.statusText);
			}
		}
	}


	//send the request
	xmlHttp.send(null);
}

//processes the received rss xml
function processRSS(rssxml)
{
	RSS = new RSS2Channel(rssxml);
	showRSS(RSS);
}

//shows the RSS content in the browser
function showRSS(RSS)
{

	//default values for html tags used
	var imageTag = "<img id='chan_image'";
	var startItemTag = "<div id='item'>";
	var startTitle = "<div id='item_title'>";
	var startLink = "<div id='item_link'>";
	var startDescription = "<div id='item_description'>";
	var endTag = "</div>";

	//populate channel data
	/*	
	var properties = new Array("title","link","description","pubDate","copyright");
	for (var i=0; i<properties.length; i++)
	{
		eval("document.getElementById('chan_"+properties[i]+"').innerHTML = ''");
		curProp = eval("RSS."+properties[i]);
		if (curProp != null)
			eval("document.getElementById('chan_"+properties[i]+"').innerHTML = curProp");
	}

	//show the image
	document.getElementById("chan_image_link").innerHTML = "";
	if (RSS.image.src != null)
	{
		document.getElementById("chan_image_link").href = RSS.image.link;
		document.getElementById("chan_image_link").innerHTML = imageTag
			+" alt='"+RSS.image.description
			+"' width='"+RSS.image.width
			+"' height='"+RSS.image.height
			+"' src='"+RSS.image.url
			+"' "+"/>";
	}
	*/

	//populate the items
	var title = "", updown = "", rankstr = "", des = "", catstr = "", imgstr = "";
	rk_tot = (RSS.items.length > rk_tot) ? rk_tot : RSS.items.length;
	for (var i=0; i<rk_tot; i++)
	{
		title = RSS.items[i].title;
		if (title == null)	title = "";
		des = RSS.items[i].description;
		if (des == null)	des = "";
		catstr = RSS.items[i].category;
		imgstr = RSS.items[i].comments;

		if (title.indexOf("<!-- ") != -1 && title.indexOf(" -->") != -1) {
			updown = title.substring(title.indexOf("<!-- ")+5, title.indexOf(" -->")); 

			if (updown.charAt(0) == "+") {
				rankstr = "<div class=rankUp><img src=http://image.chosun.com/common/200611/sys/ico_rank_up.gif align=absmiddle> " + updown.substring(1) + "</div>";
			}
			else if (updown.charAt(0) == "-") {
				rankstr = "<div class=rankDn><img src=http://image.chosun.com/common/200611/sys/ico_rank_dn.gif align=absmiddle> " + updown.substring(1) + "</div>";
			}
			else if (updown == "1") {
				rankstr = "<div class=rankUp><img src=http://image.chosun.com/common/200611/sys/ico_rank_up.gif align=absmiddle> New</div>";
			}
			else {
				rankstr = "<div class=rankNm><img src=http://image.chosun.com/common/200611/sys/ico_rank_nm.gif align=absmiddle> 0</div>";
			}
		}

		title = title.replace (/&lt;/gi,"<");
		title = title.replace (/&gt;/gi,">");
		title = title.replace (/&amp;/gi,"&");
		title = title.replace (/&quot;/gi,"\"");
		title = title.replace (/&#39;/gi,"'");

		if (title != "" && rk_len != -1 && title.length > rk_len) {
			if (title.indexOf("[") != -1 && title.indexOf("]") != -1)
				title = title.substring(0, title.indexOf("[")) + title.substring(title.indexOf("]", title.indexOf("["))+1); 
			if (title.indexOf("<!--") != -1 && title.indexOf("-->") != -1)
				title = title.substring(0, title.indexOf("<!--")) + title.substring(title.indexOf("-->", title.indexOf("<!--"))+3); 

			if (title.length > rk_len)
				title = title.substring(0, rk_len) + "..";
		}
		
		if (document.getElementById("URL"+(i+1)) != "undefined" && document.getElementById("URL"+(i+1)) != null)
			document.getElementById("URL"+(i+1)).href = RSS.items[i].link;
		if (document.getElementById("TITLE"+(i+1)) != "undefined" && document.getElementById("TITLE"+(i+1)) != null)
			document.getElementById("TITLE"+(i+1)).innerHTML = title;

		if (document.getElementById("RANK"+(i+1)) != "undefined" && document.getElementById("RANK"+(i+1)) != null)
			document.getElementById("RANK"+(i+1)).innerHTML = rankstr; 

		if (document.getElementById("CONT"+(i+1)) != "undefined" && document.getElementById("CONT"+(i+1)) != null) {
			if (des != "" && des.length > rk_contlen) des = des.substring(0, rk_contlen) + "..";
			document.getElementById("CONT"+(i+1)).innerHTML = des;
		}

		if (document.getElementById("CATEGORY"+(i+1)) != "undefined" && document.getElementById("CATEGORY"+(i+1)) != null)
			document.getElementById("CATEGORY"+(i+1)).innerHTML = catstr; 
			
		if (document.getElementById("IMG"+(i+1)) != "undefined" && document.getElementById("IMG"+(i+1)) != null) {
			if (imgstr != null)
				document.getElementById("IMG"+(i+1)).innerHTML = "<table border=0 align=left cellpadding=2 cellspacing=1><tr><td><img src=\"" + imgstr + "\"></td></tr></table>";
		}
	}

	return true;
}

getRSS();

function getFile () {

	if (typeof(RANK_SITE) != "undefined")		rk_site = RANK_SITE;
	if (typeof(RANK_SCODE) != "undefined")		rk_scode = RANK_SCODE;
	if (typeof(RANK_DATE) != "undefined")		rk_date = RANK_DATE;
	if (typeof(RANK_LEN) != "undefined")		rk_len = RANK_LEN;
	if (typeof(RANK_TOT) != "undefined")		rk_tot = RANK_TOT;
	if (typeof(RANK_CONTLEN) != "undefined")		rk_contlen = RANK_CONTLEN;
	
	if (rk_site == null || rk_site == "")	rk_site = "www";
	if (rk_scode == null || rk_scode == "")	rk_scode = "index";

	if (rk_site == "photo") {
		if (rk_len == "" || rk_len == 0)	rk_len = 10;
	}
	else if (rk_site == "spn") {
		if (rk_len == "" || rk_len == 0)	rk_len = 18;
		if (rk_tot == "" || rk_tot == 0)	rk_tot = 10;
	}	
	else if (rk_site == "www") {
		if (rk_len == "" || rk_len == 0)	rk_len = 19;
	}
	else if (rk_site == "www_daily") {
		rk_site = "www";
		if (rk_len == "" || rk_len == 0)	rk_len = -1;
		if (rk_tot == "" || rk_tot == 0)	rk_tot = 20;
	}
	else if (rk_site == "mylinker") {
		rk_site = "www";
		if (rk_len == "" || rk_len == 0)	rk_len = 19;
		if (rk_tot == "" || rk_tot == 0)	rk_tot = 20;
	}

	if (rk_tot == "" || rk_tot == 0)	rk_tot = 5;
	if (rk_len == "" || rk_len == 0)	rk_len = 25;
	if (rk_contlen == "" || rk_contlen == 0)	rk_contlen = 200;

	tmp = "/rank/xml/";

	if (rk_date != "" && rk_date.length == 8)
		tmp += rk_date.substring(0,6) + "/" + rk_date + ".xml";
	else if (rk_date != "")
		tmp += rk_date + ".xml";
	else
		tmp += "index.xml";

	return tmp;
}

