
function Wordlist(){var modified=false;var newList=true;var data={};var words=[];var hist={};data.shared=true;_this=this;this.load=function(id,includeRelated){data={};words=[];hist={};var p={'id':id};if(includeRelated!=null)p.includeRelated=includeRelated;var req=new JSONRequest('/wordlists/load.json',p,loaded,error);req.send();}
this.getURL=function(){if(newList)return null;return'/wordlists/'+data.wordlistid;}
this.setShared=function(shared){if(shared!=data.shared){data.shared=(shared==true);setModified();}}
this.setTitle=function(title){if(title!=data.name){data.name=title;setModified();}}
this.getTitle=function(){return data.name;}
this.setDescription=function(description){if(description!=data.description){data.description=description;setModified();}}
this.getDescription=function(){return data.description;}
this.setSource=function(source){if(source!=data.source){data.source=source;setModified();}}
this.getSize=function(){return words.length;}
this.getWord=function(idx){return words[idx];}
this.getProfile=function(){return data.profile;}
this.alphabetize=function(){words.sort(function(a,b){var al=a.word.toLowerCase();var bl=b.word.toLowerCase();if(al==bl){return(a.word<b.word)?-1:(a.word>b.word)?1:0;}else{return(al<bl)?-1:1;}});setModified();}
this.shuffle=function(){var o=words;for(var j,x,i=o.length;i;j=parseInt(Math.random()*i),x=o[--i],o[i]=o[j],o[j]=x);setModified();}
this.addWord=function(word,lang,description,idx){if(word){if(!lang)lang='en';var word={'word':word,'lang':lang};if(description)word.description=description;words[(idx==undefined)?words.length:Math.min(Math.max(0,idx),words.length)]=word;var key=word+':'+lang;if(!hist[key]){hist[key]=1;}else{hist[key]++;}
setModified();}}
this.removeWord=function(word,lang){if(!lang)lang='en';var key=word+':'+lang;if(hist[key]){hist[key]--;words.splice(this.indexOf(word,lang),1);setModified();}}
this.indexOf=function(word,lang){if(!lang)lang='en';for(var i=0;i<words.length;i++){if(words[i].word==word&&words[i].lang==lang){return i;}}
return-1;}
this.containsWord=function(word,lang){if(!lang)lang='en';return(hist[word+':'+lang]>0);}
this.save=function(){Event.fire(_this,'saving');var post={};post.wordlist={};var wl={};wl.name=data.name;wl.description=data.description;wl.shared=data.shared;wl.source=data.source;wl.words=[];for(var i=0;i<words.length;i++){wl.words[i]={'word':words[i].word,'lang':words[i].lang}
if(words[i].description)wl.words[i].description=words[i].description;}
post.wordlist=JSON.toJSON(wl);if(data.wordlistid){post.id=data.wordlistid;}
var req=new JSONRequest('/wordlists/save.json',post,function(result){data.wordlistid=result;newList=false;modified=false;Event.fire(_this,'save');},error);req.send();}
this.isModified=function(){return modified;}
function setModified(){modified=true;Event.fire(_this,'modified');}
function loaded(result){data=result;words=data.words;for(var i=0;i<words.length;i++){words[i].toString=wordToString;}
modified=false;newList=false;Event.fire(_this,'load');}
function error(e){var evt=new Event(_this,'error');evt.error=e;evt.fire();}
this.toString=function(){return words.join(', ');}
function wordToString(){return this.word;}}
Wordlist.SRC_UNKNOWN=0;Wordlist.SRC_VOCABGRABBER=1;