(function(a){a.extend(a.fn,{tag:function(b){var c={minWidth:100,minHeight:100,defaultWidth:100,defaultHeight:100,canDelete:true,maxHeight:null,maxWidth:null,save:null,remove:null,autoShowDrag:false,autoComplete:null,defaultTags:null,opacity:0.4,clickToTag:true};
var b=a.extend(c,b);return this.each(function(){var d=a(this);
d.data("options",b);
d.wrap('<div class="jTagArea" />');
a(this).parent().width(d.width());
a(this).parent().height(d.height());
if(b.autoShowDrag){d.showDrag()}a(".jTagTag").live("hover",function(){if(a(".jTagDrag").length===0){a(this).css({opacity:1});
d.parent().unbind("mousedown")}});
a(".jTagTag").live("mouseleave",function(){if(a(".jTagDrag").length===0){a(this).css({opacity:0});d.enableClickToTag()}});if(b.defaultTags){a.each(b.defaultTags,function(e,f){d.addTag(f.width,f.height,f.top,f.left,f.label,f.id)})}d.enableClickToTag()})},hideDrag:function(){obj=a(this);var b=obj.data("options");obj.css({opacity:1});obj.siblings(".jTagDrag").remove();a(".jTagTag",obj).show();obj.enableClickToTag()},showDrag:function(d){obj=a(this);obj.parent().unbind("mousedown");var c=obj.data("options");var b=function(){border=parseInt(a(".jTagDrag").css("borderTopWidth"));left=parseInt(a(".jTagDrag").attr("offsetLeft"))+border;top=parseInt(a(".jTagDrag").attr("offsetTop"))+border;return"-"+left+"px -"+top+"px"};if(obj.siblings(".jTagDrag").length==1){return}obj.css({opacity:c.opacity});a('<div class="jTagDrag"><div class="jTagSave"><div class="jTagInput"><input type="text" id="jTagLabel"></div><div class="jTagSaveClose"></div><div class="jTagSaveBtn"></div><div style="clear:both"></div></div>').insertAfter(obj);a(".jTagDrag").css("backgroundImage","url("+obj.attr("src")+")");if(d){x=d.pageX-obj.attr("offsetLeft");y=d.pageY-obj.attr("offsetTop");if(x+a(".jTagDrag").width()>obj.parent().width()){x=obj.parent().width()-a(".jTagDrag").width()-2}if(y+a(".jTagDrag").height()>obj.parent().height()){y=obj.parent().height()-a(".jTagDrag").height()-2}a(".jTagDrag").css("top",y);a(".jTagDrag").css("left",x);a(".jTagDrag").css({backgroundPosition:b()})}if(c.autoComplete){a("#jTagLabel").autocomplete({source:c.autoComplete})}a(".jTagSaveBtn").click(function(){label=a("#jTagLabel").val();if(label==""){alert("The label cannot be empty");return}height=a(this).parent().parent().height();width=a(this).parent().parent().width();top=a(this).parent().parent().attr("offsetTop");left=a(this).parent().parent().attr("offsetLeft");tagobj=obj.addTag(width,height,top,left,label);if(c.save){c.save(width,height,top,left,label,tag)}});a(".jTagSaveClose").click(function(){obj.hideDrag()});a(".jTagDrag").resizable({containment:"parent",minWidth:c.minWidth,minHeight:c.minHeight,maxWidht:c.maxWidth,maxHeight:c.maxHeight,resize:function(){a(".jTagDrag").css({backgroundPosition:b()})},stop:function(){a(".jTagDrag").css({backgroundPosition:b()})}});a(".jTagDrag").draggable({containment:"parent",drag:function(){a(".jTagDrag").css({backgroundPosition:b()})},stop:function(){a(".jTagDrag").css({backgroundPosition:b()})}})},addTag:function(d,e,g,f,b,h){obj=a(this);obj.css({opacity:1});var c=obj.data("options");tag=a('<div class="jTagTag" id="jTagTag'+$(".jTagTag").length+'" style="width:'+d+"px;height:"+e+"px;top:"+g+"px;left:"+f+'px;"><span>'+b+"</span></div>").insertAfter(obj);if(h){tag.setId(h)}if(c.canDelete){obj.parent().find(".jTagDeleteTag").show()}obj.hideDrag();return tag},setId:function(b){if(a(this).hasClass("jTagTag")){a(this).data("tagid",b)}else{alert("Wrong object")}},getId:function(b){if(a(this).hasClass("jTagTag")){return a(this).data("tagid")}else{alert("Wrong object")}},enableClickToTag:function(){obj=a(this);var b=obj.data("options");if(b.clickToTag){obj.parent().mousedown(function(c){obj.showDrag(c);obj.parent().unbind("mousedown")})}}})})(jQuery);
