var scrollTimer = null;
var oldScrollPosition = true;

function rssLoader() {
    this.urls = new Array();
    this.urlsParsed = 0;
    
    var callbackFunction = null;
    
    this.parseRSSUrls = function(callback) {
        if (typeof(callback) == "function") {
            callbackFunction = callback;
        
            for (var urlIndex in this.urls) {
                this.loadRSSItems(this.urls[urlIndex]);
            }
        }
        else {
            alert("The callback function is not valid.");
        }
    }
        
    this.loadRSSItems = function(url) {
        var xhr = null;
        var responseText = "";

        if (window.ActiveXObject) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        else {
            alert("your browser does not support AJAX");
        }
        
        xhr.open("GET", url, true);

        xhr.setRequestHeader("Cache-Control", "no-cache");
        xhr.setRequestHeader("Pragma", "no-cache");

        xhr.onreadystatechange = function() {
            var responseText = "";
        
            if (xhr.readyState == 4) {            
                if (xhr.status == 200) {                
                    if (xhr.responseText != null) {
                        responseText = xhr.responseText;
                    }
                    else {
                        alert("Failed to receive RSS file from the server - file not found.");
                    }
                }
                else {
			var headlineContainer = document.getElementById("headlineContainer");
			headlinerContainer.innerHTML = "Failed to connect to feeds server; re-attempting request.";
			
			initializeRSS(callbackFunction);
                }
            }
                
            if (responseText != "") {            
                var rssItems = new rssChannel();
                var rssXmlDoc = null;
                
                if (window.ActiveXObject) {
                    rssXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    rssXmlDoc.async = false;
                    rssXmlDoc.loadXML(responseText);
                }
                else if (document.implementation && document.implementation.createDocument) {
                    var xmlParser = new DOMParser();
                    rssXmlDoc = xmlParser.parseFromString(responseText, "text/xml");
                }
                else {
                    alert('Your browser cannot handle this script');
                }
                
                if (rssXmlDoc) {
                    rssItems.parseRSSXml(rssXmlDoc);
                }
                
                callbackFunction(rssItems);
            }
        }
        
        xhr.send(null);
    }
}

function rssChannel() {
    this.title = "";
    this.link = "";
    this.description = "";

    this.language = "";
    this.copyright = "";
    this.managingEditor = "";
    this.webMaster = "";
    this.pubDate = "";
    this.lastBuildDate = "";
    this.generator = "";
    this.docs = "";
    this.ttl = "";
    this.rating = "";

    this.category = "";
    this.image = "";

    this.items = new Array();
    
    this.parseRSSXml = function(rssXml) {
        var chanElement = rssXml.getElementsByTagName("channel")[0];
        var itemElements = rssXml.getElementsByTagName("item");

        for (var i = 0; i < itemElements.length; i++)
        {
            Item = new rssItem(itemElements[i]);
            this.items.push(Item);
        }

        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 rssCategory(chanElement.getElementsByTagName("category")[0]);
        this.image = new rssImage(chanElement.getElementsByTagName("image")[0]);
    }
}

function rssCategory(catElement) {
    if (catElement == null) {
        this.domain = null;
        this.value = null;
    }
    else {
        this.domain = catElement.getAttribute("domain");
        this.value = catElement.childNodes[0].nodeValue;
    }
}

function rssImage(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]+")");
            }
        }
    }
}

function rssItem(itemxml)
{
    /*A*/
    /*required properties (strings)*/
    this.title = "";
    this.link = "";
    this.description = "";

    /*optional properties (strings)*/
    this.author = "";
    this.comments = "";
    this.pubDate = "";

    /*optional properties (objects)*/
    this.category;
    this.enclosure;
    this.guid;
    this.source;

    /*B*/
    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");
    }

    /*C*/
    this.category = new rssCategory(itemxml.getElementsByTagName("category")[0]);
    this.enclosure = new rssEnclosure(itemxml.getElementsByTagName("enclosure")[0]);
    this.guid = new rssGuid(itemxml.getElementsByTagName("guid")[0]);
    this.source = new rssSource(itemxml.getElementsByTagName("source")[0]);
}

function rssEnclosure(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 rssGuid(guidElement) {
    if (guidElement == null) {
        this.isPermaLink = null;
        this.value = null;
    } else {
        this.isPermaLink = guidElement.getAttribute("isPermaLink");
        this.value = guidElement.childNodes[0].nodeValue;
    }
}

function rssSource(souElement) {
    if (souElement == null) {
        this.url = null;
        this.value = null;
    } else {
        this.url = souElement.getAttribute("url");
        this.value = souElement.childNodes[0].nodeValue;
    }
}

function displayRSSItems(items) {
    var headlineContainer = document.getElementById("headlineContainer");
    var strHeadlineHtml = "";
    
    for (var rssItemIndex in items) { 
        strHeadlineHtml += "<a onmouseover=\"clearTimeout(scrollTimer);\" onmouseout=\"scrollTimer = setTimeout('scrollHeadlines(' + document.getElementById('headlineContainer').scrollTop + ', ' + oldScrollPosition + ')', 100);\" href=\"" + items[rssItemIndex].link + "\" target=\"_blank\">" + items[rssItemIndex].title + "</a><br /><br />";
    }
    
    if (headlineContainer) {
        headlineContainer.innerHTML = strHeadlineHtml;
        scrollHeadlines(0, true);
    }
}

function scrollHeadlines(top, increase) {
    var headlineContainer = document.getElementById("headlineContainer");
    headlineContainer.scrollTop = top;
    
    if (top == (headlineContainer.scrollHeight - headlineContainer.offsetHeight)) {
        oldScrollPosition = false;
        scrollTimer = setTimeout("scrollHeadlines(" + (top - 1) + ", false)", 1000);
    }
    else if (top == 0) {
        oldScrollPosition = true;
        scrollTimer = setTimeout("scrollHeadlines(" + 1 + ", true)", 1000);
    }
    else {
        oldScrollPosition = increase;
        scrollTimer = setTimeout("scrollHeadlines(" + ((increase) ? (top + 1) : (top - 1)) + ", " + increase + ")", 100);
    }
}

var rssItemObjects = null;
var rssObject = null;

function initializeRSS() {
    var headlineContainer = document.getElementById("headlineContainer");
    headlineContainer.innerHTML = "Retrieving news feeds from FocusOnStyle.com...";
    
    rssItemObjects = new Array();
    rssObject = new rssLoader();    
    
    rssObject.urls.push("http://www.loveadamas.com/Services/RSSProxy.aspx?rssUrl=" + escape("http://www.focusonstyle.com/rss.xml"));
    rssObject.parseRSSUrls(receiveRSSItems);
}

function receiveRSSItems(rssItems) {
    rssObject.urlsParsed++;
    
    for (var rssItemIndex in rssItems.items) {
        rssItemObjects.push(rssItems.items[rssItemIndex]);
    }
    
    if (rssObject.urlsParsed == rssObject.urls.length) {
        displayRSSItems(rssItemObjects);
        setTimeout("clearTimeout(scrollTimer);initializeRSS()", 300000);
    }
}

addOnLoadEvent(initializeRSS);