/**
 * Tom Haskell / http://tomhaskell.co.uk
 * Twitter / @tom_haskell
 * 
 * (c)Copyright 2011 Tom Haskell
 * 
 */

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

window.getBlogs = function(target, url, count, options){
    if (typeof url == 'object') {
        options = url;
        url = options.url;
        count = options.count;
    } 
    
    var feed = new google.feeds.Feed(url);
    feed.setNumEntries(count)
    feed.load(function(result){
        console.log(result);
        if (!result.error) {
            var container = document.getElementById(target);
            
            // clear contents
            while (container.firstChild) {
                container.removeChild(container.firstChild);
            }
            
            function node(e) {
                return document.createElement(e);
            }
            function text(t) {
                return document.createTextNode(t);
            }
            function link(url,t){
                var a = node('a');
                a.setAttribute('href', url);
                a.appendChild(text(t));
                return a;
            }
            
            for (var i = 0; i < result.feed.entries.length; i++) {
                var entry = result.feed.entries[i];
                
                var p = node("p");
                
                var title = node("span");
                title.setAttribute('class',"title");
                title.appendChild(link(entry.link, entry.title));
                title.appendChild( text( timeAgo( new Date(entry.publishedDate),null,2)) );
                p.appendChild(title);
                p.appendChild(text(entry.contentSnippet));
                p.appendChild(link(entry.link,'more'));
                
                var div = node("div").appendChild(p);
                container.appendChild(div);
            }
        }
        
    });
    
    //takes in two dates and sends back a string with the time that has elapsed
    function timeAgo(date1, date2, granularity){
	
        var self = this;
	
        periods = [];
        periods['week'] = 604800;
        periods['day'] = 86400;
        periods['hour'] = 3600;
        periods['minute'] = 60;
        periods['second'] = 1;
	
        if(!granularity){
            granularity = 5;
        }
	
        (typeof(date1) == 'string') ? date1 = new Date(date1).getTime() / 1000 : date1 = date1.getTime() / 1000;
        (typeof(date2) == 'string') ? date2 = new Date(date2).getTime() / 1000 : date2 = new Date().getTime() / 1000;
	
        if(date1 > date2){
            difference = date1 - date2;
        }else{
            difference = date2 - date1;
        }

        output = '';
	
        for (var period in periods){
            var value = periods[period];
            
            if(difference >= value){
                time = Math.floor(difference / value);
                difference %= value;
			
                output = output +  time + ' ';
			
                if(time > 1){
                    output = output + period + 's ';
                }else{
                    output = output + period + ' ';
                }
                
            }
            granularity--;
            if(granularity == 0){
                break;
            }	
        }
	
        return output + ' ago';
    }
        
    
}
