/*
 * note-ui.js
 *
 * Marginalia has been developed with funding and support from
 * BC Campus, Simon Fraser University, and the Government of
 * Canada, the UNDESA Africa i-Parliaments Action Plan, and  
 * units and individuals within those organizations.  Many 
 * thanks to all of them.  See CREDITS.html for details.
 * Copyright (C) 2005-2007 Geoffrey Glass; the United Nations
 * http://www.geof.net/code/annotation
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: note-ui.js 417 2008-12-23 01:57:53Z geof.glass $
 */
function _toggleAnnotationAccess(a){a.stopPropagation();var b=domutil.getEventTarget(a);var c=domutil.nestedFieldValue(this,AN_ANNOTATION_FIELD);var d=b;c.setAccess(c.getAccess()=="public"?"private":"public");window.marginalia.updateAnnotation(c,null);while(d.firstChild)d.removeChild(d.firstChild);d.appendChild(document.createTextNode(c.getAccess()=="public"?AN_SUN_SYMBOL:AN_MOON_SYMBOL));d.setAttribute("title",c.getAccess()=="public"?getLocalized("public annotation"):getLocalized("private annotation"))}function _expandEdit(a){a.stopPropagation();var b=domutil.getEventTarget(a);var c=domutil.nestedFieldValue(this,AN_ANNOTATION_FIELD);var d=domutil.nestedFieldValue(this,AN_POST_FIELD);var e=domutil.parentByTagClass(b,"li",null,false,null);var f=domutil.childByTagClass(e,"button",AN_EXPANDBUTTON_CLASS,null);while(f.firstChild)f.removeChild(f.firstChild);if(AN_EDIT_NOTE_KEYWORDS==c.editing){f.appendChild(document.createTextNode(AN_EXPANDED_ICON));d.showNoteEditor(marginalia,c,new FreeformNoteEditor)}else{f.appendChild(document.createTextNode(AN_COLLAPSED_ICON));d.showNoteEditor(marginalia,c,new KeywordNoteEditor)}}function _deleteAnnotation(a){a.stopPropagation();var b=domutil.nestedFieldValue(this,AN_POST_FIELD);var c=domutil.nestedFieldValue(this,AN_ANNOTATION_FIELD);b.deleteAnnotation(window.marginalia,c)}function _cancelAnnotationEdit(a){var b=window.marginalia.noteEditor.annotation;var c=window.marginalia.noteEditor.postMicro;c.cancelAnnotationEdit(window.marginalia,b)}function _saveAnnotation(a){var b=window.marginalia.noteEditor.annotation;var c=window.marginalia.noteEditor.postMicro;c.saveAnnotation(window.marginalia,b)}function _editChangedKeyup(a){var b=domutil.getEventTarget(a);var c=domutil.nestedFieldValue(b,AN_ANNOTATION_FIELD);if(b.value!=c.note)domutil.addClass(b,AN_EDITCHANGED_CLASS);else domutil.removeClass(b,AN_EDITCHANGED_CLASS);limitText(b)}function _editNoteKeypress(a){var b=domutil.getEventTarget(a);var c=domutil.nestedFieldValue(b,AN_POST_FIELD);var d=domutil.nestedFieldValue(b,AN_ANNOTATION_FIELD);if(a.keyCode==13){c.saveAnnotation(window.marginalia,d);a.stopPropagation();return false}else{limitText(b);return true}}function limitText(a){if(a.value.length>MAX_NOTE_LENGTH){a.value=a.value.substr(0,MAX_NOTE_LENGTH-1)}return true}function _editAnnotation(a){var b=window.marginalia;var c=domutil.nestedFieldValue(this,AN_POST_FIELD);var d=domutil.nestedFieldValue(this,AN_ANNOTATION_FIELD);if(b.noteEditor&&b.noteEditor.annotation!=d)return;a.stopPropagation();var e=b.newEditor(d,this.clickEditorType);c.showNoteEditor(b,d,e)}function YuiAutocompleteNoteEditor(){this.editNode=null;this.queryNode=null;this.autocomplete=null}function KeywordNoteEditor(){this.selectNode=null}function FreeformNoteEditor(){this.editNode=null}AN_NOTES_CLASS="notes";AN_DUMMY_CLASS="dummy";AN_QUOTENOTFOUND_CLASS="quote-error";AN_NOTECOLLAPSED_CLASS="collapsed";AN_EDITCHANGED_CLASS="changed";MAX_NOTE_LENGTH=500;MAX_NOTEHOVER_LENGTH=24;AN_LINKBUTTON_CLASS="annotation-link";AN_ACCESSBUTTON_CLASS="annotation-access";AN_DELETEBUTTON_CLASS="annotation-delete";AN_EXPANDBUTTON_CLASS="expand-edit";AN_KEYWORDSCONTROL_CLASS="keywords";AN_SUN_SYMBOL="⚪";AN_MOON_SYMBOL="◆";AN_LINK_ICON="☼";AN_LINK_EDIT_ICON="☼";AN_COLLAPSED_ICON="+";AN_EXPANDED_ICON="-";AN_DELETE_ICON="×";AN_LINKEDIT_LABEL="☼";PostMicro.prototype.getNotesElement=function(a){if(!this.notesElement){var b=domutil.childByTagClass(this.getElement(),null,AN_NOTES_CLASS,PostMicro.skipPostContent);this.notesElement=b.getElementsByTagName("ol")[0]}return this.notesElement};PostMicro.prototype.getAnnotationNextNote=function(a,b){var c=this.getNotesElement(a);for(var d=c.lastChild;null!=d;d=d.previousSibling){if(ELEMENT_NODE==d.nodeType&&d.annotation){if(d.annotation.getId()==b.getId())break;else if(b.compareRange(d.annotation)>=0)break}}if(d)return d.nextSibling;else{var e;for(e=c.firstChild;e;e=e.nextSibling){if(ELEMENT_NODE==e.nodeType&&e.annotation)break}return e}};PostMicro.prototype.getNoteId=function(a){return AN_ID_PREFIX+a.getId()};PostMicro.prototype.showNoteElement=function(a,b,c){var d=this;var e=this.getNotesElement(a);var f=domutil.childByTagClass(this.getContentElement(),"em",AN_ID_PREFIX+b.getId(),null);var g=f!=null;var h=document.getElementById(this.getNoteId(b));if(h){trace("showNote"," Note already present");this.clearNote(a,b);if(!g)domutil.setClass(h,AN_QUOTENOTFOUND_CLASS,g)}else{trace("showNote"," Create new note");var h=domutil.element("li",{id:AN_ID_PREFIX+b.getId(),className:g?"":AN_QUOTENOTFOUND_CLASS,annotation:b});var i=f?f:this.getNoteAlignElement(b);if(null!=i){var j=null;if(c)j=domutil.prevByTagClass(c,"li");else{j=domutil.childrenByTagClass(e,"li");if(j)j=j[j.length-1]}if(null==j){j=domutil.element("li",{className:AN_DUMMY_CLASS});e.insertBefore(j,c)}var k=this.calculateNotePushdown(a,j,i);h.style.marginTop=""+(k>0?String(k):"0")+"px"}trace("showNote"," Note "+h+" inserted before "+c+" in "+e+"("+e.parentNode+")");e.insertBefore(h,c)}addEvent(h,"mouseover",_hoverAnnotation);addEvent(h,"mouseout",_unhoverAnnotation);return h};PostMicro.prototype.showNote=function(a,b,c){trace("showNote","Show note "+b.toString());var d=this.showNoteElement(a,b,c);if(a.showActions&&b.getAction())domutil.addClass(d,AN_ACTIONPREFIX_CLASS+b.getAction());var e={isCurrentUser:null!=a.loginUserId&&b.getUserId()==a.loginUserId,linkingEnabled:a.editors["link"]?true:false,quoteFound:null!=domutil.childByTagClass(this.getContentElement(),"em",AN_ID_PREFIX+b.getId(),null),keyword:a.keywordService?a.keywordService.getKeyword(b.getNote()):null};a.displayNote(a,b,d,e);return d};Marginalia.prototype.bindNoteBehaviors=function(a,b,c){var d=this;var e=domutil.nestedFieldValue(b,AN_POST_FIELD);for(var f=0;f<c.length;++f){var g=cssQuery(c[f][0],b);if(g.length==1){var h=g[0];var i=c[f][1];for(var j in i){var k=i[j];this.bindNoteBehavior(h,j,k)}}else trace("behaviors","Show note behavior unable to find node: "+c[f][0])}};Marginalia.prototype.bindNoteBehavior=function(a,b,c){var d={access:_toggleAnnotationAccess,"delete":_deleteAnnotation,save:_saveAnnotation};switch(b){case"click":var e=d[c];if(!e){var f=c.split(" ");if(f.length>=1&&f[0]=="edit"){if(f.length==2)a.clickEditorType=f[1];e=_editAnnotation}}if(e)addEvent(a,"click",e);else logError("Unknown note click behavior: "+c);break;default:trace("behaviors","Unknown property: "+b)}};Marginalia.defaultDisplayNote=function(a,b,c,d){var e=domutil.element("div",{className:"controls"});c.appendChild(e);if(d.customButtons){for(var f=0;f<d.customButtons.length;++f){var g=d.customButtons[f];if(a.loginUserId==b.getUserId()?g.owner:g.others)e.appendChild(domutil.element("button",g.params))}}if(d.isCurrentUser){if(a.showAccess){e.appendChild(domutil.button({className:AN_ACCESSBUTTON_CLASS,title:getLocalized(b.getAccess()==AN_PUBLIC_ACCESS?"public annotation":"private annotation"),content:b.getAccess()==AN_PUBLIC_ACCESS?AN_SUN_SYMBOL:AN_MOON_SYMBOL}))}e.appendChild(domutil.button({className:AN_DELETEBUTTON_CLASS,title:getLocalized("delete annotation button"),content:AN_DELETE_ICON}))}var h=domutil.element("p",{className:"commentText",content:b.getNote()?b.getNote():" "});var i=null;if(!d.quoteFound||!b.getSequenceRange())i=getLocalized("quote not found")+': \n"'+b.getQuote()+'"';else if(d.keyword)i=d.keyword.description;if(i)h.setAttribute("title",i);if(!d.isCurrentUser){domutil.addClass(c,"other-user");h.insertBefore(domutil.element("span",{className:"username",content:b.getUserName()+": "}),h.firstChild)}h.setAttribute("title",b.getNote());c.appendChild(h);if(d.isCurrentUser){a.bindNoteBehaviors(b,c,[["button.annotation-link",{click:"edit link"}],["button.annotation-access",{click:"access"}],["button.annotation-delete",{click:"delete"}],["p",{click:"edit"}]])}};PostMicro.prototype.showNoteEditor=function(a,b,c,d){var e=this.showNoteElement(a,b,d);var f=domutil.getWindowYScroll();var g=domutil.getWindowXScroll();var h=false;if(a.noteEditor){c.annotationOrig=a.noteEditor.annotationOrig;if(a.noteEditor.save)a.noteEditor.save();if(a.noteEditor.clear)a.noteEditor.clear();if(a.noteEditor.annotation!=b)_saveAnnotation()}if(!a.noteEditor||a.noteEditor.noteElement!=e){domutil.addClass(document.body,AN_EDITINGNOTE_CLASS);this.flagAnnotation(a,b,AN_EDITINGNOTE_CLASS,true);h=true;c.annotationOrig=clone(b)}while(e.firstChild)e.removeChild(e.firstChild);trace(null,c.constructor);c.bind(a,this,b,e);a.noteEditor=c;c.show();this.repositionNotes(a,this.nextSibling);c.focus();window.scrollTo(g,f);if(h){addEvent(document.documentElement,"click",_saveAnnotation);addEvent(e,"click",domutil.stopPropagation)}return e};FreeformNoteEditor.prototype.bind=function(a,b,c,d){this.marginalia=a;this.postMicro=b;this.annotation=c;this.noteElement=d};FreeformNoteEditor.prototype.clear=function(){this.editNode=null};FreeformNoteEditor.prototype.save=function(){this.annotation.setNote(this.editNode.value)};FreeformNoteEditor.prototype.show=function(){var a=this.postMicro;var b=this.marginalia;var c=this.annotation;var d=this.noteElement;if(this.marginalia.keywordService){var e=function(d){a.showNoteEditor(b,c,new KeywordNoteEditor)};this.noteElement.appendChild(domutil.button({className:AN_EXPANDBUTTON_CLASS,title:getLocalized("annotation expand edit button"),content:AN_EXPANDED_ICON,onclick:e}))}this.editNode=document.createElement("textarea");this.editNode.setAttribute("id","countthisarea");this.editNode.rows=3;this.editNode.appendChild(document.createTextNode(c.getNote()));this.editNode.annotationId=this.annotation.getId();addEvent(this.editNode,"keypress",_editNoteKeypress);addEvent(this.editNode,"keyup",_editChangedKeyup);this.noteElement.appendChild(this.editNode);this.charCounter=document.createElement("span");this.charCounter.setAttribute("id","charactercount");this.noteElement.appendChild(this.charCounter);$("#countthisarea").keyup(function(){var a=500-$(this).val().length;if(a<0){a=0}$("#charactercount").text("Characters left: "+a)});$("#countthisarea").focus(function(){var a=500-$(this).val().length;if(a<0){a=0}$("#charactercount").text("Characters left: "+a)})};FreeformNoteEditor.prototype.focus=function(){this.editNode.focus();if("exploder"==domutil.detectBrowser())this.editNode.focus()};KeywordNoteEditor.prototype.bind=FreeformNoteEditor.prototype.bind;KeywordNoteEditor.prototype.clear=function(){this.selectNode=null};KeywordNoteEditor.prototype.save=function(){if(-1!=this.selectNode.selectedIndex)this.annotation.setNote(this.selectNode.options[this.selectNode.selectedIndex].value)};KeywordNoteEditor.prototype.show=function(){var a=this.postMicro;var b=this.marginalia;var c=this.annotation;var d=this.noteElement;this.noteElement.appendChild(domutil.button({className:AN_EXPANDBUTTON_CLASS,title:getLocalized("annotation collapse edit button"),content:AN_COLLAPSED_ICON}));this.selectNode=document.createElement("select");this.selectNode.className=AN_KEYWORDSCONTROL_CLASS;var e=b.keywordService.keywords;addEvent(this.selectNode,"keypress",_editNoteKeypress);if(!b.keywordService.isKeyword(c.getNote())&&c.getNote()){var f=document.createElement("option");f.appendChild(document.createTextNode(c.getNote().length>12?c.getNote().substring(0,12):c.getNote()));f.setAttribute("value",c.getNote());this.selectNode.appendChild(f)}var g=c.getNote();for(var h=0;h<e.length;++h){var i=e[h];f=document.createElement("option");if(g==i.name)f.setAttribute("selected","selected");f.appendChild(document.createTextNode(i.name));f.setAttribute("value",i.name);f.setAttribute("title",i.description);this.selectNode.appendChild(f)}this.noteElement.appendChild(this.selectNode);b.bindNoteBehaviors(c,d,[["."+AN_EXPANDBUTTON_CLASS,{click:"edit freeform"}]])};KeywordNoteEditor.prototype.focus=function(){this.selectNode.focus();if("exploder"==domutil.detectBrowser())this.selectNode.focus()};YuiAutocompleteNoteEditor.prototype.bind=FreeformNoteEditor.prototype.bind;YuiAutocompleteNoteEditor.prototype.clear=FreeformNoteEditor.prototype.clear;YuiAutocompleteNoteEditor.prototype.save=FreeformNoteEditor.prototype.save;YuiAutocompleteNoteEditor.prototype.focus=FreeformNoteEditor.prototype.focus;YuiAutocompleteNoteEditor.prototype.show=function(){var a=this.postMicro;var b=this.marginalia;var c=this.annotation;var d=this.noteElement;this.editNode=document.createElement("textarea");this.editNode.rows=3;this.editNode.appendChild(document.createTextNode(c.getNote()));this.queryNode=domutil.element("div");this.editNode.annotationId=this.annotation.getId();addEvent(this.editNode,"keypress",_editNoteKeypress);addEvent(this.editNode,"keyup",_editChangedKeyup);var e=domutil.element("div",{className:"yui-skin-sam"});e.appendChild(this.editNode);e.appendChild(this.queryNode);this.queryNode.style.display="none";this.noteElement.appendChild(e);var f=b.keywordService.keywords;var g=[];for(var h=0;h<f.length;++h)g[g.length]=f[h].name;var i;if(YAHOO.util.LocalDataSource)i=new YAHOO.util.LocalDataSource(g);else i=new YAHOO.widget.DS_JSArray(g);var j=e.offsetHeight;a.repositionNotes(b,this.noteElement.nextSibling);this.autocomplete=new YAHOO.widget.AutoComplete(this.editNode,this.queryNode,i,{typeAhead:true});e.style.height=String(j)+"px"};PostMicro.prototype.positionNote=function(a,b){var c=b.getNoteElement();while(null!=c){var d=this.getNoteAlignElement(b);if(null!=d){var e=this.calculateNotePushdown(a,c.previousSibling,d);c.style.marginTop=(e>0?String(e):"0")+"px"}c=c.nextSibling}};PostMicro.prototype.getNoteAlignElement=function(a){var b=domutil.childByTagClass(this.getContentElement(),"em",AN_ID_PREFIX+a.getId(),null);if(null==b&&a.getXPathRange())b=a.getXPathRange().start.getReferenceElement(this.getContentElement());if(null==b&&a.getSequenceRange())b=a.getSequenceRange().start.getReferenceElement(this.getContentElement());return b};PostMicro.prototype.calculateNotePushdown=function(a,b,c){var d=domutil.getElementYOffset(b,null)+b.offsetHeight;var e=domutil.getElementYOffset(c,null);var f=e-d;return f>0?f:0};PostMicro.prototype.repositionNotes=function(a,b){while(b){this.repositionNote(a,b);b=b.nextSibling}};PostMicro.prototype.repositionNote=function(a,b){var c=b.annotation;if(c){var d=this.getNoteAlignElement(c);if(d){var e=false;var f=b.previousSibling;var g=this.calculateNotePushdown(a,f,d);if(!e){b.style.marginTop=(g>0?String(g):"0")+"px";domutil.removeClass(b,AN_NOTECOLLAPSED_CLASS);b.pushdown=g}}}};PostMicro.prototype.repositionSubsequentNotes=function(a,b){for(var c=b;c;c=c.nextSibling){if(ELEMENT_NODE==c.nodeType&&c.annotation){var d=this.getNoteAlignElement(c.annotation);if(d){var e=this.calculateNotePushdown(a,c.previousSibling,d);if(c.pushdown&&c.pushdown==e)break;c.style.marginTop=(e>0?String(e):"0")+"px";c.pushdown=e}}}};PostMicro.prototype.removeNote=function(a,b){var c=b.getNoteElement();var d=domutil.nextByTagClass(c,"li");c.parentNode.removeChild(c);c.annotation=null;domutil.clearEventHandlers(c,true);return d};PostMicro.prototype.clearNote=function(a,b){var c=b.getNoteElement();domutil.clearEventHandlers(c,true,true);while(c.firstChild)c.removeChild(c.firstChild);return c}

