/* Minified: jquery.manageCategories.js */
jQuery.fn.manageCategories=function(s){var settings=jQuery.extend({update:null},s);return this.flyout({width:"narrow",success:function(){var wrapper=$(this);var table=wrapper.find("table");var empty=wrapper.find("p.empty_page");var get_categories_map=function(){var categories=new App.Map();table.find("tbody tr.category").each(function(){var row=$(this);categories.set(parseInt(row.attr("category_id")),row.find("td.name").text())});return categories};var refresh=function(s){var refresh_settings=jQuery.extend({init:false,sort:false,notify:false},s);if(table.find("tbody tr.category").length>0){if(refresh_settings.init){var counter=1;table.find("tbody tr.category").each(function(){var row=$(this);if(counter%2){row.addClass("odd")}else{row.addClass("even")}init_row(row)})}empty.hide();if(refresh_settings.sort){table.SortBySelector({row_selector:"tr.category",value_selector:"td.name"}).show()}else{table.show()}}else{table.hide();empty.show()}if(refresh_settings.notify){App.Wireframe.Events.trigger("categories_updated",[{context:settings.context,type:settings.type,map:get_categories_map()}]);if(typeof(settings.update)=="function"){settings.update(get_categories_map())}}};var init_row=function(row){var category_id=parseInt(row.attr("category_id"));row.find("a.rename_category").click(function(){var link=$(this);var old_category_name=jQuery.trim(row.find("td.name").text());var new_category_name=jQuery.trim(prompt(App.lang("Please insert new category name"),old_category_name));if(new_category_name&&new_category_name!=old_category_name){var indicator=$('<img src="'+App.Wireframe.Utils.indicatorUrl()+'">');link.hide().after(indicator);$.ajax({type:"post",url:App.extendUrl(link.attr("href"),{async:1}),data:{"category[name]":new_category_name,submitted:"submitted"},success:function(category){row.find("td.name").text(category.name);indicator.remove();link.show();refresh({sort:true,notify:true});row.highlightFade();if(settings.category_updated){App.Wireframe.Events.trigger(settings.category_updated,[category,get_categories_map()])}},error:function(){indicator.remove();link.show()}})}return false});row.find("a.delete_category").asyncLink({confirmation:App.lang("Are you sure that you want to delete selected category?"),success_event:"category_deleted",success_message:App.lang("Selected category has been deleted successfully"),success:function(){row.remove();if(settings.category_deleted){App.Wireframe.Events.trigger(settings.category_deleted,[category,get_categories_map()])}refresh({notify:true})}})};refresh({init:true});wrapper.find("a.new_category").click(function(){var link=$(this);var new_category_name=jQuery.trim(prompt(App.lang("Please insert new category name")));if(new_category_name){var already_exists=false;table.find("tbody tr.category").each(function(){var row=$(this);if(jQuery.trim(row.find("td.name").text())==new_category_name){already_exists=true;row.highlightFade();return false}});if(!already_exists){var indicator=$('<img src="'+App.Wireframe.Utils.indicatorUrl()+'">');link.hide().after(indicator);$.ajax({type:"post",url:App.extendUrl(link.attr("href"),{async:1}),data:{"category[name]":new_category_name,submitted:"submitted"},success:function(category){var row=$('<tr class="category" category_id="'+category.id+'"><td class="name">'+category.name.clean()+'</td><td class="options"><a href="'+category.urls["edit"]+'" class="rename_category" title="'+App.lang("Rename Category")+'"><img src="'+App.Wireframe.Utils.imageUrl("/icons/12x12/edit.png","environment")+'"></a><a href="'+category.urls["delete"]+'" class="delete_category" title="'+App.lang("Delete Category")+'"><img src="'+App.Wireframe.Utils.imageUrl("/icons/12x12/delete.png","environment")+'"></a></td></tr>');table.find("tbody").append(row);init_row(row);indicator.remove();link.show();refresh({sort:true,notify:true});row.highlightFade();if(settings.category_created){App.Wireframe.Events.trigger(settings.category_created,[category,get_categories_map()])}},error:function(){indicator.remove();link.show()}})}}return false})}})};
