/*
 * jQuery News Viewer Plugin for WYSIWYG Web Builder 8
 * Copyright Pablo Software solutions 2011
 * http://www.wysiwygwebbuilder.com/
 */
(function(c){function h(c){return c<10?"0"+c:c}function s(e,a,d,b,k,f){a=c("<a></a>").attr("href",a).attr("target",f.target).html(d);a=c("<h4></h4>").append(a);if(f.includeDate){d=new Date(b);if(f.dateFormat=="twitter")b=parseInt(((new Date).getTime()-d.getTime())/1E3),b=b<60?"less than a minute ago":b<120?"about a minute ago":b<3600?parseInt(b/60).toString()+" minutes ago":b<7200?"about an hour ago":b<86400?"about "+parseInt(b/3600).toString()+" hours ago":b<172800?"1 day ago":parseInt(b/86400).toString()+
" days ago";else var q="January,February,March,April,May,June,July,August,September,October,November,December".split(","),g="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(","),m=d.getFullYear(),j=m.toString().substring(2),i=d.getMonth()+1,n=d.getDate(),o=d.getHours(),t=d.getMinutes(),p=d.getSeconds(),b=f.dateFormat,b=b.replace("hh",h(o)),b=b.replace("h",o),b=b.replace("nn",h(t)),b=b.replace("n",t),b=b.replace("ss",h(p)),b=b.replace("s",p),b=b.replace("yy",m),b=b.replace("y",j),b=
b.replace("mm",h(i)),b=b.replace("m",i),b=b.replace("dd",h(n)),b=b.replace("d",n),b=b.replace("DD","X1"),b=b.replace("D","X2"),b=b.replace("MM","X3"),b=b.replace("M","X4"),b=b.replace("X1",g[d.getDay()]),b=b.replace("X2",g[d.getDay()].substring(0,2)),b=b.replace("X3",q[i-1]),b=b.replace("X4",q[i-1].substring(0,3));b=c("<div></div>").addClass("publication-date").text(b)}else b=c("<div></div>");k=c("<div></div>").addClass("summary").html(k);a=c("<div></div>").addClass("headline").append(a,b,k,"<br>");
f.animation<=1&&f.mode=="rotate"&&a.css({opacity:0});f.sortOrder==0?a.appendTo(e):a.prependTo(e)}function l(e,a){var d="#"+e.attr("id")+" .headline";if(a.mode=="default")e.css("overflow-y","auto"),c(d).css("position","relative"),c(d).css("height","auto"),c(d).css("top","0");else if(a.mode=="rotate"){var b=0,k=0,f=0;if(a.animation==0||a.animation==1)c(d).eq(b).css({opacity:1});else if(a.animation==2||a.animation==3)c(d).eq(b).css("left",0),f=e.width()+10,a.animation==3&&(f=-f);else if(a.animation==
4||a.animation==5)c(d).eq(b).css("top",0),f=e.height()+10,a.animation==5&&(f=-f);var q=c(d).length,g,m=false,j={},i={},n=function(){if(!m){m=true;g=false;b=(k+1)%q;if(a.animation==0||a.animation==1)j={opacity:0},i={opacity:1};else if(a.animation==2||a.animation==3)j={left:-f},i={left:0};else if(a.animation==4||a.animation==5)j={top:-f},i={top:0};c(d).eq(k).animate(j,a.animationDuration,function(){a.animation==2||a.animation==3?c(this).css("left",f):(a.animation==4||a.animation==5)&&c(this).css("top",
f)});c(d).eq(b).animate(i,a.animationDuration,function(){m=false;g||(g=setTimeout(n,a.pause))});k=b}};g||(g=setTimeout(n,a.pause));e.hover(function(){clearTimeout(g);g=false},function(){g||(g=setTimeout(n,250))})}else if(a.mode=="scroller")c(d).css("position","relative"),c(d).css("height","auto"),c(d).css("top","0"),a.ticker_height=e.height(),a.news_height=a.container.height(),a.line_count=0,a.rotate=true,e.hover(function(){a.rotate=false},function(){a.rotate=true}),u(e,a);else if(a.mode=="paginate"){e.css("overflow-y",
"auto");c(d).css("position","relative");c(d).css("height","auto");c(d).css("top","0");for(var o=0,h=function(){e.find(".headline").hide().slice(o*a.numPerPage,(o+1)*a.numPerPage).show()},p=e.find(".headline").length,p=Math.ceil(p/a.numPerPage),l=c('<div class="pager"></div>'),r=0;r<p;r++)c('<span class="page-number"></span>').text(r+1).bind("click",{newPage:r},function(a){o=a.data.newPage;h();c(this).addClass("active").siblings().removeClass("active")}).appendTo(l).addClass("clickable");l.prependTo(e).find("span.page-number:first").addClass("active");
h()}else a.mode=="ticker"&&(c(d).css("position","relative"),c(d).css("height","auto"),c(d).css("top","0"),v(e,a))}function u(e,a){a.line_count+=a.rotate?-2:0;c("p",e).css("top",a.line_count);if(a.line_count<-1*a.news_height)a.line_count=a.ticker_height;setTimeout(function(){u(e,a)},a.speed)}function v(c,a){c.find(".headline:last").clone().prependTo(c).css("display","none");c.find(".headline:first").fadeIn(1E3).slideDown(500);c.find(".headline:last").fadeOut().remove();setTimeout(function(){v(c,a)},
a.pause)}c.fn.newsviewer=function(e){var a=c.extend({animation:0,animationDuration:500,dataSource:"local",dateFormat:"yy/mm/dd",includeDate:true,maxItems:10,mode:"default",numPerPage:1,param:null,pause:5E3,sortOrder:0,speed:50,target:"_self",url:null},e);return this.each(function(){c.newsviewer(c(this),a)})};c.newsviewer=function(e,a){e.empty();a.mode=="scroller"&&c("<p></p>").css("position","relative").appendTo(e);a.container=e;if(a.mode=="scroller")a.container=c("p",e);if(a.dataSource=="local"){if(a.param!=
null)for(var d=0;d<a.param.length;d++)s(a.container,a.param[d][0],a.param[d][1],a.param[d][2],a.param[d][3],a);l(e,a)}else if(a.dataSource=="rss"){var b=c("<img/>").attr({src:"wb.newsviewer.gif",alt:"Loading. Please wait."}).addClass("news-wait").appendTo(e);c.get(a.url,function(d){b.remove();c("rss item",d).each(function(b){if(b>=a.maxItems)return false;s(a.container,c("link",this).text(),c("title",this).text(),c("pubDate",this).text(),c("description",this).text(),a)});l(e,a)})}else a.dataSource==
"twitter"?(b=c("<img/>").attr({src:"wb.newsviewer.gif",alt:"Loading. Please wait."}).addClass("news-wait").appendTo(e),d="http://twitter.com/status/user_timeline/",d+=a.param,d+=".json?count=",d+=a.maxItems,d+="&callback=?",c.getJSON(d,function(d){b.remove();c.each(d,function(b,c){var d=c.created_at.split(" "),d=Date.parse(d[1]+" "+d[2]+", "+d[5]+" "+d[3]);s(a.container,"http://twitter.com/"+a.param,c.text,d,"",a)});l(e,a)})):a.dataSource=="flickr"&&(b=c("<img/>").attr({src:"wb.newsviewer.gif",alt:"Loading. Please wait."}).addClass("news-wait").appendTo(e),
d="http://api.flickr.com/services/feeds/photos_public.gne?format=json&",a.param!=null&&(d+=a.param,d+="&"),d+="jsoncallback=?",c.getJSON(d,function(d){b.remove();c.each(d.items,function(b,d){var e=c("<a></a>").attr("href",d.link).attr("target",a.target).text(d.title),e=c("<h4></h4>").append(e),h=c("<div></div>").addClass("publication-date").text(d.author.split("(")[1].replace(")","")),j=c("<img>").attr("src",d.media.m);c("<div></div>").addClass("headline").append(e,h,j,"<br>").appendTo(a.container);
if(b==a.maxItems)return false});l(e,a)}))}})(jQuery);
