var cookieOption={path:"/"};var topMargin=20;window.addEvent("domready",function(){var mySlide;var el;var setAjax=function(el){$(el).innerHTML='<img src="/img/spinner.gif" />'};mySlide=new Fx.Slide("sliding_pane");mySlide.hide();$("sliding_pane").removeClass("hide");$$(".switch_signup").each(function(el){el.addEvent("click",function(e){e.stop();$("signup_wrapper").setStyle("display","block");$("login_wrapper").setStyle("display","none");$("reset_wrapper").setStyle("display","none");mySlide.slideIn().chain(function(){$("signup_u").focus()})})});$$(".switch_login").each(function(el){el.addEvent("click",function(e){e.stop();$("signup_wrapper").setStyle("display","none");$("login_wrapper").setStyle("display","block");$("reset_wrapper").setStyle("display","none");mySlide.slideIn().chain(function(){$("login_u").focus()})})});$$(".toggle_signup").each(function(el){el.addEvent("click",function(e){var alreadyOpen=($("signup_wrapper").getStyle("display")!="none");$("signup_wrapper").setStyle("display","block");$("login_wrapper").setStyle("display","none");$("reset_wrapper").setStyle("display","none");if(alreadyOpen){mySlide.toggle()}else{window.scroll(0,0);mySlide.slideIn().chain(function(){$("signup_u").focus()})}})});$$(".cancel").each(function(el){el.addEvent("click",function(e){$$("#sliding_pane form").each(function(frm){frm.reset()});mySlide.slideOut()})});$$(".toggle_login").each(function(el){el.addEvent("click",function(e){e.stop();var alreadyOpen=($("login_wrapper").getStyle("display")!="none");$("signup_wrapper").setStyle("display","none");$("login_wrapper").setStyle("display","block");$("reset_wrapper").setStyle("display","none");if(alreadyOpen){mySlide.toggle()}else{mySlide.slideIn().chain(function(){$("login_u").focus()})}})});el=$("forget_password");if($chk(el)){el.addEvent("click",function(ev){ev.stop();$("signup_wrapper").setStyle("display","none");$("login_wrapper").setStyle("display","none");$("reset_wrapper").setStyle("display","block");mySlide.slideIn()})}el=$("cancel_reset");if($chk(el)){el.addEvent("click",function(ev){ev.stop();$("signup_wrapper").setStyle("display","none");$("login_wrapper").setStyle("display","block");$("reset_wrapper").setStyle("display","none");mySlide.slideIn()})}$$(".handle").each(function(el){el.addEvent("click",function(e){e.stop();mySlide.slideOut()})});$$(".signup_field").each(function(el){el.addEvent("blur",function(ea){ea.stop();var fieldName=el.get("name");var value=el.get("value");$("signup_error_"+fieldName).set("text","");var myJSONRemote=new Request.JSON({url:"/member/check",onComplete:function(j,v){var elmnt=$("signup_error_"+j.field);if(!(j.okay)){elmnt.set("html","&larr; "+j.message)}else{}}}).get({field:fieldName,value:value})})});var submitSignup=function(evt){evt.stop();setAjax("signup_error");var frm=$("form_signup");frm.set("send",{onSuccess:function(reqText){var data=eval("("+reqText+")");if(data.okay){$("signup_wrapper").set("html",'<form id="form_signup">'+data.message+"<br /><br /></form>");mySlide.show()}else{$("signup_error_"+data.field).set("text",data.message);$("signup_error").set("text","")}}});frm.send()};el=$("signup_submit");if($chk(el)){el.addEvent("click",submitSignup)}el=$("signup_e");if($chk(el)){el.addEvent("keypress",function(e){if($chk(e.key)&&e.key=="enter"){submitSignup(e)}})}var submitLogin=function(ev){ev.stop();setAjax("login_load");var frm=$("form_login");frm.set("send",{onSuccess:function(reqText){var data=eval("("+reqText+")");if(!data.okay){$("login_load").set("text","");var box=$("login_error");box.set("text",data.message)}else{window.location=data.next}}});frm.send()};el=$("login_submit");if($chk(el)){el.addEvent("click",submitLogin)}el=$("login_p");if($chk(el)){el.addEvent("keypress",function(e){if($chk(e.key)&&e.key=="enter"){submitLogin(e)}})}var elm=$("add_content");if($chk(elm)){elm.addEvent("click",function(ea){ea.stop();var addContent=$("add_content");if(!$chk(addContent.retrieve("firstTimeDropDown"))){$("text_wrapper").addClass("hide");$("photo_wrapper").removeClass("hide");addContent.store("firstTimeDropDown",true)}mySlide.toggle()})}$$(".switch_text").each(function(el){el.addEvent("click",function(e){e.stop();$("text_wrapper").removeClass("hide");if(!$chk($("text_wrapper").retrieve("tinyMCE_init"))){tinyMCE.init({mode:"none",plugins:"paste",theme:"simple",paste_auto_cleanup_on_paste:true,paste_remove_styles:false,editor_deselector:"plainText"});tinyMCE.execCommand("mceAddControl",false,"postEdit_editor");tinyMCE.execCommand("mceAddControl",false,"editor");$("text_wrapper").store("tinyMCE_init",true)}$("photo_wrapper").addClass("hide");$("video_wrapper").addClass("hide");mySlide.slideIn()})});$$(".switch_photo").each(function(el){el.addEvent("click",function(e){e.stop();$("text_wrapper").addClass("hide");$("photo_wrapper").removeClass("hide");$("video_wrapper").addClass("hide");mySlide.slideIn()})});$$(".switch_video").each(function(el){el.addEvent("click",function(e){e.stop();$("text_wrapper").addClass("hide");$("photo_wrapper").addClass("hide");$("video_wrapper").removeClass("hide");mySlide.slideIn()})});$$(".photo_input_switch").each(function(el){el.addEvent("click",function(ev){ev.stop();var div=el.getParent().getParent();div.getElements("div").each(function(dv){dv.toggleClass("hide")})})});$$(".tag_input").each(function(el){new Autocompleter.Ajax.Json(el.get("id"),"/tags/suggest",{postVar:"tag",delay:10,multiple:true,separatorSplit:"/[,;]+/"})});function validateForm(formElmt){var isValid=true;try{eval("tinyMCE.triggerSave();")}catch(err){}$$(".error").each(function(errElmt){errElmt.set("html","")});formElmt.getElements(".field").each(function(fld){if(fld.hasClass("required")){var val="";var field=fld.getFirst("div");if($chk(field)){field.getElements("div").each(function(div){inp=div.getElement("input").get("value");val=inp.replace(/\s*/,"")})}else{field=fld.getFirst("input");if(!$chk(field)){field=fld.getFirst("textarea")}val=field.get("value").replace(/\s*/,"")}if(val==""){var errElmt=fld.getElement("label").getElement(".error");errElmt.set("html","&larr;This field is required.");isValid=false;return }}});if(!isValid){return false}if(formElmt.get("id")=="form_text"){}else{if(formElmt.get("id")=="form_video"){}else{if(formElmt.get("id")=="form_photo"){var showError=function(msg){$("post_photo_field").getElements("div").each(function(divElmt){divElmt.getElement(".error").set("html","&larr;"+msg)})};var uploadBox=$("post_photo_upload");if(uploadBox.hasClass("hide")){var url=$("photo_url_post").value;if(!$chk(url)||url==""){showError("this field is required.");return false}var urlValid=$("photo_url_post").retrieve("validated");if(!$chk(urlValid)){showError("this field is required.");return false}if(urlValid!="okay"){showError(urlValid);return false}}else{var filename=($("photo_file_upload").get("value"));filename=filename.toLowerCase();if(!$chk(filename)||filename==""){showError("please upload a file.");return false}else{if(!filename.match(".(jpg|png|gif|jpeg)$")){showError("only jpg, gif, png are supported.");return false}}}}}}return true}el=$("photo_url_post");if($chk(el)){el.addEvent("blur",function(evt){var urlBox=$("photo_url_post");var url=urlBox.get("value");urlBox.store("validated","okay");if((url.indexOf(".")==-1)||(url.indexOf("/")==-1)){urlBox.store("validated","this link is invalid")}})}$$(".postSubmit").each(function(submitBtn){submitBtn.addEvent("click",function(ev){var formElmt=submitBtn.getParent().getParent();if(validateForm(formElmt)){var ajaxElem=submitBtn.getParent().getLast("span");setAjax(ajaxElem);formElmt.submit()}})});$$(".delete_link").each(function(el){el.addEvent("click",function(ev){if(!confirm(confirm_delete_message)){ev.stop()}})});var initBlocks=function(){$$(".promotion_block, .archive_block").each(function(el){el.addEvent("mouseenter",function(ev){el.getLast("div").removeClass("hide")});el.addEvent("mouseleave",function(ev){el.getLast("div").addClass("hide")})})};initBlocks();var langPicker=$("language_picker");var langExpanded=false;langPicker.set("tween",{duration:"short"});langPicker.setStyles({display:"block",opacity:0});el=$("footer_language_link");el.addEvent("click",function(ev){if(langExpanded){langPicker.tween("opacity",0)}else{langPicker.tween("opacity",1)}langExpanded=!langExpanded});langPicker.addEvent("blur",function(ev){langPicker.tween("opacity",0)});var elem=$("country_selector");if($chk(elem)){elem.addEvent("change",function(ev){$("country_selector_flag").set("src","/img/flag/"+elem.value+".gif")})}if($chk($("blog_title"))){new EditInPlace($$(".blog_title"),{tip:false,url:"/profile/ajaxupdate",data:"name=blogTitle",maxlength:50})}var voteFunction=function(el){var voteUrl="/vote/up/id/";var voteUp=true;if(el.hasClass("loved")){voteUrl="/vote/down/id/";voteUp=false;el.removeClass("loved")}el.addClass("loving");var myJSONRemote=new Request.JSON({url:voteUrl+el.get("rel"),onComplete:function(j,v){el.set("text",j.msg);el.removeClass("loving");if(voteUp){el.addClass("loved")}if($chk($("loved_list"))){$("loved_list").set("html",j.savers)}var id="loved_count_"+j.id;if($chk($(id))){$(id).set("text",j.score)}}}).get()};$$(".love_button").each(function(el){if(el.hasClass("toggle_signup")){return }if(el.hasClass("signup_attract")){}el.removeEvents("click");el.addEvent("click",function(ea){if($chk(ea)){ea.stop()}voteFunction(el)})});$$(".share_entry").each(function(el){el.addEvent("click",function(ev){ev.stop();var link=el.get("href");$("overbox_link").set("value","http://9gag.com"+link);$("overbox").removeClass("hide");$("overlay").removeClass("hide")})});$$(".overbox_close").each(function(el){el.addEvent("click",function(ev){ev.stop();$("overlay").addClass("hide");$("overbox").addClass("hide")})});function sendtoclipboard(s,el){if(window.clipboardData&&clipboardData.setData){clipboardData.setData("text",s)}else{ffcopy(el)}}function ffcopy(inElement){if(inElement.createTextRange){var range=inElement.createTextRange();if(range&&BodyLoaded==1){range.execCommand("Copy")}}else{var flashcopier="flashcopier";if(!document.getElementById(flashcopier)){var divholder=document.createElement("div");divholder.id=flashcopier;document.body.appendChild(divholder)}document.getElementById(flashcopier).innerHTML="";var divinfo='<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';document.getElementById(flashcopier).innerHTML=divinfo}}el=$("overbox_copy");if($chk(el)){el.addEvent("click",function(){var txt=$("overbox_link");txt.select();var t=txt.get("value");sendtoclipboard(t,txt)})}$$(".photo_big").each(function(el){el.addEvent("click",function(ev){ev.stop();el.setStyle("display","none");var id=el.get("id");$(id.replace("_big","_small")).setStyle("display","block")})});$$(".photo_small").each(function(el){el.addEvent("click",function(ev){ev.stop();el.setStyle("display","none");var id=el.get("id");$(id.replace("_small","_big")).setStyle("display","block")})});$$(".youtube_preview_pic").each(function(el){el.addEvent("mouseover",function(ev){var intv=setInterval(function(){var src=el.get("src");if(src.match("1.jpg")){src=src.replace("1.jpg","2.jpg")}else{if(src.match("2.jpg")){src=src.replace("2.jpg","3.jpg")}else{if(src.match("3.jpg")){src=src.replace("3.jpg","1.jpg")}}}el.set("src",src)},500);el.store("interval",intv)});el.addEvent("mouseout",function(ev){var intv=el.retrieve("interval");clearInterval(intv)})});var checkCommentLength=function(ev){var commentBox=$("commentTextBox");var len=commentBox.value.length;var str=len+"/3000 char";$("commentTextBoxRemain").set("text",str);if(len>3000){$("commentTextBoxRemain").setStyle("color","#FF0000")}else{$("commentTextBoxRemain").setStyle("color","#AAAAAA")}};var initCommentBox=function(){var commentBox=$("commentTextBox");var captcha=$("capcha_box");if($chk(commentBox)){commentBox.addEvent("change",checkCommentLength);commentBox.addEvent("keyup",checkCommentLength);if($chk(captcha)){var img=captcha.getFirst("img");commentBox.addEvent("focus",function(ev){captcha.removeClass("hide");img.set("src",img.get("_src"));img.set("_src","");commentBox.removeEvents("focus")});img.set("_src",img.get("src"));img.set("src","")}}elm=$("ajax_comment_submit");if($chk(elm)){elm.set("href","javascript:");elm.addEvent("click",function(ea){ea.stop();var req=new Request.HTML({url:"/read/ajaxcomment",method:"post",onSuccess:function(tree,elem,html,js){$("entry_comments").set("html",html);initCommentBox()},onFailure:function(req){alert("Comment could not be posted. Please try again.")}});req.post($("form_post_comment"));setAjax("comment_submit_loadicon")})}};initCommentBox();var initSearchBox=function(){var searchBox=$("search_textbox");if($chk(searchBox)){if(!searchBox.hasClass("focus")){var origText=searchBox.value;searchBox.addEvent("click",function(ea){if(searchBox.value==origText){searchBox.value=""}else{this.select()}searchBox.addClass("focus")});searchBox.addEvent("blur",function(ea){if(searchBox.value==""){searchBox.value=origText;searchBox.removeClass("focus")}})}}};initSearchBox();el=$("notification_close");if($chk(el)){el.addEvent("click",function(evt){$("notification_box").addClass("hide")})}el=$("notification_content");if($chk(el)){el.addEvent("click",function(evt){window.scroll(0,0);$("notification_box").addClass("hide")})}$$(".signup_attract").each(function(elmt){elmt.addEvent("click",function(evt){var notifBox=$("notification_box");if($chk(notifBox)){notifBox.setStyle("opacity",0);notifBox.removeClass("hide");notifBox.set("tween",{duration:500});notifBox.fade("in");setTimeout(function(){notifBox.fade("out")},6000)}})});function jump_next(direction){var prev;var curr;var next;var found=false;var sTop=(Browser.Engine.trident)?document.documentElement.scrollTop:window.pageYOffset;$$(".jump_stop").each(function(elmt){if(!found){prev=curr;curr=elmt}else{if(!$chk(next)){next=elmt;return }else{return }}if(curr.getPosition().y>=sTop+topMargin){found=true}});if(direction=="L"){scroll(0,curr.getPosition().y-topMargin);var voteLink=curr.getFirst("a");voteLink.fireEvent("click")}else{if(direction<0){if($chk(prev)){scroll(0,prev.getPosition().y-topMargin);prev.focus();var linkToFocus=prev.getElement(".jump_focus");if($chk(linkToFocus)){linkToFocus.focus()}}else{if($chk($("jump_prev"))){Cookie.write("jumpGAG","goLast",cookieOption);window.location=$("jump_prev").get("href")}else{scroll(0,0)}}}else{var bottomY=(Browser.Engine.trident)?(document.documentElement.scrollHeight-document.documentElement.clientHeight):(document.documentElement.scrollHeight-window.innerHeight);if(sTop==bottomY&&$chk($("jump_next"))){window.location=$("jump_next").get("href");Cookie.write("jumpGAG","goFirst",cookieOption)}else{if($chk(next)){var jumpElem;if(curr.getPosition().y==(sTop+topMargin)){jumpElem=next}else{jumpElem=curr}scroll(0,jumpElem.getPosition().y-topMargin);jumpElem.focus();var linkToFocus=jumpElem.getElement(".jump_focus");if($chk(linkToFocus)){linkToFocus.focus()}}else{if($chk($("jump_next"))){Cookie.write("jumpGAG","goFirst",cookieOption);window.location=$("jump_next").get("href")}}}}}}function flipThis(){var iframe=$("hideFrame");if($chk(iframe)){var src=iframe.get("src");if(!$chk(src)||src==""){iframe.set("src","http://www.rollinegg.com")}$("container").toggleClass("hide");$("hideFrame").toggleClass("hide")}}var bodyElm=document.getElementsByTagName("body")[0];if($(bodyElm).hasClass("key_nav")){var shortCutOptions={type:"keypress",propagate:false,target:document,disable_in_input:true};shortcut.add("j",function(){jump_next(1)},shortCutOptions);shortcut.add("k",function(){jump_next(-1)},shortCutOptions);shortCutOptions.keycode=null;if($chk($("post_nav_love"))){shortcut.add("l",function(){jump_next("L")},shortCutOptions)}}if($chk($("post_nav_next"))){$("post_nav_next").addEvent("click",function(ev){jump_next(1)});$("post_nav_prev").addEvent("click",function(ev){jump_next(-1)})}if($chk($("post_nav_love"))){$("post_nav_love").addEvent("click",function(ev){jump_next("L")})}$$(".has_drop_down").each(function(elmt){var ulElmt=elmt.getParent().getFirst("ul");if($chk(ulElmt)){ulElmt.setStyle("opacity",0);ulElmt.removeClass("hide");ulElmt.set("tween",{duration:200})}elmt.addEvent("mouseover",function(evt){$$(".has_drop_down").each(function(toClose){var ulEmt=toClose.getParent().getFirst("ul");if($chk(ulEmt)){ulEmt.tween("opacity",0)}});if($chk(ulElmt)){var opened=(ulElmt.getStyle("opacity")>0);if(!opened){ulElmt.tween("opacity",1)}}});if($chk(ulElmt)){elmt.addEvent("blur",function(evt){ulElmt.tween("opacity",0)})}});$$(".range").each(function(ulElm){ulElm.addEvent("mouseleave",function(evt){ulElm.tween("opacity",0)})});if(Browser.Engine.trident){$$(".link_child").each(function(tdElmt){var href=tdElmt.getFirst("a").get("href");tdElmt.addEvent("click",function(evt){window.location=href})})}});function initJumpGAG(C){var A=Cookie.read("jumpGAG");if($chk(A)){var E=$$(".jump_stop");var D=0;if($chk(E)&&E.length>0){if(C&&A=="goLast"){var B=E.getLast();D=B.getPosition().y-topMargin;scroll(0,D)}else{if(false&&A=="goFirst"){var B=E[0];D=B.getPosition().y-topMargin;scroll(0,D)}}}}}window.addEvent("load",function(){initJumpGAG(true);Cookie.dispose("jumpGAG",cookieOption)});
