/* Copyright (C) YOOtheme GmbH, http://www.gnu.org/licenses/gpl.html GNU/GPL */

!function(e,a,t){e(function(){var a=e("#config"),n=Joomla.submitbutton;e(".nav-tabs").on("click","[data-toggle]",function(){t._tab=e(this).attr("href")}),setTimeout(function(){t._tab&&e(".nav-tabs [href='"+t._tab+"']").trigger("click"),e("[data-layout-selector]").val(t._profile||"default").on("change",function(){t._profile=this.value}).trigger("change")},1),Joomla.submitbutton=function(a){"style.save"==a?o.save("save",a):"style.save2copy"==a?o.save("save-copy",a):"style.apply"==a?o.save("apply",a):("style.cancel"==a&&e("#config").remove(),n(a))},e("#templatestyleOptions").length?a.closest("#templatestyleOptions").before(a).remove():e("#attrib-options").prepend(a).find(".control-group").hide(),setTimeout(function(){a.find(".chzn-container").each(function(){e(this).prev().show().end().remove()})},200)});var o={save:function(a,t){var o={},n=e('form[name="adminForm"]'),s=n.find("#jform_template").val(),r=e("#toolbar-"+a);r.addClass("saving"),parse_str(e(":input","#config").serialize(),o),e.ajax({url:n.attr("action"),type:"post",data:{option:"com_templates",template:s,callback:"config",tmpl:"warp-ajax",config:JSON.stringify(o,null,"	")},success:function(o){r.removeClass("saving");try{if(o=e.parseJSON(o),"success"!=o.message)return void alert("Save failed!")}catch(s){}a.match(/(apply|save)$/)&&e("#config").remove(),Joomla.submitform(t,n[0])}})},saveFiles:function(a){var t=e.Deferred(),o=e('form[name="adminForm"]'),n=o.find("#jform_template").val(),s=new FormData;return s.append("option","com_templates"),s.append("template",n),s.append("callback","files"),s.append("tmpl","warp-ajax"),s.append("no_html",1),s.append("files",new Blob([window.btoa(unescape(encodeURIComponent(JSON.stringify(a))))],{type:"text/plain"})),e.ajax({url:o.attr("action"),type:"POST",data:s,processData:!1,contentType:!1}).always(function(a,o,n){var s=!1;if("error"==o)s=n;else try{a=e.parseJSON(a),"success"!=a.message&&(s=a.message)}catch(r){s="Saving failed!"}t[s?"reject":"resolve"](s)}),t.promise()},data:function(){var a=e.Deferred(),t=e('form[name="adminForm"]'),o=t.find("#jform_template").val();return e.ajax({url:t.attr("action"),type:"post",data:{option:"com_templates",template:o,callback:"styles",tmpl:"warp-ajax",no_html:1}}).always(function(t,o,n){var s=!1;if("error"==o)s=n;else try{if(t=e.parseJSON(t),"error"!=t.message)return void a.resolve(t);s=t.message}catch(r){s="Retrieving styles data failed!"}a.reject(s)}),a.promise()}};a.System=o}(jQuery,this,window.sessionStorage||{});