
var ToDoList=new Class({Extends:Widget,initialize:function(){this.cfg={hasRefreshBtn:false,title_prefix:loc.text("todolist_title_prefix"),title:"",module:"ToDoList"}
this.elements={};this.tags={};this.defaultProfile["items"]=[];this.defaultProfile["title"]="";this.defaultProfile["bgcolor"]="orig";},onBuildInterface:function(){this.settings="<form id='f"+this.id+"' onsubmit='return false'>"+"<div class='settings_section'>"+" <span class='settings_label'>"+loc.text("inp_title")+"</span>"+" <input name='title' type='text' size='15'>"+"</div>"+"</form>"+this.changeColors;this.setSettings(this.settings);var self=this;var frm=$('f'+this.id);this.tags['title']=$(frm.elements[0]);this.tags['title'].addEvent('keydown',function(e){if(e.key=='enter'){self.settingsAll();}});this.colorsAction();this.setBgColor(this.profile.bgcolor,1);this.renderItems();},onOpen:function(){this.setTitle(this.profile.title);this.tags['title'].value=this.profile["title"];},onClose:function(){board.todoListSort.removeLists(this.mid.getElement('div[id=todos_'+this.id+']'));for(var o in board.todoListId){if(board.todoListId[o]==this.id){board.todoListId.splice(o,1);break;}}},setTitle:function(h){if(!h)h=this.tags['title'].value;this.mid.getElement('span.box-title').innerHTML=this.cfg.title_prefix+h;},renderItems:function(){var self=this;if(!ident.isPublic()||(ident.isPublic()&&ident.isPublicLogged())){board.todoListSort.removeLists(this.mid.getElement('div[id=todos_'+this.id+']'));for(var o in board.todoListId){if(board.todoListId[o]==this.id){board.todoListId.splice(o,1);break;}}}
this.setContent('');var todoList=new Element('div',{'id':'todos_'+this.id,'styles':{'min-height':'20px'}});todoList.inject(this.mid.getElement('div.box-content'));var todoP=new Element('p',{'styles':{'margin-top':'10px'}});todoP.inject(this.mid.getElement('div.box-content'));if(!ident.isPublic()||(ident.isPublic()&&ident.isPublicLogged())){var todoAddNew=new Element('a',{'href':'javascript:void(0);','html':loc.text("todolist_addnew_todo"),'events':{'click':function(){self.addNew()}}});todoAddNew.inject(todoP);}
for(var i=0;i<this.profile.items.length;i++){this.renderItem(i);}
if(!ident.isPublic()||(ident.isPublic()&&ident.isPublicLogged())){board.todoListSort.addLists(this.mid.getElement('div[id=todos_'+this.id+']'));board.todoListId[board.todoListId.length]=this.id;}},renderItem:function(n,needSort){var item,row,box,self=this;row=new Element('div',{'id':'todo_'+this.id+'_'+n,'class':'sort_list','styles':{'clear':'both'}});if(this.profile.items[n].bgcolor)
row.addClass(this.profile.items[n].bgcolor);if(!ident.isPublic()||(ident.isPublic()&&ident.isPublicLogged())){item=new Element('div',{'class':'left'});box=new Element('input',{'id':'todo_'+this.id+'_box_'+n,'type':'checkbox','checked':this.profile.items[n].completed,'events':{'click':function(){self.switchItem(n)}}});box.inject(item);item.inject(row);}
item=new Element('div',{'class':'left','html':'<div id="todo_'+this.id+'_title_'+n+'" style="margin-left:5px;text-decoration:'+(this.profile.items[n].completed?"line-through":"")+'">'+this.profile.items[n].title+'</div>'});if(!ident.isPublic()||(ident.isPublic()&&ident.isPublicLogged())){item.addEvent('click',function(){self.editItem(n);});}
item.inject(row);if(!ident.isPublic()||(ident.isPublic()&&ident.isPublicLogged())){item=new Element('div',{'class':'left'});box=new Element('input',{'id':'todo_'+this.id+'_title_'+n+'_input','type':'text','class':'inp','styles':{'display':'none'}});box.inject(item);item.inject(row);}
if(!ident.isPublic()||(ident.isPublic()&&ident.isPublicLogged())){item=new Element('div',{'class':'right'});box=new Element('img',{'src':'pix/edit.gif','styles':{'margin-right':'5px'},'events':{'click':function(){self.editItem(n)}}});box.inject(item);box=new Element('img',{'src':'pix/colors.png','styles':{'margin-right':'5px'},'events':{'click':function(e){self.colorizeItem(e,n)}}});box.inject(item);box=new Element('img',{'src':'pix/delete_link.gif','styles':{'margin-right':'5px'},'events':{'click':function(){self.deleteItem(n)}}});box.inject(item);item.inject(row);}
row.inject(this.mid.getElement('div[id=todos_'+this.id+']'));if(needSort){if(!ident.isPublic()||(ident.isPublic()&&ident.isPublicLogged())){board.todoListSort.removeLists(this.mid.getElement('div[id=todos_'+this.id+']'));for(var o in board.todoListId){if(board.todoListId[o]==this.id){board.todoListId.splice(o,1);break;}}
board.todoListSort.addLists(this.mid.getElement('div[id=todos_'+this.id+']'));board.todoListId[board.todoListId.length]=this.id;}}},settingsAll:function(){this.setTitle();if(this.tags['title'].value!=this.profile["title"]){this.profile["title"]=this.tags['title'].value;this.save();}
return;},addNew:function(){var newId=this.profile.items.length;var newItem={title:loc.text("todolist_addnew_todo")+" "+(newId+1),completed:false,bgcolor:'cwhite'};this.profile.items[newId]=newItem;this.renderItem(newId,true);this.save();this.editItem(newId);},switchItem:function(id){this.profile.items[id].completed=!this.profile.items[id].completed;$('todo_'+this.id+'_title_'+id).style.textDecoration=this.profile.items[id].completed?"line-through":"";this.save();},editItem:function(id){var inp,self=this;if(this.mid.getElement('div.colorPicker'))
this.mid.getElement('div.colorPicker').destroy();$('todo_'+this.id+'_title_'+id).style.display='none';$('todo_'+this.id+'_title_'+id).removeEvents();$('todo_'+this.id+'_'+id).removeEvents();inp=$('todo_'+this.id+'_title_'+id+'_input');inp.value=this.profile.items[id].title;inp.style.display='inline';inp.focus();inp.select();inp.addEvent('keydown',function(e){if(e.key=='enter'){self.inpblur(id);}});inp.addEvent('blur',function(e){self.inpblur(id);});},inpblur:function(id){var todoTitle=$('todo_'+this.id+'_title_'+id);var inp=$('todo_'+this.id+'_title_'+id+'_input');var text=trim(inp.value.replace(/\\/g,""));inp.style.display='none';todoTitle.innerHTML=text2html(text);todoTitle.style.display='inline';todoTitle.addEvent('click',function(){self.editItem(id);});if(text!=this.profile.items[id].title){this.profile.items[id].title=text.jsEscape();this.save();}
this.renderItems();},colorizeItem:function(e,id){var self=this;if(this.mid.getElement('div.colorPicker'))
this.mid.getElement('div.colorPicker').destroy();var colorPicker=new Element('div',{'class':'colorPicker'});var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cred')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(216,0,0)'},'events':{'click':function(){self.changeBgColor(id,'cred')}}});color.inject(span);var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cyellow')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(255,222,0)'},'events':{'click':function(){self.changeBgColor(id,'cyellow')}}});color.inject(span);var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cgreen')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(105,171,0)'},'events':{'click':function(){self.changeBgColor(id,'cgreen')}}});color.inject(span);var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cblue')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(70,192,237)'},'events':{'click':function(){self.changeBgColor(id,'cblue')}}});color.inject(span);var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cpurple')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(137,79,202)'},'events':{'click':function(){self.changeBgColor(id,'cpurple')}}});color.inject(span);var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cmagenta')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(234,0,226)'},'events':{'click':function(){self.changeBgColor(id,'cmagenta')}}});color.inject(span);var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cblack')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(40,40,40)'},'events':{'click':function(){self.changeBgColor(id,'cblack')}}});color.inject(span);var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cgray')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(193,193,193)'},'events':{'click':function(){self.changeBgColor(id,'cgray')}}});color.inject(span);var span=new Element('span');if(this.profile.items[id].bgcolor&&this.profile.items[id].bgcolor=='cwhite')
span.addClass('selected');span.inject(colorPicker);var color=new Element('a',{'href':'javascript:void(0);','styles':{'background-color':'rgb(238,238,238)'},'events':{'click':function(){self.changeBgColor(id,'cwhite')}}});color.inject(span);colorPicker.injectAfter($('todo_'+this.id+'_title_'+id+'_input').parentNode);colorPicker.style.top=e.page.y+10+'px';colorPicker.style.left=(e.page.x-10)+'px';},deleteItem:function(id){if(confirm(loc.text("todolist_delete_confirm",this.profile.items[id].title))){var tmp=[];for(var i=0;i<this.profile.items.length;i++){if(i!=id){tmp.push(this.profile.items[i]);}}
this.profile.items=tmp;tmp=null;this.renderItems();this.save();}},changeBgColor:function(id,color){$('todo_'+this.id+'_'+id).removeProperty('class');$('todo_'+this.id+'_'+id).addClass('sort_list');$('todo_'+this.id+'_'+id).addClass(color);this.mid.getElement('div.colorPicker').destroy();this.profile.items[id].bgcolor=color;this.save();},swapItems:function(a,b){var tmp=this.profile.items[a];this.profile.items[a]=this.profile.items[b];this.profile.items[b]=tmp;}});