var IMAGES_PER_ROW=3,THUMB_LAYOUT_HEIGHT=80,GALLERY_TABS_HEIGHT=27,imgFeed="flickr",lastImgFeed="flickr",defs,imgs,loadedWords=[],wordNo=-1,winHeight=0,winWidth=0,winScrollTop=0,frozenGallery="",focusedStack=[],curFocusImgID=-1,hoverImg,focusImg,fIStyle,focusCap,bgOverlay,focusImgOn=false,fIInterval,curFrame;var Shahi={doWord:function(b){if(b.replace(/\s+/,"")==""){return false}loadedWords[++wordNo]=b;var a=wordNo;defs.prepend('<div id="i'+a+'" class="item"><span class="word">loading...</span><br /><div>Fetching definition for "'+b.replace(/[\>\<]+/,"")+'"</div></div>');$('<div id="g'+a+'" class="gallery"><div class="flickr board"></div><div class="google board"></div><div class="yahoo board"></div></div>').prepend($(".gal-tabs:first").clone(true)).prependTo(imgs).hover(galleriaOn,galleriaOff);$.getJSON("/shahi/define.php?words="+encodeURIComponent(b),{},function(d,e){var c="";switch(d.stat){case"ok":c='<span class="word"><a target="_blank" href="http://en.wiktionary.org/wiki/'+d.word+'">'+d.word+"</a></span>"+(d.see!=""?'<div class="see"><strong>See also</strong> '+d.see+"</div>":"")+'<div class="definition">'+d.def+"</div>";break;case"sp":c='<span class="word sp">typo</span><br /><div class="definition"><ul><li><strong>Did you mean: </strong> ';$.each(d.def.split(","),function(f,g){c+=(f!=0?" or ":"")+'<a href="'+g+'" rel="'+g+'">'+g+"</a>"});c+="?</li></ul></div>";break;case"nf":c='<span class="word nf"><a href="404" rel="404">404</a></span><br /><div class="definition"><ul><li>Sorry, couldn\'t find \''+d.word.replace(/[\>\<]+/,"")+"' in the dictionary.</li></ul></div>";break}$("#g"+a).height($("#i"+a).html(c).height()-5)});Shahi.prepareGallery(a)},prepareGallery:function(g,e){var a=$("#g"+g),b=a.find(".gal-tabs li"),d=a.find(".board"),c;if(imgFeed=="none"){a.find(".board:first").show().html('<p><span class="load-imgs">Load images</span></p>');return}function f(m){e=m;c=a.find("."+e);d.hide();b.removeClass("on");b.filter("[title="+e+"]").addClass("on");c.show();if(c.hasClass("loaded")){return}c.html("<p>loading images from "+e+"...</p>");c.addClass("loaded");switch(e){case"google":var l=0,k=0,h=0;while(l<16){$.getJSON("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&key=ABQIAAAAmp3mAQ6HrFjb1y5gqPStpRSMrVI_abpm6s5uqCVg88xHjU_uRhQ8UKTNYj1XnXCh1-2E1IPa7L6HWw&rsz=large&start="+l+"&q="+encodeURIComponent('"'+loadedWords[g]+'"')+"&callback=?",{},function(i){if(i.responseStatus!=200||(!i.responseData.results.length&&k==0)){c.html((!i.responseData.results.length&&k==0?"<p class='img-error'>Couldn't find images using Google Image Search</p>":""));f("yahoo");return}h++;if(k==0){c.html("")}for(k=0;k<i.responseData.results.length;k++){$('<a rel="noblow" target="_blank" href="'+i.responseData.results[k]["originalContextUrl"]+'" title="'+i.responseData.results[k]["contentNoFormatting"]+'"><img class="thumb" title="'+i.responseData.results[k]["contentNoFormatting"]+'" src="'+i.responseData.results[k]["tbUrl"]+'" /></a>').appendTo(c)}if(h==2){c.find(".thumb").bind("mousemove",showHoverImg).bind("mouseleave",hideHoverImg)}});l=l+8}return;case"yahoo":$.getJSON("http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=cYudx1zV34Fy7FbRdnPSToBWCtbq2qAaJX2Bvj6GJs3cVqVAl6qxS6UKhpa741s6GcUs&query="+encodeURIComponent('"'+loadedWords[g]+'"')+"&results=15&output=json&callback=?",{},function(n){if(!n.ResultSet.Result.length){c.html("<p class='img-error'>Couldn't find images using Yahoo! Image Search</p>");return}c.html("");for(var i=0;i<n.ResultSet.Result.length;i++){$('<a rel="noblow" target="_blank" href="'+n.ResultSet.Result[i]["RefererUrl"]+'" title="'+n.ResultSet.Result[i]["Summary"]+'"><img class="thumb" title="'+n.ResultSet.Result[i]["Summary"]+'" src="'+n.ResultSet.Result[i]["Thumbnail"]["Url"]+'" /></a>').appendTo(c)}c.find(".thumb").bind("mousemove",showHoverImg).bind("mouseleave",hideHoverImg)});return;default:$.getJSON("http://www.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=ca732dc3191c479e7c9aa57982f5d857&tags="+encodeURIComponent(loadedWords[g])+"&per_page=15&sort=relevance&jsoncallback=?",{},function(n){if(n.stat!="ok"||!n.photos.photo.length){c.html((!n.photos.photo.length?"<p class='img-error'>Couldn't find images using Flickr</p>":""));f("google");return}c.html("");var j=[];for(var i in n.photos.photo){j[j.length]=new Image();j[j.length-1].onload=function(){$('<a rel="'+this.alt+'" href="'+this.src.replace(/\_s\.jpg$/,".jpg")+'" title="'+this.title+'"><img alt="flickr" class="thumb" title="'+this.title+'" src="'+this.src+'" /></a>').appendTo(c)};j[j.length-1].src="http://farm"+n.photos.photo[i]["farm"]+".static.flickr.com/"+n.photos.photo[i]["server"]+"/"+n.photos.photo[i]["id"]+"_"+n.photos.photo[i]["secret"]+"_s.jpg";j[j.length-1].title=n.photos.photo[i]["title"];j[j.length-1].alt=n.photos.photo[i]["owner"]+"/"+n.photos.photo[i]["id"]}});return}}if(typeof e!="string"){e=imgFeed}f(e)}};function clearImgFocus(){focusedStack[curFocusImgID].onload=function(){};clearInterval(fIInterval);bgOverlay.hide();$(focusImg).hide();focusImg.src="/images/loader.gif";focusCap.hide();frozenGallery="";focusImgOn=false}function blowUpImg(i,c,b,h,a,f,e){if(curFrame!==e+1){var d=curFrame,j=0;if((d/=e/2)>=1){j=2,d-=2}var g=d*d*d+j;fIStyle.opacity=(1/e)*curFrame;fIStyle.left=h/2*g+b+"px";fIStyle.top=f/2*g+a+"px";fIStyle.width=i/2*g+75+"px";fIStyle.height=c/2*g+75+"px";curFrame++}else{clearInterval(fIInterval);$("#focus-cap-content").text(focusImg.title);$("#focus-cap-link").attr("href","http://www.flickr.com/photos/"+focusImg.alt);focusCap.css({top:$(focusImg).offset().top+$(focusImg).height()+2,left:$(focusImg).offset().left,width:$(focusImg).width()});focusCap.fadeIn()}}function showHoverImg(a){hoverImg.src=a.target.src;hoverImg.style.left=a.pageX+8+"px";if(winHeight/(a.pageY-winScrollTop)<2){hoverImg.style.bottom=winHeight-a.pageY+8+"px";hoverImg.style.top=""}else{hoverImg.style.top=a.pageY+8+"px";hoverImg.style.bottom=""}hoverImg.style.display="inline"}function hideHoverImg(a){hoverImg.style.display="none";hoverImg.src="/images/loader.gif"}function galleriaOn(c){var a=c.target.className!="gallery"?$(c.target).parents(".gallery"):$(c.target),b=Math.ceil(a.find(".board:visible").children("a").size()/IMAGES_PER_ROW)*THUMB_LAYOUT_HEIGHT+GALLERY_TABS_HEIGHT;a.queue("fx",[]);if(b>a.height()){a.stop().queue(function(){$(this).animate({height:b+"px"})})}$("#i"+a.attr("id").match(/([0-9]+)$/)[1]).find(".word").css("background-color","#EEFD86");setTimeout(function(){a.dequeue()},400)}function galleriaOff(b){var a=b.target.className!="gallery"?$(b.target).parents(".gallery"):$(b.target);if(frozenGallery==a.attr("id")){return}a.queue("fx",[]);a.stop().queue(function(){$(this).animate({height:($("#i"+this.id.replace(/g/,"")).height()-5)+"px"})});$("#i"+a.attr("id").match(/([0-9]+)$/)[1]).find(".word").css("background-color","#F8FECD");setTimeout(function(){a.dequeue()},200)}function setImgFeed(){if(arguments.length){lastImgFeed=imgFeed;imgFeed=arguments[0]}document.cookie="imgFeed="+imgFeed+"; expires="+(new Date("December 7, 2015 12:12:12")).toGMTString()+"; path=/shahi";$("#img-feed-selector img[alt="+imgFeed+"]").siblings().hide().end().show();$("#img-feeds").hide();$("#word-form").css("margin-left",imgFeed=="none"?"0":"100px");defs.animate({left:(imgFeed=="none"?"-268px":"-15px")})}$(document).ready(function(){var a=$("#word-form"),c=$("#word");imgs=$("#imgs");defs=$("#defs");winHeight=$(window).height();winScrollTop=$(window).scrollTop();bgOverlay=$('<div id="bg-overlay">&nbsp</div>').prependTo("body").click(clearImgFocus);focusImg=$('<img id="focus-img" src="/images/loader.gif" alt="" title="" style="" />').prependTo("body").click(clearImgFocus).get(0);fIStyle=focusImg.style;focusCap=$('<div id="focus-cap"><strong id="focus-cap-content"></strong> - <a id="focus-cap-link" target="_blank" href="#">photo</a> by <span id="focus-cap-author">Flickr User</span></div>').prependTo("body");hoverImg=$('<img id="hover-img" src="/images/loader.gif" alt="loading..." title="" style="" />').prependTo("body").get(0);if($.browser.msie&&$.browser.version<7){alert("Please upgrade your browser-- it's 2009 already!!! This website does not fully support IE < 7. I suggest you Google for Firefox.")}if(/imgFeed=(.*);?/.test(document.cookie)){lastImgFeed=imgFeed;imgFeed=document.cookie.split("imgFeed=")[1].split(";")[0]}setImgFeed();$(window).resize(function(){winHeight=$(window).height()});$(window).scroll(function(){winScrollTop=$(window).scrollTop()});$("#img-feed-selector").click(function(d){var e=$(this).offset();$("#img-feeds").css({top:e.top+$(this).height()+4,left:e.left}).bind("mouseleave",function(){$(this).hide()}).toggle()});$("#shahi-status-link, #shahi-contact-link").click(function(d){var e=$(this).offset();$("#"+this.id.replace(/-link$/,"")).css({bottom:winHeight-e.top,left:e.left}).slideToggle().bind("mouseleave",function(){$(this).slideUp()})});$("#img-feed-selector img[alt="+imgFeed+"]").show();$("#img-feeds div").click(function(){setImgFeed($(this).find(":first-child").is("img")?$(this).find(":first-child").attr("alt"):"none")});$(".gal-tabs:first li").click(function(d){Shahi.prepareGallery($(this).parents(".gallery").attr("id").match(/([0-9]+)$/)[1],$(this).attr("title"))});imgs.click(function(f){var h=f.target;if(h.tagName=="IMG"){h=f.target.parentNode}if(h.tagName=="A"&&h.rel!="noblow"){var d=$(h).parents(".gallery");frozenGallery=d.attr("id");bgOverlay.height($(document).height()).show();f.preventDefault();var e=$(h),g=e.find("img:first-child").offset();focusedStack[focusedStack.length]=new Image();curFocusImgID=focusedStack.length-1;focusImg.src="/images/loader.gif";fIStyle.filter="alpha(opacity=50)";fIStyle.opacity="0.5";fIStyle.width="48px";fIStyle.height="48px";fIStyle.backgroundColor="#ffffff";fIStyle.left=g.left+13+"px";fIStyle.top=g.top+13+"px";focusImg.title=e.attr("title");focusImg.alt=e.attr("rel");fIStyle.display="inline";$("#focus-cap-author").text("[loading author...]");$.getJSON("http://www.flickr.com/services/rest/?method=flickr.people.getInfo&format=json&api_key=ca732dc3191c479e7c9aa57982f5d857&user_id="+focusImg.alt.split("/")[0]+"&jsoncallback=?",{},function(i){$("#focus-cap-author").html(i.stat!="ok"?"Flickr User":'<a target="_blank" href="'+i.person.photosurl._content+'">'+i.person.username._content+"</a>")});focusedStack[curFocusImgID].onload=function(){winWidth=$(window).width();winHeight=$(window).height();var q=this.width,m=this.height,k=q/m;fIStyle.opacity=0;fIStyle.width="75px";fIStyle.height="75px";fIStyle.backgroundColor="transparent";focusImg.src=this.src;if(q-60>winWidth){q=winWidth-60;m=q/k}if(m+130>winHeight){m=winHeight-130;q=m*k}curFrame=0;fIStyle.filter="alpha(opacity=100)";var p=q-75,l=m-75,j=g.left,o=(winWidth/2)-(q/2)-g.left,i=g.top,n=(winHeight/2)-(m/2)-g.top;fIInterval=setInterval(function(){blowUpImg(p,l,j,o,i,n+winScrollTop,30)},15);focusImgOn=true};focusedStack[curFocusImgID].src=h.href}else{if($(h).is(".load-imgs")){Shahi.prepareGallery($(h).parents(".gallery").attr("id").match(/([0-9]+)$/)[1])}else{if(imgFeed=="none"){setImgFeed(lastImgFeed)}}}});defs.click(function(d){var e=$(d.target);if(e.is("a[rel]")){d.preventDefault();Shahi.doWord(e.attr("rel"))}}).dblclick(function(e){var d=window.getSelection?window.getSelection().toString():(document.selection?document.selection.createRange().text:"");if(d!=""){Shahi.doWord(d)}});c.focus(function(){$(this).select()}).val(getWord);a.submit(function(d){d.preventDefault();$.each(c.val().split(";"),function(e,f){if(e>=10){return false}Shahi.doWord(f)})});var b=defs.find(".item");if(b.size()){wordNo=b.size()-1;$.each(getWord.split(";"),function(d,e){if(d>=10){return false}loadedWords[loadedWords.length]=e});b.each(function(d){$("#g"+d).prepend($(".gal-tabs:first").clone(true)).hover(galleriaOn,galleriaOff).height($("#i"+d).height()-5);Shahi.prepareGallery(d)})}});
