jQuery(function($){ var maxLength = 160; $.fn.prettyDate = function(){ return this.each(function(){ var date = new Date((this.title || "").replace(/-/g,"/").replace(/[TZ]/g," ")), diff = (((new Date()).getTime() - date.getTime()) / 1000), day_diff = Math.floor(diff / 86400); if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 ) return; $(this).text( day_diff == 0 && ( diff < 60 && "just now" || diff < 120 && "1 minute ago" || diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" || diff < 7200 && "1 hour ago" || diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") || day_diff == 1 && "Yesterday" || day_diff < 7 && day_diff + " days ago" || day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ); }); }; $.fn.addValues = function(opt){ var self = this; jQuery.each( opt, function(name,val){ self.append(""); }); return this; }; $.fn.reverse = function(){ return this.pushStack( [].reverse.apply( this ) ); }; $("#entries").each(function(){ var entries = this; // Make sure the post dates are constantly pretty $("a", entries).prettyDate(); setInterval(function(){ $("a", entries).prettyDate(); }, 5000); // Pull new posts in once a minute setInterval(function(){ $.ajax({ url: "?ajax=true", type: "GET", success: function(html){ var found = false; $("li", html).reverse().each(function(){ if ( !$("#" + this.id).length ) { $("#entries").prepend( this ); $("#entries li:last").remove(); found = true; } }); if ( found ) { restripe(); $("a", entries).prettyDate(); } }}); }, 60000); }); $("#submit:visible textarea").focus().select(); $("#login:not(.hidden) input:first").focus().select(); $("#submit input.submit").click(function(e) { $(this).attr("disabled","disabled"); }); $("#submit") .addValues({ ajax: "true" }) .ajaxForm(function(html){ $("#submit input.submit").attr("disabled",""); $("#submit textarea").val('').focus(); $(html).hide().prependTo("#entries").slideDown("slow"); $("#entries li:last").remove(); restripe(); $("a").prettyDate(); }); var submit = $("#text") .keydown(function(e){ if ( e.keyCode == 13 ) { $("#submit").submit(); $("#submit input.submit").attr("disabled","disabled"); $(this).blur(); return false; } }); $("#login") .addValues({ ajax: "true", "theme_page": "menu" }) .ajaxForm(function(html){ if ( html.indexOf("login") >= 0 ) { $("#login").addClass("error"); $("#login input:first").focus(); } else { $("div.navmenu").html( html ); $("#login").removeClass("error").hide(); submit = $("#submit").show().find("textarea").focus().select(); bindMenu(); startWatch(); } }); restripe(); bindMenu(); startWatch(); function restripe() { $("li.entry").removeClass("odd").filter(":odd").addClass("odd"); } function bindMenu() { $("#header a") .filter(".post").click(function(){ $("form.header:not(#submit)").hide(); $("#submit").toggle(); $("#text").focus().select(); return false; }).end() .filter(".auth").click(function(){ var self = this; $("form.header:not(#login)").hide(); this.blur(); if ( this.className.match(/login/) ) { if ( $("#login:visible").length ) $("#login").hide(); else $("#login").show().find("input:first").focus(); } else { $("div.navmenu").load( window.location.href, { auth: "logout", ajax: "true", "theme_page": "menu" }, function(){ $("#login, #submit").hide(); bindMenu(); }); } return false; }).end(); } function startWatch() { if ( submit.length && typeof submitWatch == "undefined" ) { submitWatch = setInterval(function(){ var str = submit[0].value .replace(/\[\S+\s+([^\]]+)(\]|$)/, "$1") .replace(/\[\S+\s*$/, "") .replace(/(^\s*|\s*$)/, ""); var per = Math.floor((1 - ((maxLength - str.length) / maxLength)) * 100); per = per > 100 ? 100 : per; $("#proglen,#proglen2").html( maxLength - str.length ); $("#progbar").width( per + "%" ); }, 100); } } });