/*
-- Thanks to LinkedList.js - prateekrungta.com/linkedlist
-- Really helped me speed up the development of this script.
*/                     

google.load("feeds", "1");

function photoFeeds (containerId) {
	var fotoFeed = this;
	fotoFeed.feeds = new Array();
    fotoFeed.containerId = containerId;
    fotoFeed.numLoaded = 0;
	fotoFeed.dots=0;

	fotoFeed.add = function(feedURL, ns, localName, displayName) {
		fotoFeed.feeds[fotoFeed.feeds.length] = new grabThumb(feedURL, ns, localName, displayName);  		
	}

	fotoFeed.fetch	= function() { // public method
	    for (var i = fotoFeed.feeds.length - 1; i >= 0; i--) {
			if (fotoFeed.feeds[i].isFeed && !window.google) {
				fotoFeed.showError('Google\'s AJAX Feed API not loaded.');
				return false;
			}
			if (!window.google.feeds || !window.google.feeds.Feed) {
				fotoFeed.showError('Google\'s AJAX Feed API not loaded. You probably forgot to add google.load(\'feeds\', \'1\');');
				return false;
			}
			fotoFeed.feeds[i].load(fotoFeed);
		}   
		fotoFeed.startLoading()
	}

	fotoFeed.loadedFeed	= function(loadedObj) { // private method
		fotoFeed.numLoaded++; 
		
		// sorting the loaded link in the right place
		var j = fotoFeed.feeds.length - 1;
		while (j > 0 && loadedObj != fotoFeed.feeds[j]) {
			j--;
		}
		while (j > 0 && loadedObj.published > fotoFeed.feeds[j-1].published) {
			fotoFeed.feeds[j] = fotoFeed.feeds[j-1];
			j--;
		}
		fotoFeed.feeds[j] = loadedObj;
		 
		if (fotoFeed.numLoaded >= fotoFeed.feeds.length) { fotoFeed.show(); }   
	}

	fotoFeed.show = function() { // private method    
		var container = fotoFeed.containerId;
        P=document.getElementById(container);  
	   // var displayPhotos = document.createElement("div");
		for (var i=0; i < fotoFeed.feeds.length; i++) {
			var feed = fotoFeed.feeds[i];
			if (feed.error) { continue; }
			P.appendChild(feed.show());
			
		}
        fotoFeed.stopLoading();  
	    fading(P,0);
//		K.appendChild(P);
		var Q=0;
		var L=35;
fotoFeed.currentlyAppearing=window.setInterval(function(){fading(P,(Q*10)/L);Q++;if(Q>L){window.clearInterval(fotoFeed.currentlyAppearing)}},15);        
	
};

	
	fotoFeed.startLoading=function(){
		var K=document.getElementById(fotoFeed.containerId);  
		if(!K)
		{
			return false;
		}
		if(K._loading)
		{
			K.removeChild(K._loading);
		}
		fotoFeed.dots=fotoFeed.dots%3;
		fotoFeed.dots++;
		
		var M="Loading";
		for(var L=0;L<fotoFeed.dots;L++)
		{
			M+="&nbsp;";
		}
		K._loading=document.createElement("center");
		K._loading.innerHTML=M;
		K.appendChild(K._loading); 
		fotoFeed.loading=window.setTimeout(fotoFeed.startLoading,30);
	};   
	
	fotoFeed.stopLoading=function(){
		var K=document.getElementById(fotoFeed.containerId);
		if(!K)
		{
			return false;
		}
		if(K._loading)
		{
			K.removeChild(K._loading)
		}
		window.clearTimeout(fotoFeed.loading)
	};
}         
	
	
	function grabThumb (feedURL, ns, localName, displayName) { 
		this.feedURL = feedURL;
		this.displayName = displayName;
		this.error = false;
        this.thumb = new Array();
        this.ht = new Array();
        this.wd = new Array();
		this.url = this.feedURL;
		this.entries = null;
		this.published = 0;
		

		this.load = function(photoFeedObj) {
			this.photofeed = new google.feeds.Feed(this.feedURL);
			this.photofeed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
			this.photofeed.setNumEntries(6);
            var parentObj = this;
			this.photofeed.load(function(result) {
					parentObj.loadedFeed(result,photoFeedObj);
			});
		}

		this.loadedFeed	= function(result,photoFeedObj) {
			if (!result.error) {
				this.url = result.feed.link;
				this.entries = result.feed.entries;
				this.displayName = (this.displayName == null) ? result.feed.title : this.displayName;
				if (this.entries.length > 0) { this.published = new Date(this.entries[0]["publishedDate"]); }

				for(i=0; i<this.entries.length; i++ )
				{
					
					var thumbNodes = google.feeds.getElementsByTagNameNS(this.entries[i].xmlNode,"http://search.yahoo.com/mrss/","thumbnail");
	                var node = thumbNodes[0];
				  	this.thumb[i] = null;
				  	this.thumb[i] = node.getAttribute("url");
				  	this.ht[i] = node.getAttribute("height");
				  	this.wd[i] = node.getAttribute("width");
				  	if (!this.thumb[i]) {
				    	thumb[i] = node.firstChild.nodeValue;
				  	}
                }
			} else {
				this.error = true;
			}

			photoFeedObj.loadedFeed(this);
		}

		this.show = function() {
			if (this.error) { return false; }
		      var div = document.createElement("div");
		      div.className = "photoGrp"; 
			  var title = document.createElement('a');
			  title.appendChild(document.createTextNode(this.displayName));
			  title.href = this.url;
			  title.target = "_blank";
			  div.appendChild(title);
			  div.appendChild(getDateSpan(new Date(this.entries[0]["publishedDate"])));			
			  div.appendChild(document.createElement("br"));
			 for(i=0; i<this.thumb.length; i++)
			   {
				  var newimg = document.createElement("img");			
				  if(this.ht[i]>75)
				  newimg.height = 75;
				  newimg.src = this.thumb[i]; 
				  newimg.className = "thumbnails";
				  	var thmblink = document.createElement('a');
				  	thmblink.appendChild(newimg);
				  	thmblink.href = this.entries[i]["link"];
				  	thmblink.target = "_blank";
				  	div.appendChild(thmblink); 
				//alert(ht[i]+" "+wd[i]);
			   }                     

			return div;
		} 
		
		function getDateSpan(dateString) {
			var date = new Date(dateString);
			var today = new Date();
			var dateSpan = document.createElement('span');
			dateSpan.className = 'll-entryDate';
			dateSpan.appendChild(document.createTextNode(' - ' + gv_month[date.getMonth()] + ' ' + date.getDate()));
			if (date.getFullYear() != today.getFullYear()) {
				dateSpan.appendChild(document.createTextNode(" '" + String(date.getFullYear()).substr(2) ));
			}
			return dateSpan;
		}

	}    
          
