var xmlHttpHit;
var xmlFileHit;
var startDay = "20061204";

//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", "dc:date", "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) {
			if (properties[i] == "dc:date")
				eval("this.pubDate=tmpElement.childNodes[0].nodeValue");
			else
				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)
		xmlHttpHit = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xmlHttpHit = new XMLHttpRequest();
	else {
		//alert("not supported");
		return;
	}

	//prepare the xmlhttprequest object
	xmlFileHit = getFile ();
	xmlHttpHit.open("GET", xmlFileHit, true);
	xmlHttpHit.setRequestHeader("Cache-Control", "no-cache");
	xmlHttpHit.setRequestHeader("Pragma", "no-cache");
	xmlHttpHit.onreadystatechange = function() {
		if (xmlHttpHit.readyState == 4)
		{
			if (xmlHttpHit.status == 200)
			{
				if (xmlHttpHit.responseText != null)
					processRSS(xmlHttpHit.responseXML);
				else
				{
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else {
				alert("Error code " + xmlHttpHit.status + " received: " + xmlHttpHit.statusText);
				return false;
			}
		}
	}

	//send the request
	xmlHttpHit.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)
{
	//populate the items
	var title = "", des = "", catstr = "", hitstr = "", cntstr = "", datestr = "";
	var rk_tot = RSS.items.length;

	var newsurl = location.href;
	for (var i=0; i<rk_tot; i++) {
		if (RSS.items[i].link == newsurl) {
			title = RSS.items[i].title;
			if (title == null)	title = "";
			des = RSS.items[i].description;
			if (des == null)	des = "";
			datestr = RSS.items[i].pubDate;
			catstr = RSS.items[i].category;

			break;
		}
	}

	if (title != "") {
		if (title.indexOf("<!-- ") != -1 && title.indexOf(" -->") != -1) {
			title = title.substring(0,title.indexOf("<!-- "));
		}

		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,"'");
		title = title.replace (/\"/g,"\\\"");

		endtagstr = " -->";
		tagstr = "<!-- total:";
		if (des.indexOf(tagstr) != -1 && des.indexOf(endtagstr,des.indexOf(tagstr)) != -1) {
			cntstr = des.substring(des.indexOf(tagstr)+tagstr.length, des.indexOf(endtagstr,des.indexOf(tagstr))); 
		}
		tagstr = "<!-- hit:";
		if (des.indexOf(tagstr) != -1 && des.indexOf(endtagstr,des.indexOf(tagstr)) != -1) {
			hitstr = des.substring(des.indexOf(tagstr)+tagstr.length, des.indexOf(endtagstr,des.indexOf(tagstr))); 
		}	

		result = cntstr + "<br><a href='javascript:viewHit(\"" + escape(title) + "\",\"" + escape(catstr) + "\",\"" +escape(datestr) + "\",\"" + hitstr + "\");'><img src='http://image.chosun.com/newsplus/images/graph.gif' border=0></a>";
	}
	else {
		result = "<a href='http://newsplus.chosun.com/ranking' target=new><img src='http://image.chosun.com/newsplus/images/hottopic.gif' border=0></a>";
	}
	if (document.getElementById("Graph") != "undefined" && document.getElementById("Graph") != null)
		document.getElementById("Graph").innerHTML = result;

	return true;
}

getRSS();

function getFile () {
	tmp = "http://" + location.host + "/rank/books/index/hour.xml";
	return tmp;
}

function viewHit(title, cate, date_time, hit) {
	var w_url = "http://newsplus.chosun.com/hitdata/api/view.html?title=" + unescape(title) + "&category=" + unescape(cate) + "&stime=" + unescape(date_time) + "&hit=" + unescape(hit);
	window.open(w_url,"graph", "menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,width=720,height=420,top=210,left=200");
}

