var idthis={apikey:"8d3b165318535b969b1282bde9aaf179",username:"anonymous",wins:0,category:0,data:null,setup:function(){$.ajaxSetup({cache:false});shortcut.add("N",idthis.loadRandom);shortcut.add("I",idthis.toggleIdentify);shortcut.add("F",idthis.flagPhoto);if(!idthis.initial_loadunid){$("#photo").addClass("photoborder");idthis.photos.load("/api/get_unidentified/",{category:idthis.category});idthis.initial_loadunid=true}},loadRandom:function(){$("#photodescwrap").css("opacity",0.7);$("#newanswerborder").css("opacity",0.7);var callvars={apikey:idthis.apikey};if(idthis.data&&idthis.data.index!=undefined){callvars.index=idthis.data.index}if(!idthis.showid){callvars.category=idthis.category;$.getJSON("/api/get_random/",callvars,idthis.receiveRandomData)}else{if(idthis.data.extra_info){$("#photodescwrap").slideDown("fast")}idthis.showid=null}},receiveRandomData:function(data){if(data.id){idthis.data=data;$("#photo").html('<a href="javascript:void(0)" onClick="idthis.loadRandom()"><img src="http://static.idthis.org/'+data.path+'" onLoad="idthis.showRandom()"></a>')}else{if(idthis.category){$("#photo").html("no unidentified photos in this category.")}else{$("#photo").html("Photo not found")}}},showRandom:function(){idthis.showAnswers();if(idthis.data.extra_info){$("#photodesc").html(idthis.data.extra_info);$("#photodescwrap").slideDown("fast")}else{$("#photodescwrap").slideUp("fast")}var link="http://idthis.org/id/"+idthis.data.id;$("#photolink").html('Link to this photo <a href="'+link+'">'+link+"</a>")},toggleIdentify:function(){if($("#newanswer").is(":hidden")){$("#newanswer").fadeIn("normal");$("#newanswerborder").fadeIn("normal");$("#newanswerinput").focus()}else{$("#newanswer").fadeOut("normal");$("#newanswerborder").fadeOut("normal");$("#newanswerinput").val("")}},closeDesc:function(){$("#photodescwrap").slideUp("fast")},upVote:function(id){if($("#downarrow"+id).css("opacity")==1){var score=parseInt($("#score"+id).attr("score"))+1;var scoremsg=(score==1)?"1 vote":score+" votes";$("#score"+id).html(scoremsg);$("#downarrow"+id).fadeTo("normal",0.3);$("#uparrow"+id).fadeTo("normal",1);idthis.submitVote(id,1)}},downVote:function(id){if($("#uparrow"+id).css("opacity")==1){var score=parseInt($("#score"+id).attr("score"))-1;var scoremsg=(score==1)?"1 vote":score+" votes";$("#score"+id).html(scoremsg);$("#uparrow"+id).fadeTo("normal",0.3);$("#downarrow"+id).fadeTo("normal",1);idthis.submitVote(id,0)}},submitVote:function(id,upvote){$.getJSON("/api/send_vote/",{apikey:idthis.apikey,id:id,upvote:upvote})},flagPhoto:function(){if(confirm("Please only flag inappropriate photos.  Click okay ONLY if this photo contains obscene or offensive content.")){$.getJSON("/api/flag/",{apikey:idthis.apikey,photo:idthis.data.id});idthis.loadRandom()}},cleanAnswer:function(txt){txt=txt.replace(/<[^>]*?>/g,"",txt);txt=txt.replace(/((https?:\/\/)[A-Za-z0-9.-]+\.[A-Za-z]+[^ \n!,)]*)/g,'<a href="$1" target="_blank">$1</a>',txt);txt=txt.replace(/\n/g,"<br>");return txt},submitAnswer:function(){if(idthis.data.answers.length>=15){return alert("There are too many answers for this photo.  Please vote for an answer below.")}var txt=$("#newanswerinput").val();if(txt&&txt!=""){idthis.toggleIdentify();$.getJSON("/api/send_answer/",{apikey:idthis.apikey,id:idthis.data.id,answer:txt},function(data){if(data&&data.error){alert(data.error)}else{if(data){idthis.data.answers.unshift({upvote:1,downvote:0,id:"new"+Math.floor(Math.random()*10000000),user:{username:idthis.username,wins:idthis.wins},answer:idthis.cleanAnswer(txt),winner:0});idthis.showAnswers()}}})}else{alert("Please enter an answer.")}},setCategorySelected:function(){if(idthis.category){$("#cat"+idthis.category).addClass("categoryselect");$("#cat"+idthis.category).removeClass("category");$("#cat0").removeClass("categoryselect");$("#cat0").addClass("category")}else{$("#cat0").removeClass("category");$("#cat0").addClass("categoryselect")}},userDetails:function(user){window.location="/user/"+user.username+"/"},prettyDate:function(d){var months=new Array(13);months[0]="Jan.";months[1]="Feb.";months[2]="Mar.";months[3]="Apr.";months[4]="May";months[5]="June";months[6]="July";months[7]="Aug.";months[8]="Sept.";months[9]="Oct.";months[10]="Nov.";months[11]="Dec.";var monthnumber=d.getMonth();var monthname=months[monthnumber];var monthday=d.getDate();var year=d.getYear();if(year<2000){year=year+1900}var dateString=monthname+" "+monthday+", "+year;var hour=d.getHours();var minute=d.getMinutes();var second=d.getSeconds();var ap="a.m.";if(hour>11){ap="p.m."}if(hour>12){hour=hour-12}if(hour==0){hour=12}if(minute<10){minute="0"+minute}var timeString=hour+":"+minute+" "+ap;return dateString+", "+timeString},showAnswers:function(){$("#curanswers").empty();if(idthis.data.answers.length){$("#answers").show();$.each(idthis.data.answers,function(){var score=this["upvote"]-this["downvote"];var scoremsg=(score==1)?"1 vote":score+" votes";var id="answer"+this["id"];var aid=this["id"];var user=this["user"];var ans=$("#answer").clone();ans.attr("id",id);ans.appendTo($("#curanswers"));$("#"+id+" .answerscore").html(scoremsg);$("#"+id+" .answerscore").attr("id","score"+aid);$("#"+id+" .answerscore").attr("score",score);var tsdate=idthis.prettyDate(new Date(this["timestamp"]));var datestr='<font color="#aaaaaa">posted at '+tsdate+" by</font> ";if(user.username=="anonymous"){$("#"+id+" .answeruser").html(datestr+this["user"]["username"]);$("#"+id+" .answeruser").css("color","#494949")}else{$("#"+id+" .answeruser").html(datestr+this["user"]["username"]+" ("+this["user"]["wins"]+")");$("#"+id+" .answeruser").css("cursor","pointer");$("#"+id+" .answeruser").bind("click",function(){idthis.userDetails(user)})}$("#"+id+" .answerbody").html(this["answer"]);if(idthis.data.votesmet){$("#identifybtn").fadeOut();if(this["winner"]){var uparr=$("#"+id+" .uparrow");uparr.removeClass("uparrow");uparr.addClass("winner")}else{$("#"+id+" .uparrow").hide()}$("#"+id+" .downarrow").hide()}else{$("#identifybtn").show();$("#"+id+" .uparrow").bind("click",function(){idthis.upVote(aid)});$("#"+id+" .uparrow").attr("id","uparrow"+aid);$("#"+id+" .downarrow").bind("click",function(){idthis.downVote(aid)});$("#"+id+" .downarrow").attr("id","downarrow"+aid)}$("#"+id+" .answeraddcomment").attr("id","answeraddcomment"+aid);var commenttxt="Comment on answer";if(this["comments"]){commenttxt="View comments ("+this["comments"]+")"}$("#"+id+" .answeraddcomment").html(commenttxt);$("#"+id+" .answeraddcomment").bind("click",function(){idthis.loadComments(aid)});$("#"+id+" .commentstable").attr("id","commentstable"+aid);$("#"+id+" .addcomment").attr("id","addcomment"+aid);$("#"+id+" .button").bind("click",function(){idthis.addComment(aid)});ans.show()})}else{$("#answers").hide()}},loadComments:function(aid){if(idthis.loading_aid){return false}idthis.loading_aid=aid;$.getJSON("/api/get_comments/",{apikey:idthis.apikey,id:aid},idthis.receiveComments)},receiveComments:function(data){var aid=idthis.loading_aid;idthis.data["comments"+aid]=data;idthis.showComments()},showComments:function(){var aid=idthis.loading_aid;idthis.loading_aid=false;var comments=$("#commentstable"+aid+" .comments");comments.empty();$.each(idthis.data["comments"+aid],function(){var id="comment"+this["id"];var user=this["user"];var comment=$("#comment").clone();comment.attr("id",id);comment.appendTo(comments);if(user.username=="anonymous"){$("#"+id+" .answeruser").html(this["user"]["username"]);$("#"+id+" .answeruser").css("color","#494949")}else{$("#"+id+" .answeruser").html(this["user"]["username"]+" ("+this["user"]["wins"]+")");$("#"+id+" .answeruser").css("cursor","pointer");$("#"+id+" .answeruser").bind("click",function(){idthis.userDetails(user)})}$("#"+id+" .commentbody").html(this["comment"]);comment.show()});idthis.showCommentsLink(aid)},addComment:function(aid){var txt=$("#addcomment"+aid).val();if(txt==""||txt==undefined){return alert("Please enter a comment.")}$.getJSON("/api/add_comment/",{apikey:idthis.apikey,id:aid,comment:txt},function(data){if(data&&data.error){alert(data.error)}else{if(data){idthis.data["comments"+aid].push({id:"new"+Math.floor(Math.random()*10000000),user:{username:idthis.username,wins:idthis.wins},comment:idthis.cleanAnswer(txt)});idthis.loading_aid=aid;idthis.showComments()}}});$("#addcomment"+aid).val("")},closeCommentsLink:function(aid){$("#commentstable"+aid).fadeOut();$("#answeraddcomment"+aid).html("View comments");$("#answeraddcomment"+aid).unbind("click");$("#answeraddcomment"+aid).bind("click",function(){idthis.showCommentsLink(aid)})},showCommentsLink:function(aid){$("#commentstable"+aid).fadeIn();$("#answeraddcomment"+aid).html("Close comments");$("#answeraddcomment"+aid).unbind("click");$("#answeraddcomment"+aid).bind("click",function(){idthis.closeCommentsLink(aid)})}};