// AJAX XML Reader
// Ben Thomson - <ben@bam.com.au> - 13th August, 2007

//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
	//required
	this.title;
	this.link;
	this.description;

	var properties = new Array("title", "link", "description");
	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");
	}
}

//object containing the parsed RSS 2.0 items
function RSS2Items(rssxml, div) {
	
	//array of RSS2Item objects
	eval(div + '_array = new Array()');
	
	var itemElements = rssxml.getElementsByTagName("item");

	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		eval(div + '_array.push(Item)');
	}
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function getRSS(rssfeed, div) {
	var xhr = null;
	//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 {
		error('Browser not supported');
		return false;
	}

	//prepare the xmlhttprequest object
	xhr.open("GET",rssfeed,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, div);
				}
				else
				{
					error('Feed unavailable', div);
					return false;
				}
			}
			else {
				error("Error code " + xhr.status + " received: " + xhr.statusText, div);
			}
		}
	}

	//send the request
	xhr.send(null);
	eval(div + "_turning_page = false");
}

// error message function
function error(message, div) {
	document.getElementById(div).innerHTML = message;
}

//processes the received rss xml
function processRSS(rssxml, div)
{
	RSS = new RSS2Items(rssxml, div);
	showRSS(RSS, div);
}

//shows the RSS content in the browser
function showRSS(RSS, div)
{
	//populate the items
	eval('cur_array = ' + div + '_array');
	
	//set page at 1
	eval(div + '_cur_page = 1');
	var max_pages = Math.ceil(cur_array.length / 1);
	eval(div + '_max_pages = ' + max_pages);
	
	document.getElementById(div).innerHTML = "";
	for (var i = 0; i < (( cur_array.length > 1 ) ? 1 : cur_array.length); i++)
	{
		item_html = '<div>';
		item_html += (cur_array[i].title == null || cur_array[i].link == null) ? "" : '<a href="' + cur_array[i].link + '">' + cur_array[i].title + '</a>';
		item_html += (cur_array[i].description == null) ? "" : '<p>' + cur_array[i].description + '</p>';
		item_html += '</div><br />';
		document.getElementById(div).innerHTML += item_html;
	}

	//we're done
	return true;
}

// Next page of RSS feeds
function nextPage(div)
{
	if (eval(div + '_turning_page') == false) {
		eval(div + '_turning_page = true');
		eval('var cur_page = ' + div + '_cur_page');
		eval('var max_pages = ' + div + '_max_pages');
		opacity(div, 100, 0, 1000);
		setTimeout("nextPageSecond('" + div + "')", 1000);
		return true;
	}
}

function nextPageSecond(div) {
	eval('var cur_array = ' + div + '_array');
	eval('var cur_page = ' + div + '_cur_page');
	eval('var max_pages = ' + div + '_max_pages');
	if (cur_page < max_pages) {
		cur_page++;
		end_page = (cur_page * 1);
		if (cur_array.length < end_page) {
			end_page = cur_array.length;
		}
		document.getElementById(div).innerHTML = "";
		for (var i = ((cur_page - 1) * 1); i < end_page; i++)
		{
			item_html = '<div>';
			item_html += (cur_array[i].title == null || cur_array[i].link == null) ? "" : '<a href="' + cur_array[i].link + '">' + cur_array[i].title + '</a>';
			item_html += (cur_array[i].description == null) ? "" : '<p>' + cur_array[i].description + '</p>';
			item_html += '</div><br />';
			document.getElementById(div).innerHTML += item_html;
		}
		eval(div + '_cur_page++');
		document.getElementById(div).left = '-100px';
		opacity(div, 0, 100, 1000);
		setTimeout(div + '_turning_page = false', 1000);
	} else {
		cur_page = 1;
		end_page = (cur_page * 1);
		if (cur_array.length < end_page) {
			end_page = cur_array.length;
		}
		document.getElementById(div).innerHTML = "";
		for (var i = ((cur_page - 1) * 1); i < end_page; i++)
		{
			item_html = '<div>';
			item_html += (cur_array[i].title == null || cur_array[i].link == null) ? "" : '<a href="' + cur_array[i].link + '">' + cur_array[i].title + '</a>';
			item_html += (cur_array[i].description == null) ? "" : '<p>' + cur_array[i].description + '</p>';
			item_html += '</div><br />';
			document.getElementById(div).innerHTML += item_html;
		}
		eval(div + '_cur_page = 1');
		document.getElementById(div).left = '-100px';
		opacity(div, 0, 100, 1000);
		setTimeout(div + '_turning_page = false', 1000);
	}
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			setTimeout("document.getElementById('" + id + "').style.left = ((100 - " + i + ") / 2) + 'px'",(timer*speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			setTimeout("document.getElementById('" + id + "').style.left = ((" + i + " - 100) / 2) + 'px'",(timer*speed));
            timer++;
        }
    }
} 

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

