(function($){$.fn.jQueryNotes=function(settings){settings=jQuery.extend({debugMode:false,minWidth:30,minHeight:30,maxWidth:800,maxHeight:800,aspectRatio:false,allowAdd:true,allowEdit:true,allowDelete:true,allowHide:true,allowReload:true,allowLink:true,allowAuthor:false,dateFormat:"",hideNotes:false,loadNotes:true,helper:"",maxNotes:null,operator:"/_scripts/ajaxtargets/jquery_notes.php"},settings);var ID={};var _getProperties=function(pointer){_errorLog("1 getproperties");var image=$("#jquery-notes_"+pointer+" img");ID.timeout;ID.firstLoad=true;ID.add=false;ID.edit=false;ID.set=false;ID.move=false;ID.image=image.attr("src");ID.image=ID.image.split("/")[6];if((typeof ID.image!="undefined")&&(ID.image!=undefined)){ID.image=ID.image.replace(".jpg","");ID.image=ID.image.replace(".jpeg","");ID.image=ID.image.replace(".gif","");ID.image=ID.image.replace(".png","");}ID.width=image.width();ID.height=image.height();ID.left=image.offset().left;ID.top=image.offset().top;};var _configSettings=function(pointer){_errorLog("2 configsettings");ID.minWidth=(settings.minWidth.toString().match("%"))?_percentToPixel(pointer,settings.minWidth,"width"):ID.minWidth=settings.minWidth;if(ID.minWidth>ID.width){ID.minWidth=ID.width;ID.maxWidth="";}else{ID.maxWidth=(settings.maxWidth.toString().match("%"))?_percentToPixel(pointer,settings.maxWidth,"width"):ID.maxWidth=settings.maxWidth;ID.maxWidth=(ID.maxWidth>ID.width)?ID.width:ID.maxWidth;}ID.minHeight=(settings.minHeight.toString().match("%"))?_percentToPixel(pointer,settings.minHeight,"height"):ID.minHeight=settings.minHeight;if(ID.minHeight>ID.height){ID.minHeight=ID.height;ID.maxHeight="";}else{ID.maxHeight=(settings.maxHeight.toString().match("%"))?_percentToPixel(pointer,settings.maxHeight,"height"):ID.maxHeight=settings.maxHeight;ID.maxHeight=(ID.maxHeight>ID.height)?ID.height:ID.maxHeight;}ID.aspectRatio=(settings.aspectRatio)?ID.minWidth/ID.minHeight:false;};var _initPlugin=function(matchedObj){_errorLog("3 initplugin");var timestamp=new Date().getTime();var src=$(matchedObj).attr("src");_errorLog("Line 139 src:"+src);$(matchedObj).attr({src:src+"?timestamp="+timestamp});$(matchedObj).one("load",function(){$(this).attr({src:src});_errorLog("Line 152 src:"+src);var pointer=$(".jquery-notes-container").length;pointer++;ID={};_setContainer(this,pointer);_getProperties(pointer);_configSettings(pointer);_createHtml(pointer);_getNotes(pointer);});_errorLog("End of init");};var _setContainer=function(matchedObj,pointer){_errorLog("4 setcontainer");var cssClass=$(matchedObj).attr("class");var style=$(matchedObj).attr("style");$(matchedObj).wrap('<div id="jquery-notes_'+pointer+'" class="jquery-notes-container clearfix" />').removeAttr("class","style").css({padding:0,margin:0,border:"none",outline:"none",background:"none"});$("#jquery-notes_"+pointer).addClass(cssClass).addClass(settings.helper).attr({style:style});};var _createHtml=function(pointer){_errorLog("5 createHtml");$("#jquery-notes_"+pointer+" img").wrap('<div class="notes clearfix" />');$("#jquery-notes_"+pointer+" .notes").append('<div class="layer"></div><div class="loading"><div class="message"></div></div>');$("#jquery-notes_"+pointer+" .notes .loading").css({marginTop:((ID.height/2)-13)+"px"});$("#jquery-notes_"+pointer+" .notes").mousedown(function(event){_errorLog("1 anonymous in createHtml mousedown");_addNote(pointer,event);}).mouseup(function(event){_errorLog("1 anonymous in createHtml mouseup");_setNote(pointer,event);});$("#jquery-notes_"+pointer).append('<div class="controller clearfix" />').width(ID.width);var html="";html+='<a href="javascript:void(0);" id="counterText" class="counter">&nbsp;</a>';html+=(settings.allowAdd)?'<a href="javascript:void(0);" class="add-note" title="Skapa" /><div id="add-note-MSG" style="display: none; margin:left: 16px; border: 1px solid black;  padding: 4px; background-color: #EEE;  "></div> ':"";html+=(settings.allowHide&&settings.loadNotes)?'<a href="javascript:void(0);" class="hide-notes" title="Visa inte" />':"";html+=(settings.allowReload&&settings.loadNotes)?'<a href="javascript:void(0);" class="reload-notes" title="Ladda om" />':"";$("#jquery-notes_"+pointer+" .controller").append(html);$("#jquery-notes_"+pointer+" .add-note").click(function(){_errorLog("anonymous #jaqury_notes-ponter. .add-note cklick");_startAdd(pointer);});$("#jquery-notes_"+pointer+" .hide-notes").click(function(){_errorLog("anonymous #jaqury_notes-ponter. .hide-notes cklick");_hideNotes(pointer);});$("#jquery-notes_"+pointer+" .reload-notes").click(function(){_reload(pointer);});_removeLink(pointer);};var _removeLink=function(pointer){_errorLog("6 removelink");var wrapperLink=$("#jquery-notes_"+pointer).parent("a");if(wrapperLink.length==1){$("#jquery-notes_"+pointer+" .controller").append('<a href="'+$(wrapperLink).attr("href")+'"  class="link" title="Lank" />');$("#jquery-notes_"+pointer).unwrap();}};var _countNotes=function(pointer){return $("#jquery-notes_"+pointer+" .notes").children(".note").length;};var _startLoading=function(pointer,message){_errorLog("Startloadig");$("#jquery-notes_"+pointer+" .notes .layer").fadeIn("middle");$("#jquery-notes_"+pointer+" .notes .loading").fadeIn("middle");$("#jquery-notes_"+pointer+" .notes .loading .message").text(message);};var _stopLoading=function(pointer){_errorLog("7 stoploading");ID.timeout=undefined;(ID.timeout!=undefined)?clearTimeout(ID.timeout):"";ID.timeout=setTimeout(function(){ID.timeout=undefined;$("#jquery-notes_"+pointer+" .notes .layer").fadeOut("middle");$("#jquery-notes_"+pointer+" .notes .loading").fadeOut("middle");$("#jquery-notes_"+pointer+" .notes .loading .message").text("");},1000);_devoteMod(pointer,true);};var _getNotes=function(pointer){_errorLog("8 getnotes");if(settings.loadNotes){$.ajax({url:settings.operator,global:false,timeout:15000,dataType:"json",type:"POST",beforeSend:function(){(ID.firstLoad)?_startLoading(pointer,"laddar taggar"):"";},data:"get=true&image="+ID.image,success:function(data){if(data){firstLoad=false;$.each(data,function(){_errorLog("_getNotes: pointer:"+pointer+" Note: "+this.NOTE+" this.id"+this.ID+" this.width"+this.WIDTH+" this.height"+this.HEIGHT+" this.top"+this.TOP+" this.left"+this.LEFT);_printNote(pointer,this);});var counter=_countNotes(pointer);if(counter>0){$("#jquery-notes_"+pointer+" .controller .counter").html((counter==1)?counter+" tagg":counter+" taggar");}else{$("#jquery-notes_"+pointer+" .controller .counter").hide();}(counter>=settings.maxNotes&&settings.maxNotes!=null)?$("#jquery-notes_"+pointer+" .add-note").hide():$("#jquery-notes_"+pointer+" .add-note").show();(settings.hideNotes)?_hideNotes(pointer):"";}_stopLoading(pointer);}});}else{_stopLoading(pointer);}};var _printNote=function(pointer,note){_errorLog("9 printnote");var html='<div id="n_'+pointer+"-"+note.ID+'" class="note"><div class="border"><div class="bg">';html+=(note.LINK!=""&&settings.allowLink)?'<a href="'+note.LINK+'" target="_blank"></a>':"";html+='</div></div></div><div id="t_'+pointer+"-"+note.ID+'" class="text"><span class="txt">';if(note.LINK!=""){html+='<a href="'+note.LINK+'" target="_blank">'+note.NOTE+"</a>";}else{html+=note.NOTE;}html+="</span>";html+=(note.AUTHOR!=""&&settings.allowAuthor)?'<span class="author"> - '+note.AUTHOR+"</span>":"";html+=(note.DATE!=""&&settings.dateFormat!="")?'<span class="date">'+_formatDate(note.DATE)+"</span>":"";html+="</div>";$("#jquery-notes_"+pointer+" .notes").append(html);if(settings.allowDelete||settings.allowEdit){$("#jquery-notes_"+pointer+" .notes #n_"+pointer+"-"+note.ID+",#jquery-notes_"+pointer+" .notes #t_"+pointer+"-"+note.ID).mousedown(function(e){$(this).mouseup(function(){$(this).unbind("mouseup");(e.button==2)?_openSettings(pointer,note.ID):null;});})[0].oncontextmenu=function(){return false;};}var position={left:_percentToPixel(pointer,note.LEFT,"left"),top:_percentToPixel(pointer,note.TOP,"top"),width:_percentToPixel(pointer,note.WIDTH,"width"),height:_percentToPixel(pointer,note.HEIGHT,"height")};_errorLog(position);$("#jquery-notes_"+pointer+" .notes #n_"+pointer+"-"+note.ID).css({left:position.left+"px",top:position.top+"px",width:position.width+"px",height:position.height+"px"}).hover(function(){_errorLog("line ~466 pointer: "+pointer+" id: "+note.ID);$("#jquery-notes_"+pointer).css("display","block");},function(){_errorLog("line ~469");});$("#jquery-notes_"+pointer+" .notes #t_"+pointer+"-"+note.ID).css({left:position.left+"px",top:(parseFloat(position.top)+parseFloat(position.height))+"px"});};var _focusOnNote=function(pointer,id){_errorLog("_focusOnNote");};var _focusOffNote=function(pointer,id){};var _addNote=function(pointer,event){_errorLog("addnote");if(ID.add&&!ID.set){ID.set=true;ID.move=true;var position={};position.left=event.pageX-ID.left;position.top=event.pageY-ID.top;$("#jquery-notes_"+pointer+" .notes").append('<div class="note select"><div class="border"><div class="bg"></div></div></div>');position.maxLeft=ID.width-ID.minWidth;position.maxTop=ID.height-ID.minHeight;position.left=(position.left>position.maxLeft)?position.maxLeft:position.left;position.top=(position.top>position.maxTop)?position.maxTop:position.top;$("#jquery-notes_"+pointer+" .notes .select").css({width:ID.minWidth,height:ID.minHeight,left:position.left,top:position.top,display:"block",cursor:"move"}).draggable({containment:"parent",cursor:"move"}).resizable({containment:"parent",minWidth:ID.minWidth,minHeight:ID.minHeight,maxWidth:ID.maxWidth,maxHeight:ID.maxHeight,aspectRatio:ID.aspectRatio,handles:"ne, se, sw, nw"});}};var _setNote=function(pointer,event){_errorLog("10 setnote");if(ID.add&&ID.set&&ID.move){ID.move=false;var html='<div class="text-box">';html+=(settings.allowAuthor)?'<input type="text" name="author" value="" /><br />':"";html+='<textarea name="note"></textarea><br />';html+=(settings.allowLink)?'<input type="text" name="link" value="http://" /><br />':"";html+='<a href="javascript:void(0);" class="save-note" title="spara tagg"></a><a href="javascript:void(0);" class="cancel-note" title="Avbryt"></a></div>';$("#jquery-notes_"+pointer+" .notes .select").append(html);_inputFocus(pointer);$("#jquery-notes_"+pointer+" .notes .select .text-box .save-note").click(function(){_saveNote(pointer,null,"add");});$("#jquery-notes_"+pointer+" .notes .select .text-box .cancel-note").click(function(){$("#jquery-notes_"+pointer+" .controller .cancel-note").removeClass("cancel-note").attr({title:"Skapa tagg"});$("#add-note-MSG").fadeOut();_abort(pointer);});}};var _inputFocus=function(pointer){$("#jquery-notes_"+pointer+' .notes .select input[name="link"]').focusout(function(){($(this).val()=="")?$(this).val("http://"):null;});};var _saveNote=function(pointer,id,operation){_errorLog("savenote");$("#add-note-MSG").fadeOut();var note=$("#jquery-notes_"+pointer+" .notes .select .text-box textarea").val();var link=$("#jquery-notes_"+pointer+' .notes .select .text-box input[name="link"]').val();var author=$("#jquery-notes_"+pointer+' .notes .select .text-box input[name="author"]').val();_errorLog(link);link=(link==undefined)?"":link;author=(author==undefined)?"":author;var position=_getNotePosition(pointer);$.ajax({url:settings.operator,global:false,timeout:15000,dataType:"json",type:"POST",beforeSend:function(){(id==undefined)?_startLoading(pointer,"saving note"):_startLoading(pointer,"editing note");},data:operation+"=true&image="+ID.image+"&id="+id+"&position="+position.left+","+position.top+","+position.width+","+position.height+"&note="+note+"&link="+link+"&author="+author,success:function(data){if(data){$("#jquery-notes_"+pointer+" .controller .cancel-note").removeClass("cancel-note").attr({title:"Skapa tagg"});_reload(pointer);}else{_stopLoading(pointer);alert("Fel: Kan inte spara tagg...");}}});};var _deleteNote=function(pointer,id){if(settings.allowDelete){$.ajax({url:settings.operator,global:false,timeout:15000,dataType:"json",type:"POST",beforeSend:function(){_startLoading(pointer,"deleting note");},data:"delete=true&image="+ID.image+"&id="+id,success:function(data){_stopLoading(pointer);(data)?_reload(pointer):alert("Fel: Kan inte ta bort tagg.");}});}};var _startAdd=function(pointer){_errorLog("startadd");if(settings.allowAdd&&(_countNotes(pointer)<settings.maxNotes||settings.maxNotes==null)){$("[id^=t_"+pointer+"]").css("display","none");if(ID.add){$("#jquery-notes_"+pointer+" .controller .cancel-note").removeClass("cancel-note").attr({title:"Skapa tagg"});_abort(pointer);}else{if(!ID.add&&!ID.edit){$("#add-note-MSG").html("<ul><li>Klicka i bilden f&ouml;r att b&ouml;rja l&auml;gga till en tagg</li><li>F&ouml;r att redigera en befintlig tagg h&ouml;gerklickar du p&aring; den.</li></ul>").fadeIn();_abort(pointer);_focusOnNote(pointer);ID.add=true;$("#jquery-notes_"+pointer+" .controller .add-note").addClass("cancel-note").attr({title:"Avbryt"});}}}};var _openSettings=function(pointer,id){_errorLog("11 opensettings");if((settings.allowEdit||settings.allowDelete)&&!ID.edit&&!ID.add){ID.edit=true;$("#jquery-notes_"+pointer+" .notes #n_"+pointer+"-"+id).addClass("select");var note=$("#jquery-notes_"+pointer+" .notes #t_"+pointer+"-"+id+" .txt").text();var link=$("#jquery-notes_"+pointer+" .notes #n_"+pointer+"-"+id+" .border .bg a").attr("href");var author=$("#jquery-notes_"+pointer+" .notes #t_"+pointer+"-"+id+" .author").text();author=author.substr(3,author.length);$("#jquery-notes_"+pointer+" .notes #t_"+pointer+"-"+id).remove();$("#jquery-notes_"+pointer+" .notes #n_"+pointer+"-"+id+" .border .bg a").remove();note=(note==link)?"":note;link=(link==null||link=="")?"http://":link;var html="";html+='<div class="text-box">';if(settings.allowEdit){html+=(settings.allowAuthor)?'<input type="text" name="author" value="'+author+'" /><br />':"";html+='<textarea name="note">'+note+"</textarea><br />";html+=(settings.allowLink)?'<input type="text" name="link" value="'+link+'" /><br />':"";html+='<a href="javascript:void(0);" class="edit-note" title="Redigera" />';}html+='<a href="javascript:void(0);" class="cancel-note" title="Avbryt" />';html+=(settings.allowDelete)?'<a href="javascript:void(0);" class="delete-note" title="Radera" />':"";html+="</div>";$("#jquery-notes_"+pointer+" .notes .select").append(html);_inputFocus(pointer);$("#jquery-notes_"+pointer+" .notes .select").draggable({containment:"parent",cursor:"move"}).resizable({containment:"parent",minWidth:ID.minWidth,minHeight:ID.minHeight,maxWidth:ID.maxWidth,maxHeight:ID.maxHeight,aspectRatio:ID.aspectRatio,handles:"ne, se, sw, nw"});$("#jquery-notes_"+pointer+" .notes .select .text-box .edit-note").click(function(){_saveNote(pointer,id,"edit");});$("#jquery-notes_"+pointer+" .notes .select .text-box .cancel-note").click(function(){_reload(pointer);});$("#jquery-notes_"+pointer+" .notes .select .text-box .delete-note").click(function(){_deleteNote(pointer,id);});}};var _hideNotes=function(pointer){_errorLog("hidenotes");if(!ID.add&&!ID.edit){if($("#jquery-notes_"+pointer+" .controller .hide-notes").hasClass("show-notes")){$("#jquery-notes_"+pointer+" .notes .note").css({visibility:"visible"});$("#jquery-notes_"+pointer+" .controller .hide-notes").removeClass("show-notes").attr({title:"Visa inte taggar"});}else{$("#jquery-notes_"+pointer+" .notes .note").css({visibility:"hidden"});$("#jquery-notes_"+pointer+" .controller .hide-notes").addClass("show-notes").attr({title:"Visa taggar"});}}};var _reload=function(pointer){_errorLog("reload");$("#jquery-notes_"+pointer+" .notes .note").remove();$("#jquery-notes_"+pointer+" .notes .text").remove();_getProperties(pointer);_getNotes(pointer);};var _abort=function(pointer){$("#jquery-notes_"+pointer+" .notes .select").remove();_getProperties(pointer);_focusOffNote(pointer);_devoteMod(pointer,false);};var _formatDate=function(date){var string=settings.dateFormat;string=string.replace("Y",date.Y);string=string.replace("M",date.M);string=string.replace("D",date.D);string=string.replace("H",date.H);string=string.replace("I",date.I);return string;};var _getNotePosition=function(pointer){_errorLog("tag-width: "+$("#jquery-notes_"+pointer+" .notes .select").css("width"));_errorLog("tag-left: "+$("#jquery-notes_"+pointer+" .notes .select").css("left"));return{left:_pixelToPercent(pointer,$("#jquery-notes_"+pointer+" .notes .select").css("left"),"left"),top:_pixelToPercent(pointer,$("#jquery-notes_"+pointer+" .notes .select").css("top"),"top"),width:_pixelToPercent(pointer,$("#jquery-notes_"+pointer+" .notes .select").css("width"),"width"),height:_pixelToPercent(pointer,$("#jquery-notes_"+pointer+" .notes .select").css("height"),"height")};};var _pixelToPercent=function(pointer,pixel,type){return pixel;_errorLog("PixelToPercent - pointer:"+pointer+" pixel:"+pixel+" type:"+type);pixel=parseInt(pixel.toString().replace("px",""));switch(type){case"left":case"width":var percent=(100/ID.width)*pixel;break;case"top":case"height":var percent=(100/ID.height)*pixel;break;}_errorLog("PixelTopercent Retval: "+percent);return percent;};var _percentToPixel=function(pointer,percent,type){return percent;_errorLog("PersontToPixel - pointer:"+pointer+" percent:"+percent+" type:"+type);percent=parseFloat(percent.toString().replace("%",""));switch(type){case"left":case"width":var pixel=(percent/100)*ID.width;break;case"top":case"height":var pixel=(percent/100)*ID.height;break;}_errorLog("PersontToPixel Retval: "+pixel);return pixel;};var _devoteMod=function(pointer,doBind){if(doBind){$("#jquery-notes_"+pointer).mouseenter(function(){if(ID.add==false&&ID.edit==false){$("[id^=t_"+pointer+"]").show().index(9990);}});$("#jquery-notes_"+pointer).mouseleave(function(){if(ID.add==false&&ID.edit==false){$("[id^=t_"+pointer+"]").hide().index(0);}});$("[id^=t_"+pointer+"]").each(function(intIndex,element){$(this).mouseenter(function(){id=element.id.replace("t_","");$("#n_"+id).css("display","block");});});$("[id^=t_"+pointer+"]").each(function(intIndex,element){$(this).mouseleave(function(){id=element.id.replace("t_","");$("#n_"+id).css("display","");});});}else{$("[id^=t_"+pointer+"]").show().index(9990);}};var _errorLog=function(msg){if((window.console)&&(settings.debugMode==true)){console.log(msg);}};$(this).each(function(){_initPlugin(this);});};})(jQuery);
