$=jQuery;jQuery(document).ready(function()
{window.site=new DamnInteresting();});function DamnInteresting()
{var that=this;this.settings={"menuOpenDuration":200,"menuCloseDuration":150};this.submenuMouseOutTimer=null;this.openMenus=[];this.cookies={};this.loggedIn=false;this.regexp={'blanklines':/\n{2,}/g,'tags':/<[^>]+>/ig};this.init=function()
{this.enableEventHandlers();this.fillMutableBlurbs();this.cookies=this.parseCookies();this.detectLogin();};this.detectLogin=function()
{if(this.loggedIn)
{var displayName=this.getDisplayName();var emailHash=this.getEmailHash();if(displayName&&emailHash)
{$(".current-user-avatar").attr("src","http://www.gravatar.com/avatar/"+ emailHash+"?s=50");$(".fill-with-display-name").html(displayName);$(".show-when-logged-in").show();$(".show-when-logged-out").hide();}}};this.parseCookies=function()
{var cookies=decodeURIComponent(document.cookie).split(';');var cookieArray={};var nameVal='';var name='';var val='';for(var i=0;i<cookies.length;i++)
{nameVal=cookies[i].split('=');name=$.trim(nameVal[0]);val=$.trim(nameVal[1]);if(name.indexOf("damncache_")==0)
{val=val.split("|");cookieArray["email_hash"]=val[0];cookieArray["display_name"]=val[1];this.loggedIn=true;}
else
{cookieArray[name]=val;}}
return cookieArray;};this.getDisplayName=function()
{return this.cookies["display_name"];};this.getEmailHash=function()
{return this.cookies["email_hash"];};this.openMenu=function(el)
{if($(".menu-contents-container",el).css('display')=='block')
{return false;}
this.closeMenus();$(".menu-contents-container",el).show();$(".menu-contents .menu-option",el).css({'display':'none'}).slideDown(this.settings.menuOpenDuration);$(".menu-contents .menu-panel",el).css({'display':'none'}).slideDown(this.settings.menuOpenDuration,function()
{if($(el).attr('setfocus')){$($(el).attr('setfocus')).focus();}});this.openMenus.push(el);return true;};this.closeMenu=function(el)
{this.closeSubmenus($(el).parent());if($(".menu-contents-container",el).css('display')=='none')
{return false;}
$(".menu-contents .menu-option",el).slideUp(this.settings.menuCloseDuration);$(".menu-contents .menu-panel",el).slideUp(this.settings.menuCloseDuration);setTimeout(function()
{$(".menu-contents-container",el).hide();},this.settings.menuCloseDuration);if($(el).attr('setfocus'))
{$($(el).attr('setfocus')).blur();}
for(i=0;i<that.openMenus.length;i++)
{if(that.openMenus[i]==el)
{that.openMenus.splice(i,1);return;}}
return true;};this.closeMenus=function()
{var openMenus=this.openMenus;for(i=0;i<openMenus.length;i++)
{this.closeMenu(openMenus[i]);}};this.openSubmenu=function(el)
{this.closeSubmenus($(el).parent().parent(),el[0]);if($(el).css('display')=='block')
{return false;}
$('.submenu-option',el).hide();el.css('width','1px').show();el.animate({'width':'88px'},this.settings.menuOpenDuration/2,function()
{$(".submenu-option",el).css({'display':'none'}).slideDown(that.settings.menuOpenDuration/2);});};this.closeSubmenu=function(el)
{if($(el).css('display')=='none')
{return false;}
$(".submenu-option",el).slideUp(this.settings.menuCloseDuration/2);setTimeout(function()
{$(el).animate({'width':'1px'},that.settings.menuOpenDuration/2,function()
{$(this).hide();});},this.settings.menuCloseDuration/2);};this.closeSubmenus=function(menuEl,submenuEl)
{$(".submenu-contents-container",menuEl).each(function()
{if($(this).css('display')=='block')
{if(!submenuEl||this!=submenuEl)
{that.closeSubmenu(this);}}});};this.scrollWindowTo=function(selector,time,callback)
{$('body, html').animate({'scrollTop':$(selector).offset().top},time);if(callback)
{callback();}};this.getDocHeight=function()
{var D=document;return Math.max(Math.max(D.body.scrollHeight,D.documentElement.scrollHeight),Math.max(D.body.offsetHeight,D.documentElement.offsetHeight),Math.max(D.body.clientHeight,D.documentElement.clientHeight));};this.quoteComment=function(name,body)
{var ta,val;ta=$("textarea#comment");val=$.trim(ta.val());if(val!='')
{val+="\n\n";}
body=body.replace("</p>","\n\n");body=body.replace(this.regexp.tags,"");body=$.trim(body.replace(this.regexp.blanklines,"\n\n"));ta.val(val+'[quote]'+ $.trim(name)+' said: "'+ body+'"[/quote]'+"\n\n");this.scrollWindowTo("#new-comment-area",1000,function()
{ta.focus();ta.setCursorPosition(ta.val().length);});};this.fillMutableBlurbs=function()
{a="Damn Interesting is made poossible by donations from readers like you.|If you can read this, you're using some bandwidth. Help fund it.|Even a few dollars helps. When I was your age that was a lot of money.|Information wants to be free, but web hosting doesn't.|Saturate your head-meat with sweet dopamine.|Always remember to tip the waitstaff and the writestaff.|Think of it as 'thank you' for the lack of ads.|Buttons are fun to click. Try it and see!".split("|");$(".donation-plea").each(function(){$(this).html(a[that.getRandomInt(0,a.length)]);});b="Go retro. Get it on pulp.|Now in handy book form.|Also available in tree-flesh form.|We are total sellouts.|Tactile alternative available now.|Books smell nice.|Defeat over 140 rocks!".split("|");$(".book-peddle").each(function(){$(this).html(b[that.getRandomInt(0,b.length)]);});c="Stay abreast of when something's afoot.|Cyber-stalking is not an electronic sock.|Let us commingle our online personas.|Fraternize with even more of our electrons.|Feast your eyes on the sweet social media meat.|Take these broken links and learn to fly again.".split("|");$(".be-a-stalker").each(function(){$(this).html(c[that.getRandomInt(0,c.length)]);});};this.getRandomInt=function(min,max)
{return Math.floor(Math.random()*(max- min))+ min;};this.enableEventHandlers=function()
{$("body").click(function()
{that.closeMenus();});$("#damn-main-menu .menu-label").click(function(ev)
{ev.stopPropagation();if(!$(this).is(':animated'))
{if(!that.openMenu(this))
{that.closeMenu(this);}}});$("#damn-main-menu .menu-label").mouseenter(function()
{if(that.openMenus.length>0)
{that.openMenu(this);}});$("#damn-main-menu .menu-option,#damn-main-menu .menu-panel").click(function(ev)
{ev.stopPropagation();});$(".submenu-contents-container").each(function()
{var parent=$(this).parent();$(parent).mouseenter(function()
{clearTimeout(that.submenuMouseOutTimer);that.openSubmenu($(".submenu-contents-container",this));});$(parent).mouseleave(function()
{el=$(".submenu-contents-container",this);that.submenuMouseOutTimer=setTimeout(function()
{that.closeSubmenu(el);},250);});});$(".comment-reply-link").click(function()
{var cont,name,text;cont=$(this).parent().parent().parent().parent();name=$(".commenter-name",cont).html();text=$(".comment-body",cont).html();that.quoteComment(name,text);});$("#comments-link").click(function()
{that.scrollWindowTo("#comments-area",500);return false;});$("textarea.elastic").each(function()
{$(this).elastic();});};this.init();}
new function($){$.fn.setCursorPosition=function(pos){if($(this).get(0).setSelectionRange){$(this).get(0).setSelectionRange(pos,pos);}else if($(this).get(0).createTextRange){var range=$(this).get(0).createTextRange();range.collapse(true);range.moveEnd('character',pos);range.moveStart('character',pos);range.select();}}}(jQuery);(function($){$.fn.extend({elastic:function(){var mimics=['paddingTop','paddingRight','paddingBottom','paddingLeft','fontSize','lineHeight','fontFamily','width','fontWeight'];return this.each(function(){if(this.type!='textarea'){return false;}
var $textarea=$(this),$twin=$('<div />').css({'position':'absolute','display':'none'}),lineHeight=parseInt($textarea.css('lineHeight'),10)||parseInt($textarea.css('fontSize'),'10'),minheight=parseInt($textarea.css('height'),10)||lineHeight*3,maxheight=parseInt($textarea.css('max-height'),10)||Number.MAX_VALUE,goalheight=0,i=0;$twin.appendTo($textarea.parent());var i=mimics.length;while(i--){$twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));}
function setHeightAndOverflow(height,overflow){curratedHeight=Math.floor(parseInt(height,10));if($textarea.height()!=curratedHeight){$textarea.css({'height':curratedHeight+'px','overflow':overflow});}}
function update(){var textareaContent=$textarea.val().replace(/<|>/g,' ').replace(/\n/g,'<br />').replace(/&/g,"&amp;");var twinContent=$twin.html();if(textareaContent+'&nbsp;'!=twinContent){$twin.html(textareaContent+'&nbsp;');if(Math.abs($twin.height()+lineHeight-$textarea.height())>3){var goalheight=$twin.height()+lineHeight;if(goalheight>=maxheight){setHeightAndOverflow(maxheight,'auto');}else if(goalheight<=minheight){setHeightAndOverflow(minheight,'hidden');}else{setHeightAndOverflow(goalheight,'hidden');}}}}
$textarea.css({'overflow':'hidden'}).bind('focus',function(){self.periodicalUpdater=window.setInterval(function(){update();},50);}).bind('blur',function(){clearInterval(self.periodicalUpdater);});update();});}});})(jQuery);
