/* 	Google-Buzz Widget v1.0
	Blog : http://www.moretechtips.net
	Project: http://code.google.com/p/google-buzz-widget/
	Copyright 2009 [Mike @ moretechtips.net]
	Licensed under the Apache License, Version 2.0
	(the "License"); you may not use this file except in compliance with the License.
	You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
(function($){
    $.fn.googleBuzz=function(allOptions){
        var defaults={
            debug:0,
            username:"",
            n:10,
            show_n:1,
            snippet:1,
            stay_time:5000,
            enter_time:200,
            exit_time:200,
            animate:"opacity",
            show_date:0,
            show_source:0,
            header:"",
            info:''
        };

        allOptions=$.extend({},defaults,allOptions);
        return this.each(function(){
            var div=$(this);
            var count=0,ul=null,vp=-1,startI=-1,endI=-1;
            var op=allOptions;
            var effectParams=new Object;
            var requested=function(json){
                if(json.responseStatus!=200){
                    if(op.debug){
                        div.html('<b style="color:red">Error: '+json.responseDetails+"</b>")
                    }
                    return
                }
                var rs=json.responseData.feed.entries;
                count=rs.length;
                if(count==0){
                    return
                }
                ul=$('<ul class="gbw"></ul>').appendTo(div.html(op.header));
                for(var i=0;i<count;i++){
                    addLI(rs[i])
                }
                if(op.show_n>0){
                    fadeIn()
                }
            };

            var addLI=function(x){
                var src=x.title.replace(/^buzz\sby\s[\w\s]+\sfrom\s([\s\w]+)$/i,"$1");
                $("<div"+(op.show_n>0?' style="display:none"':"")+'><span class="gbw-content">'+(x.content)+'</span><span class="gbw-meta">'+(op.show_date?'<a class="gbw-date" href="'+x.link+'">'+formatDate(x.publishedDate)+"</a>":"")+(op.show_source?' - <span class="gbw-source">'+src+"</span>":"")+(op.info?" - "+op.info:"")+"</span></div>").appendTo(ul)
            };

            var formatDate=function(dstr){
                var dat=new Date(),tody=new Date();
                dat.setTime(Date.parse(dstr));
                var td=tody.getDate(),tm=tody.getMonth()+1,ty=tody.getFullYear(),th=tody.getHours(),tmn=tody.getMinutes(),ts=tody.getSeconds();
                var d=dat.getDate(),m=dat.getMonth()+1,y=dat.getFullYear(),h=dat.getHours(),mn=dat.getMinutes(),s=dat.getSeconds();
                if(y==ty&&m==tm&&d==td){
                    var dh=th-h;
                    if(dh>0){
                        return dh+" hour"+(dh>1?"s":"")+" ago"
                    }
                    var dmn=tmn-mn;
                    if(dmn>0){
                        return dmn+" minute"+(dmn>1?"s":"")+" ago"
                    }
                    var ds=ts-s;
                    return ds+" second"+(ds>1?"s":"")+" ago"
                }else{
                    return m+"/"+d+"/"+y
                }
            };

            var fadeOut=function(){
                $("li",ul).eq(startI).fadeOut(op.exit_time,fadeIn);
                if(op.show_n>1){
                    $("li",ul).slice(startI+1,endI).fadeOut(op.exit_time)
                }
            };

            var fadeIn=function(){
                vp++;
                if(vp*op.show_n>=count){
                    vp=0
                }
                startI=vp*op.show_n;
                endI=(vp+1)*op.show_n;
                $("li",ul).eq(startI).animate(effectParams,op.enter_time,"linear",fadeStill);
                if(op.show_n>1){
                    $("li",ul).slice(startI+1,endI).animate(effectParams,op.enter_time,"linear")
                }
            };

            var fadeStill=function(){
                ul.animate({
                    opacity:1
                },op.stay_time,"linear",fadeOut)
            };

            var linkify=function(str,source){
                str=str.replace(/\bhttps?\:\/\/\S+/gi,function(b){
                    var c="";
                    b=b.replace(/(\.*|\?*|\!*)$/,function(m,a){
                        c=a;
                        return""
                    });
                    return'<a class="gbw-link" href="'+b+'">'+((b.length>25)?b.substr(0,24)+"...":b)+"</a>"+c
                });
                if(source=="Twitter"){
                    str=str.replace(/\B\@([A-Z0-9_]{1,15})/gi,'@<a class="gbw-at" href="http://twitter.com/$1">$1</a>').replace(/\B\#([A-Z0-9_]+)/gi,'<a class="gbw-hashtag" href="http://search.twitter.com/search?q=%23$1">#$1</a>')
                }
                return str
            };

            var request=function(){
                var data={
                    q:"http://buzz.googleapis.com/feeds/"+op.username+"/public/posted",
                    output:"json",
                    v:"1.0",
                    num:op.n
                };

                $.ajax({
                    url:"http://ajax.googleapis.com/ajax/services/feed/load",
                    data:data,
                    success:requested,
                    dataType:"jsonp"
                })
            };

            var init=function(){
                if(div.attr("options")){
                    try{
                        op=eval("("+div.attr("options")+")")
                    }catch(e){
                        div.html('<b style="color:red">'+e+"</b>");
                        return
                    }
                    op=$.extend({},defaults,op)
                }
                effectParams[op.animate]="show";
                request()
            };

            init()
        })
    }
})(jQuery);
jQuery(document).ready(function(){
    jQuery("div.google-buzz").googleBuzz()
});
