﻿
google.load("feeds", "1");

function loadFeed1(displayLim){
  
  var feed = new google.feeds.Feed("http://digg.com/users/photomask/history.rss");
  
  feed.setNumEntries(displayLim+1);
  feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
  feed.load(function(result) {

    //prepare the container for the feed
    var container = document.getElementById("iNewsFeed");
    while (container.firstChild) {
       container.removeChild(container.firstChild);
    } 
    
    if (!result.error) {
      var items = result.xmlDocument.getElementsByTagName("item");
      
      //alert(items.length);

      if (items.length < displayLim) {
      	var loopLim = items.length;
      } else {
      	var loopLim = displayLim;
      }

      for (var i = 0; i < loopLim; i++) {
        var titleElement = items[i].getElementsByTagName("title")[0];

        if (i>0){
        	var prevTitleElement = items[i-1].getElementsByTagName("title")[0];
        } else {
        	var prevTitleElement = null;
        }

        var title = titleElement.firstChild.nodeValue;

        if (prevTitleElement){
        	var prevTitle = prevTitleElement.firstChild.nodeValue;
        } else {
        	var prevTitle = null;
        }

        var descriptionElement = items[i].getElementsByTagName("description")[0];
        var description = descriptionElement.firstChild.nodeValue;
        var linkElement = items[i].getElementsByTagName("link")[0];
        var link = linkElement.firstChild.nodeValue;

		if (title != prevTitle) {
	        var div = document.createElement("div");
	        var a = document.createElement("a");
	        a.setAttribute('href',link);
	        a.setAttribute('target',"_blank");
	        a.appendChild(document.createTextNode(title));
	        div.appendChild(a);
	        div.appendChild(document.createElement("br"));
	        div.appendChild(document.createTextNode(description));
	        div.appendChild(document.createElement("br"));
	        div.appendChild(document.createElement("br"));
	        container.appendChild(div);
		}

      }//for

		//display the more button?
	    div = document.createElement("div");
	    a = document.createElement("a");
		if (items.length > displayLim) {
		    if (displayLim == 5) {
			    a.setAttribute('href','javascript:loadFeed1(10)');
     		    a.appendChild(document.createTextNode('more'));
			}
		} else {
		    a.setAttribute('href','http://digg.com/users/photomask');			
		    a.setAttribute('target','_blank');			
	        a.appendChild(document.createTextNode('more at digg.com'));
		}
	    div.appendChild(a);
	    div.appendChild(document.createElement("br"));
	    container.appendChild(div); 
      
    }
  });
}//loadFeed1

function loadFeed2(displayLim){

  var feed = new google.feeds.Feed("http://purecare.wordpress.com/rss/");
  feed.setNumEntries(displayLim+1);
  feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
  feed.load(function(result) {

    //prepare the container for the feed
    var container = document.getElementById("pNewsFeed");
    while (container.firstChild) {
       container.removeChild(container.firstChild);
    } 
    
    if (!result.error) {
      var items = result.xmlDocument.getElementsByTagName("item");

      if (items.length < displayLim) {
      	var loopLim = items.length;
      } else {
      	var loopLim = displayLim;
      }

      for (var i = 0; i < loopLim; i++) {
        var titleElement = items[i].getElementsByTagName("title")[0];
        var title = titleElement.firstChild.nodeValue;
        var descriptionElement = items[i].getElementsByTagName("description")[0];
        var description = descriptionElement.firstChild.nodeValue;
        description = description.replace(/<[a-zA-Z\/][^>]*>/g,"")
        //    "<(.|\n)*?>",
        //alert(description);
        var linkElement = items[i].getElementsByTagName("link")[0];
        var link = linkElement.firstChild.nodeValue;

        var div = document.createElement("div");
        var a = document.createElement("a");
        a.setAttribute('href',link);
        a.setAttribute('target',"_blank");
        a.appendChild(document.createTextNode(title));
        div.appendChild(a);
        div.appendChild(document.createElement("br"));
        div.appendChild(document.createTextNode(description));
        div.appendChild(document.createElement("br"));
        div.appendChild(document.createElement("br"));
        container.appendChild(div);
      }//for

		//display the more button?
	    div = document.createElement("div");
	    a = document.createElement("a");
		if (items.length > displayLim) {
		    if (displayLim == 5) {
			    a.setAttribute('href','javascript:loadFeed2(10)');
     		    a.appendChild(document.createTextNode('more'));
			}
		} else {
		    a.setAttribute('href','http://purecare.wordpress.com');			
		    a.setAttribute('target','_blank');			
	        a.appendChild(document.createTextNode('more at wordpress.com'));
		}	
	    div.appendChild(a);
	    div.appendChild(document.createElement("br"));
	    container.appendChild(div); 

    }
  });
}//loadFeed2

//this function is obsolete, remove when not needed.
function loadFeed2a(numEntries){

  var feed = new google.feeds.Feed("http://purecare.wordpress.com/feed/");
  feed.setNumEntries(numEntries);
  feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
  feed.load(function(result) {
    var container = document.getElementById("pNewsFeed");
    if (!result.error) {
      var items = result.xmlDocument.getElementsByTagName("item");
      for (var i = 0; i < items.length; i++) {
        var titleElement = items[i].getElementsByTagName("title")[0];
        var title = titleElement.firstChild.nodeValue;
        var descriptionElement = items[i].getElementsByTagName("description")[0];
        var description = descriptionElement.firstChild.nodeValue;
        var linkElement = items[i].getElementsByTagName("link")[0];
        var link = linkElement.firstChild.nodeValue;

        var div = document.createElement("div");
        var a = document.createElement("a");
        a.setAttribute('href',link);
        a.appendChild(document.createTextNode(title));
        div.appendChild(a);
        div.appendChild(document.createElement("br"));
        div.appendChild(document.createTextNode(description));
        div.appendChild(document.createElement("br"));
        div.appendChild(document.createElement("br"));
        container.appendChild(div);
      }
    }
  });
}//loadFeed2

function initialize() {

  loadFeed1(5);
  loadFeed2(5);

}
google.setOnLoadCallback(initialize);


