/**
 * This file is part of the smilText parser implemented in JavaScript,
 *
 * Copyright (C) 2003-2009 Stichting CWI, 
 * Science Park 123, 1098 XG Amsterdam, The Netherlands.
 *
 * smilText parser in JavaScript is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * smilText parser in JavaScript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with smilText parser in JavaScript; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

Namespace("cwi.smilText");Import("cwi.adt.Hashtable");Import("cwi.adt.PriorityQueue");Import("cwi.adt.DoubleLinkedList");Import("cwi.util");Import("cwi.smilText.Time.Playable");cwi.smilText.STDocument=function(b){var a=JSINER.extend(this,"Playable");this.layout=b;this.stylingHash=new Hashtable();this.translateFunc=null;this.dictionary=null;this.hasFillSemantics=true;this.timingQueue=new PriorityQueue();this.doneQueue=new PriorityQueue();return a};cwi.smilText.STDocument.prototype.getStoredAttribute=function(b,a){return this.stylingHash.get(b+a)};cwi.smilText.STDocument.prototype.updateAttribute=function(b,a,c){this.stylingHash.put(b+a,c)};cwi.smilText.STDocument.prototype.applyStyle=function(b,c,a){switch(b){case"smiltext":this.updateAttribute(c,"dur",this.getStoredAttribute(a,"dur"));this.updateAttribute(c,"width",this.getStoredAttribute(a,"width"));this.updateAttribute(c,"height",this.getStoredAttribute(a,"height"));case"textstyle":this.updateAttribute(c,"textmode",this.getStoredAttribute(a,"textmode"));this.updateAttribute(c,"textplace",this.getStoredAttribute(a,"textplace"));this.updateAttribute(c,"textconceal",this.getStoredAttribute(a,"textconceal"));this.updateAttribute(c,"textrate",this.getStoredAttribute(a,"textrate"));case"div":this.updateAttribute(c,"textalign",this.getStoredAttribute(a,"textalign"));case"p":this.updateAttribute(c,"xml:space",this.getStoredAttribute(a,"xml:space"));this.updateAttribute(c,"textwrapoption",this.getStoredAttribute(a,"textwrapoption"));this.updateAttribute(c,"textwritingmode",this.getStoredAttribute(a,"textwritingmode"));case"span":this.updateAttribute(c,"textbackgroundcolor",this.getStoredAttribute(a,"textbackgroundcolor"));this.updateAttribute(c,"textcolor",this.getStoredAttribute(a,"textcolor"));this.updateAttribute(c,"textfontfamily",this.getStoredAttribute(a,"textfontfamily"));this.updateAttribute(c,"textfontsize",this.getStoredAttribute(a,"textfontsize"));this.updateAttribute(c,"textfontstyle",this.getStoredAttribute(a,"textfontstyle"));this.updateAttribute(c,"textfontweight",this.getStoredAttribute(a,"textfontweight"));this.updateAttribute(c,"textstyle",this.getStoredAttribute(a,"textstyle"));if(b!="span"&&b!="textstyle"){this.updateAttribute(c,"textdirection",this.getStoredAttribute(a,"textdirection"))}}};cwi.smilText.STDocument.prototype.getStyleXML=function(a,c){var b="";switch(a){case"smiltext":if(this.getStoredAttribute(c,"dur")){b+=" dur='"+this.getStoredAttribute(c,"dur")+"'"}if(this.getStoredAttribute(c,"width")){b+=" width='"+this.getStoredAttribute(c,"width")+"px'"}if(this.getStoredAttribute(c,"height")){b+=" height='"+this.getStoredAttribute(c,"height")+"px'"}case"textstyle":if(this.getStoredAttribute(c,"textmode")){b+=" textMode='"+this.getStoredAttribute(c,"textmode")+"'"}if(this.getStoredAttribute(c,"textplace")){b+=" textPlace='"+this.getStoredAttribute(c,"textplace")+"'"}if(this.getStoredAttribute(c,"textconceal")){b+=" textConceal='"+this.getStoredAttribute(c,"textconceal")+"'"}if(this.getStoredAttribute(c,"textrate")){b+=" textRate='"+this.getStoredAttribute(c,"textrate")+"'"}case"div":if(this.getStoredAttribute(c,"textalign")){b+=" textAlign='"+this.getStoredAttribute(c,"textalign")+"'"}case"p":if(this.getStoredAttribute(c,"xml:space")){b+=" xml:space='"+this.getStoredAttribute(c,"xml:space")+"'"}if(this.getStoredAttribute(c,"textwrapoption")){b+=" textWrapOption='"+this.getStoredAttribute(c,"textwrapoption")+"'"}if(this.getStoredAttribute(c,"textwritingmode")){b+=" textWritingMode='"+this.getStoredAttribute(c,"textwritingmode")+"'"}case"span":if(this.getStoredAttribute(c,"textbackgroundcolor")){b+=" textBackgroundColor='"+this.getStoredAttribute(c,"textbackgroundcolor")+"'"}if(this.getStoredAttribute(c,"textcolor")){b+=" textColor='"+this.getStoredAttribute(c,"textcolor")+"'"}if(this.getStoredAttribute(c,"textfontfamily")){b+=" textFontFamily='"+this.getStoredAttribute(c,"textfontfamily")+"'"}if(this.getStoredAttribute(c,"textfontsize")){b+=" textFontSize='"+this.getStoredAttribute(c,"textfontsize")+"'"}if(this.getStoredAttribute(c,"textfontstyle")){b+=" textFontStyle='"+this.getStoredAttribute(c,"textfontstyle")+"'"}if(this.getStoredAttribute(c,"textfontweight")){b+=" textFontWeight='"+this.getStoredAttribute(c,"textfontweight")+"'"}if(this.getStoredAttribute(c,"textstyle")){b+=" textStyle='"+this.getStoredAttribute(c,"textstyle")+"'"}if(a!="span"&&a!="textstyle"){if(this.getStoredAttribute(c,"textdirection")){b+=" textDirection='"+this.getStoredAttribute(c,"textdirection")+"'"}}}return b};cwi.smilText.STDocument.prototype.getXML=function(){var e="<smilText>";var g="";var d="\n</smilText>";var l=false;var f=-1;var c=0;var h=false;var a=null;if(this.isPlaying()){this.pause();h=true}var i=new PriorityQueue();while(this.doneQueue&&!this.doneQueue.isEmpty()){var k=this.doneQueue.lookFirst();var j=this.doneQueue.pop();switch(j[0]){case cwi.smilText.Render.appendContainer:var b=j[1][1].charAt(0);if(j[1][1].length>8&&j[1][1].substring(0,8)=="smiltext"){b="smiltext";l=true;if(j[1][1]!=""){e="<smilText id='"+j[1][1]+"' "+this.getStyleXML("smiltext",j[1][1])+" >"}}switch(b){case"smiltext":break;case"d":g+="\n<div ";g+="id='"+j[1][1]+"' "+this.getStyleXML("div",j[1][1])+" >";d="\n</div>"+d;break;case"p":g+="\n<p ";g+="id='"+j[1][1]+"' "+this.getStyleXML("p",j[1][1])+" >";d="\n</p>"+d;break;case"s":g+="\n<span ";g+="id='"+j[1][1]+"' "+this.getStyleXML("span",j[1][1])+" >";d="\n</span>"+d;break;f=0}break;case cwi.smilText.Render.appendLineBreak:g+="\n<br/>";f=0;break;case cwi.smilText.Render.appendText:if(k!=c){c=k;g+="\n<tev begin='"+k/1000+"s' />";f=0}if(f==1){g+=" "}else{g+="\n"}g+=j[1][1];f=1;break;case cwi.smilText.Render.clearLayout:g+="\n<clear begin='"+k/1000+"s' />";f=0;c=k;break;case cwi.smilText.Render.closeContainer:break}i.push(k,j)}this.doneQueue=i;i=new PriorityQueue();while(this.timingQueue&&!this.timingQueue.isEmpty()){var k=this.timingQueue.lookFirst();var j=this.timingQueue.pop();switch(j[0]){case cwi.smilText.Render.appendContainer:var b=j[1][1].charAt(0);if(!l&&j[1][1].length>8&&j[1][1].substring(0,8)=="smiltext"){b="smiltext";l=true;if(j[1][1]!=""){e="<smilText id='"+j[1][1]+"' "+this.getStyleXML("smiltext",j[1][1])+" >"}}switch(b){case"smiltext":break;case"d":g+="\n<div ";g+="id='"+j[1][1]+"' "+this.getStyleXML("div",j[1][1])+" >";d="\n</div>"+d;break;case"p":g+="\n<p ";g+="id='"+j[1][1]+"' "+this.getStyleXML("p",j[1][1])+" >";d="\n</p>"+d;break;case"s":g+="\n<span ";g+="id='"+j[1][1]+"' "+this.getStyleXML("span",j[1][1])+" >";d="\n</span>"+d;break}f=0;break;case cwi.smilText.Render.appendLineBreak:g+="\n<br/>";f=0;break;case cwi.smilText.Render.appendText:if(k!=c){c=k;g+="\n<tev begin='"+k/1000+"s' />";f=0}if(f==1){g+=" "}else{g+="\n"}g+=j[1][1];f=1;break;case cwi.smilText.Render.clearLayout:g+="\n<clear begin='"+k/1000+"s' />";f=0;c=k;break;case cwi.smilText.Render.closeContainer:break}i.push(k,j)}this.timingQueue=i;if(a){e=e.substring(0,e.length-2)+" dur='"+(a/1000)+"s' >"}g=e+g;if(d){g+=d}if(h){this.play()}return g};cwi.smilText.STDocument.prototype.addTimingEntry=function(c,a,b){if(c==cwi.smilText.Render.scrollticker){this.motionEntry=new Array(0,c,a)}else{this.timingQueue.push(b,new Array(c,a))}};cwi.smilText.STDocument.prototype.clearTimingEntries=function(){this.doneQueue.clear();this.timingQueue.clear();this.motionEntry=null};cwi.smilText.STDocument.prototype.getTimingEntries=function(){var b=new DoubleLinkedList();this.pause();var a=new PriorityQueue();while(this.doneQueue&&!this.doneQueue.isEmpty()){var d=this.doneQueue.lookFirst();var c=this.doneQueue.pop();b.insert(new Array(d,c));a.push(d,c)}this.doneQueue=a;a=new PriorityQueue();while(this.timingQueue&&!this.timingQueue.isEmpty()){var d=this.timingQueue.lookFirst();var c=this.timingQueue.pop();b.insert(new Array(d,c));a.push(d,c)}this.timingQueue=a;return b};cwi.smilText.STDocument.prototype.setTime=function(a){cwi.smilText.STDocument.superClass.setTime.call(this,a);this.processTimingQueue()};cwi.smilText.STDocument.prototype.batchAllEntries=function(){this.mergeTimingQueues();while(true){if(!this.timingQueue.isEmpty()){var b=this.timingQueue.lookFirst();var a=this.timingQueue.pop();a[0].apply(this,a[1]);this.doneQueue.push(b,a)}else{break}}this.mergeTimingQueues()};cwi.smilText.STDocument.prototype.processTimingQueue=function(){while(true){if(this.motionEntry&&this.motionEntry[0]<=this.timeNow&&this.isPlaying()){this.motionEntry[1].apply(this,this.motionEntry[2]);this.motionEntry[0]=+50}if(this.timingQueue&&!this.timingQueue.isEmpty()){var b=this.timingQueue.lookFirst();if(b<=this.timeNow){var a=this.timingQueue.pop();a[0].apply(this,a[1]);this.doneQueue.push(b,a);if(this.timingQueue.isEmpty()&&!this.motionEntry){cwi.smilText.STDocument.superClass.fireEvent.call(this,cwi.smilText.Time.EVENT_END)}}else{break}}else{break}}};cwi.smilText.STDocument.prototype.mergeTimingQueues=function(){this.doneQueue.merge(this.timingQueue);this.timingQueue=this.doneQueue;this.doneQueue=new PriorityQueue()};cwi.smilText.STDocument.prototype.seekTo=function(a){cwi.smilText.STDocument.superClass.seekTo.call(this,a);this.mergeTimingQueues()};cwi.smilText.STDocument.prototype.stop=function(){cwi.smilText.STDocument.superClass.stop.call(this);cwi.smilText.Render.clearLayout(this,this.layout,true);if(this.motionEntry){this.motionEntry[0]=0}};cwi.smilText.STDocument.prototype.getTranslatedSentence=function(b){if(this.dictionary){var a=this.dictionary.get(b);if(!a||a==""){this.updateDictionary(b,b);if(this.translateFunc){this.translateFunc(b)}return b}return a}return b};cwi.smilText.STDocument.prototype.updateDictionary=function(a,b){if(a&&b){this.dictionary.put(a,b)}};cwi.smilText.STDocument.prototype.setTranslateFunction=function(a){this.translateFunc=a;this.dictionary=new Hashtable();if(a){this.batchAllEntries()}};Namespace("cwi.smilText.Parser");Import("cwi.smilText.STDocument");cwi.smilText.Parser.parseHTML=function(){var l=new Array();var o=new STDocument(null);var b=document.getElementsByTagName("textstyle");for(var e=0;e<b.length;e++){var d;for(var f=0;f<b[e].attributes.length;f++){if(b[e].attributes[f].nodeName.toLowerCase()=="id"){d=b[e].attributes[f].nodeValue;break}}if(d==undefined){alert("Error: a textStyle element must have an ID!")}else{cwi.smilText.Parser.parseAttributes(o,d,b[e])}}var c;var p=document.getElementsByTagName("smiltext");for(c=0;c<p.length;c++){var m=p[c].parentNode;var n;for(var g=0;g<m.attributes.length;g++){if(m.attributes[g].nodeName.toLowerCase()=="id"){n=m.attributes[g].nodeValue;break}}if(n==undefined){alert("Error: All smilText containers must have an ID!")}var h=new STDocument(n);h.stylingHash=o.stylingHash;cwi.smilText.Parser.parseSmilTextElem(h,p[c],n,0,undefined);l[c]=h;var a=document.createElement("div");a.appendChild(p[c].cloneNode(false));p[c].parentNode.replaceChild(a,p[c]);a.style.visibility="hidden"}return l};cwi.smilText.Parser.parseFile=function(a,d){var c;if(document.getElementById(d)==null){alert("Error: smilText content must be encapsulated in a container. Region '"+d+"' not found!")}if(document.implementation&&document.implementation.createDocument){c=document.implementation.createDocument("","",null);c.async=false;var b=c.load(a);if(b){return cwi.smilText.Parser.parseDoc(c,d)}else{return null}}else{if(window.ActiveXObject){c=new ActiveXObject("Microsoft.XMLDOM");c.async=false;c.preserveWhiteSpace=true;c.load(a);while(c.readyState!=4){}return cwi.smilText.Parser.parseDoc(c,d)}else{alert("Your browser can't handle this script");return null}}};cwi.smilText.Parser.parseString=function(d,c){var a;if(document.getElementById(c)==null){alert("Error: smilText content must be encapsulated in a container. Region '"+c+"' not found!")}try{a=new ActiveXObject("Microsoft.XMLDOM");a.async="false";a.loadXML(d)}catch(b){try{var f=new DOMParser();a=f.parseFromString(d,"text/xml")}catch(b){alert(b.message);return null}}return cwi.smilText.Parser.parseDoc(a,c)};cwi.smilText.Parser.parseHTMLElement=function(a,b){var c=new STDocument(b);if(a){cwi.smilText.Parser.parseSmilTextElem(c,a,b,0,undefined)}return c};cwi.smilText.Parser.parseDoc=function(e,f){var g=new STDocument(f);var d=e.getElementsByTagName("textStyle");for(var c=0;c<d.length;c++){var h;for(var b=0;b<d[c].attributes.length;b++){if(d[c].attributes[b].nodeName.toLowerCase()=="id"){h=d[c].attributes[b].nodeValue;break}}if(d[c].parentNode.nodeName.toLowerCase()!="textstyling"){alert("Warning: a textStyle element should be inside a textStyling tag")}if(h==undefined){alert("Error: a textStyle element must have an ID!")}else{cwi.smilText.Parser.parseAttributes(g,h,d[c])}}var a=e.getElementsByTagName("smilText");for(var c=0;c<a.length;c++){cwi.smilText.Parser.parseSmilTextElem(g,a[c],f,0,undefined)}return g};cwi.smilText.Parser.parseSmilTextElem=function(w,q,r,p,l){var e=false;var d;if(l==undefined||p<=l){d=true}else{d=false}if(q.nodeName.toLowerCase()=="smiltext"||q.nodeName.toLowerCase()=="smil:smiltext"){var u="smiltext"+Math.random()*10000;w.addTimingEntry(cwi.smilText.Render.clearLayout,new Array(w,r,d),p*1000);cwi.smilText.Parser.parseAttributes(w,u,q);w.addTimingEntry(cwi.smilText.Render.appendContainer,new Array(w,u,r,d),p*1000);r=u;if(w.getStoredAttribute(u,"dur")!=undefined&&w.getStoredAttribute(u,"dur")!=null){l=parseFloat(w.getStoredAttribute(u,"dur"));w.addTimingEntry(cwi.smilText.Render.closeContainer,new Array(w),l*1000)}}for(var m=0;m<q.childNodes.length;m++){if(q.childNodes[m].nodeType!=1){var k;if(w.getStoredAttribute(r,"xml:space")=="preserve"){k=q.childNodes[m].nodeValue;var c=k.split(/\n/g);for(var g=0;g<c.length;g++){w.addTimingEntry(cwi.smilText.Render.appendText,new Array(w,c[g],r,d),p*1000);if(w.getStoredAttribute(r,"textmode")!="crawl"){if(g!=c.length-1){w.addTimingEntry(cwi.smilText.Render.appendLineBreak,new Array(w,r,d),p*1000)}}}}else{k=(cwi.util.trim(q.childNodes[m].nodeValue));k=k.replace(new RegExp("\\n","g"),"");k=k.replace(new RegExp("\\t","g"),"");k=k.replace(new RegExp(" +","g")," ");if(k.length!=0){w.addTimingEntry(cwi.smilText.Render.appendText,new Array(w,k,r,d),p*1000)}else{}}}else{var v=q.childNodes[m].nodeName.toLowerCase();switch(v){case"smil:tev":case"tev":var s=null;var h=null;if((w.getStoredAttribute(r,"textmode")=="crawl"||w.getStoredAttribute(r,"textmode")=="scroll"||w.getStoredAttribute(r,"textmode")=="jump")&&(w.getStoredAttribute(r,"textrate")==null||w.getStoredAttribute(r,"textrate")==undefined||w.getStoredAttribute(r,"textrate")=="auto")){break}for(var o=0;o<q.childNodes[m].attributes.length;o++){if(q.childNodes[m].attributes[o].nodeName.toLowerCase()=="begin"){s=parseFloat(q.childNodes[m].attributes[o].nodeValue);break}else{if(q.childNodes[m].attributes[o].nodeName.toLowerCase()=="next"){h=parseFloat(q.childNodes[m].attributes[o].nodeValue);break}}}if(h!=null){p+=h}else{if(s!=null){if(s>p){p=s}}}if(l==undefined||p<=l){d=true}else{d=false}if(w.getStoredAttribute(r,"textmode")=="replace"){w.addTimingEntry(cwi.smilText.Render.clearLayout,new Array(w,r,d),p*1000)}break;case"smil:clear":case"clear":var s=null;var h=null;if((w.getStoredAttribute(r,"textmode")=="crawl"||w.getStoredAttribute(r,"textmode")=="scroll"||w.getStoredAttribute(r,"textmode")=="jump")&&(w.getStoredAttribute(r,"textrate")==null||w.getStoredAttribute(r,"textrate")==undefined||w.getStoredAttribute(r,"textrate")=="auto")){break}for(var o=0;o<q.childNodes[m].attributes.length;o++){if(q.childNodes[m].attributes[o].nodeName.toLowerCase()=="begin"){s=parseFloat(q.childNodes[m].attributes[o].nodeValue);break}else{if(q.childNodes[m].attributes[o].nodeName.toLowerCase()=="next"){h=parseFloat(q.childNodes[m].attributes[o].nodeValue);break}}}if(h!=null){p+=h}else{if(s!=null){if(s>p){p=s}}}if(l==undefined||p<=l){d=true}else{d=false}w.addTimingEntry(cwi.smilText.Render.clearLayout,new Array(w,r,d),p*1000);break;case"smil:br":case"br":if(w.getStoredAttribute(r,"textmode")!="crawl"){w.addTimingEntry(cwi.smilText.Render.appendLineBreak,new Array(w,r,d),p*1000)}break;case"smil:div":case"div":var a="div"+Math.random()*10000;cwi.smilText.Parser.parseAttributes(w,a,q.childNodes[m]);if(w.getStoredAttribute(a,"xml:space")==undefined||w.getStoredAttribute(a,"xml:space")==null||(w.getStoredAttribute(a,"xml:space")!="default"&&w.getStoredAttribute(a,"xml:space")!="preserve")){if(w.getStoredAttribute(r,"xml:space")!=undefined&&w.getStoredAttribute(r,"xml:space")!=null&&(w.getStoredAttribute(r,"xml:space")=="default"||w.getStoredAttribute(r,"xml:space")=="preserve")){w.updateAttribute(a,"xml:space",w.getStoredAttribute(r,"xml:space"))}else{w.updateAttribute(a,"xml:space","default")}}w.updateAttribute(a,"textmode",w.getStoredAttribute(r,"textmode"));w.updateAttribute(a,"textrate",w.getStoredAttribute(r,"textrate"));w.addTimingEntry(cwi.smilText.Render.appendContainer,new Array(w,a,r,d),p*1000);p=cwi.smilText.Parser.parseSmilTextElem(w,q.childNodes[m],a,p,l);break;case"smil:p":case"p":var f="p"+Math.random()*10000;cwi.smilText.Parser.parseAttributes(w,f,q.childNodes[m]);if(w.getStoredAttribute(f,"xml:space")==undefined||w.getStoredAttribute(f,"xml:space")==null||(w.getStoredAttribute(f,"xml:space")!="default"&&w.getStoredAttribute(f,"xml:space")!="preserve")){if(w.getStoredAttribute(r,"xml:space")!=undefined&&w.getStoredAttribute(r,"xml:space")!=null&&(w.getStoredAttribute(r,"xml:space")=="default"||w.getStoredAttribute(r,"xml:space")=="preserve")){w.updateAttribute(f,"xml:space",w.getStoredAttribute(r,"xml:space"))}else{w.updateAttribute(f,"xml:space","default")}}w.updateAttribute(f,"textmode",w.getStoredAttribute(r,"textmode"));w.updateAttribute(f,"textrate",w.getStoredAttribute(r,"textrate"));w.addTimingEntry(cwi.smilText.Render.appendContainer,new Array(w,f,r,d),p*1000);p=cwi.smilText.Parser.parseSmilTextElem(w,q.childNodes[m],f,p,l);break;case"smil:span":case"span":var t="span"+Math.random()*10000;cwi.smilText.Parser.parseAttributes(w,t,q.childNodes[m]);if(w.getStoredAttribute(t,"xml:space")==undefined||w.getStoredAttribute(t,"xml:space")==null||(w.getStoredAttribute(t,"xml:space")!="default"&&w.getStoredAttribute(t,"xml:space")!="preserve")){if(w.getStoredAttribute(r,"xml:space")!=undefined&&w.getStoredAttribute(r,"xml:space")!=null&&(w.getStoredAttribute(r,"xml:space")=="default"||w.getStoredAttribute(r,"xml:space")=="preserve")){w.updateAttribute(t,"xml:space",w.getStoredAttribute(r,"xml:space"))}else{w.updateAttribute(t,"xml:space","default")}}w.updateAttribute(t,"textmode",w.getStoredAttribute(r,"textmode"));w.updateAttribute(t,"textrate",w.getStoredAttribute(r,"textrate"));w.addTimingEntry(cwi.smilText.Render.appendContainer,new Array(w,t,r,d),p*1000);p=cwi.smilText.Parser.parseSmilTextElem(w,q.childNodes[m],t,p,l);break;default:alert("Unknown smilText tag: "+v)}}}return p};cwi.smilText.Parser.parseAttributes=function(f,h,c){var e=c.nodeName.toLowerCase();if(e=="textstyle"){h="textstyle"+h}else{for(var b=0;b<c.attributes.length;b++){var a=c.attributes[b].nodeName.toLowerCase();var g=c.attributes[b].nodeValue;if(a=="textstyle"){f.applyStyle(e,h,"textstyle"+g);break}}}for(var b=0;b<c.attributes.length;b++){var d=false;var a=c.attributes[b].nodeName.toLowerCase();var g=c.attributes[b].nodeValue;switch(e){case"smil:smiltext":case"smiltext":switch(a){case"dur":d=true;break;case"width":d=true;break;case"height":d=true;break;default:}if(d){f.updateAttribute(h,a,g);break}case"textstyle":switch(a){case"textmode":d=true;break;case"textplace":d=true;break;case"textconceal":d=true;break;case"textrate":d=true;break;default:}if(d){f.updateAttribute(h,a,g);break}case"smil:div":case"div":switch(a){case"textalign":d=true;break;default:}if(d){f.updateAttribute(h,a,g);break}case"smil:p":case"p":switch(a){case"xml:space":case"textwrapoption":case"textwritingmode":d=true;break;default:}if(d){f.updateAttribute(h,a,g);break}case"smil:span":case"span":switch(a){case"textbackgroundcolor":case"textcolor":case"textfontfamily":case"textfontsize":case"textfontstyle":case"textfontweight":case"textstyle":d=true;break;case"textdirection":if(e!="span"&&e!="textstyle"){break}d=true;break;default:}if(d){f.updateAttribute(h,a,g)}break;default:return}}};Namespace("cwi.smilText.Render");Import("cwi.smilText.STDocument");cwi.smilText.Render.appendText=function(c,d,a,b){if(c==null||c==undefined){return}if(!b){return}d=c.getTranslatedSentence(d);switch(c.getStoredAttribute(a,"textwrapoption")){case"noWrap":break;case"inherit":break;case"wrap":default:d=d.replace(/(.*?)/g,"<wbr/>")}var e=document.createElement("span");if(document.getElementById(a)!=null){document.getElementById(a).appendChild(e)}e.innerHTML=d};cwi.smilText.Render.appendLineBreak=function(d,b,c){if(d==null||d==undefined){return}if(!c){return}var a=document.createElement("BR");if(document.getElementById(b)!=null){document.getElementById(b).appendChild(a)}};cwi.smilText.Render.clearLayout=function(d,b,c){if(d==null||d==undefined){return}if(!c){return}var a=document.getElementById(b);if(a){while(child=a.firstChild){a.removeChild(child)}}};cwi.smilText.Render.appendContainer=function(f,d,a,b){if(f==null||f==undefined){return}if(!b){return}var h;var g=d.charAt(0);if(d.length>8&&d.substring(0,8)=="smiltext"){g="smiltext"}switch(g){case"smiltext":case"d":h=document.createElement("div");try{h.style.display="block"}catch(c){}break;case"p":h=document.createElement("div");try{h.style.display="block"}catch(c){}break;case"s":h=document.createElement("div");try{h.style.display="inline"}catch(c){}break}if(f.getStoredAttribute(d,"textwrapoption")==undefined||f.getStoredAttribute(d,"textwrapoption")==null){f.updateAttribute(d,"textwrapoption","wrap")}else{if(f.getStoredAttribute(d,"textwrapoption")=="inherit"){if(f.getStoredAttribute(a,"textwrapoption")!=undefined&&f.getStoredAttribute(a,"textwrapoption")!=null){f.updateAttribute(d,"textwrapoption",f.getStoredAttribute(a,"textwrapoption"))}else{f.updateAttribute(d,"textwrapoption","wrap")}}}if(f.getStoredAttribute(d,"textmode")=="crawl"){f.updateAttribute(d,"textwrapoption","noWrap")}if(f.getStoredAttribute(d,"xml:space")=="preserve"){switch(f.getStoredAttribute(d,"textwrapoption")){case"noWrap":try{h.style.whiteSpace="pre"}catch(c){}break;case"wrap":default:try{h.style.whiteSpace="pre-wrap"}catch(c){}}}else{switch(f.getStoredAttribute(d,"textwrapoption")){case"noWrap":try{h.style.whiteSpace="nowrap"}catch(c){}break;case"wrap":default:try{h.style.whiteSpace="normal"}catch(c){}}}if(f.getStoredAttribute(d,"textmode")=="crawl"&&g!="smiltext"){try{h.style.display="inline"}catch(c){}}if(document.getElementById(a)!=null){document.getElementById(a).appendChild(h)}g=document.createAttribute("id");g.nodeValue=d;h.setAttributeNode(g);cwi.smilText.Render.applyAttributes(f,d,h)};cwi.smilText.Render.closeContainer=function(a){if(!a.hasFillSemantics){cwi.smilText.Render.clearLayout(a,a.layout,true)}};cwi.smilText.Render.applyAttributes=function(g,h,c){var d;if(h.length>8&&h.substring(0,8)=="smiltext"){d="smiltext"}else{if(h.length>9&&h.substring(0,9)=="textstyle"){d="textstyle";for(var b=0;b<c.attributes.length;b++){if(c.attributes[b].nodeName.toLowerCase()=="id"){var a=c.attributes[b].nodeValue;if(a.length>8&&a.substring(0,8)=="smiltext"){}else{if(a.length>3&&a.substring(0,3)=="div"){d+="1"}else{if(a.length>1&&a.substring(0,1)=="p"){d+="2"}else{if(a.length>4&&a.substring(0,4)=="span"){d+="3"}}}}break}}}else{d=h.charAt(0)}}if(d!="textstyle"&&g.getStoredAttribute(h,"textstyle")!=undefined&&g.getStoredAttribute(h,"textstyle")!=null){cwi.smilText.Render.applyAttributes(g,"textstyle"+g.getStoredAttribute(h,"textstyle"),c)}switch(d){case"smiltext":if(g.getStoredAttribute(h,"width")!=undefined&&g.getStoredAttribute(h,"width")!=null){try{c.style.width=parseFloat(g.getStoredAttribute(h,"width"));c.style.overflow="hidden"}catch(f){}}if(g.getStoredAttribute(h,"height")!=undefined&&g.getStoredAttribute(h,"height")!=null){try{c.style.height=parseFloat(g.getStoredAttribute(h,"height"));c.style.overflow="hidden"}catch(f){}}case"textstyle":if(g.getStoredAttribute(h,"textplace")!=undefined&&g.getStoredAttribute(h,"textplace")!=null){}if(g.getStoredAttribute(h,"textmode")!=undefined&&g.getStoredAttribute(h,"textmode")!=null){switch(g.getStoredAttribute(h,"textmode")){case"replace":break;case"crawl":g.addTimingEntry(cwi.smilText.Render.scrollticker,new Array(g,h,"1"),0);break;case"scroll":g.addTimingEntry(cwi.smilText.Render.scrollticker,new Array(g,h,"2"),0);break;case"jump":break;case"append":default:}}if(g.getStoredAttribute(h,"textconceal")!=undefined&&g.getStoredAttribute(h,"textconceal")!=null){switch(g.getStoredAttribute(h,"textconceal")){case"none":break;case"initial":break;case"final":break;case"both":break;case"inherit":default:}}if(g.getStoredAttribute(h,"textrate")!=undefined&&g.getStoredAttribute(h,"textrate")!=null){}case"textstyle1":case"d":if(g.getStoredAttribute(h,"textalign")!=undefined&&g.getStoredAttribute(h,"textalign")!=null){switch(g.getStoredAttribute(h,"textalign")){case"inherit":case"start":case"end":case"left":case"right":case"center":try{c.style.textAlign=g.getStoredAttribute(h,"textalign")}catch(f){}break;default:}}case"textstyle2":case"p":if(g.getStoredAttribute(h,"textwritingmode")!=undefined&&g.getStoredAttribute(h,"textwritingmode")!=null){switch(g.getStoredAttribute(h,"textwritingmode")){case"lr-tb":case"rl-tb":case"tb-rl":case"tb-lr":case"rl":case"lr":case"inherit":try{c.style.writingMode=g.getStoredAttribute(h,"textwritingmode")}catch(f){}break;default:}}case"textstyle3":case"s":if(g.getStoredAttribute(h,"textbackgroundcolor")!=undefined&&g.getStoredAttribute(h,"textbackgroundcolor")!=null){try{c.style.backgroundColor=g.getStoredAttribute(h,"textbackgroundcolor")}catch(f){}}if(g.getStoredAttribute(h,"textcolor")!=undefined&&g.getStoredAttribute(h,"textcolor")!=null){try{c.style.color=g.getStoredAttribute(h,"textcolor")}catch(f){}}if(g.getStoredAttribute(h,"textfontfamily")!=undefined&&g.getStoredAttribute(h,"textfontfamily")!=null){try{c.style.fontFamily=g.getStoredAttribute(h,"textfontfamily")}catch(f){}}if(g.getStoredAttribute(h,"textfontsize")!=undefined&&g.getStoredAttribute(h,"textfontsize")!=null){try{c.style.fontSize=g.getStoredAttribute(h,"textfontsize")}catch(f){}}if(g.getStoredAttribute(h,"textfontstyle")!=undefined&&g.getStoredAttribute(h,"textfontstyle")!=null){try{c.style.fontStyle=g.getStoredAttribute(h,"textfontstyle")}catch(f){}}if(g.getStoredAttribute(h,"textfontweight")!=undefined&&g.getStoredAttribute(h,"textfontweight")!=null){try{c.style.fontWeight=g.getStoredAttribute(h,"textfontweight")}catch(f){}}if(g.getStoredAttribute(h,"textdirection")!=undefined&&g.getStoredAttribute(h,"textdirection")!=null){if(d=="s"){if(g.getStoredAttribute(h,"textdirection")=="rtlo"||g.getStoredAttribute(h,"textdirection")=="rtl"){try{c.style.direction="rtl";c.style.unicodeBidi="bidi-override"}catch(f){}}else{if(g.getStoredAttribute(h,"textdirection")=="ltro"||g.getStoredAttribute(h,"textdirection")=="ltro"){try{c.style.direction="ltr";c.style.unicodeBidi="bidi-override"}catch(f){}}}}}break;default:}};cwi.smilText.Render.cps=1;cwi.smilText.Render.scrollticker=function(m,f,c){if(!document.getElementById(f)){return}var b=document.getElementById(f).parentNode;b.style.overflow="hidden";var n=b.style.position;b.style.position="relative";for(var d=0;d<b.childNodes.length;d++){var g=b.childNodes[d];var k;if(c=="1"){if(m.getStoredAttribute(f,"textwritingmode")!=undefined&&m.getStoredAttribute(f,"textwritingmode")!=null){switch(m.getStoredAttribute(f,"textwritingmode")){case"lr-tb":case"lr":k="left";break;case"rl-tb":case"rl":k="right";break;case"tb-rl":case"tb-lr":k="up";case"inherit":if(cwi.util.getStyleByElem(b,"writingMode")!=undefined&&cwi.util.getStyleByElem(b,"writingMode")!=null&&cwi.util.getStyleByElem(b,"writingMode").length>0){if(cwi.util.getStyleByElem(b,"writingMode").charAt(0)=="r"){k="right"}else{if(cwi.util.getStyleByElem(b,"writingMode").charAt(0)=="t"){k="up"}else{k="left"}}}else{k="left"}break}}else{if(cwi.util.getStyleByElem(b,"writingMode")!=undefined&&cwi.util.getStyleByElem(b,"writingMode")!=null&&cwi.util.getStyleByElem(b,"writingMode").length>0){if(cwi.util.getStyleByElem(b,"writingMode").charAt(0)=="r"){k="right"}else{if(cwi.util.getStyleByElem(b,"writingMode").charAt(0)=="t"){k="up"}else{k="left"}}}else{k="left"}}}else{if(c=="2"){if(m.getStoredAttribute(f,"textwritingmode")!=undefined&&m.getStoredAttribute(f,"textwritingmode")!=null){switch(m.getStoredAttribute(f,"textwritingmode")){case"lr-tb":case"lr":case"rl-tb":case"rl":k="up";break;case"tb-rl":k="right";break;case"tb-lr":k="left";break;case"inherit":if(cwi.util.getStyleByElem(b,"writingMode")!=undefined&&cwi.util.getStyleByElem(b,"writingMode")!=null&&cwi.util.getStyleByElem(b,"writingMode").length>0){if(cwi.util.getStyleByElem(b,"writingMode").charAt(0)=="r"||cwi.util.getStyleByElem(b,"writingMode").charAt(0)=="l"){k="up"}else{if(cwi.util.getStyleByElem(b,"writingMode")=="tb-lr"){k="left"}else{k="right"}}}else{k="left"}break}}else{if(cwi.util.getStyleByElem(b,"writingMode")!=undefined&&cwi.util.getStyleByElem(b,"writingMode")!=null&&cwi.util.getStyleByElem(b,"writingMode").length>0){if(cwi.util.getStyleByElem(b,"writingMode").charAt(0)=="r"||cwi.util.getStyleByElem(b,"writingMode").charAt(0)=="l"){k="up"}else{if(cwi.util.getStyleByElem(b,"writingMode")=="tb-lr"){k="left"}else{k="right"}}}else{k="up"}}}}var o=cwi.smilText.Render.cps;if(m.getStoredAttribute(f,"textrate")!=undefined&&m.getStoredAttribute(f,"textrate")!=null&&m.getStoredAttribute(f,"textrate")!="auto"){o=0.05*parseInt(m.getStoredAttribute(f,"textrate"))}if(g.nodeType!=1){}else{var l=parseInt(cwi.util.getStyleByElem(b,"width"));var e=parseInt(cwi.util.getStyleByElem(b,"height"));g.style.position="absolute";var a=g.offsetWidth>l?g.offsetWidth:l;var i=g.offsetHeight;g.style.position="relative";if(!a){a=g.offsetWidth}if(!i){i=g.offsetHeight}if(!l){l=a}if(!e){e=i}switch(k){case"left":if(!g.style.left){g.style.left=0}g.style.left=parseInt(g.style.left)>parseInt(-a)?parseFloat(g.style.left)-o:l;break;case"up":if(!g.style.top){g.style.top=0}g.style.top=parseInt(g.style.top)>parseInt(-i)?parseFloat(g.style.top)-o:e;break;case"right":if(!g.style.left){g.style.left=0}g.style.left=parseInt(g.style.left)<parseInt(l)?parseFloat(g.style.left)+o:-a;break;case"down":break}}}b.style.position=n};cwi.smilText.Style=function(){var a=JSINER.extend(this);this.stylingHash=new Hashtable();return a};cwi.smilText.Style.prototype.getAttribute=function(b,a){return this.stylingHash.get(b+a)};cwi.smilText.Style.prototype.updateAttribute=function(b,a,c){this.stylingHash.put(b+a,c)};cwi.smilText.Style.prototype.toXML=function(b,d,a){var c="";switch(b){case"smiltext":this.updateAttribute(d,"dur",this.getStoredAttribute(a,"dur"));this.updateAttribute(d,"width",this.getStoredAttribute(a,"width"));this.updateAttribute(d,"height",this.getStoredAttribute(a,"height"));case"textstyle":this.updateAttribute(d,"textmode",this.getStoredAttribute(a,"textmode"));this.updateAttribute(d,"textplace",this.getStoredAttribute(a,"textplace"));this.updateAttribute(d,"textconceal",this.getStoredAttribute(a,"textconceal"));this.updateAttribute(d,"textrate",this.getStoredAttribute(a,"textrate"));case"div":this.updateAttribute(d,"textalign",this.getStoredAttribute(a,"textalign"));case"p":this.updateAttribute(d,"xml:space",this.getStoredAttribute(a,"xml:space"));this.updateAttribute(d,"textwrapoption",this.getStoredAttribute(a,"textwrapoption"));this.updateAttribute(d,"textwritingmode",this.getStoredAttribute(a,"textwritingmode"));case"span":this.updateAttribute(d,"textbackgroundcolor",this.getStoredAttribute(a,"textbackgroundcolor"));this.updateAttribute(d,"textcolor",this.getStoredAttribute(a,"textcolor"));this.updateAttribute(d,"textfontfamily",this.getStoredAttribute(a,"textfontfamily"));this.updateAttribute(d,"textfontsize",this.getStoredAttribute(a,"textfontsize"));this.updateAttribute(d,"textfontstyle",this.getStoredAttribute(a,"textfontstyle"));this.updateAttribute(d,"textfontweight",this.getStoredAttribute(a,"textfontweight"));this.updateAttribute(d,"textstyle",this.getStoredAttribute(a,"textstyle"));if(b!="span"&&b!="textstyle"){this.updateAttribute(d,"textdirection",this.getStoredAttribute(a,"textdirection"))}}return c};
