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

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