$.widget("ui.Backgrounds",{_create:function(){var i=this,e=this.element,t={};t[i18n("confirm_")]=function(){i.confirm()&&e.dialog("close")};t[i18n("cancel")]=function(){e.dialog("close")};e.dialog({width:550,height:400,minWidth:500,minHeight:400,buttons:t,modal:!0,closeText:i18n("close_")});e.append("<div class='ui-layout-west properties'><label class='resource-dialog-label'>"+i18n("name")+"</label><input class='resource-dialog-input name ui-widget-content ui-corner-all'/><fieldset class='resource-dialog-fieldset fill'><legend>"+i18n("fill")+"</legend><button class='dialogButton select-image'>"+i18n("select_image")+"</button><label class='resource-dialog-label'>"+i18n("or")+"</label><input class='resource-dialog-input ui-widget-content ui-corner-all select-color'/></fieldset></div><div class='ui-layout-center preview inner-edit-area'><div class='show-preview'></div></div>").layout({west__size:250,west__spacing_open:0,closable:!1,resiable:!1});e.find(".properties").css({"z-index":1,overflow:"visible"});e.find(".name").keyup(function(e){if(!e.ctrlKey){var t=$.utils.validInput($(this),"validId");void 0!==t&&i.onchange("name",{name:t})}});e.find(".select-image").button().click(function(){i.selectImageDialog()});e.find(".select-color").minicolors({change:function(e){i.onchange("fill",{fill:e})}});e.find(".preview").dblclick(function(){i.selectImageDialog()}).on("contextmenu",function(e){var t=$.utils.getSelectMenu(["select_image"],function(){i.selectImageDialog();return!0});t&&t.show().position({my:"left top",of:e})})},updateBackground:function(i,e,t){delete this.imagesSize;this.callback=e;this.json=i;this.name(t);this.fill(i.fill||"",!0);i.imgSize&&this.setImagesSize(i.imgSize);this.updatePreview();this.addHistory();this.history.clear()},onchange:function(i,e){var t=this,l=this[i]&&this[i](),n=e[i];if(l!==n){this[i]&&this[i](n);this.addHistory({oldValue:l,newValue:n,undo:function(e){t[i]&&t[i](e)},redo:function(e,l){t[i]&&t[i](l)}})}},addHistory:function(){$.utils.addHistory.apply(this,arguments)},fill:function(i,e){if(void 0===i)return this.element.data("fill");var t=this.element.find(".select-color");$.utils.isColor(i)?t.minicolors("value")!==i&&t.minicolors("value",i):t.minicolors("value",null);this.element.data("fill",i);e||this.updatePreview()},name:function(i){if(void 0===i)return this.element.data("name");this.element.data("name",i);var e=this.element.find(".name");e.val()!==i&&e.val(i)},updatePreview:function(){var i=this.fill(),e=this.element.find(".show-preview"),t=e.parent();i?this.getImageSize(function(l){var n={width:t.width(),height:t.height()},s=Math.min(n.width/l.width,n.height/l.height);$.utils.setIconFill(e,{size:l,imgSize:l,fill:i},{width:l.width*s,height:l.height*s})}):e.css({background:""})},selectImageDialog:function(){var i=this;$.utils.selectImage({fileType:"image",multiple:!1,callback:function(e){i.onchange("fill",{fill:e});return!0}})},confirm:function(){var i=this,e=this.callback,t=this.name();t?this.fill()?this.getImageSize(function(l){var n={fill:i.fill(),size:l,anchorPoint:{x:0,y:0}};e&&e(n,t)&&i.element.dialog("close")}):$.utils.alert(i18n("please_select_a_",i18n("image")+i18n("or_color"))):$.utils.alert(i18n("no_name"))},getImageSize:function(i){var e=this;this.imagesSize||(this.imagesSize={});var t=this.fill();if($.utils.isColor(t))i&&i({width:32,height:32});else{if(this.imagesSize[t]){i&&i(this.imagesSize[t]);return this.imagesSize[t]}$.utils.getImageSize(t,function(l){e.imagesSize[t]=l;i&&i(e.imagesSize[t])})}},setImagesSize:function(i){this.imagesSize||(this.imagesSize={});var e=this.fill();$.utils.isColor(e)||(this.imagesSize[e]=i)}});