
//OBJECTS

var api_colors=new Array();
api_colors[0]="#33CC33";
api_colors[1]="#FFFF00";
api_colors[2]="#FF6600";
api_colors[3]="#FF0000";
api_colors[4]="#000000";

//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;

	//optional objects
	this.category;
	this.enclosure;
	this.guid;
	this.source;

	var properties = new Array("title", "link", "description", "author", "comments", "pubDate");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = 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.category;
	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)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = 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(userPreferUrl,lang)
{
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else
		alert("not supported");

	//prepare the xmlhttprequest object
	if ( userPreferUrl=="" || userPreferUrl==null ) {
		userPreferUrl="general_tchi.xml";
		//alert ("SelectBox is: "+document.rssform.rssurl.value);
	}
	
	if (userPreferUrl!="default" && userPreferUrl!="default1") {
		therssurl="../rss/"+userPreferUrl;
		xhr.open("GET", therssurl, true);
		xhr.setRequestHeader("Cache-Control", "no-cache");
		xhr.setRequestHeader("Pragma", "no-cache");
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					if (xhr.responseText != null) 
						processRSS(xhr.responseXML,lang);
					else {
						alert("Failed to receive RSS file from the server - file not found.");
						return false;
					}
				}
				else 
					alert("Error code " + xhr.status + " received: " + xhr.statusText + "("+therssurl+")");
			}
		}
		
		//send the request
		xhr.send(null);
	}
	
	else { //Just show the welcome picture in the displayIndex Area if default is chosen.
		document.getElementById("displayIndexArea").innerHTML = 
		"<table width='95%' border='0' cellspacing='0' cellpadding='0'>\
		<tr>\
			<td colspan='2'><img src='images/hkmap.gif'></td>\
		</tr>\
		</table>";
	}
}

//processes the received rss xml
function processRSS(rssxml,lang)
{
	RSS = new RSS2Channel(rssxml);
	showRSS(RSS,lang);
}

//shows the RSS content in the browser
function showRSS(RSS,lang)
{
	//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 startTag = "<div>";
	var startTrTag = "<tr>";
	var endTrTag = "</tr>";
	var endTdTag = "</td>";
	var endTableTag = "</table>";
	var endTag = "</div>";

	var currentTitle, currentDescription,currentPubDate;
	var str_num_of_violation,str_api_image; 
	
	var txt_num_of_violation_sentence_start_tchi="<span class=\"epd_03\">現在</span>有";
	var txt_num_of_violation_sentence_start_en="<span class=\"epd_03\">Now</span> ";
	
	var txt_num_of_violation_sentence_end_tchi="種空氣污染物<br><div class=\"text03\">超過世衛指標</div>";
	var txt_num_of_violation_sentence_end_en=" kind(s) of air <br> pollutants have exceeded the WHO standards";

	var txt_num_of_violation_sentence_end_simple_tchi="種空氣污染物超過世衛指標";
	var txt_num_of_violation_sentence_end_simple_en=" kind(s) of air pollutants have exceeded the WHO standards";

	var txt_zero_violation_sentence_end_tchi="沒有空氣污染物超過世衛指標<br />";
	var txt_zero_violation_sentence_end_en=" no air pollutants have exceeded the WHO standards<br />";


	var txt_update_time_tchi="最新修訂時間:";
	var txt_update_time_en="Last updated:";
	
	var url_api_details_tchi="http://www.greenpeace.org/china/ch/campaigns/air-pollution/problems/levels";
	var url_api_details_en="http://www.greenpeace.org/china/en/campaigns/air-pollution/real-index/google-map";

	var txt_epd_title_tchi="環保署指數";
	var txt_epd_title_en="EPD API";
	
	var txt_times_tchi="倍";
	var txt_times_en=" times";
	
	var txt_exceed_tchi="超標";
	var txt_exceed_en="";


	var str_desc="";
	//populate channel data
	var properties = new Array("title","link","description","pubDate","copyright");
	for (var i=0; i<properties.length; i++)
	{
		curProp = eval("RSS."+properties[i]);
		
		if (properties[i]=="title") {
			currentTitle=curProp;
		}
		else if (properties[i]=="description") {
			currentDescription=curProp;
		}
		else if (properties[i]=="pubDate") {
			currentPubDate=curProp;
		}
	}
	
	str_desc="<strong>"+currentTitle+ "</strong>"+ " - ";
	
	if (currentDescription<=0) {
		str_desc+=eval("txt_zero_violation_sentence_end_"+lang);
	}
	else {
		str_desc+=currentDescription + eval("txt_num_of_violation_sentence_end_simple_"+lang)+"<br />";
	}
	
	//2008-08-25
	var $arr_descriptions=new Array();
	var violation_count=0;
	
	for (var i=0;i<RSS.items.length;i++) {
		if (RSS.items[i].description.match("violated")!=null) {
			current_title=RSS.items[i].title;
			var $arr_descriptions=RSS.items[i].description.split(",");
			current_violation_level=$arr_descriptions[2];
			
			if (lang=="en") {
				current_violation_level=parseFloat(current_violation_level);
				current_violation_level++;
			}
			
			str_desc+=current_title+ " : "+ eval("txt_exceed_"+lang)+current_violation_level+eval("txt_times_"+lang)+"<br />";
			violation_count++;
		}
	}
	
		str_num_of_violation=currentTitle+":<br>"+eval("txt_num_of_violation_sentence_start_"+lang)+"<a class='api_lv' target='_blank' href='"+eval("url_api_details_"+lang)+"?desc="+escape(str_desc)+"'>"+currentDescription+"</a>\
"+eval("txt_num_of_violation_sentence_end_"+lang)+"";

		str_api_image="<a href='"+eval("url_api_details_"+lang)+"?desc="+escape(str_desc)+"' target='_blank'>\
<img src='images/meter_lv0"+currentDescription+".jpg' alt='' width='150' height='229' border='0'>\
</a>";
	
		document.getElementById("num_of_violation").innerHTML =str_num_of_violation;
		document.getElementById("image_gpc_api").innerHTML =str_api_image;
		document.getElementById("lastupdate_time").innerHTML =eval("txt_update_time_"+lang)+currentPubDate;

	
	return true;
}

var xhr;


