(function($){
    var current=null;
    $.fn.rssfeed=function(url,options){
        var defaults={
            limit:10,
            header:false,
            titletag:'h4',
            date:true,
            content:true,
            snippet:true,
            showerror:true,
            errormsg:'',
            key:null
        };

        var options=$.extend(defaults,options);
        return this.each(function(i,e){
            var $e=$(e);
            if(!$e.hasClass('rssFeed'))$e.addClass('rssFeed');
            if(url==null)return false;
            var api="http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+url;
            if(options.limit!=null)api+="&num="+options.limit;
            if(options.key!=null)api+="&key="+options.key;
            $.getJSON(api,function(data){
                if(data.responseStatus==200){
                    _callback(e,data.responseData.feed,options);
                }else{
                    if(options.showerror)
                        if(options.errormsg!=''){
                            var msg=options.errormsg;
                        }else{
                            var msg=data.responseDetails;
                        };

                    $(e).html('<div class="rssError"><p>'+msg+'</p></div>');
                };

            });
        });
};

var _callback=function(e,feeds,options){
    if(!feeds){
        return false;
    }
    var html='';
    var row='odd';

    for(var i=0;i<feeds.entries.length;i++){
        var entry=feeds.entries[i];
        var entryDate=new Date(entry.publishedDate);
        var pubDate=entryDate.toLocaleDateString()+' '+entryDate.toLocaleTimeString();

        html += '<div class="panel">';
        
        html += '<img src="' + $('img', entry.content).attr("src") + '" width="459" height="305" border="0" />';
        html += '<div class="panel-overlay">';
        html += '<a href="' + entry.link + '" title="Clicca per leggere l\'intero articolo" target="_blank">';
        html += '<h2>' + entry.title + '</h2>';
        //html += '<p>' + entry.contentSnippet + '</p>';
        html += '</a>';
        html += '</div>';
        html += '</div>';

    }
   
    //filmstrip
    html+= '<ul class="filmstrip">';

    for(var i=0;i<feeds.entries.length;i++){
        var entry=feeds.entries[i];
        var entryDate=new Date(entry.publishedDate);
        var pubDate=entryDate.toLocaleDateString()+' '+entryDate.toLocaleTimeString();

        //html+='<li>'+'<a href="'+entry.link+'" title="'+feeds.title+'"><img src="' + $('img', entry.content).attr("src") + '" width="100" height="75" border="0" /></a>'
        html+='<li><img src="' + $('img', entry.content).attr("src") + '" width="100" height="75" border="0" /></li>';
    }

    /*for(var i=0;i<feeds.entries.length;i++){
        var entry=feeds.entries[i];
        var entryDate=new Date(entry.publishedDate);
        var pubDate=entryDate.toLocaleDateString()+' '+entryDate.toLocaleTimeString();
        html+='<li class="rssRow '+row+'">'+'<'+options.titletag+'><a href="'+entry.link+'" title="View this feed at '+feeds.title+'">'+entry.title+'</a></'+options.titletag+'>'
        if(options.date)html+='<div>'+pubDate+'</div>'

        alert($('img', entry.content).attr("src"));
        //alert($(entry.content).find('img').html());


        if(options.content){
            if(options.snippet&&entry.contentSnippet!=''){
                var content=entry.contentSnippet;
            }else{
                var content=entry.content;
            }

            //alert(entry.contentSnippet + " " + entry.content);


            //html+='<p>'+content+'</p>'
            }
        html+='</li>';
        if(row=='odd'){
            row='even';
        }else{
            row='odd';
        }
    }*/
html+='</ul>'
$(e).html(html);
    };

})(jQuery);

