$(document).ready(function() {
    $('#blog-feed').each(function() {
        var $container = $(this);
        $container.empty();

        var fadeHeight = $container.height() / 4;
        for (var yPos = 0; yPos < fadeHeight; yPos += 2) {
            $('<div></div>').css({
                opacity: yPos / fadeHeight,
                top: $container.height() - fadeHeight + yPos
            }).addClass('blog-fade-slice').appendTo($container);
        }

        var $loadingIndicator = $('<img/>')
      .attr({
          'src': '/images/loading.gif', //path gif per messaggio loading
          'alt': 'Caricamento news in corso' //messaggio loading
      })
      .addClass('blog-wait')
      .appendTo($container);

        //inserisci qui link a pagina proxy
        $.get('/Public/Rss/RssProxy.aspx?Feed=Blog', function(data) {
            $loadingIndicator.remove();
            $('rss item', data).each(function() {
                var $link = $('<a></a>')
          .attr({ 'href': $('link', this).text(), 'target': '_blank' })
          .text($('title', this).text());
                var $headline = $('<h4></h4>').append($link);

                //formattazione data: due formati = {pDate = blog es: "2 ore fa"} {dataEstesa = formato classico}
                var pDate = $(this).find('pubDate').text();
                pDate = new Date(pDate).getTime();
                var nowTime = new Date().getTime();
                var diff = (nowTime - pDate) / 1000;
                //alert(diff);
                if (diff < 300) {
                    pDate = "Meno di 5 minuti fa"
                } else if (diff < 3600) {
                    diff = Math.round(diff / 60);
                    pDate = "circa " + diff + " minuti fa";
                } else if (diff < 6000) {
                    diff = Math.round((diff - 3600) / 60);
                    pDate = "1 ora e " + diff + " minuti fa";
                } else if (diff < 86400) {
                    diff = Math.round(diff / 3600);
                    pDate = "circa " + diff + " ore fa";
                } else if (diff < 172800) {
                    diff = Math.round((diff - 86400) / 3600);
                    pDate = "1 giorno e " + diff + " ore fa";
                } else {
                    diff = Math.round(diff / 86400);
                    pDate = "circa " + diff + " giorni fa";
                }
                var pDate2 = new Date($('pubDate', this).text());
                var pubMonth = pDate2.getMonth() + 1;
                if (pubMonth < 10) { pubMonth = '0' + pubMonth; }
                var pubDay = pDate2.getDate();
                var pubYear = pDate2.getFullYear();
                var dataEstesa = pubDay + '/' + pubMonth + '/' + pubYear;

                var $publication = $('<div></div>')
          .addClass('publication-date')
                //visualizza data formato classico: es:01/07/2009
                //.text( dataEstesa);		  
                //visualizza data formato blog es: "2 ore fa"
    	  .text(pDate);

                var $summary = $('<div></div>')
          .addClass('summary')
          .html($('description', this).text());

                $('<div></div>')
          .addClass('headline')
          .append($headline)
          .append($publication)
          .append($summary)
          .appendTo($container);
            });

            var currentHeadline = 0, oldHeadline = 0;
            var hiddenPosition = $container.height() + 10;
            $('div.headline').eq(currentHeadline).css('top', 0);
            var headlineCount = $('div.headline').length;
            var pause;
            var rotateInProgress = false;

            var headlineRotate = function() {
                if (!rotateInProgress) {
                    rotateInProgress = true;
                    pause = false;
                    currentHeadline = (oldHeadline + 1)
            % headlineCount;
                    $('div.headline').eq(oldHeadline).animate(
            { top: -hiddenPosition }, 'slow', function() {
                $(this).css('top', hiddenPosition);
            });
                    $('div.headline').eq(currentHeadline).animate(
            { top: 0 }, 'slow', function() {
                rotateInProgress = false;
                if (!pause) {
                    pause = setTimeout(headlineRotate, 5000);
                }
            });
                    oldHeadline = currentHeadline;
                }
            };
            if (!pause) {
                pause = setTimeout(headlineRotate, 5000);
            }

            $container.hover(function() {
                clearTimeout(pause);
                pause = false;
            }, function() {
                if (!pause) {
                    pause = setTimeout(headlineRotate, 250);
                }
            });
        });
    });
 });