/**
 * Feed view controller
 * @author Peng Wang
 * @version 1
 * @requires jQuery 1.4+, jQuery.tmpl, feed API scripts, feed templates, cookie plugin
 * @todo queue box-height animation after fadeOut before fadeIn
 * @todo write tests
 */

window.jQuery&&(function(a){a.pjp=a.pjp||{};a.pjp.pager={options:{}}}(jQuery),window.Site&&function(a,g){var l,i;g.fn=a.extend(g.fn||{},{feed:{}});l=g.fn.feed.build=function(b){var d=0,m=0,h=[],c;a.each(Site.feedApiData,function(f,e){var j=Site.serviceUrl+"/feed."+f+".php",k,e=a.extend({},e);a.each(e,function(a){/^custom_/.test(a)&&delete e[a]});m+=1;a.ajax({dataType:g.feedApiData[f].custom_as_xml?"html":"json",type:"GET",data:decodeURIComponent(a.param(e,!0)),url:j,success:function(c){c=i(c,f);k=
a("#tpl-"+f+"-feed").render(c);h[g.feedOrder.indexOf(f)]=a(k).html()},error:function(){},complete:function(){d+=1;d===m&&(c=a("#tpl-feeds-container").render({feeds:h.join("\n")}),b(a(c).html()))}})})};i=g.fn.feed.enhance=function(b,d){switch(d){case "posterous":a.each(b.post,function(){a.extend(this,{humanDate:function(){return(new Date(this.date)).toDateString().replace(/\d{4}$/,"")},tags:function(){return this.tag.join(", ")}})});break;case "twitter":a.each(b.status,function(){this.text=this.text.replace(/([a-z]+:\/\/[\-_.\w\/]+)/g,
'<a class="inline" target="_blank" href="$1">$1</a>').replace(/(#([\-_\w]{2,}))/ig,'<a class="inline" target="_blank" href="http://search.twitter.com/search?q=%23$2">$1</a>').replace(/(@([\-_\w]+))/ig,'<a class="inline" target="_blank" href="http://twitter.com/$2">$1</a>')});break;case "github":b.entry=b.entry.slice(0,g.feedApiData[d].custom_count);a.each(b.entry,function(){var b=a(this.content);a("a.committer, a.compare-link, br:last",b).remove();a("br",b).replaceWith('<span class="delim">|</span>');
a("*:not(span)",b).addClass("inline");a(".message",b).addClass("meta");a("a",b).attr("target","_blank").attr("href",function(a,b){return b.replace(/^\//,"http://github.com/")});this.content=b.html();this.title=this.title.replace("hlfcoding ","")});break;case "deviantart":b={item:a(b).find("item").slice(0,g.feedApiData[d].custom_count).map(function(b,d){var c=a(d),f=a("[url]:eq(2)",c),e=a("pubDate",c).text();return{title:a('[type="plain"]',c).text(),permalink:a('guid[ispermalink="true"]',c).text(),
thumb:{url:f.attr("url"),width:f.attr("width"),height:f.attr("height")},date:{pub:e,human:(new Date(e)).toDateString()}}}).get()}}return b};a(document).ready(function(){var b=a("#content #ouFeed:eq(0) .in"),d=a("a.jFeed:eq(0)",b),g=a(".bd.mnLinked:eq(0)",b),h=d.clone().hide().toggleClass("jFeed jFeedRemember").find(".jOn").html("stay").end().find(".jOff").html("leave").hide().end().insertBefore(d),c=a.cookie("pengxwang_feed_preferred"),f={domain:window.location.hostname,path:window.location.pathname.substr(1),
secure:window.location.protocol==="https",expires:7},e,j=function(){d.find(".jOn").hide().end().find(".jOff").show();g.fadeOut("fast",function(){e.fadeIn("fast");h.fadeIn("fast")})},k=function(){d.find(".jOn").show().end().find(".jOff").hide();e.fadeOut("fast",function(){g.fadeIn("fast");h.fadeOut("fast")})},i=function(){l(function(c){e=a(c).appendTo(b);j()})};d.bind("click",function(b){b.preventDefault();a(".jOn:visible",d).length>0?e===void 0?i():j():k()});c===null&&a.cookie("pengxwang_feed_preferred",!1,
f);a.cookie("pengxwang_feed_preferred")==="true"&&(d.trigger("click"),h.find(".jOn").hide().end().find(".jOff").show());h.bind("click",function(b){b.preventDefault();c=a.cookie("pengxwang_feed_preferred");a.cookie("pengxwang_feed_preferred",c==="true"?!1:!0,f);h.find(".jOn")[c==="true"?"show":"hide"]().end().find(".jOff")[c==="true"?"hide":"show"]()})})}(jQuery,Site));
