/*
 * jquery.widget.js
 * jQuery Widget Plugin
 *
 * Author:  NextCode.it <info@nextcode.it>
 * Version: 1.2.0 
 * Date:    31 Jan 2011
 * Support: http://www.nextcode.it/demo/widget/
 * 
 * Changelog
 * 1.0    4 Jan 2011: released first version
 * 1.1	  10 Jan 2011: fixed a bug for date in Twitter mode, only for Opera
 * 1.2    31 Jan 2011: console activated by debug parameter
 * 1.3    15 Jun 2011:
 */
function widgetTwitterCallBack(){}function widgetTwitterXmlCallBack(b){console&&console.log(b)}function widgetParseTwitterDate(b){$.browser.msie?(b=b.split(" "),b=b[1]+" "+b[2]+" "+b[5]):(b=(new Date(Date.parse(b))).toLocaleString(),b=b.split(" "),b=$.browser.opera?b[0]:b[1]+" "+b[2]+" "+b[3]);return b}if($.browser.msie||$.type(console)=="undefined")var console=null; (function(b){b.fn.widget=function(k){b.fn.widgetReadUserInfo=function(){if(b.type(b(this).data("jquery.widget"))!=="undefined"){var a=b(this).data("jquery.widget");if(a.defaults.type=="twitter"){var c="http://api.twitter.com/1/users/show.json?id="+a.defaults.account,c=a.defaults.proxy+"?"+b.param({type:"twitter",type2:"userinfo",url:c});a.defaults.debug&&console&&console.log("Reading user info from "+c);b.ajax({type:"GET",url:c,dataType:"json",success:function(b){a.defaults.debug&&console&&(console.log(b), console.log("Read user info from "+c));var d=a.find("._body"),g=d.find("._content ._userinfo");a.find("._body ._title").html(b.name+" ("+a.defaults.account+") on Twitter");var h=d.find("._title").height()-Math.abs(parseInt(d.find("._title").css("top")))+5,e=d.height()-h;d.find("._content ._text").animate({height:e-(a.layout.useUserInfo?a.defaults.contentUserInfoHeight:0)-a.defaults.contentTypeHeight*2});d.find("._content").animate({top:h,height:e});g.find("._img img").attr("src",b.profile_image_url); g.find("._info").html('<div class="_userlabel">'+b.name+'</div><div class="_username">'+a.defaults.account+'</div><div class="_follow"></div>').click(function(){window.open("http://twitter.com/"+a.defaults.account,"_blank")});g.find("._info > div").css({overflow:"hidden",maxWidth:g.width()-g.find("._img").outerWidth()+"px"})}})}}};b.fn.widgetReadFeed=function(a){if(b.type(b(this).data("jquery.widget"))!=="undefined"){var c=b(this).data("jquery.widget"),a=parseInt(a);a==0&&(a=1);var f=c.defaults.maxItems; if(c.defaults.type=="youtube"){var d="http://gdata.youtube.com/feeds/api/users/"+c.defaults.account+"/"+c.defaults.section+"?v=2&alt=jsonc&start-index="+a+"&max-results="+f,d=c.defaults.proxy+"?"+b.param({type:"youtube",url:d});c.defaults.debug&&console&&console.log("Youtube GETTING "+d+" ...");b.getJSON(d,function(a){c.defaults.debug&&console&&(console.log("Youtube GET "+d+": Found "+a.data.items.length+" records "),console.log(a));c.find("._body ._content ._text ._row").remove();for(var b=0;b<a.data.items.length;b++){var e= a.data.items[b];c.defaults.section=="uploads"?c.find("._body ._content ._text").append('<div class="_row" rel="http://www.youtube.com/watch?v='+e.id+'"><div class="_image"><img src="'+e.thumbnail.sqDefault+'"></div><div class="_info" style="width:'+c.rowWidth+'px;">'+e.title+"</div></div>"):c.defaults.section=="favorites"&&c.find("._body ._content ._text").append('<div class="_row" rel="http://www.youtube.com/watch?v='+e.video.id+'"><div class="_image"><img src="'+e.video.thumbnail.sqDefault+'"></div><div class="_info" style="width:'+ c.rowWidth+'px;">'+e.video.title+"</div></div>")}})}else c.defaults.type=="twitter"?(d="http://api.twitter.com/1/statuses/user_timeline/"+c.defaults.account+".json?page="+a+"&count="+f,d=c.defaults.proxy+"?"+b.param({type:"twitter",type2:"timeline",url:d}),c.defaults.debug&&console&&console.log("Twitter GETTING "+d+" ..."),b.ajax({type:"GET",url:d,dataType:"json",success:function(a){c.find("._body ._content ._text ._row").remove();c.defaults.debug&&console&&(console.log(a),console.log("Twitter GET "+ d+": Found "+a.length+" record"));for(var b=0;b<a.length;b++){var e=a[b],f=widgetParseTwitterDate(e.created_at),e=e.text,i="",j=e.match(/(http:\/\/fb.me\/[a-zA-Z0-9]*)/);j!=null&&(i=j[0]);c.find("._body ._content ._text").append('<div class="_row" rel="'+i+'"><div class="_info" style="width:'+c.rowFullWidth+'px;">'+e+'</div><div class="_date" style="width:'+c.rowFullWidth+'px;">'+f+"</div></div>")}},error:function(a,b){c.defaults.debug&&console&&console.log("Twitter GET "+d+": Error '"+b+"'")}})): c.find("._body ._content ._text").html("Content not available")}};b(this).each(function(){var a=b(this);b.type(a.attr("id"))=="undefined"&&a.attr("id","widget_"+Math.round(Math.random()*1E17));a.id=a.attr("id");a.defaults={width:200,height:400,type:"youtube",section:"uploads",account:"",maxItems:15,contentTypeHeight:15,contentRowImageWidth:49,contentUserInfoHeight:50,imagePath:"images/widgets/",proxy:"js/proxy.php",debug:!1};a.defaults=b.extend(a.defaults,k);a.defaults.speedBackup=a.defaults.speed; a.layout={useRowImage:a.defaults.type=="youtube",useUserInfo:a.defaults.type=="twitter"};a.data("jquery.widget",a);a.addClass("_widget _"+a.defaults.type);a.css({width:a.defaults.width+"px",height:a.defaults.height+"px"});a.append('<div class="_body"><div class="_title"><span></span></div><div class="_content"><div class="_type"></div><div class="_text"></div></div><div class="_left"></div><div class="_center"></div><div class="_right"></div></div>');var c=a.find("._body");a.defaults.type=="twitter"&& c.find("._content").append('<div class="_userinfo"><div class="_img"><img></div><div class="_info"></div></div>');a.defaults.type=="youtube"?(c.find("._title span").html(a.defaults.account+"'s Channel").click(function(){window.open("http://www.youtube.com/user/"+a.defaults.account,"_blank")}),c.find("._content ._type").html("Youtube"+(a.defaults.section=="favorites"?" (Favorites)":""))):a.defaults.type=="twitter"&&(c.find("._title span").click(function(){window.open("http://twitter.com/"+a.defaults.account, "_blank")}),c.find("._content ._type").html("Twitter"));var f=a.defaults.height-a.find("._top").height()-a.find("._bottom").height();a.find("._top ._center").css({width:a.defaults.width-a.find("._top ._left").width()-a.find("._top ._right").width()});a.find("._body, ._body > div").css({height:f});c.find("._center").css({width:a.defaults.width-c.find("._left").width()-c.find("._right").width()});c.find("._title").css({left:0,height:"auto",width:c.find("._center").width()});var f=c.find("._title").outerHeight()- Math.abs(parseInt(c.find("._title").css("top")))+5,d=c.height()-f,g=c.width()-c.find("._left").width()*2;c.find("._content").css({top:f,left:c.find("._left").width(),height:d,width:g});a.rowWidth=g-(a.layout.useRowImage?a.defaults.contentRowImageWidth:0)-10-17;a.rowFullWidth=g-10-17;c.find("._content ._text").css({height:d-(a.layout.useUserInfo?a.defaults.contentUserInfoHeight:0)-a.defaults.contentTypeHeight*2});a.find("._bottom ._center").css({width:a.defaults.width-a.find("._bottom ._left").width()- a.find("._bottom ._right").width()});c.find("._title").show();c.find("._content").fadeIn(500);a.widgetReadUserInfo();a.find("._row[rel]").live("click",function(){window.open(b(this).attr("rel"),"_blank")});a.widgetReadFeed(1)})}})(jQuery);
