/**
 * 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
 */

/**
 @version 1.0
 @author <a href="mailto:rlaiola@cwi.nl">Rodrigo Laiola Guimaraes</a>
*/

var currentPosition=0;var currentState="NONE";var previousState="NONE";var currentLoaded=0;var currentRemain=0;var currentItem=null;var currentMute=false;var currentTitle="";var currentAuthor="";var currentDescription="";var currentURL="";var player=null;var currentExpanded=false;var firstLoading=true;var placeHolder=null;var embededMode=false;var currentPlayer="jw";function playerReady(b){player=window.document[b.id];currentPlayer="jw";addListeners();if(firstLoading){var a=gup("videoURL");if(a){setTimeout("playVideo('"+a+"')",1000)}if(subURL){showLoadingDialog();stDoc=cwi.smilText.Parser.parseString(subURL,"sub");pause();stDoc.setExternalClock(true);populateSubtitles();hideLoadingDialog()}else{if(subStr){showLoadingDialog();stDoc=cwi.smilText.Parser.parseString(subStr,"sub");pause();stDoc.setExternalClock(true);populateSubtitles();hideLoadingDialog()}}firstLoading=false}}function addListeners(){if(player){player.addControllerListener("ITEM","itemListener");player.addModelListener("LOADED","loadedListener");player.addModelListener("STATE","stateListener");player.addModelListener("TIME","positionListener");player.addModelListener("TIME","updateNormalPlayerInfo")}else{setTimeout("addListeners()",100)}}function itemListener(a){if(a.index!=currentItem){currentItem=1;printItemData(currentItem)}}function stateListener(b){currentState=b.newstate;previousState=b.oldstate;updateHTML("playerstate",currentState);var a=getPlayerState();if(a===0){document.getElementById("playButton").style.display="inline";document.getElementById("pauseButton").style.display="none"}else{if(a===1){document.getElementById("playButton").style.display="none";document.getElementById("pauseButton").style.display="inline"}else{if(a===2){document.getElementById("playButton").style.display="inline";document.getElementById("pauseButton").style.display="none"}}}}function positionListener(a){currentPosition=a.position;updateTimebar();updateHTML("durationTimeBar",formatTime(getDuration()));updateHTML("timeTimeBar",formatTime(currentPosition));updateHTML("videotime",formatTime(currentPosition))}function loadedListener(a){updateHTML("bytesloaded",0);updateHTML("bytesremain",0);currentLoaded=a.loaded;currentRemain=a.total-currentLoaded;updateHTML("bytesloaded",currentLoaded);updateHTML("bytesremain",currentRemain);updateHTML("videodur",formatTime(getDuration()))}function printItemData(b){var a=null;a=player.getPlaylist();if(a){updateHTML("titlevideo",currentTitle);updateHTML("authorvideo",currentAuthor);updateHTML("linkvideo",currentURL);updateHTML("descvideo",currentDescription)}}function deletePlayer(c,d,a){swfobject.removeSWF(a);var b=document.getElementById(c);if(b){b.innerHTML="<div id='"+d+"'></div>"}}function createPlayer(e,b,d,g){var a={file:encodeURIComponent(e),autostart:d,controlbar:"none",skin:b};var f={allowfullscreen:"true",allowscriptaccess:"always",wmode:"opaque"};var c={id:"playerJW",name:"playerJW"};swfobject.embedSWF("scripts/player/player.swf",placeHolder,"100%","100%","9.0.115",false,a,f,c);stDoc.play()}var currentFile;var currentSkin;function playVideo(f,a,d,b){pause();if(currentURL&&!resetSubtitles()){goPlay();return}if(makeRequest("php/get_refresh.php?url="+f)){currentTitle=a;currentAuthor=d;currentDescription=b;try{document.getElementById("pauseButton").focus()}catch(e){}try{document.getElementById("pauseButton").blur()}catch(c){}goPlay()}else{goHome()}}function initPlayer(c,a,b,d){currentFile=c;currentSkin=a;currentPosition=0;currentState="NONE";previousState="NONE";currentLoaded=0;currentRemain=0;currentItem=null;deletePlayer("wrapper",placeHolder,"playerJW");createPlayer(c,a,b,d);setSize("wrapper")}function setSize(d){var c=document.getElementById(d);if(c){if(!currentExpanded){var e=456;var a=285;var b="px"}else{var e=684;var a=428;var b="px"}}sizeWrap(d,e,a,b)}function sizeWrap(d,e,a,b){var c=document.getElementById(d);if(c){if(!b){var b="px"}c.style.width=parseInt(e)+b;c.style.height=parseInt(a)+b}}var playerNotLoaded="Flash player not loaded yet. Try again after a few seconds. Check the player area for more info if it does not work.";var selectVideo="You must select a video to perform this action.";var requestFailed="This video could not be loaded. Try another one.";function play(){if(player){if(player.getPlaylist()){removeAllWarnings();resetCaptionMode();resetCommentMode();player.sendEvent("PLAY");stDoc.play()}else{alert(selectVideo);goHome()}}else{alert(playerNotLoaded)}}function pause(){if(player){if(player.getPlaylist()){if(getPlayerState()==1){player.sendEvent("PLAY")}stDoc.pause()}else{}}else{}}function stop(){if(player){if(player.getPlaylist()){player.sendEvent("STOP");stDoc.stop()}else{alert(selectVideo);goHome()}}else{alert(playerNotLoaded)}}function getDuration(){if(player){if(player.getPlaylist()){var a=player.getPlaylist();return a[0].duration}}return 0}function getCurrentTime(){if(player){if(player.getPlaylist()){return currentPosition}}return 0}function getPlayerState(){switch(currentState){case"PLAYING":return 1;case"PAUSED":return 2;case"IDLE":case"NONE":case"BUFFERING":case"COMPLETED":default:return 0}}function getCurrentVideoURL(){if(currentItem){return currentURL}return null}function mute(){if(player){player.sendEvent("MUTE");document.getElementById("unmuteButton").style.display="none";document.getElementById("muteButton").style.display="inline";currentMute=true}}function unMute(){if(player){player.sendEvent("MUTE");document.getElementById("unmuteButton").style.display="inline";document.getElementById("muteButton").style.display="none";currentMute=false}}function expandedScreenMode(){if(player){if(!currentExpanded){sizeWrap("wrapper","684","428","px");sizeWrap("tvset","770","428","px");document.getElementById("normalSizeButton").style.display="inline";document.getElementById("expandedSizeButton").style.display="none";currentExpanded=true}else{sizeWrap("wrapper","456","285","px");sizeWrap("tvset","550","","px");document.getElementById("normalSizeButton").style.display="none";document.getElementById("expandedSizeButton").style.display="inline";currentExpanded=false}}}function seekTo(b){if(player){var a=getPlayerState();player.sendEvent("SEEK",b);stDoc.seekTo(b*1000);if(a==2){setTimeout("player.sendEvent('PLAY', false)",200)}}}var timeSlider=null;function updateTimebar(){var b=getDuration();var a=getCurrentTime();var c=getPercent(b,a);if(timeSlider){timeSlider.set(c)}}var http_request=false;function makeRequest(a){showLoadingDialog();http_request=false;if(window.XMLHttpRequest){http_request=new XMLHttpRequest();if(http_request.overrideMimeType){http_request.overrideMimeType("text/xml")}}else{if(window.ActiveXObject){try{http_request=new ActiveXObject("Msxml2.XMLHTTP")}catch(b){try{http_request=new ActiveXObject("Microsoft.XMLHTTP")}catch(b){}}}}if(!http_request){alert(requestFailed);return false}http_request.onreadystatechange=alertContents;http_request.open("GET",a,true);http_request.send(null);setTimeout("hideLoadingDialog()",5000);return true}function alertContents(){if(http_request.readyState==4){if(http_request.status==200){try{var a=http_request.responseText;currentURL=a;videoURL=a;var c="";hideLoadingDialog();initPlayer(videoURL,currentSkin,"true",c)}catch(b){alert(requestFailed);goHome()}}else{alert(requestFailed);goHome()}}}function getPercent(b,a){return(b>0)?(100/b)*a:0}function formatTime(d){var a=getHours(d);var c=getMins(d);var b=getSecs(d);return timestamp=a+":"+c+":"+b}function getSecs(c){var b=parseInt(c%60);var a=parseInt(((c%60)*10)%10);b=b>=0?b:0;a=a>=0?a:0;return""+(b<10?"0"+b:b)+"."+a}function getMins(b){var a=parseInt((b/60)%60);a=a>=0?a:0;return""+(a<10?"0"+a:a)}function getHours(b){var a=parseInt(b/3600);a=a>=0?a:0;return""+(a<10?a:a)}function updateHTML(a,b){if(document&&document.getElementById(a)){document.getElementById(a).innerHTML=b}}var stDoc=new STDocument("sub");function embedPlayer(){turnOnSubtitles();placeHolder="placeholder"+Math.random()*1000000000;document.getElementById("placeholder1").id=placeHolder;initPlayer("","scripts/player/modieus.swf",false);stDoc.setExternalClock(true);if(subURL||subStr){goPlay()}else{goHome()}}function updateNormalPlayerInfo(){stDoc.setTime(getCurrentTime()*1000)}var subList=new DoubleLinkedList();var subEnabled=true;var currSubBox=null;function turnOnSubtitles(){var a=document.getElementById("sub");if(!hasExternalSubtitles()){a.title="Captions/subtitles are presented here. Click on them to edit."}else{a.title="Captions/subtitles are presented here."}subEnabled=true;if(document.getElementById("captionInput")){document.getElementById("captionInput").style.display="inline"}insertSubtitle();if(document.getElementById("subOff")){document.getElementById("subOff").style.display="none"}if(document.getElementById("subOn")){document.getElementById("subOn").style.display="inline"}}function turnOffSubtitles(){var a=document.getElementById("sub");a.title="";subEnabled=false;document.getElementById("captionInput").style.display="none";document.getElementById("subOn").style.display="none";document.getElementById("subOff").style.display="inline"}function flashInput(a){a.style.backgroundColor="#555555"}function unflashInput(a){a.style.backgroundColor="#333333"}function match(b,d,a,c){return !(b<=a&&d<=a)}function matchEndStart(k,j,h){var f=j;var a=f.childNodes;var g,d,b;var m=true;var l="End time changed to be before next start time";for(var c=0;c<a.length;c++){var e=a[c].name;if(e=="startH[]"){g=a[c].value}else{if(e=="startM[]"){d=a[c].value}else{if(e=="startS[]"){b=a[c].value}}}}f=k;a=f.childNodes;for(var c=0;c<a.length;c++){var e=a[c].name;if(e=="endH[]"){a[c].value=g;if(h){writeWarning(a[c],l,m)}m=false}else{if(e=="endM[]"){a[c].value=d;if(h){writeWarning(a[c],l,m)}m=false}else{if(e=="endS[]"){a[c].value=b;if(h){writeWarning(a[c],l,m)}m=false}}}}}function matchStartEnd(k,j,h,g){var e=j;var a=e.childNodes;var b,l,f;var n=true;var m="Start time changed to be after previous end time";for(var c=0;c<a.length;c++){var d=a[c].name;if(d=="endH[]"){b=a[c].value}else{if(d=="endM[]"){l=a[c].value}else{if(d=="endS[]"){f=a[c].value}}}}e=k;a=e.childNodes;for(var c=0;c<a.length;c++){var d=a[c].name;if(d=="startH[]"){a[c].value=b;if(h){writeWarning(a[c],m,n)}n=false}else{if(d=="startM[]"){a[c].value=l;if(h){writeWarning(a[c],m,n)}n=false}else{if(d=="startS[]"){a[c].value=f;if(h){writeWarning(a[c],m,n)}n=false}else{if(g&&d=="endH[]"){a[c].value=b;if(h){writeWarning(a[c],m,n)}n=false}else{if(g&&d=="endM[]"){a[c].value=l;if(h){writeWarning(a[c],m,n)}n=false}else{if(g&&d=="endS[]"){a[c].value=f;if(h){writeWarning(a[c],m,n)}n=false}}}}}}}}var startTime;var endTime;var textEntry="";var savedEntry=false;var modifiedEntry=false;var userSetStart=false;var userSetEnd=false;var captionType=true;function validateTime(d){if(!d){return}var b=d;var f=b.childNodes;for(var c=0;c<f.length;c++){if(f[c].hasError){f[c].removeError()}if(f[c].hasWarning){f[c].removeWarning()}}var a=true;var g="Field(s) out of range";textEntry="";startTime=0;endTime=0;for(var c=0;c<f.length;c++){var e=f[c].name;if(e=="startH[]"){if(!f[c].value||isNaN(f[c].value)||parseInt(f[c].value)<0){writeError(f[c],g,a);a=false}else{startTime+=parseFloat(f[c].value)*3600;f[c].value=getHours(startTime)}}else{if(e=="startM[]"){if(!f[c].value||isNaN(f[c].value)||parseInt(f[c].value)<0||parseInt(f[c].value)>=60){writeError(f[c],g,a);a=false}else{startTime+=parseFloat(f[c].value)*60;f[c].value=getMins(startTime)}}else{if(e=="startS[]"){if(!f[c].value||isNaN(f[c].value)||parseInt(f[c].value)<0||parseInt(f[c].value)>=60){writeError(f[c],g,a);a=false}else{startTime+=parseFloat(f[c].value);f[c].value=getSecs(startTime)}}else{if(e=="endH[]"){if(!f[c].value||isNaN(f[c].value)||parseInt(f[c].value)<0){writeError(f[c],g,a);a=false}else{endTime+=parseFloat(f[c].value)*3600;f[c].value=getHours(endTime)}}else{if(e=="endM[]"){if(!f[c].value||isNaN(f[c].value)||parseInt(f[c].value)<0||parseInt(f[c].value)>=60){writeError(f[c],g,a);a=false}else{endTime+=parseFloat(f[c].value)*60;f[c].value=getMins(endTime)}}else{if(e=="endS[]"){if(!f[c].value||isNaN(f[c].value)||parseInt(f[c].value)<0||parseInt(f[c].value)>=60){writeError(f[c],g,a);a=false}else{endTime+=parseFloat(f[c].value);f[c].value=getSecs(endTime)}}else{if(e=="textEntry[]"){textEntry=f[c].value}else{if(e=="saved[]"){savedEntry=f[c].value!="false"}else{if(e=="modified[]"){modifiedEntry=f[c].value!="false"}else{if(e=="captionMode[]"){captionType=f[c].value!="false"}else{if(e=="userSetStart[]"){userSetStart=f[c].value!="false"}else{if(e=="userSetEnd[]"){userSetEnd=f[c].value!="false"}}}}}}}}}}}}}if(a){if(startTime>endTime){for(var c=0;c<f.length;c++){if(f[c]&&f[c].nodeName&&f[c].nodeName.toLowerCase()=="input"){writeError(f[c],"Start time must be before end time",a);a=false}}}else{if(startTime>getDuration()||endTime>getDuration()){for(var c=0;c<f.length;c++){if(f[c]&&f[c].nodeName&&f[c].nodeName.toLowerCase()=="input"){writeError(f[c],"Time limited to video's duration",a);a=false}}}}}return a}function writeError(d,b,a){if(d.hasError){return}d.className+=" error";d.removeError=removeError;var c=document.createElement("div");c.className="error";c.appendChild(document.createTextNode(b));d.firstError=a;if(a){d.parentNode.appendChild(c)}d.hasError=c}function removeError(){this.className=this.className.substring(0,this.className.lastIndexOf(" "));if(this.firstError){this.parentNode.removeChild(this.hasError)}this.hasError=null;this.removeError=null;this.firstError=false}function writeWarning(d,b,a){if(d.hasWarning){return}d.className+=" warning";d.removeWarning=removeWarning;var c=document.createElement("div");c.className="warning";c.appendChild(document.createTextNode(b));d.firstWarning=a;if(a){d.parentNode.appendChild(c)}d.hasWarning=c}function removeWarning(){this.className=this.className.substring(0,this.className.lastIndexOf(" "));if(this.firstWarning){this.parentNode.removeChild(this.hasWarning)}this.hasWarning=null;this.removeWarning=null;this.firstWarning=false}function removeAllWarnings(){var e=document.getElementById("captionInput");if(!e){return}var b=e.childNodes;for(var d=0;d<b.length;d++){if(b[d].className=="captionBox"){var a=b[d];var f=a.childNodes;for(var c=0;c<f.length;c++){if(f[c].hasWarning){f[c].removeWarning()}}}}}function getPreviousEnd(b){var c=0;subList.resetIterator();while(subList.hasNext()){var a=subList.getCurrent();if(b<=a[1][0]){break}else{if(b>a[1][0]&&b<=a[1][1]){break}else{c=a[1][1];subList.moveToNext()}}}return c}function guessStart(){var g=2;var a=0;var e=currSubBox;validateTime(e);if(userSetStart||userSetEnd){return}if(predictionEnabled){var c=new Array();c=textEntry.split(" ");for(var d=0;d<c.length;d++){a+=c[d].length*0.075}a+=(c.length-1)*0.1}if(a<g){a=g}var b=getPreviousEnd(endTime);startTime=endTime-a;startTime=startTime>=b?startTime:b;var h=e.childNodes;for(var d=0;d<h.length;d++){var f=h[d].name;if(f=="startH[]"){h[d].value=""+getHours(startTime)}else{if(f=="startM[]"){h[d].value=""+getMins(startTime)}else{if(f=="startS[]"){h[d].value=""+getSecs(startTime)}else{if(f=="endH[]"){h[d].value=""+getHours(endTime)}else{if(f=="endM[]"){h[d].value=""+getMins(endTime)}else{if(f=="endS[]"){h[d].value=""+getSecs(endTime)}}}}}}}setModifiedSubtitle()}function getNextStart(b){var c=getDuration();subList.resetIterator();while(subList.hasNext()){var a=subList.getCurrent();if(b<a[1][0]){c=a[1][0];break}else{subList.moveToNext()}}return c}function guessEnd(){var g=2;var d=0;var c=currSubBox;validateTime(c);if(userSetEnd||userSetStart){return}if(predictionEnabled){var a=new Array();a=textEntry.split(" ");for(var b=0;b<a.length;b++){d+=a[b].length*0.075}d+=(a.length-1)*0.1}if(d<g){d=g}var f=getNextStart(startTime);endTime=startTime+d;endTime=endTime<=f?endTime:f;var h=c.childNodes;for(var b=0;b<h.length;b++){var e=h[b].name;if(e=="startH[]"){h[b].value=""+getHours(startTime)}else{if(e=="startM[]"){h[b].value=""+getMins(startTime)}else{if(e=="startS[]"){h[b].value=""+getSecs(startTime)}else{if(e=="endH[]"){h[b].value=""+getHours(endTime)}else{if(e=="endM[]"){h[b].value=""+getMins(endTime)}else{if(e=="endS[]"){h[b].value=""+getSecs(endTime)}}}}}}}setModifiedSubtitle()}var predictionEnabled=true;var captionModeEnabled=true;function guessTime(){if(captionModeEnabled){guessStart()}else{guessEnd()}}function gotoPoint(b){pause();var a=b.parentNode;if(validateTime(a)){if(getCurrentTime()>endTime||getCurrentTime()<startTime){seekTo(endTime)}}}function insertSubtitle(){if(!subEnabled){return}if(embededMode){return}if(currSubBox){return}var c=getInnerText(document.getElementById("sub"));var a=document.getElementById("captionBox").cloneNode(true);a.id="";a.style.display="block";var b=document.getElementById("captionInput");b.appendChild(a);currSubBox=a}function getPreviousSubtitle(){subList.resetIterator();var b=getCurrentTime();var a=null;while(subList.hasNext()){var c=subList.getCurrent();if(b<c[1][1]){break}else{if(b>=c[1][1]){a=c}}subList.moveToNext()}if(a){seekTo(a[1][1])}}function getNextSubtitle(){var a=getCurrentTime();subList.resetIterator();while(subList.hasNext()){var b=subList.getCurrent();if(b[1][0]>a){seekTo(b[1][1]);return}subList.moveToNext()}}function updateTimeField(g,b){if(!g){return}var a=g.name.substring(0,3)!="end";var f=g.parentNode.childNodes;for(var c=0;c<f.length;c++){var d=f[c].name;if((a&&d=="userSetStart[]")||(!a&&d=="userSetEnd[]")){f[c].value="true";break}}if(b&&isNumeric(g.value)){var e=0;if(b=="up"){e=1}else{e=-1}if((g.name=="startH[]"||g.name=="endH[]")&&(parseInt(g.value)+e<=9)&&(parseInt(g.value)+e>=0)){g.value=parseInt(g.value)+e}else{if((g.name=="startM[]"||g.name=="endM[]")&&(parseFloat(g.value)+e<=59)&&(parseFloat(g.value)+e>=0)){g.value=parseFloat(g.value)+e}else{if((g.name=="startS[]"||g.name=="endS[]")&&(parseFloat(g.value)+e<=59.9)&&(parseFloat(g.value)+e>=0)){g.value=parseFloat(g.value)+e}}}}validateTime(currSubBox);setModifiedSubtitle()}function cancelEditingSubtitle(){var a=currSubBox;validateTime(a);if(!savedEntry){removeSubtitle()}else{a.style.display="none"}currSubBox=null}function saveSubtitle(){var a=currSubBox;if(!validateTime(currSubBox)){setModifiedSubtitle();setSavedSubtitle();showSavedSubtitle(false);return true}if(cwi.util.trim(textEntry)==""){removeSubtitle();return}if(!modifiedEntry){return true}if(savedEntry){updateSubtitle(a)}else{addSubtitle(a)}showSavedSubtitle(true);setSavedSubtitle();return true}function addSubtitle(b){removeAllWarnings();if(validateTime(b)){setSavedSubtitle();var c=false;var d=new Array(b,new Array(startTime,endTime,textEntry));subList.resetIterator();while(subList.hasNext()){var a=subList.getCurrent();if(!match(a[1][0],a[1][1],d[1][0],d[1][1])){}else{if(!match(d[1][0],d[1][1],a[1][0],a[1][1])){subList.moveToPrevious();subList.insert(d);c=true;break}else{if(a[1][0]<=d[1][0]){a[1][1]=d[1][0];matchEndStart(a[0],d[0],true)}else{a[1][0]=d[1][1];if(a[1][0]>a[1][1]){a[1][1]=a[1][0];matchStartEnd(a[0],d[0],true,true)}else{matchStartEnd(a[0],d[0],true,false)}subList.moveToPrevious();subList.insert(d);c=true;break}}}subList.moveToNext()}if(!c){subList.insertEnd(d)}}issueSubtitles()}function updateSubtitle(d){removeAllWarnings();subList.resetIterator();var a=null;while(subList.hasNext()){var g=subList.getCurrent();if(g[0]==d){a=g;break}subList.moveToNext()}if(a&&!validateTime(d)){subList.remove()}else{if(a&&startTime==a[1][0]&&endTime==a[1][1]){a[1][2]=textEntry}else{var b=true;if(!b){}else{subList.remove();var h=new Array(d,new Array(startTime,endTime,textEntry));var f=false;var c=null;subList.resetIterator();while(subList.hasNext()){a=subList.getCurrent();if(!match(a[1][0],a[1][1],h[1][0],h[1][1])){}else{if(!match(h[1][0],h[1][1],a[1][0],a[1][1])){if(!f){c=a;f=true}break}else{if(a[1][0]<h[1][0]){a[1][1]=h[1][0];matchEndStart(a[0],h[0],true)}else{a[1][0]=h[1][1];if(a[1][0]>a[1][1]){a[1][1]=a[1][0];matchStartEnd(a[0],h[0],true,true)}else{matchStartEnd(a[0],h[0],true,false)}if(!f){c=a;f=true}}}}subList.moveToNext()}if(f){subList.resetIterator();while(subList.hasNext()){a=subList.getCurrent();if(a[0]==c[0]){subList.moveToPrevious();subList.insert(h);break}subList.moveToNext()}}else{subList.insertEnd(h)}}}setSavedSubtitle();if(getCurrentTime()>endTime||getCurrentTime()<startTime){seekTo(endTime)}}issueSubtitles()}function removeSubtitle(){var b=currSubBox;subList.resetIterator();while(subList.hasNext()){var a=subList.getCurrent();if(a[0]==b){subList.remove();break}subList.moveToNext()}b.parentNode.removeChild(b);issueSubtitles();currSubBox=null}function removeAllSubtitles(){var c=true;if(!document.getElementById("captionInput")){return}if(subList.getSize()>0){var a=window.confirm("If you continue, all entries will be removed and it is irreversible. Are you sure you want to continue?");c=a;if(a){pause();var b=document.getElementById("captionInput");while(b.firstChild){b.removeChild(b.firstChild)}subList=new DoubleLinkedList();currSubBox=null;issueSubtitles()}}else{}return c}function issueSubtitles(){stDoc.clearTimingEntries();stDoc.addTimingEntry(cwi.smilText.Render.clearLayout,new Array(stDoc,"sub",true),0);subList.resetIterator();while(subList.hasNext()){var a=subList.getCurrent();stDoc.addTimingEntry(cwi.smilText.Render.appendText,new Array(stDoc,a[1][2],"sub",true),a[1][0]*1000);stDoc.addTimingEntry(cwi.smilText.Render.clearLayout,new Array(stDoc,"sub",true),a[1][1]*1000);subList.moveToNext()}}function populateSubtitles(){subList=new DoubleLinkedList();var e=-1;var b=-1;var d=null;var c=stDoc.getTimingEntries();c.resetIterator();while(c.hasNext()){var a=c.getCurrent();switch(a[1][0]){case cwi.smilText.Render.appendText:if(e<0){e=0}if(d){d+=" "+a[1][1][1]}else{d=a[1][1][1]}break;case cwi.smilText.Render.appendContainer:case cwi.smilText.Render.appendLineBreak:if(d){d+=" "}break;case cwi.smilText.Render.clearLayout:case cwi.smilText.Render.closeContainer:default:if(!d){e=a[0]}else{b=a[0];createSubtitleEntry(e,b,d);e=b;d=null}break}c.moveToNext()}if(d){if(getDuration()>0){b=getDuration()*1000}else{b=e+5000}createSubtitleEntry(e,b,d)}issueSubtitles()}function createSubtitleEntry(b,d,h){b=b/1000;d=d/1000;var g=document.getElementById("captionBox").cloneNode(true);g.id="";g.style.display="none";var j=document.getElementById("captionInput");j.appendChild(g);var a=g.childNodes;for(var e=0;e<a.length;e++){var f=a[e].name;if(f=="startH[]"){a[e].value=getHours(b)}else{if(f=="startM[]"){a[e].value=getMins(b)}else{if(f=="startS[]"){a[e].value=getSecs(b)}else{if(f=="endH[]"){a[e].value=getHours(d)}else{if(f=="endM[]"){a[e].value=getMins(d)}else{if(f=="endS[]"){a[e].value=getSecs(d)}else{if(f=="textEntry[]"){a[e].value=h}else{if(f=="saved[]"){a[e].value="true"}else{if(f=="userSetStart[]"){a[e].value="true"}else{if(f=="userSetEnd[]"){a[e].value="true"}}}}}}}}}}}var c=new Array(g,new Array(b,d,h));subList.insertEnd(c)}var commentMode=false;var captionMode=false;function setCommentMode(){if(hasExternalSubtitles()){return}if(!captionMode&&!commentMode){commentMode=true}}function setCaptionMode(){if(oPanel){oPanel.hide()}if(hasExternalSubtitles()){return}if(getSavedSubtitle()){return}if(!captionMode&&!commentMode){captionMode=true;endTime=getCurrentTime()+0.1<=getDuration()?getCurrentTime()+0.1:getDuration();startTime=getCurrentTime();var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="startH[]"){c[a].value=""+getHours(startTime)}else{if(b=="startM[]"){c[a].value=""+getMins(startTime)}else{if(b=="startS[]"){c[a].value=""+getSecs(startTime)}else{if(b=="endH[]"){c[a].value=""+getHours(endTime)}else{if(b=="endM[]"){c[a].value=""+getMins(endTime)}else{if(b=="endS[]"){c[a].value=""+getSecs(endTime)}}}}}}}setCaptionModeSubtitle()}else{if(commentMode){endTime=getCurrentTime()+0.1<=getDuration()?getCurrentTime()+0.1:getDuration();startTime=getCurrentTime();var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="startH[]"){c[a].value=""+getHours(startTime)}else{if(b=="startM[]"){c[a].value=""+getMins(startTime)}else{if(b=="startS[]"){c[a].value=""+getSecs(startTime)}else{if(b=="endH[]"){c[a].value=""+getHours(endTime)}else{if(b=="endM[]"){c[a].value=""+getMins(endTime)}else{if(b=="endS[]"){c[a].value=""+getSecs(endTime)}}}}}}}setCommentModeSubtitle()}}}function resetSubtitles(){if(removeAllSubtitles()){if(oPanel){oPanel.hide()}resetCaptionMode();resetCommentMode();return true}return false}function resetCaptionMode(){if(oPanel){oPanel.hide()}if(hasExternalSubtitles()){return}captionMode=false;if(currSubBox){if(getSavedSubtitle()&&!validateTime(currSubBox)){return}if(modifiedEntry){saveSubtitle()}cancelEditingSubtitle()}insertSubtitle()}function resetCommentMode(){if(hasExternalSubtitles()){return}if(commentMode){commentMode=false;if(currSubBox){if(getSavedSubtitle()&&!validateTime(currSubBox)){return}if(modifiedEntry){saveSubtitle()}cancelEditingSubtitle()}insertSubtitle()}}function showCurrentSubtitleBox(){var d=getCurrentTime();var e=null;if(getSavedSubtitle()&&!validateTime(currSubBox)){showSavedSubtitle(false);return}subList.resetIterator();while(subList.hasNext()){var a=subList.getCurrent();if(d<a[1][0]){break}if(d>=a[1][0]&&d<=a[1][1]){e=a[0]}subList.moveToNext()}if(e){if(currSubBox&&saveSubtitle()){cancelEditingSubtitle()}else{return}currSubBox=e;e.style.display="";var b=null;var g=e.childNodes;for(var c=0;c<g.length;c++){var f=g[c].name;if(f=="textEntry[]"){b=g[c]}else{if(f=="captionMode[]"){captionMode=g[c].value=="true";commentMode=!captionMode}}}b.focus()}}function showAllSubtitleBoxes(){var b=getCurrentTime();var c=null;subList.resetIterator();while(subList.hasNext()){var a=subList.getCurrent();a[0].style.display="";if(b<a[1][0]){}else{if(b>=a[1][0]&&b<=a[1][1]){c=a[0]}}subList.moveToNext()}if(c){if(currSubBox&&saveSubtitle()){cancelEditingSubtitle()}else{return}currSubBox=c}}function setModifiedStart(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="userSetStart[]"){c[a].value="true";break}}setModifiedSubtitle()}}function setModifiedEnd(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="userSetEnd[]"){c[a].value="true";break}}setModifiedSubtitle()}}function setModifiedSubtitle(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="modified[]"){c[a].value="true";break}}}}function unsetModifiedSubtitle(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="modified[]"){c[a].value="false";break}}}}function getModifiedSubtitle(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="modified[]"){return c[a].value!="false"}}}return false}function setSavedSubtitle(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="saved[]"){c[a].value="true";break}}unsetModifiedSubtitle()}}function getSavedSubtitle(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="saved[]"){return c[a].value!="false"}}}return false}function setCaptionModeSubtitle(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="captionMode[]"){c[a].value="true";break}}}}function setCommentModeSubtitle(){if(currSubBox){var c=currSubBox.childNodes;for(var a=0;a<c.length;a++){var b=c[a].name;if(b=="captionMode[]"){c[a].value="false";break}}}}var externalSub=false;function hasExternalSubtitles(){return externalSub}function setExternalSubtitles(a){externalSub=a}function goHome(){var a=new Fx.Scroll(window,{duration:1500,wait:false,transition:Fx.Transitions.quadInOut});a.toElement("find")}function goPlay(){var a=new Fx.Scroll(window,{duration:1500,wait:false,transition:Fx.Transitions.quadInOut});a.toElement("play")}function goAbout(){var a=new Fx.Scroll(window,{duration:1500,wait:false,transition:Fx.Transitions.quadInOut});a.toElement("about")}function showLoadingDialog(){YAHOO.namespace("example.container");if(!YAHOO.example.container.wait){YAHOO.example.container.wait=new YAHOO.widget.Panel("wait",{width:"240px",fixedcenter:true,close:false,draggable:false,zindex:4,modal:true,visible:false});YAHOO.example.container.wait.setHeader("Processing, please wait...");YAHOO.example.container.wait.setBody('<img src="http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif"/>');YAHOO.example.container.wait.render(document.body)}YAHOO.example.container.wait.show()}function hideLoadingDialog(){YAHOO.example.container.wait.hide()}var currentLanguage=null;function googleTranslator(a){google.language.detect(a,function(b){if(!b.error&&b.language){google.language.translate(a,b.language,currentLanguage,function(c){if(c.translation){stDoc.updateDictionary(a,c.translation)}else{stDoc.updateDictionary(a,a)}})}else{stDoc.updateDictionary(a,a)}})}function translate(a){showLoadingDialog();if(currSubBox){if(getSavedSubtitle()&&!validateTime(currSubBox)){return}if(modifiedEntry){saveSubtitle()}cancelEditingSubtitle()}insertSubtitle();currentLanguage=a;if(a&&a!=""){stDoc.setTranslateFunction(googleTranslator)}else{stDoc.setTranslateFunction(null)}seekTo(0);hideLoadingDialog()}YAHOO.namespace("example.container");YAHOO.namespace("example.keylistener");var panel_six,panel_seven,oPanel,panel_help,color_picker,panel_options;YAHOO.example.panels=function(){panel_six=new YAHOO.widget.Panel("sidebar1",{close:true,visible:false,draggable:true,underlay:"none",context:["videoInfo","tl","tr"],effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.2}});panel_six.hideEvent.subscribe(function(){infoBarVisible=false});panel_six.render();panel_seven=new YAHOO.widget.Panel("sidebar2",{close:true,visible:false,draggable:true,underlay:"none",context:["translateBar","bl","br"],effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.2}});panel_seven.hideEvent.subscribe(function(){translateBarVisible=false});panel_seven.render();oPanel=new YAHOO.widget.Panel("sidebar3",{close:true,visible:false,underlay:"none",context:["show-dialog-1","bl","br"],effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.2}});oPanel.render(document.body);panel_help=new YAHOO.widget.Panel("sidebar4",{close:true,visible:false,draggable:true,underlay:"none",context:["helpButton","tl","tr"],effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.2}});panel_help.hideEvent.subscribe(function(){helpVisible=false});panel_help.render();panel_options=new YAHOO.widget.Panel("sidebar6",{close:true,visible:false,draggable:true,underlay:"none",context:["optionsPanel","tl","tr"],effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.2}});panel_options.hideEvent.subscribe(function(){optionsBarVisible=false});panel_options.render();YAHOO.example.keylistener.kpl0=new YAHOO.util.KeyListener(document,{ctrl:true,alt:true,shift:false,keys:[80]},{fn:handlePlayback});YAHOO.example.keylistener.kpl0.enable();YAHOO.example.keylistener.kpl1=new YAHOO.util.KeyListener(document,{ctrl:false,alt:false,shift:false,keys:[13]},{fn:handleSmartMode});YAHOO.example.keylistener.kpl1.enable();YAHOO.example.keylistener.kpl2=new YAHOO.util.KeyListener(document,{ctrl:false,alt:false,shift:false,keys:[37]},{fn:handleSeekLeft});YAHOO.example.keylistener.kpl2.enable();YAHOO.example.keylistener.kpl3=new YAHOO.util.KeyListener(document,{ctrl:false,alt:false,shift:false,keys:[39]},{fn:handleSeekRight});YAHOO.example.keylistener.kpl3.enable();YAHOO.example.keylistener.kpl6=new YAHOO.util.KeyListener(document,{ctrl:true,alt:true,shift:false,keys:[77]},{fn:handleVolume});YAHOO.example.keylistener.kpl6.enable();YAHOO.example.keylistener.kpl7=new YAHOO.util.KeyListener(document,{ctrl:true,alt:true,shift:false,keys:[85]},{fn:handleSubtitles});YAHOO.example.keylistener.kpl7.enable();YAHOO.example.keylistener.kpl8=new YAHOO.util.KeyListener(document,{ctrl:true,alt:true,shift:false,keys:[73]},{fn:handleVideoInfo});YAHOO.example.keylistener.kpl8.enable();YAHOO.example.keylistener.kpl9=new YAHOO.util.KeyListener(document,{ctrl:true,alt:true,shift:false,keys:[84]},{fn:handleTranslateBar});YAHOO.example.keylistener.kpl9.enable();YAHOO.example.keylistener.kpl10=new YAHOO.util.KeyListener(document,{ctrl:true,alt:true,shift:false,keys:[72]},{fn:handleHelp});YAHOO.example.keylistener.kpl10.enable()};YAHOO.util.Event.addListener(window,"load",YAHOO.example.panels);function handleSubtitles(){if(subEnabled){turnOffSubtitles()}else{turnOnSubtitles()}}function handleColorPickerCancel(){handleColorPickup()}function handleColorPickerSubmit(){handleColorPickup()}function handleSeekLeft(){if(player){if(getCurrentVideoURL()){if(getPlayerState()===1){var a=getCurrentTime()-10;a>=0?seekTo(a):seekTo(0)}}}}function handleSeekRight(){if(player){if(getCurrentVideoURL()){if(getPlayerState()===1){var a=getCurrentTime()+10;a<=getDuration()?seekTo(a):seekTo(getDuration())}}}}function handleVolume(){if(currentMute){unMute()}else{mute()}}var isSmartMode=true;function disableSmartMode(){isSmartMode=false}function enableStartMode(){isSmartMode=true}function handleSmartMode(){if(player&&isSmartMode){if(getCurrentVideoURL()){if(getPlayerState()===1){pause();if(subEnabled){var a=null;var d=currSubBox.childNodes;for(var b=0;b<d.length;b++){var c=d[b].name;if(c=="textEntry[]"){a=d[b]}}if(a){a.focus()}}else{}}else{if(getPlayerState()===2){play()}else{if(getPlayerState()===0){play()}}}}}}function handlePlayback(){if(player){if(getCurrentVideoURL()){if(getPlayerState()===1){pause()}else{if(getPlayerState()===2){play()}else{if(getPlayerState()===0){play()}}}}}}var infoBarVisible=false;var translateBarVisible=false;var helpVisible=false;var colorBarVisible=false;var optionsBarVisible=false;function handleColorPickup(){if(colorBarVisible){colorBarVisible=false}else{panel_seven.hide();oPanel.hide();panel_help.hide();panel_six.hide();panel_options.hide();colorBarVisible=true}}function handleVideoInfo(){if(infoBarVisible){panel_six.hide();infoBarVisible=false}else{panel_seven.hide();oPanel.hide();panel_help.hide();panel_options.hide();panel_six.show();infoBarVisible=true}}function handleTranslateBar(){if(translateBarVisible){panel_seven.hide();translateBarVisible=false}else{panel_six.hide();oPanel.hide();panel_help.hide();panel_options.hide();panel_seven.show();translateBarVisible=true}}function handleHelp(){if(helpVisible){panel_help.hide();helpVisible=false}else{panel_six.hide();panel_seven.hide();oPanel.hide();panel_options.hide();panel_help.show();helpVisible=true}}function handleOptions(){if(optionsBarVisible){panel_options.hide();optionsBarVisible=false}else{panel_six.hide();panel_seven.hide();oPanel.hide();panel_help.hide();panel_options.show();optionsBarVisible=true}}function changeActionSubForm(a){document.subForm.action=a}function checkDownload(){if(getCurrentVideoURL()){pause();saveSubtitle();document.subForm.target=null;document.subForm.videoURL.value=getCurrentVideoURL();changeActionSubForm("php/get_smil_transcript.php");document.subForm.submit()}else{alert(selectVideo);goHome()}}function getPublishURL(){if(getCurrentVideoURL()){panel_six.hide();panel_seven.hide();panel_help.hide();panel_options.hide();pause();saveSubtitle();var a=document.subForm.name.value;if(!document.subForm.name.value||document.subForm.name.value==""){a=document.subForm.name.value=Math.random()*1000000000}else{if(document.subForm.videoURL.value!=getCurrentVideoURL()){a=document.subForm.name.value=Math.random()*1000000000}}var b=document.subForm.videoURL.value=getCurrentVideoURL();document.subForm.target="publishSmilText";changeActionSubForm("php/save_transcript.php");window.open("about:blank","publishSmilText","width=600,height=600");setTimeout("document.subForm.submit()",200);oPanel.show();if(document.getElementById("inputEmbedCode")){document.getElementById("inputEmbedCode").value=getWebAppURL()+"/embed.php?videoURL="+getCurrentVideoURL()+"&subURL="+getWebAppURL()+"/tmp/"+a+".xml";document.getElementById("inputEmbedCode").select();showPublishedStory(true)}}else{alert(selectVideo);goHome()}}function shareOnFacebook(){if(getCurrentVideoURL()){panel_six.hide();panel_seven.hide();panel_help.hide();panel_options.hide();pause();saveSubtitle();var a=document.subForm.name.value;if(!document.subForm.name.value||document.subForm.name.value==""){a=document.subForm.name.value=Math.random()*1000000000}else{if(document.subForm.videoURL.value!=getCurrentVideoURL()){a=document.subForm.name.value=Math.random()*1000000000}}var c=document.subForm.videoURL.value=getCurrentVideoURL();document.subForm.target="publishSmilText";var b=jQuery.noConflict();b.ajax({type:"POST",url:"php/save_transcript.php",data:b("#subForm").serialize(),beforeSend:function(){},success:function(){try{callPublish(getWebAppURL()+"/embed.php?videoURL="+getCurrentVideoURL()+"&subURL="+getWebAppURL()+"/tmp/"+a+".xml");showPublishedStory(true)}catch(d){showPublishedStory(false)}}})}else{alert(selectVideo);goHome()}}function showPublishedStory(a){var b=jQuery.noConflict();if(a){b.gritter.add({title:"Your comments have been saved!",text:"And your story is now ready to be shared!",image:"images/Ok.png"})}else{b.gritter.add({title:"Problem found!",text:"It was not possible to share your story on Facebook. Try it later.",image:"images/No.png"})}}function showSavedSubtitle(a){var b=jQuery.noConflict();if(a){b.gritter.add({title:"Your comment has been saved!",text:"To see the result go back a few seconds. To edit an entry click on it when it is presented.",image:"images/Ok.png"})}else{b.gritter.add({title:"Comment NOT saved!",text:"A comment has NOT been saved! Look for errors in the caption box and try to fix it.",image:"images/No.png"})}}function isNumeric(b){var d="0123456789.";var c=true;var a;for(i=0;i<b.length&&c==true;i++){a=b.charAt(i);if(d.indexOf(a)==-1){c=false}}return c}function getInnerText(a){return a.textContent?a.textContent:a.innerText}function gup(b){b=b.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var a="[\\?&]"+b+"=([^&#]*)";var d=new RegExp(a);var c=d.exec(window.location.href);if(c==null){return null}else{return c[1]}}function clearOnFocus(a){if(a.value==a.title){a.value=""}}function fillOnBlur(a){if(a.value==""){a.value=a.title}}function getWebAppURL(){var a=window.location;if(a.pathname&&a.pathname!="/"){return a.protocol+"//"+a.host+a.pathname}else{return a.protocol+"//"+a.host}};
