
var TextCycler=new Class({Implements:Options,options:{runOnce:false,randomize:9000,pause:9000,waitForAll:false},words:new Array(),animated:0,initialize:function(container,options){this.element=$(container);if($(this)===null)return null;this.setOptions(options);$(this).getElements('li').each(function(li){this.words.include(new TextCycler.Words(li,this,this.options));}.bind(this));this.cycleWords(true);},done:function(words){if(!this.options.waitForAll){words.cycle.delay(this.options.pause,words);}else{this.running--;if(this.running<=0)this.cycleWords.delay(this.options.pause,this);}},cycleWords:function(force){if(!this.options.waitForAll&&force!==true)return;if(this.options.runOnce&&force!==true)return;this.running=this.words.length;if(this.options.randomize!==0){this.words.each(function(words){words.cycle.delay(Math.floor(Math.random()*this.options.randomize),words);}.bind(this));}else{this.words.each(function(words){words.cycle();});}},toElement:function(){return this.element;}});TextCycler.Words=new Class({Implements:Options,element:null,options:{capitalize:true,wrap:true,speed:100,update:2,letters:' abcdefghijklmnopqrstuvwxyz'},letterString:null,words:null,currentWord:0,done:true,initialize:function(container,parent,options){this.element=$(container);this.parent=parent;this.setOptions(options);this.letterString=this.options.letters;this.words=this.element.get('id').split('|');if(this.words.length<=0){this.words=null;return null;}else{this.setText(this.words[0]);}
if(!this.options.capitalize){this.capitalize=this.doNotCapitalize;}},cycle:function(){this.done=false;this.currentWord++;this.currentWord=this.currentWord%this.words.length;var current=this.getText();var nextWord=this.words[this.currentWord];if(nextWord.length!==current.length){if(nextWord.length<current.length){for(i=0;i<(current.length-nextWord.length);i++)nextWord+=" ";}else{for(i=0;i<(nextWord.length-current.length);i++)current+=" ";this.setText(current);}}
this.cycleTo(nextWord);},cycleTo:function(finalText){if(this.done){this.parent.done(this);return;}
var nextWord=this.getNextWord(this.getText(),finalText);if(nextWord==finalText){this.done=true;}
this.setText(nextWord);this.cycleTo.delay(this.options.speed,this,[finalText]);},removeBlock:function(){$(this).removeClass('block');},getNextWord:function(currentWord,nextWord){var result="";if(this.options.update===0){for(i=0;i<currentWord.length;i++){var nextLetter=currentWord.charAt(i).toLowerCase();var targetLetter=nextWord.charAt(i).toLowerCase();if(nextLetter!==targetLetter){var nextIndex=(this.letterString.indexOf(nextLetter)+1)%this.letterString.length;nextLetter=this.letterString.charAt(nextIndex);}else if(nextLetter===targetLetter){nextLetter=nextWord.charAt(i);}
result+=nextLetter;}}else{var updates=1;for(i=0;i<currentWord.length;i++){var nextLetter=currentWord.charAt(i).toLowerCase();var targetLetter=nextWord.charAt(i).toLowerCase();if((nextLetter!==targetLetter)&&updates<=this.options.update){updates++;var nextIndex=(this.letterString.indexOf(nextLetter)+1)%this.letterString.length;nextLetter=this.letterString.charAt(nextIndex);}else if(nextLetter===targetLetter){nextLetter=nextWord.charAt(i);}
result+=nextLetter;}}
return result;},getText:function(){var text=$(this).get('html');if(this.options.wrap){text=text.replace(/\<span\>/g,"");text=text.replace(/\<\/span\>/g,"");}
text=text.replace(/&nbsp;/g," ");return text.toLowerCase();},setText:function(newText){newText=this.capitalize(newText);text="";if(this.options.wrap){for(i=0;i<newText.length;i++){text+="<span>"+newText.charAt(i)+"</span>";}}else{text=newText;}
text=text.replace(/\s/g,"&nbsp;");$(this).set('html',text);},capitalize:function(newText){var text=newText.split(" ");newText="";for(i=0;i<text.length;i++){var word=text[i];newText+=word.charAt(0).toUpperCase()+word.substring(1,word.length);if(i<text.length-1){newText+=" ";}}
return newText;},doNotCapitalize:function(text){return text;},toElement:function(){return this.element;}});
