/**
 * 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.Time");Import("cwi.adt.Hashtable");Import("cwi.adt.DoubleLinkedList");cwi.smilText.Time.EVENT_PLAY=1;cwi.smilText.Time.EVENT_PAUSE=2;cwi.smilText.Time.EVENT_STOP=0;cwi.smilText.Time.EVENT_END=-1;cwi.smilText.Time.Playable=function(){JSINER.extend(this);this.timeNow=-1;this.state=0;this.externalClock=null;this.eventListeners=new Hashtable();this.eventListeners.put(cwi.smilText.Time.EVENT_STOP,new DoubleLinkedList());this.eventListeners.put(cwi.smilText.Time.EVENT_PLAY,new DoubleLinkedList());this.eventListeners.put(cwi.smilText.Time.EVENT_PAUSE,new DoubleLinkedList());this.eventListeners.put(cwi.smilText.Time.EVENT_END,new DoubleLinkedList())};cwi.smilText.Time.Playable.prototype.addEventListener=function(a,c){var b=this.eventListeners.get(a);if(b&&c){this.removeEventListener(a,c);b.insertEnd(c)}};cwi.smilText.Time.Playable.prototype.removeEventListener=function(a,c){var b=this.eventListeners.get(a);if(b&&c){b.resetIterator();while(b.hasNext()){if(b.getCurrent()==c){b.remove();break}b.moveToNext()}}};cwi.smilText.Time.Playable.prototype.fireEvent=function(a){switch(a){case cwi.smilText.Time.EVENT_PLAY:this.play();break;case cwi.smilText.Time.EVENT_PAUSE:this.pause();break;case cwi.smilText.Time.EVENT_STOP:this.stop();break;case cwi.smilText.Time.EVENT_END:if(this.isPlaying()){this.state=cwi.smilText.Time.EVENT_END;this.notifyAll(a);this.notifyAll(cwi.smilText.Time.EVENT_STOP)}break}};cwi.smilText.Time.Playable.prototype.notifyAll=function(a){var b=this.eventListeners.get(a);if(b){b.resetIterator();while(b.hasNext()){b.getCurrent().apply(this);b.moveToNext()}}};cwi.smilText.Time.Playable.prototype.getTime=function(){return this.timeNow};cwi.smilText.Time.Playable.prototype.setTime=function(a){if(this.isStopped()||this.isPaused()){return}if(this.timeNow>a){this.seekTo(a)}this.timeNow=a};cwi.smilText.Time.Playable.prototype.play=function(){if(this.externalClock==undefined||this.externalClock==null){this.setExternalClock(false)}if(this.state!=cwi.smilText.Time.EVENT_PLAY){if(this.state==cwi.smilText.Time.EVENT_STOP||this.state==cwi.smilText.Time.EVENT_END){this.seekTo(0)}this.notifyAll(cwi.smilText.Time.EVENT_PLAY)}this.state=cwi.smilText.Time.EVENT_PLAY};cwi.smilText.Time.Playable.prototype.isPlaying=function(){return this.state==cwi.smilText.Time.EVENT_PLAY};cwi.smilText.Time.Playable.prototype.pause=function(){if(this.state==cwi.smilText.Time.EVENT_PLAY){this.notifyAll(cwi.smilText.Time.EVENT_PAUSE);this.state=cwi.smilText.Time.EVENT_PAUSE}};cwi.smilText.Time.Playable.prototype.isPaused=function(){return this.state==cwi.smilText.Time.EVENT_PAUSE};cwi.smilText.Time.Playable.prototype.stop=function(){if(this.state!=cwi.smilText.Time.EVENT_STOP){this.notifyAll(cwi.smilText.Time.EVENT_STOP)}this.seekTo(0);this.state=cwi.smilText.Time.EVENT_STOP};cwi.smilText.Time.Playable.prototype.isStopped=function(){return this.state==cwi.smilText.Time.EVENT_STOP};cwi.smilText.Time.Playable.prototype.seekTo=function(a){this.timeNow=a};cwi.smilText.Time.Playable.prototype.setExternalClock=function(a){if(this.externalClock==a){return}this.externalClock=a;if(!this.externalClock){cwi.smilText.Time.getInstance().register(this)}else{cwi.smilText.Time.getInstance().unregister(this)}};Import("cwi.adt.DoubleLinkedList");Import("cwi.smilText.Time.Playable");cwi.smilText.Time.instance=new function(){this.list=new DoubleLinkedList();this.timeInc=100;this.register=function(a){this.list.insertEnd(a)};this.unregister=function(a){this.list.resetIterator();while(this.list.hasNext()){var b=this.list.getCurrent();if(b===a){this.list.remove();return true}this.list.moveToNext()}return false};this.updateTime=function(){this.list.resetIterator();while(this.list.hasNext()){var a=this.list.getCurrent();a.setTime(a.getTime()+this.timeInc);this.list.moveToNext()}setTimeout("cwi.smilText.Time.getInstance().updateTime()",this.timeInc)};this.updateTime()};cwi.smilText.Time.register=function(a){cwi.smilText.Time.getInstance().register(a)};cwi.smilText.Time.unregister=function(a){return cwi.smilText.Time.getInstance().unregister(a)};cwi.smilText.Time.getInstance=function(){return cwi.smilText.Time.instance};