function fading(K,L)
{                           
	K.style.opacity=L/10;
	K.style.filter="alpha(opacity="+L*10+")";
}

function initiateLinks()
{
	var photobox = document.createElement("div");
	var tweetbox = document.createElement("div");
	var bloglinkedlist = document.createElement("div");
	var otherlinkedlist = document.createElement("div");

	var blogLinks = new LinkedList("livebloglinks", 0); //number of entries per link = 0

	blogLinks.add("http://www.blogger.com/feeds/1252640030880841746/posts/default", "Journal (skaran);");
	blogLinks.add("http://prateekrungta.com/feed.xml", "Rungta's");
	blogLinks.add("http://varenya.blogspot.com/atom.xml", "Varenya's Ad-libs");
	blogLinks.add("http://bhavya.blogspot.com/atom.xml", "Bhavya's Blog - Blah!");
	blogLinks.add("http://impulsivehighlighters.blogspot.com/atom.xml", "Impulsive Highlighters");
	blogLinks.add("http://www.lnexun.com/exun.xml", "ln(exun)");
	blogLinks.add("http://blogs.sun.com/souvik/feed/entries/atom", "Sunny Affairs - blogs.sun.com");
	blogLinks.add("http://varunsp.blogspot.com/feeds/posts/default", "Varun Shahdadpuri");
	blogLinks.add("http://ankitaggarwal.me/feed/", "Ankit Aggarwal .me");
	
	
	var twitterFeeds = new LinkedList("livetwitterlinks",1);
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/10065952.rss");
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/16107421.rss");
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/52436551.rss");
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/18372088.rss");
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/25378640.rss");
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/14757080.rss");
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/31121669.rss");
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/17539952.rss");
	twitterFeeds.add("http://twitter.com/statuses/user_timeline/46144978.rss");

	var otherLinks = new LinkedList("liveotherlinks", 0); //number of entries per link = 0

	otherLinks.add("http://www.tuaw.com/rss.xml");
	otherLinks.add("http://daringfireball.net/index.xml");  
	otherLinks.add("http://news.com.com/2547-1_3-0-20.xml");
	otherLinks.add("http://www.macrumors.com/macrumors.xml");
	otherLinks.add("http://feeds.feedburner.com/TheSecretDiaryOfSteveJobs?format=xml");
	otherLinks.add("http://www.engadget.com/rss.xml");
	otherLinks.add("http://feeds.feedburner.com/OfficialGoogleMacBlog?format=xml");

	var photoThumbs = new photoFeeds("livephotos");
	photoThumbs.add("http://picasaweb.google.com/data/feed/base/featured?alt=rss&kind=photo&hl=en_US", "http://search.yahoo.com/mrss/","thumbnail","Picasa Featured Photos");
	photoThumbs.add("http://api.flickr.com/services/feeds/groups_pool.gne?id=22258661@N00&lang=en-us&format=rss_200", "http://search.yahoo.com/mrss/","thumbnail");
	photoThumbs.add("http://api.flickr.com/services/feeds/groups_pool.gne?id=649667@N25&lang=en-us&format=rss_200", "http://search.yahoo.com/mrss/","thumbnail");
	photoThumbs.add("http://api.flickr.com/services/feeds/photos_public.gne?id=58196392@N00&lang=en-us&format=rss_200", "http://search.yahoo.com/mrss/","thumbnail");
	photoThumbs.add("http://api.flickr.com/services/feeds/photos_public.gne?id=9620344@N08&lang=en-us&format=rss_200", "http://search.yahoo.com/mrss/","thumbnail");
	photoThumbs.add("http://picasaweb.google.com/data/feed/base/user/souvikdg?alt=rss&kind=album&hl=en_US&access=public", "http://search.yahoo.com/mrss/","thumbnail");
	
	photoThumbs.fetch();
	blogLinks.display();
	twitterFeeds.display();
	otherLinks.display();
	
	//document.getElementById("livephotos").appendChild(photobox);
}
