var rssSource ="/xml/xmlRedirect.cfm?category=" + category
var isIE = false;
var req;

function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            //clearTopicList();
            //buildTopicList();
		//clearList();
		buildList();
         } else {
            alert("There was a problem retrieving the XML data:\n" + req.status);
         }
    }
}

// loads chosen XML document
function loadDoc(source) {
	 try {
    		loadXMLDoc(source);
            }
            catch(e) {
                var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
                alert("Unable to get XML data:\n" + msg);
                return;
            }
}

// retrieve text of an XML document element, including elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

// empty Topics select list content
function clearTopicList() {
    var select = document.getElementById("topics");
    while (select.length > 0) {
        select.remove(0);
    }
}
/* work in progress */
function clearList() {
    var newsDiv = document.getElementById("news");
	var titleDiv = document.getElementById("div");
	var descDiv = document.getElementById("div");
	var urlDiv = document.getElementById("div");
	var dateDiv = document.getElementById("div");
    titleDiv.innerHTML = "";
	descDiv.innerHTML = "";
	urlDiv.innerHTML = "";
	dateDiv.innerHTML = "";
}

// add item to select element the less elegant, but compatible way.
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}

// fill Topics select list with items from the current XML document
function buildTopicList() {
    var select = document.getElementById("topics");
    var items = req.responseXML.getElementsByTagName("item");
    // loop through <item> elements, and add each nested
    // <title> element to Topics select element
    for (var i = 0; i < items.length; i++) {
        appendToSelect(select, i,
            document.createTextNode(getElementTextNS("", "title", items[i], 0)));
    }
    // clear detail display
    document.getElementById("details").innerHTML = "";
}

// display details retrieved from XML document
function showDetail(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    var item, content, div;
    if (evt) {
        var select = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (select && select.options.length > 1) {
            // copy <content:encoded> element text for
            // the selected item
            item = req.responseXML.getElementsByTagName("item")[select.value];
            content = getElementTextNS("content", "encoded", item, 0);
            div = document.getElementById("details");
            div.innerHTML = "";
            // blast new HTML content into "details" <div>
            div.innerHTML = content;
        }
    }
}

/* ================================================== */
function appendItem(title,description,url,date) {
	var newsDiv = document.getElementById("news");
	var titleDiv = document.createElement("div");
	var descDiv = document.createElement("div");
	var urlDiv = document.createElement("div");
	var dateDiv = document.createElement("div");
	/* append to main div */
	newsDiv.appendChild(titleDiv);
	newsDiv.appendChild(descDiv);
	newsDiv.appendChild(urlDiv);
	newsDiv.appendChild(dateDiv);
	/* style */
	titleDiv.className = "title";
	descDiv.className = "copy";
	urlDiv.className = "url";
	dateDiv.className = "date";
	/* content */
	if(typeof(description)=='string') {
		titleDiv.innerHTML = "&#187;&nbsp;<a href=" + url + " target='_blank'>" + title + "</a>";
		descDiv.innerHTML = description;
		urlDiv.innerHTML = "<a href='"+url+"' target='_blank'>"+url.substring(0,70)+"...</a>"+"<br clear='all' />";
		if(typeof(date)=='string') dateDiv.innerHTML = date;
	}

}
function appendSource(source,url) {
	var sourceLogo = document.getElementById("source");
	var sourceUrl = document.getElementById("sourceUrl");
	sourceLogo.src = source;
	sourceUrl.href = url;
}

function getElementText(childElem, parentElem, index) {
	var result = "";
   	result = parentElem.getElementsByTagName(childElem)[index];
	if (result) {
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } 
		else {
			if(result.firstChild) return result.firstChild.nodeValue;
			else return "";
        }
    } 
	else return "";
}

function buildList() {
	/*
	if(document.getElementById("source")) {
		if (category =="Tygodnik_Powszechny") {
			sSource = '/images/xml/tp.gif';
			sLink = 'http://tygodnik.onet.pl/';
		}
		else {
			if(req.responseXML.getElementsByTagName('image')[0] != null) {
				srcObj = req.responseXML.getElementsByTagName('image');
				sSource = getElementText('url',srcObj[0], 0);
				sLink = getElementText('link',srcObj[0], 0);
			}
			else {
				sSource = '/images/spacer.gif';
				sLink = '';
			}
		}
		appendSource(sSource,sLink);
	}
	*/
	//newsy
	var curDate     = new Date();
	var items = req.responseXML.getElementsByTagName('item');
	maxnum = 10;
	if(typeof maxNews !="undefined") maxnum = maxNews;
	for (var i = 0; i < maxnum; i++) {
		sLink = getElementText('link', items[i], 0); // child, parent, index
		/* sLink = sLink.replace("?skad=rss","");
		sLink = sLink.replace("?source=rss","");
		sLink = sLink.replace("http://infoo.pl/red.php?u=","");
		sLink = sLink.replace("?nav=rss_print/asection","");
		sLink = sLink.replace("#reqRss","");*/
		sTitle = getElementText('title', items[i], 0);
		sDescription = getElementText('description', items[i], 0);
		sDate = getElementText('pubDate', items[i], 0);
		if(sDate=="") sDate = curDate;
		appendItem(sTitle,sDescription,sLink,sDate);
	}
}
function init() {
	loadDoc(rssSource);
}
window.onload=init;
