var DraggableNote=Class.create();Object.extend(Object.extend(DraggableNote.prototype,Draggable.prototype),{initialize:function(A,D,C){try{this.options={};this.element=$(A);this.handle=this.element;this.dragging=false;this.delta=this.currentDelta();this.drawCallback=D;this.editable=(C===undefined?true:C);Element.makePositioned(this.element);if(this.editable){this.eventMouseDown=this.initDrag.bindAsEventListener(this);Event.observe(this.element,"mousedown",this.eventMouseDown)}Draggables.register(this)}catch(B){}},draw:function(B){try{var A=$(this.element).cumulativeOffset();var E=this.currentDelta();A[0]-=E[0];A[1]-=E[1];var D=[0,1].map(function(F){return(B[F]-A[F]-this.offset[F])}.bind(this));this.drawCallback(this.element.id,D)}catch(C){}}});var PhotoNoteManager=Class.create();PhotoNoteManager.prototype={initialize:function(A,B){debug("new PhotoNoteManager()",A,B);this.img=$(A);this.photoID=$("photoID").value;this.userID=$("userID").value;this.notes={};this.noteCount=0;this.clickCoords={};this.startCoords={};this.resizing=false;this.dragging=false;this.editNoteID=0;this.textNoteID=0;this.editable=Element.hasClassName($("noteManager"),"editable")&&$("addNoteButton");this.isWebKit=/Konqueror|Safari|KHTML/.test(navigator.userAgent);this.minimumSize=150;this.isWithin=false;this.loadNotesOnInit=B==="undefined"?true:B;if(typeof (this.img)==="undefined"){debug("Image doesn't exist")}if(this.img.complete&&(this.img.width>0)){this.onImageCompleted()}else{this.img.observe("load",this.onImageCompleted.bindAsEventListener(this))}},onImageCompleted:function(){var F=this.img.parentNode;var H=this.img.width;var I=this.img.height;if(H<=0){H=540}if(I<=0){I=400}this.photoWrapper=Builder.node("div",{"class":"photoWrapper",id:"photoWrapperDiv"});this.dragArea=Builder.node("div",{"class":"dragArea",id:"photoDragAreaDiv"});this.overlay=Builder.node("div",{"class":"overlay",id:"photoOverlayDiv",style:"display: none;"});this.photoWrapper.appendChild(this.img);this.photoWrapper.appendChild(this.dragArea);this.photoWrapper.appendChild(this.overlay);this.dragArea.appendChild(Builder.node("div",{"class":"clickArea"}));$("noteManager").setStyle({width:H+"px"});this.noteForm=$("noteForm");$(this.noteForm).setStyle({zIndex:300});this.photoWrapper.appendChild(this.noteForm);this.noteText=$("noteText");$(this.noteText).setStyle({zIndex:300});this.noteTextWidth=Element.getDimensions(this.noteText).width;this.photoWrapper.appendChild(this.noteText);this.noteDialog=$("noteDialog");this.photoWrapper.appendChild(this.noteDialog);F.appendChild(this.photoWrapper);$(this.photoWrapper).setStyle({width:H+"px",height:I+"px"});$(this.dragArea).setStyle({width:H+"px",height:I+"px"});$(this.overlay).setStyle({width:H+"px",height:I+"px",zIndex:350});var C=Element.getDimensions(this.noteDialog);$(this.noteDialog).setStyle({zIndex:400,top:(I-C.height)/2+"px",left:(H-C.width)/2+"px"});Event.observe(document,"mousemove",this.onResizeNote.bindAsEventListener(this));Event.observe(document,"mouseup",this.endResizeNote.bindAsEventListener(this));if(!this.editable){var B=this.showNotes.bind(this);var E=this.hideNotes.bind(this);Event.observe(document,"mousemove",function(J){if(this.isWithin!==Position.within($("noteManager"),Event.pointerX(J),Event.pointerY(J))){if(!this.isWithin){this.isWithin=true;B()}else{this.isWithin=false;E()}}})}this.resizeBind=this.startResizeNote.bindAsEventListener(this);this.editBind=this.displayNoteForm.bindAsEventListener(this);this.dragBind=this.startDragNote.bindAsEventListener(this);this.overBind=this.displayNoteText.bindAsEventListener(this);this.outBind=this.hideNoteText.bindAsEventListener(this);this.clickBind=this.onNoteClicked.bindAsEventListener(this);if(this.editable){Event.observe($("addNoteButton"),"click",this.onAddNote.bindAsEventListener(this));Event.observe($("cancelNoteButton"),"click",this.onCancelNote.bindAsEventListener(this));Event.observe($("saveNoteButton"),"click",this.onSaveNote.bindAsEventListener(this));Event.observe($("deleteNoteButton"),"click",this.onDeleteNote.bindAsEventListener(this));Event.observe($("removeNoteFriend"),"click",this.onRemoveNoteFriend.bindAsEventListener(this));var G=this.onFriendSelected.bind(this);var A=new ComCore.SearchHelperPeople($("noteFriendText"),$("noteFriendsList"),{selectItemCallBack:G,includeSelf:true})}Event.observe($("noteFriendText"),"focus",function(J){$("noteFriendText").setStyle({color:"#333"});$("noteFriendText").value=""});var D=this.setNoteChanged.bind(this);Event.observe($("noteTextarea"),"keypress",function(J){D()});this.setNoteChanged(false);if(this.loadNotesOnInit){this.loadNotes()}if($("autoAddNote")&&$("autoAddNote").value==="true"){this.onAddNote(false)}this.notify("onNotesReady")},setNoteChanged:function(A){if(A===undefined){A=true}$("saveNoteButton").disabled=!A},emptyNoteFriendText:function(){$("noteFriendText").setStyle({color:"#999"});$("noteFriendText").value=$("emptyNoteFriendTextMessage").value},onRemoveNoteFriend:function(B){var A=this.getNote(this.editNoteID);$("noteFriendNickname").value="";this.emptyNoteFriendText();$("selectedNoteFriend").hide();$("noteFriendsForm").show();this.setNoteChanged();this.drawNote(this.editNoteID);Event.stop(B);return false},onFriendSelected:function(E,D,A,C){E.stop();var F=$("selectedNoteFriend");if(F){F.getElementsByTagName("div")[0].innerHTML=D.innerHTML}var B=$("noteFriendNickname");if(B){B.value=C.n}F.show();$("noteFriendsForm").hide();this.setNoteChanged();this.drawNote(this.editNoteID)},resetNotes:function(){$$("#photoDragAreaDiv div.note").invoke("remove");if(this.noteForm){this.noteForm.hide()}this.noteText.hide();this.notes={};this.noteCount=0},loadNotes:function(B){if(this.photoID===undefined){return false}var D=this.onNotesLoaded.bind(this);if(B!==undefined){D(B);return }var A=ComCore.Page.buildAjaxURL("photos","loadPhotoNotes")+"&photoID="+this.photoID+"&userID="+this.userID;var C=new Ajax.Request(A,{method:"get",onSuccess:function(F,E){D(F.responseText.evalJSON())}})},onNotesLoaded:function(F){if(F){for(var E=0;E<F.length;E++){var C=parseInt(F[E].x1,10);var A=parseInt(F[E].x2,10);var G=parseInt(F[E].y1,10);var D=parseInt(F[E].y2,10);if(A>this.img.width){C=C-(A-this.img.width)-2;A=this.img.width-2}if(D>this.img.height){G=G-(D-this.img.height)-2;D=this.img.height-2}var B=this.addNote(F[E].noteID,{x1:C,y1:G,x2:A,y2:D},F[E].noteText,F[E].noteHTML,F[E].nickname,F[E].userHTML);this.drawNote(B)}if($("hasNotesMessage")){$("hasNotesMessage").show()}}},addNote:function(B,N,L,F,E,O){var C="note"+(++this.noteCount);var H={};if(!N){N={x1:10,y1:10,x2:60,y2:60}}if(!L){L=""}if(!F){F=""}if(!B){B=0}if(!E){E=""}if(!O){O=""}var I="";if(!this.editable&&E!==""){I=$("visitTitleText").value.replace(/\[nickname\]/,E)}var D=Builder.node("div",{"class":"handle top right"});var A=Builder.node("div",{"class":"handle bottom right"});var K=Builder.node("div",{"class":"handle bottom left"});var G=Builder.node("div",{"class":"handle top left"});var J=Builder.node("div",{"class":"inner"});var M=Builder.node("div",{title:I,"class":"note",id:"note"+this.noteCount},[J,D,A,K,G]);this.dragArea.appendChild(M);if(this.editable){Event.observe(D,"mousedown",this.resizeBind);Event.observe(A,"mousedown",this.resizeBind);Event.observe(K,"mousedown",this.resizeBind);Event.observe(G,"mousedown",this.resizeBind);Event.observe(J,"mouseup",this.editBind);Event.observe(J,"mousedown",this.dragBind)}else{Event.observe(J,"click",this.clickBind)}Event.observe(J,"mouseover",this.overBind);Event.observe(J,"mouseout",this.outBind);H.element=M;H.handle=J;H.handleTopRight=D;H.handleBottomRight=A;H.handleBottomLeft=K;H.handleTopLeft=G;H.coords=N;H.oldCoords=this.cloneCoords(N);H.text=L;H.html=F;H.draggable=new DraggableNote(M,this.moveNote.bind(this),this.editable);H.noteID=B;H.nickname=E;H.userHTML=O;this.notes[C]=H;return C},removeNote:function(A){var B=this.getNote(A);Event.stopObserving(B.handleTopRight,"mousedown",this.resizeBind);Event.stopObserving(B.handleBottomRight,"mousedown",this.resizeBind);Event.stopObserving(B.handleBottomLeft,"mousedown",this.resizeBind);Event.stopObserving(B.handleTopLeft,"mousedown",this.resizeBind);Event.stopObserving(B.element,"mouseup",this.editBind);Event.stopObserving(B.element,"mousedown",this.dragBind);Event.stopObserving(B.element,"mouseover",this.overBind);Event.stopObserving(B.element,"mouseout",this.outBind);B.draggable.destroy();delete this.notes[A];this.dragArea.removeChild($(A))},getNote:function(A){return this.notes[A]},moveNote:function(B,A){if(this.editNoteID!==0&&this.editNoteID!==B){return false}else{this.setNoteChanged();var C=this.getNote(B);this.setNoteCoords(B,{x1:A[0],y1:A[1],x2:A[0]+(C.coords.x2-C.coords.x1),y2:A[1]+(C.coords.y2-C.coords.y1)},true);this.drawNote(B)}},showNotes:function(){for(var A in this.notes){if(A){$(this.notes[A].element).show();this.drawNote(A)}}},hideNotes:function(){for(var A in this.notes){if(A){$(this.notes[A].element).hide()}}},onAddNote:function(B){if(this.editNoteID===0){if(Element.getDimensions(this.img).width<this.minimumSize||Element.getDimensions(this.img).height<this.minimumSize){ComCore.ActionStateMessage.show("error",$("pictureTooSmallMessage").value.replace(/\[minimumSize\]/g,this.minimumSize));Event.stop(B);return false}var A=this.addNote();this.editNoteID=A;this.textNoteID=0;$("noteTextarea").value="";$("noteTextarea").select();this.emptyNoteFriendText();$("noteFriendNickname").value="";$("noteFriendsForm").show();$("selectedNoteFriend").hide();Element.addClassName(this.getNote(A).element,"selected");this.drawNote(A)}if(B){Event.stop(B)}},onDeleteNote:function(A){if($("confirmDeleteText")&&confirm($("confirmDeleteText").value)){this.noteForm.hide();this.noteText.hide();this.deleteNote();$("emptyNoteError").hide()}Event.stop(A);return false},deleteNote:function(){var A=this.getNote(this.editNoteID);if(A.noteID<1){return false}var B=this;this.showDialog("delete");var C=new Ajax.Request(ComCore.Page.buildAjaxURL("photos","deletePhotoNote")+"&noteID="+A.noteID+"&photoID="+this.photoID,{method:"get",onSuccess:B.onNoteDeleted.bind(B)})},onNoteDeleted:function(){this.removeNote(this.editNoteID);this.editNoteID=0;this.dragNoteID=0;this.notify("onResetCache",this.photoID,$H({notes:0}));this.hideDialog()},onCancelNote:function(C){var B=this.getNote(this.editNoteID);if(B.noteID===0){this.removeNote(this.editNoteID)}else{B.coords=this.cloneCoords(B.oldCoords)}this.noteForm.hide();this.noteText.hide();this.editNoteID=0;this.dragNoteID=0;for(var A in this.notes){if(A){this.drawNote(A);Element.removeClassName(this.notes[A].element,"selected")}}$("emptyNoteError").hide();Event.stop(C)},onSaveNote:function(B){var C=$("noteTextarea").value.trim();if(C!==""||(C===""&&$("noteFriendNickname").value!=="")){var A=this.getNote(this.editNoteID);A.text=$("noteTextarea").value;if($("noteFriendNickname").value===""){A.nickname="";A.userHTML=""}else{A.nickname=$("noteFriendNickname").value;A.userHTML=$("selectedNoteFriend").getElementsByTagName("div")[0].innerHTML}this.noteText.hide();this.noteForm.hide();this.saveNote();$("emptyNoteError").hide()}else{$("emptyNoteError").show();this.drawNote(this.editNoteID)}},saveNote:function(){var B=this.getNote(this.editNoteID);var C=this;var A=ComCore.Page.buildAjaxURL("photos","savePhotoNote");var E="";if(B.noteID>0){E="photoID="+this.photoID+"&noteID="+B.noteID+"&coords="+B.coords.x1+","+B.coords.y1+","+B.coords.x2+","+B.coords.y2+"&note="+encodeURIComponent(B.text)+"&nickname="+$("noteFriendNickname").value}else{E="photoID="+this.photoID+"&coords="+B.coords.x1+","+B.coords.y1+","+B.coords.x2+","+B.coords.y2+"&note="+encodeURIComponent(B.text)+"&nickname="+$("noteFriendNickname").value}this.showDialog("save");var D=new Ajax.Request(A,{method:"post",postBody:E,onSuccess:function(G,F){if(!F){F=G.responseJSON}C.onNoteSaved(F.noteID,F.html)}})},onNoteSaved:function(A,D){var E=this.getNote(this.editNoteID);if(E.noteID===0){E.noteID=A}E.html=D;this.editNoteID=0;this.textNoteID=0;for(var C in this.notes){if(C){this.drawNote(C);Element.removeClassName(this.notes[C].element,"selected")}}var B=[];$H(this.notes).each(function(F){var G=F.value;B.push({noteID:G.noteID,noteText:G.text,x1:G.coords.x1,y1:G.coords.y1,x2:G.coords.x2,y2:G.coords.y2,userID:this.userID,userHTML:G.userHTML,noteHTML:G.text})});this.notify("onResetCache",this.photoID,$H({notes:B}));this.hideDialog()},showDialog:function(A){Element[A==="save"?"hide":"show"]($("deleteMessage"));Element[A==="delete"?"hide":"show"]($("saveMessage"));$(this.overlay).show();$(this.noteDialog).show()},hideDialog:function(){$(this.noteDialog).hide();$(this.overlay).hide()},startDragNote:function(D){if(this.editNoteID===0){var A=D.findElement("div.note");if(A&&A.className.match(/note/)){var B=A.id;var C=this.getNote(B);Element.addClassName(A,"selected");C.oldCoords=this.cloneCoords(C.coords);this.textNoteID=B;this.dragging=true;this.displayNoteForm(D)}}},displayNoteForm:function(C){this.dragging=false;if(this.editNoteID===0){var A=C.findElement("div.note");if(A&&A.className.match(/note/)){var B=A.id;Element.removeClassName(A,"hover");$("noteTextarea").value=this.notes[B].text;$("noteTextarea").select();this.emptyNoteFriendText();this.editNoteID=B;this.textNoteID=0;this.setNoteChanged(false);if(this.notes[B].nickname){$("noteFriendNickname").value=this.notes[B].nickname;$("selectedNoteFriend").getElementsByTagName("div")[0].innerHTML=this.notes[B].userHTML;$("noteFriendsForm").hide();$("selectedNoteFriend").show()}else{$("noteFriendNickname").value="";$("noteFriendsForm").show();$("selectedNoteFriend").hide()}this.drawNote(B)}}},displayNoteText:function(C){if(this.editNoteID===0){var A=C.findElement("div.note");if(A&&A.className.match(/note/)&&!this.dragging){var B=A.id;Element.addClassName(A,"hover");$("defaultNote").innerHTML=(this.notes[B].text==="")?"&nbsp;":this.notes[B].html;this.textNoteID=B;if(this.notes[B].nickname!==""){$("defaultNoteFriend").innerHTML=this.notes[B].userHTML;$("defaultNoteFriend").show()}else{$("defaultNoteFriend").hide()}this.drawNote(B);if(!this.editable&&this.notes[B].nickname.length>0){$(A).setStyle({cursor:"pointer"})}}}},onNoteClicked:function(D){var A=D.findElement("div.note");if(A&&A.className.match(/note/)){var B=A.id;var C=this.getNote(B);if(C.nickname.length>0){document.location="/"+C.nickname}}},hideNoteText:function(C){if(this.editNoteID===0&&!this.dragging){var A=C.findElement("div.note");if(A&&A.className.match(/note/)){var B=A.id;Element.removeClassName(A,"hover");this.textNoteID=0;this.drawNote(B)}}},cloneCoords:function(A){return{x1:A.x1,y1:A.y1,x2:A.x2,y2:A.y2}},setNoteCoords:function(C,L,H,M,K){var I=this.getNote(C);if(!I){return }if(H){var N=L.x2-L.x1;var B=L.y2-L.y1;if(L.x1<0){L.x1=0;L.x2=N}if(L.y1<0){L.y1=0;L.y2=B}if(L.x2>this.img.width){L.x2=this.img.width-2;L.x1=this.img.width-2-N}if(L.y2>this.img.height){L.y2=this.img.height-2;L.y1=this.img.height-2-B}}else{if(L.x1<0){L.x1=0}if(L.y1<0){L.y1=0}if(L.x2>this.img.width){L.x2=this.img.width-2}if(L.y2>this.img.height){L.y2=this.img.height-2}if(M!==null){var A=[20,20];var J=[this.img.width,this.img.height];if(A[0]>0||A[1]>0||J[0]>0||J[1]>0){var G={a1:L.x1,a2:L.x2};var F={a1:L.y1,a2:L.y2};var E={min:0,max:this.img.width};var D={min:0,max:this.img.height};if(A[0]>0){this.applyDimensionRestriction(G,A[0],M.x,E,"min")}if(A[1]>1){this.applyDimensionRestriction(F,A[1],M.y,D,"min")}if(J[0]>0){this.applyDimensionRestriction(G,J[0],M.x,E,"max")}if(J[1]>1){this.applyDimensionRestriction(F,J[1],M.y,D,"max")}L={x1:G.a1,y1:F.a1,x2:G.a2,y2:F.a2}}}}I.coords=L},applyDimensionRestriction:function(A,E,F,C,D){var B;if(D==="min"){B=((A.a2-A.a1)<E)}else{B=((A.a2-A.a1)>E)}if(B){if(F===1){A.a2=A.a1+E}else{A.a1=A.a2-E}if(A.a1<C.min){A.a1=C.min;A.a2=E}else{if(A.a2>C.max){A.a1=C.max-E;A.a2=C.max}}}},debugCoords:function(A){this.debug("x1: "+A.x1+" y1: "+A.y1+" x2: "+A.x2+" y2: "+A.y2)},drawNote:function(A){var C=this.getNote(A);C.element.style.left=C.coords.x1+"px";C.element.style.top=C.coords.y1+"px";C.element.style.width=(C.coords.x2-C.coords.x1)+"px";C.element.style.height=(C.coords.y2-C.coords.y1)+"px";C.handle.style.width=(C.coords.x2-C.coords.x1-2)+"px";C.handle.style.height=(C.coords.y2-C.coords.y1-2)+"px";var F=0;var E=0;if(this.editNoteID===A){var D=Element.getDimensions(this.noteForm).width;var B=Element.getDimensions(this.noteForm).height;Element[C.noteID>0?"show":"hide"]($("deleteNoteButton"));F=(C.coords.y2+B+5<this.img.height)?C.coords.y2+5:C.coords.y1-B-5;E=C.coords.x1+(C.coords.x2-C.coords.x1)/2-(D/2);if(E<0){E=0}else{if(E>this.img.width-D){E=this.img.width-D}}this.noteForm.style.top=F+"px";this.noteForm.style.left=E+"px";this.noteForm.show()}else{this.noteForm.hide()}if(this.textNoteID===A&&this.editNoteID!==A){F=C.coords.y2+5;E=C.coords.x1+(C.coords.x2-C.coords.x1)/2-(this.noteTextWidth/2);if(E<0){E=0}else{if(E>this.img.width-this.noteTextWidth){E=this.img.width-this.noteTextWidth}}this.noteText.style.top=F+"px";this.noteText.style.left=E+"px";this.noteText.show()}else{this.noteText.hide()}},startResizeNote:function(C){var A=Event.element(C).parentNode.id;if(this.editNoteID===A){var B=this.getNote(A);this.startCoords=this.cloneCoords(B.coords);this.resizing=true;this.resizeHandle=Event.element(C)}Event.stop(C)},getMousePosition:function(B){var A=$(this.photoWrapper).cumulativeOffset();var C={x:Event.pointerX(B)-A[0],y:Event.pointerY(B)-A[1]};return C},onResizeNote:function(D){if(this.resizing){this.setNoteChanged();var B=this.resizeHandle.parentNode.id;var C=this.getNote(B);var F=this.getMousePosition(D);var A=this.cloneCoords(C.coords);var E={x:1,y:1};if(this.resizeHandle.className.match(/right/)){this.transformCoords(F.x,this.startCoords.x1,A,"x");if(F.x<this.startCoords.x1){E.x=-1}}else{if(this.resizeHandle.className.match(/left/)){this.transformCoords(F.x,this.startCoords.x2,A,"x");if(F.x<this.startCoords.x2){E.x=-1}}}if(this.resizeHandle.className.match(/top/)){this.transformCoords(F.y,this.startCoords.y2,A,"y");if(F.y<this.startCoords.y2){E.y=-1}}else{if(this.resizeHandle.className.match(/bottom/)){this.transformCoords(F.y,this.startCoords.y1,A,"y");if(F.y<this.startCoords.y1){E.y=-1}}}this.setNoteCoords(B,A,false,E,this.resizeHandle);this.drawNote(B);Event.stop(D)}},transformCoords:function(E,B,A,D){var C=[E,B];if(E>B){C.reverse()}A[D+"1"]=C[0];A[D+"2"]=C[1]},endResizeNote:function(){this.resizing=false}};ComCore.Events.extend(PhotoNoteManager);