function photo_rotate_throbber(controlElemId,on){if(on){hide($(controlElemId));show($(controlElemId+'_throbber'));}else{hide($(controlElemId+'_throbber'));show($(controlElemId));}}
function photo_rotate_onSaveSuccess(controlElemId,photoId,successCallback,result){if(typeof result.url=='undefined'){photo_rotate_onSaveError();return;}
successCallback(result,photoId);photo_rotate_throbber(controlElemId,false);}
function photo_rotate_onSaveError(controlElemId){alert("Sorry, there was an error rotating the photo. Please try again later.");photo_rotate_throbber(controlElemId,false);}
function photo_set(photoElem,url){photoElem.src=url;}
function photo_rotate(controlElemId,photoId,dir,size,successCallback){photo_rotate_throbber(controlElemId,true);var post={photo_id:photoId,dir:dir,size:size};sendXhrJson(heart.makeUrl("photos","rotate"),post,dojo.partial(photo_rotate_onSaveSuccess,controlElemId,photoId,successCallback),dojo.partial(photo_rotate_onSaveError,controlElemId));}
var threshold=5;function resizeImage(img,w,h,centerOnly){var width=img.width;var height=img.height;if(!width){var ieSucks=new Image();ieSucks.src=img.src;width=ieSucks.width;height=ieSucks.height;}
var ratio=w/h;var x=0;var y=0;if(width/height>=ratio){if(!centerOnly&&(height+threshold>h)){scale=h/height;width=width*scale;height=height*scale;}
y=Math.round((height-h)/-2);x=Math.round((width-w)/-2);}
else{if(!centerOnly&&(width+threshold>w)){scale=w/width;height=height*scale;width=width*scale;}
x=Math.round((width-w)/-2);y=Math.round((height-h)/-2);}
img.width=width;img.height=height;img.style.margin=y+"px 0 0 "+x+"px";}
var imageArr;var total;var curIdx=0;var div_id;var first_id;var left_id;var right_id;var last_id;var caption_id;var preloadArr;var pb_img;var pb_hidden_img;var pb_loading;var pb_div;var comment_id;var tags_id;var curUhid;var hostname;var albumId;var curTagShowing;function keyListener(e){if(!imageArr)return true;if(tagger&&tagger.showing())return true;if(heart.getCurrentLightbox())return true;e=dojo.fixEvent(e);if(e.altKey||e.ctrlKey){return true;}
if(e.keyCode==dojo.keys.LEFT_ARROW||e.keyCode==37){nextslide(-1);return false;}
else if(e.keyCode==dojo.keys.RIGHT_ARROW||e.keyCode==39){nextslide(1);return false;}
else if(e.keyCode==dojo.keys.HOME||e.keyCode==36){nextslide(-1,true);return false;}
else if(e.keyCode==dojo.keys.END||e.keyCode==35){nextslide(1,true);return false;}}
function inRange(val){return(val>=0&&val<imageArr.length);}
function drawImage(){var img=imageArr[curIdx];if(img.type==3||img.type==5){hide($('photo_actions'));pb_div.innerHTML=img.video;pb_hidden_img.src=null;}else{show($('photo_actions'));if(!preloadArr[curIdx].loaded)
pb_div.innerHTML='<div style="height:50px; padding-top:8px;"><span class="ajaxStatus"><img src="/img/throbber.gif" align="absbottom" />&nbsp;Loading...</span></div>';pb_hidden_img.src=imageArr[curIdx].img;}}
var h=512;var w=512;var ratio=w/h;function imageLoaded(){var width=pb_hidden_img.width;var height=pb_hidden_img.height;var scale=null;if(width/height>ratio&&width>w){scale=w/width;width=w;height=height*scale;}
else if(width/height<ratio&&height>h){scale=h/height;height=h;width=width*scale;}
var htmlElems=[];if(imageArr[curIdx].url){htmlElems.push('<a href="'+imageArr[curIdx].url+'" target=_blank>');}
htmlElems.push('<img id="imgCurPhoto" class="photoBrowserImg" src="'+imageArr[curIdx].img+'" height="'+height+'" width="'+width+'"');if(typeof tag_handlePhotoMouseMove=='function')
htmlElems.push(' onmousemove="tag_handlePhotoMouseMove('+imageArr[curIdx].id+', event);"');htmlElems.push('>');if(imageArr[curIdx].url){htmlElems.push("</a>");}
pb_div.innerHTML=htmlElems.join('');}
function setLoaded(){this.loaded=true;}
function preloadOne(idx){if(inRange(idx)){if(!preloadArr[idx]){preloadArr[idx]=new Image();preloadArr[idx].src=imageArr[idx].img;preloadArr[idx].loaded=false;preloadArr[idx].onload=setLoaded;}}}
function preloadSurrounding(idx){preloadOne(idx);preloadOne(idx+1);preloadOne(idx-1);preloadOne(idx+2);preloadOne(idx-2);}
function hideButtons(){var first=document.getElementById(first_id);var left=document.getElementById(left_id);var right=document.getElementById(right_id);var last=document.getElementById(last_id);var length=imageArr.length;if(curIdx===0){left.childNodes[0].style.display='';left.childNodes[1].style.display='none';first.childNodes[0].style.display='';first.childNodes[1].style.display='none';}
else{left.childNodes[1].style.display='';left.childNodes[0].style.display='none';first.childNodes[1].style.display='';first.childNodes[0].style.display='none';}
if(curIdx==length-1){right.childNodes[0].style.display='';right.childNodes[1].style.display='none';last.childNodes[0].style.display='';last.childNodes[1].style.display='none';}
else{right.childNodes[1].style.display='';right.childNodes[0].style.display='none';last.childNodes[1].style.display='';last.childNodes[0].style.display='none';}}
function addlistener(){document.onkeydown=keyListener;}
function makeUrl(host,controller,func,uhid,tag){var tmp=host+"/"+controller+"/"+func+"/"+uhid;if(tag)tmp+="?ps="+tag;return tmp;}
function setupslideshow(images,idx,div,first,left,right,last,cap,title,commentDiv,tagsDiv,cUhid,curAlbumId,host){div_id=div;first_id=first;left_id=left;right_id=right;last_id=last;caption_id=cap;title_id=title;comment_id=commentDiv;tags_id=tagsDiv;curUhid=cUhid;albumId=curAlbumId;hostname=host;imageArr=images;curIdx=idx;preloadArr=[];pb_div=document.getElementById("photo_browser");pb_hidden_img=new Image();pb_hidden_img.onload=imageLoaded;$('pb_cap_total').innerHTML='/'+String(imageArr.length);loadpic();addlistener();}
function updateAlbumInfo(idx){if(viewerAlbums){var albumDiv=$('photo_browser_album');if(!albumDiv)return;var uhid=imageArr[curIdx].photo_owner;var curAlbum=imageArr[curIdx].album_id;var index=uhid+"_"+curAlbum;albumDiv.innerHTML="From \""+viewerAlbums[index].albumName+"\" by "+viewerAlbums[index].albumOwner;}}
function drawLikes(idx){var likesDiv=$('likesContent');var contentArr=[];var likes=imageArr[curIdx].likes;var likesPlural="Likes";if(likes&&likes.length==1)likesPlural="Like";var likedByCurrUser=false;if(likesDiv){if(likes){contentArr.push('<p>');var likesList=[];for(var i=0;i<likes.length;i++){if(likes[i].uhid==curUhid){likedByCurrUser=true;var objectId=likes[i].objectId;var type=likes[i].type;if(1==likes.length){contentArr.push('You like it. (<a href="javascript:;" onclick="removePhotoLike(\''+heart.makeUrl('likes','removePhotoLike')+'\','+objectId+','+type+')">un-like</a>)</p>');likesDiv.innerHTML=contentArr.join(' ');show($('likedText'));hide($('likeLink'));return;}else{likesList.push('You (<a href="javascript:;" onclick="removePhotoLike(\''+heart.makeUrl('likes','removePhotoLike')+'\','+objectId+','+type+')">un-like</a>)');}}else{likesList.push(likes[i].profileLink);}}
var likeString=concatenate_list(likesList,'and');contentArr.push(likeString+' liked this.');contentArr.push('</p>');}
if(!likedByCurrUser){contentArr.unshift('<p><a href="javascript:;" onclick="photoLike(\''+heart.makeUrl('likes','addPhotoLike')+'\')">I like it!</a></p>');}
likesDiv.innerHTML=contentArr.join(' ');}
if(likedByCurrUser){show($('likedText'));hide($('likeLink'));}else{show($('likeLink'));hide($('likedText'));}}
function drawComments(){var commentDiv=$(comment_id);var contentArr=[];var count=0;var comments=imageArr[curIdx].comments;var commentsPlural="Comments";if(commentDiv){if(comments&&comments.length>0){commentDiv.style.display='';contentArr.push('<table cellspacing="0" cellpadding="0" class="detailsComment">');for(var i=0;i<comments.length;i++){contentArr.push('<tr><td width="32" align="center" valign="top" class="icon">');contentArr.push(comments[i].photo);contentArr.push('</td><td class="comment"');if(count+1!=comments.length)contentArr.push('style="border-bottom: 1px solid #CCC"');contentArr.push('><div id="comment_'+count+'" class="innerComment" style="width: 431px;"><div class="commentText">');contentArr.push(comments[i].comment);contentArr.push('</div><div class="commentDetails">');if(comments[i].uhid!=curUhid)
contentArr.push(comments[i].profileLink+", ");contentArr.push(comments[i].timeAgo);contentArr.push('</div></div>');if((imageArr[curIdx].photo_owner==curUhid)||(comments[i].uhid==curUhid)){contentArr.push('</td><td width="7" class="deleteColumn"');if(count+1!=comments.length)contentArr.push('style="border-bottom: 1px solid #CCC"');var deleteLink='><a class="deleteLink" href="javascript:;" onclick="deletePhotoComment(\''+
makeUrl(hostname,"photos","deleteComment",curUhid,"photo")+'\', '+comments[i].commentId+','+imageArr[curIdx].photo_owner+', drawCommentsCallback);">x</a>';contentArr.push(deleteLink);}else{contentArr.push('</td><td width="7" class="deleteColumn"');if(count+1!=comments.length)contentArr.push('style="border-bottom: 1px solid #CCC"');contentArr.push('>&nbsp;');}
contentArr.push('</td></tr>');count++;}
contentArr.push('</table>');commentDiv.innerHTML=contentArr.join(' ');}else{commentDiv.style.display='none';commentDiv.innerHTML='';}}}
function pb_show_nav(){var cap=$('pb_cap_num');var cap_edit=$('pb_cap_num_edit');var text_box=$('pb_counter');text_box.value=cap.innerHTML;hide(cap);show(cap_edit);text_box.focus();text_box.select();}
function pb_update_and_hide_nav(){var cap=$('pb_cap_num');var cap_edit=$('pb_cap_num_edit');var text_box=$('pb_counter');var curPic=parseInt(text_box.value,10)-1;hide(cap_edit);show(cap);gotoSlide(curPic);}
function pb_nav_keydown(e){dojo.fixEvent(e);var key=e.keyCode;if(key==dojo.keys.ENTER){pb_update_and_hide_nav();}
stopPropagation(e);return true;}
function gotoSlide(slideNum){if(!inRange(slideNum))return;var editCapBox=$('editCapBox_1');var editCapStatus=$('capStatus_1');if((editCapBox&&!editCapBox.style.display)||(editCapStatus&&!editCapStatus.style.display)){return;}
if(tagger)tagger.remove();if(profilePhotoChooser)profilePhotoChooser.remove();curIdx=slideNum;loadpic();heart.trackPageview("pulse/profile/photoViewer/?ps=photo");}
function setupDeleteParams(){var formEle=$('deletePhotoForm');if(formEle){var nextPhoto=-1;if(curIdx+1==imageArr.length){if(imageArr.length>1){nextPhoto=imageArr[0].id;}}
else{nextPhoto=imageArr[curIdx+1].id;}
formEle.elements.photo_id.value=imageArr[curIdx].id;formEle.elements.next_photo_id.value=nextPhoto;}}
function loadpic(){var caption=document.getElementById(caption_id);var title=document.getElementById(title_id);var len=imageArr.length;if(len===0){if(tagger&&(tagger.currentView===0||tagger.currentView==3)&&albumId)
heart.redirect("profile","viewPhotosInAlbum",curUhid,{"album_id":albumId},null,null,"photo");else if(tagger&&(tagger.currentView===0||tagger.currentView==3))
heart.redirect("profile","showPhotoAlbums",curUhid,null,null,null,"photo");else
heart.redirect("profile","taggedPhotos",curUhid,null,null,null,"photo");return;}
if(title){title.innerHTML=imageArr[curIdx].title;title.setAttribute("plainValue",imageArr[curIdx].plainTitle);var fakeCap=$('fakecap_1');if(fakeCap){if(!imageArr[curIdx].title){hide($('cap_1'));show($('fakecap_1'));}
else{hide($('fakecap_1'));show($('cap_1'));}}}
caption.innerHTML=String(curIdx+1);hideButtons();setupDeleteParams();var formEle=$('setAlbumPhotoForm');if(formEle)formEle.elements.photo_id.value=imageArr[curIdx].id;drawComments();drawLikes(curIdx);updateAlbumInfo(curIdx);if(window.hideTagFrame){hideTagFrame();}
if(window.drawTags){drawTags(imageArr[curIdx]);}
preloadSurrounding(curIdx);drawImage();}
function hidePhotoCommentActions(event_id){if(event_id){event_id='_'+event_id;}else{event_id='';}
var submitBtn=$('commentSubmitRow'+event_id);var text=$('commentForm_comment'+event_id);if(text.value===''||text.value==text.getAttribute('defaultText')){hide(submitBtn);text.style.height='18px';}}
function showPhotoCommentActions(event_id){if(event_id){event_id='_'+event_id;}else{event_id='';}
var submitBtn=$('commentSubmitRow'+event_id);var text=$('commentForm_comment'+event_id);show(submitBtn);}
function cancelPhotoCommentActions(event_id){if(event_id){event_id='_'+event_id;}else{event_id='';}
var text=$('commentForm_comment'+event_id);text.value='';text.focus();text.blur();}
function nextslide(direction,jump){var dir=1;if(direction<0)
dir=-1;var newIdx;if(jump){if(dir==1)newIdx=imageArr.length-1;else newIdx=0;}else{newIdx=curIdx+dir;}
gotoSlide(newIdx);}
function removePhotoLike(actionUrl){var curPhoto=imageArr[curIdx].id;var photoOwner=imageArr[curIdx].photo_owner;var postData={'photo_id':curPhoto,'owner_uhid':photoOwner};var callBack=function(response){show(dojo.query($('likeLink'))[0]);hide(dojo.query($('likedText'))[0]);};var errorCallback=function(response){alert('Oops! That didn\'t work.  Please try again in a moment');};sendXhrJs(actionUrl,postData,callBack,errorCallback);heart.trackPageview('/likes/removePhotoLike');}
function getViewport(){var _window=dojo.global;var _document=dojo.doc;var w=0,h=0;var de=_document.documentElement;var dew=de.clientWidth,deh=de.clientHeight;if(dojo.isMozilla||dojo.isSafari){var minw,minh,maxw,maxh;var dbw=_document.body.clientWidth;if(dbw>dew){minw=dew;maxw=dbw;}else{maxw=dew;minw=dbw;}
var dbh=_document.body.clientHeight;if(dbh>deh){minh=deh;maxh=dbh;}else{maxh=deh;minh=dbh;}
w=(maxw>_window.innerWidth)?minw:maxw;h=(maxh>_window.innerHeight)?minh:maxh;}else if(_window.innerWidth){w=_window.innerWidth;h=_window.innerHeight;}else if(dojo.isIE&&de&&deh){w=dew;h=deh;}else if(dojo.body().clientWidth){w=dojo.body().clientWidth;h=dojo.body().clientHeight;}
var scroll=dojo._docScroll();return{w:w,h:h,l:scroll.x,t:scroll.y};}
function showTooltip(elem,text,direction){var coords=dojo.coords(elem,true);var ttDiv=document.getElementById('ttDiv');if(!ttDiv){ttDiv=dojo.doc.createElement("div");ttDiv.className="tooltipDivContainer";ttDiv.style.position="absolute";ttDiv.id="ttDiv";document.body.appendChild(ttDiv);}
var windowCoords=getViewport();ttDiv.innerHTML='';var tipDiv=document.createElement("div");tipDiv.className="tooltipDiv";tipDiv.style['float']='right';tipDiv.innerHTML=text;ttDiv.appendChild(tipDiv);ttDiv.style.zIndex=2000;ttDiv.style.right=null;ttDiv.style.top=null;ttDiv.style.left=null;ttDiv.style.bottom=null;var offset=2;switch(direction){case'right':case'left':ttDiv.style.right=(windowCoords.w-coords.x+offset)+'px';ttDiv.style.top=coords.y+'px';dojo.style(tipDiv,"float","right");arrowDiv=document.createElement("div");ttDiv.appendChild(arrowDiv);arrowDiv.className='ttRightArrow';break;default:ttDiv.style.bottom=(windowCoords.h-coords.y+offset)+'px';ttDiv.style.left=coords.x+'px';arrowDiv=document.createElement("div");ttDiv.appendChild(arrowDiv);arrowDiv.className='ttBottomArrow';break;}
show(ttDiv);}
function hideTooltip(){var ttDiv=document.getElementById('ttDiv');hide(ttDiv);}
function photoLike(actionUrl){var curPhoto=imageArr[curIdx].id;var photoOwner=imageArr[curIdx].photo_owner;var postData={'photo_id':curPhoto,'owner_uhid':photoOwner};var callBack=function(response){hide(dojo.query($('likeLink'))[0]);show(dojo.query($('likedText'))[0]);};var errorCallBack=function(response){alert('Oops! That didn\'t work.  Please try again in a moment');};sendXhrJs(actionUrl,postData,callBack,errorCallBack);heart.trackPageview('/likes/addPhotoLike');}
function postPhotoCommentCallback(photo,success,formEle,resp){formEle=dojo.byId(formEle);dojo.query('.ajaxStatus',formEle).forEach(function(i){hide(i);});if(formEle.submitBtn)formEle.submitBtn.disabled='';if(!success||(!resp.html&&!resp.comments)){alert('Error posting comment. Please try again in a minute.');}else{var updateComments=function(){if(resp.comments){photo.comments=resp.comments;if(photo===imageArr[curIdx])drawComments();}else if(resp.html){photo.innerHTML=resp.html;}
if(formEle.comment)formEle.comment.value='';var textBox=$('commentForm_comment');if(!textBox)
textBox=$('commentForm_comment_'+formEle.photo_id.value);textBox.focus();textBox.blur();};if(formEle.link){fadeOut(formEle,700,true,function(){updateComments();fadeIn(formEle.link,500);});}else updateComments();}}
function addPhotoComment(formEle,multiMode,imgUrl){if(!multiMode&&!imageArr)
alert('Error trying to comment.  Please wait a moment and try again');var comment=formEle.comment.value;if(!multiMode){formEle.photo_id.value=imageArr[curIdx].id;formEle.photo_owner_uhid.value=imageArr[curIdx].photo_owner;}
if(comment.length===0||comment==formEle.comment.getAttribute('defaultText')){return false;}
else if(comment.length>1500){alert('Your comment is '+(comment.length-1500)+' characters too long.');return false;}
dojo.query('.ajaxStatus',formEle).forEach(function(i){show(i);});formEle.submitBtn.disabled='disabled';var photo=multiMode?$('photo_comments_div_'+formEle.photo_id.value):imageArr[curIdx];var successCallback=dojo.partial(postPhotoCommentCallback,photo,true,formEle);var errorCallback=dojo.partial(postPhotoCommentCallback,photo,false,formEle);if(!imgUrl)imgUrl=imageArr[curIdx].img;var pk=getQueryObject(imgUrl).pk;sendXhrForm(formEle,successCallback,errorCallback,'json',{'pk':pk,'multiMode':(multiMode?1:0)});return false;}
function deletePhotoComment(actionUrl,comment_id,owner_uhid,callback){if(!confirm("Are you sure you want to delete this comment?"))return;var errorCallback=function(response){var statusElem=$(spinner);if(statusElem)statusElem.style.display='none';alert('Error deleting comment. Please try again in a minute.');};var postData={'comment_id':comment_id,'photo_owner_uhid':owner_uhid};sendXhrJson(actionUrl,postData,dojo.partial(deleteCommentCallback,callback,postData),errorCallback);}
function deleteCommentCallback(callback,postData,resp){var comments=resp.comments;var photo_id=resp.photo_id;callback.call(this,comments,photo_id,postData);}
function drawCommentsCallback(comments){imageArr[curIdx].comments=comments;drawComments();}
function commentsViewCallback(comments,photo_id,postData){var comment_id=postData.comment_id;var comment='comment_'+comment_id;comment=$(comment);comment.parentNode.removeChild(comment);var commentRows=dojo.query('.comment_'+photo_id);if(commentRows)
commentRows[(comments.length-1)].style.border='none';var deleteCommentRows=dojo.query('.deleteComment_'+photo_id);if(deleteCommentRows)
deleteCommentRows[(comments.length-1)].style.border='none';if(comments.length===0){dojo.query('.photo_row_'+photo_id).forEach(function(i){hide(i);});}}
function modifyPhotoComment(formEle,comment_id){var commentText=formEle.comment.value;var spinner='editPhotoCommentForm_status_'+comment_id;if(!commentText||commentText.length===0){alert("You cannot make a comment empty, please delete it instead");return false;}
dojo.query('.ajaxStatus',formEle).forEach(function(i){show(i);});formEle.submitBtn.disabled='disabled';var successCallback=dojo.partial(postPhotoCommentCallback,imageArr[curIdx],true,formEle);var errorCallback=dojo.partial(postPhotoCommentCallback,imageArr[curIdx],false,formEle);sendXhrForm(formEle,successCallback,errorCallback,'json');return false;}
function deletePhoto(){if(!confirm('Are you sure you want to delete this photo?'))
return false;document.forms.deletePhotoForm.submit();return true;}
function setAlbumPhoto(){document.forms.setAlbumPhotoForm.submit();}
function rotatePhoto(controlElem,photoElemId,dir,size){var successCallback=function(response){imageArr[curIdx].img=response.url;imageArr[curIdx].tags=response.tags;loadpic();};photo_rotate(controlElem,imageArr[curIdx].id,dir,size,successCallback);}