﻿google.load('feeds', '1');
var FeedWidget = new Class({	
    Implements:Options,
    options: 
    {
        source:'http://www.bellme.se/bellme.se_rss.xml',
        timerToRefresh:'refresh',
        classNameForFeeds:'feedentry',	
        classNameForTitle:'',
        feedsElement:'feeds',
        feedHTML:'<div class="{classname_for_title}">{title}</div><div class="{classname_for_feeds}"><a href="{link}" title="{title}">{content}</a></div>',
        fxDuration:2000,
        secondsToRefresh:{from:8,to:20,interval:60,defaultInterval:3600},
        outputText:'uppdateras om {value}...'
    },  
    initialize:function(options)
    {
        this.setOptions(options);         
	    this.entries =  [];
	    this.timer = 0;
	    this.interval = 0;
	    this.timerToRefresh = $(this.options.timerToRefresh);	
	    this.feedsElement = $(this.options.feedsElement);
		this.paletteFeed = new google.feeds.Feed(this.options.source);			
		this.colourFeed = new google.feeds.Feed(this.options.source);	
		this.classes = {'classname_for_title':this.options.classNameForTitle,'classname_for_feeds':this.options.classNameForFeeds};
		this.poll.periodical(1000, this);	                                                                  				                
    },    
	poll: function()
	{
		var now = new Date();
		var ref = new Date();
		var interval = 0;
		var hours = now.getHours();
		if((hours >= this.options.secondsToRefresh.from) && (hours < this.options.secondsToRefresh.to))
		{
		    interval = this.options.secondsToRefresh.interval;
		}
		else
		{
		    interval = this.options.secondsToRefresh.defaultInterval;
		}
		if(this.interval != interval)
		{
		    this.timer = 0;
		    this.interval = interval
		}	
		if (--this.timer <= 0) 
		{			
			this.colourFeed.load(this.onLoadCallback.bind(this));
			this.paletteFeed.load(this.onLoadCallback.bind(this));		
			this.timer = this.interval;		
		}
    
        var diff = this.timer;
        
        var days = Math.floor(diff / (60 * 60 * 24));
        
        diff %= (60 * 60 * 24);
        
        var hours = Math.floor(diff / (60 * 60));
        
        diff %= (60 * 60);
        
        var minutes = Math.floor(diff /60);
        
        diff %= 60;
        
        var seconds = Math.floor(diff);       
        var milliSeconds = Math.floor(1000 * (diff-seconds));
        
        var labelDays = ' dagar ';
        var labelHours = ' timmar ';
        var labelMinutes = ' minuter ';
        var labelSeconds = ' sekunder ';
      
        //time and labels

        if(days == 1)
        {
            labelDays = ' dag ';
        }
        if(hours == 1)
        {
            labelHours = ' timme ';
        }        
        if(minutes == 1)
        {
            labelMinutes = ' minut ';
        }          
        if(seconds == 1)
        {
            labelSeconds = ' sekund ';
        }     
        
        var timeString = seconds + labelSeconds;
        if(hours > 0)
        {        
            timeString = hours + labelHours + ', ' + minutes + labelMinutes + ' och ' + seconds + labelSeconds;
        }
        else if(minutes > 0)
        {
            timeString = minutes + labelMinutes + ' och ' + seconds + labelSeconds;
        }
		this.timerToRefresh.set('html',this.options.outputText.replace('{value}',timeString));
	},    
	onLoadCallback:function(json){
		if (json.error) return;
		for (var i = json.feed.entries.length - 1; i >= 0; i--) 
		{
			var entry = json.feed.entries[i];
			if (!this.entries.contains(entry.title)) 
			{
				this.entries.push(entry.title);				
				$extend(entry, this.classes);
				this.feedsElement.innerHTML = this.options.feedHTML.substitute(entry) + this.feedsElement.innerHTML;				
			}
		}
		
		$$('.' + this.options.classNameForFeeds).set('tween',{duration:2000}).highlight().removeClass(this.options.classNameForFeeds);

	}	
       
});

