
lang.lists={'es':{title:'¿Estas seguro?',msg:'Este proceso no puede ser revertido.',reordering:'Actualizando orden...'},'en':{title:'Are you sure?',msg:'This process cannot be reversed.',reordering:'Updating order...'}};listManagers={};ListManager=Class.create();ListManager.prototype={initialize:function(id,app,options){this.id=id;this.self=$(this.id);this.setOptions(options);this.injectBehaviour();if(this.options.updater){this.updater=updaters[this.options.updater];this.list_update_cmd='';}else{if(!updaters[this.id+'_update']){updaters[this.id+'_update']=new Updater(this.id+'_update',app,'DeleteListItem');}
this.updater=updaters[this.id+'_update'];}},setOptions:function(options){this.options={updateHeader:false,additional_params:null}.extend(options||{});},deleteSync:function(id){var callParams=this.options.additional_params||[];callParams.push('item='+id);this.updater.run(callParams,'DeleteListItem','');},ajaxUpdate:function(ajaxResponse){switch(ajaxResponse.getAttribute('id')){case this.id+'_delete':break;}},injectBehaviour:function(){var items=this.self.getElementsByTagName('li');if(items.length){for(var i=0;i<items.length;++i){var itm=items[i];var del=document.createElement('div');del.className="deletethisflat";del.onmouseover=this.overHandler.bindAsEventListener(this,del);del.onmouseover=this.overHandler.bindAsEventListener(this,del);del.onclick=this.confirmDelete.bindAsEventListener(this,itm);itm.insertBefore(del,itm.firstChild);}}},overHandler:function(e,div){div.style.cursor='pointer';},outHandler:function(e,div){div.style.cursor='';},confirmDelete:function(e,itm){var me=this;alert_win.show({id:'ListManager',title:lang.lists[cur_lang].title,msg:lang.lists[cur_lang].msg,oncancel:function(){screen_overlay.hide();},onconfirm:function(){screen_overlay.hide();me.deleteItem(itm);}});},deleteAll:function(){var items=this.self.getElementsByTagName('li');var params=['deleteAll=1'];if(items.length){for(var i=0;i<items.length;++i){items[i].style.display='none';params.push('items[]='+items[i].id);}}
this.updater.run(params,'DeleteListItem','');},deleteItem:function(itm){if(console)console.log(itm.id);itm.style.display='none';if(this.options.updateHeader){var items=this.self.getElementsByTagName('li');var count=items.length;var h2=this.self.parentNode.getElementsByTagName('h2')[0];if(h2){var contents=h2.innerHTML;h2.innerHTML=contents.replace(/\([^\)]+\)/gi,"("+count+")");}
var h3=this.self.parentNode.getElementsByTagName('h3')[0];if(h3){var contents=h3.innerHTML;h3.innerHTML=contents.replace(/\([^\)]+\)/gi,"("+count+")");}
if(!count){var divs=this.self.parentNode.getElementsByTagName('div');for(var i=0;i<divs.length;++i){var d=divs[i];if(d.className=='list_headers'){d.style.display='none';}}
this.self.style.display='none';}}
this.deleteSync(itm.id);},injectDragHandles:function(list){for(var i=0;i<list.childNodes.length;++i){var node=list.childNodes[i];if(console)console.log('NODE NAME: '+node.nodeName);if(node.nodeName=='LI'){var div=document.createElement('div');div.className='dragthis';div.style.padding="0 5px 0 0";node.insertBefore(div,node.firstChild);}}},removeDragHandles:function(list){for(var i=0;i<list.childNodes.length;++i){var node=list.childNodes[i];if(node.firstChild.className=='dragthis'){node.removeChild(node.firstChild);}}},makeSortable:function(switch_label,element,cmd){if(switch_label){$(switch_label+'_reorder_on').style.display='';$(switch_label+'_reorder_off').style.display='none';}
var id=(element)?element:this.id;if(cmd){this.list_update_cmd=cmd;}
if(console)console.log('id : '+id);Sortable.create(id,{dropOnEmpty:true,containment:[id],handle:'dragthis',onUpdate:this.updateOrder.bind(this)});this.injectDragHandles($(id));},disableSorting:function(switch_label,element){var id=(element)?element:this.id;this.removeDragHandles($(id));Sortable.destroy(id);if(switch_label){$(switch_label+'_reorder_on').style.display='none';$(switch_label+'_reorder_off').style.display='';}},updateOrder:function(id){if(this.updater&&this.list_update_cmd){var str=Sortable.serialize(id,{name:'order_index'});var callParams=str.split('&');callParams.push('list_id='+this.id);if(this.options.additional_params){for(var i=0;i<this.options.additional_params.length;++i){callParams.push(this.options.additional_params[i]);}}
this.updater.run(callParams,this.list_update_cmd,lang.lists[cur_lang].reordering);}}};
