CwMedia=new Class({Implements:[Events,Options],options:{implementMediaAdditions:true},video:false,defaultPlaybackRate:1,startTime:0,duration:0,initialize:function(element,options)
{this.video=$(element);this.setOptions(options);if(this.options.implementMediaAdditions){CwMedia.mediaProperties={videoWidth:false,videoHeight:false,poster:true,error:false,networkState:false,preload:true,buffered:false,readyState:false,seeking:false,currentTime:true,startTime:false,duration:false,paused:true,defaultPlaybackRate:true,playbackRate:true,played:false,seekable:false,ended:false,autoplay:true,loop:true,controls:true,volume:true,muted:true,autobuffer:true};$each(CwMedia.mediaProperties,function(is_writable,key){if(is_writable){Element.Properties.set(key,{set:function(avalue){this[key]=avalue;},get:function(){return this[key];}});}else{Element.Properties.set(key,{get:function(){return this[key];}});}});CwMedia.mediaEvents={loadstart:2,progress:2,suspend:2,abort:2,error:2,emptied:2,stalled:2,play:2,pause:2,loadedmetadata:2,loadeddata:2,waiting:2,playing:2,canplay:2,canplaythrough:2,seeking:2,seeked:2,timeupdate:2,ended:2,ratechange:2,durationchange:2,volumechange:2};Element.NativeEvents=$merge(Element.NativeEvents,CwMedia.mediaEvents);}
this.video.addEvents({durationchange:function(event){if($defined(this.video.get('duration'))){this.duration=this.video.get('duration');};if($defined(this.video.get('startTime'))){this.startTime=this.video.get('startTime');};if($defined(event.target.get('defaultPlaybackRate'))){this.defaultPlaybackRate=event.target.get('defaultPlaybackRate');};}.bind(this),loadstart:function(event){this.fireEvent('playbackHasChanged',event);}.bind(this),pause:function(event){this.fireEvent('playbackHasChanged',event);}.bind(this),play:function(event){this.fireEvent('playbackHasChanged',event);}.bind(this)});},rewind:function()
{this.video.set('currentTime',this.startTime);},move:function(secs)
{re=/^([0-9]*):([0-9]+)$/;matches=secs.toString().match(re);if(matches&&matches.length==3){newtime=this.startTime+matches[1].toInt()*60+matches[2].toInt();}
else{newtime=this.video.get('currentTime')+parseFloat(secs);}
this.video.set('currentTime',newtime.limit(this.startTime,this.startTime+this.duration));},toggleMute:function()
{this.video.set('muted',!this.video.get('muted'));},volumeChange:function(amount)
{newvol=this.video.get('volume')+parseFloat(amount);this.video.set('volume',newvol.limit(0.0,1.0));},play:function()
{this.video.play();},pause:function()
{this.video.pause();},togglePlay:function()
{if(this.video.paused){this.video.play();}
else{this.video.pause();}},stop:function()
{this.rewind();this.video.pause();},getNetworkState:function()
{switch(this.video.get('networkState')){case this.video.NETWORK_EMPTY:return'empty';case this.video.NETWORK_IDLE:return'idle';case this.video.NETWORK_LOADING:return'loading';case this.video.NETWORK_LOADED:return'loaded';case this.video.NETWORK_NO_SOURCE:return'no source';default:return'unknown state';}},getReadyState:function()
{switch(this.video.get('readyState')){case this.video.HAVE_NOTHING:return'have nothing';case this.video.HAVE_METADATA:return'have meta';case this.video.HAVE_CURRENT_DATA:return'have current';case this.video.HAVE_FUTURE_DATA:return'have future data';case this.video.HAVE_ENOUGH_DATA:return'have enough data';default:return'unknown state';}}});CwMedia.Timeline=new Class({Extends:Slider,Implements:[Events,Options],options:{video:false,timeDisplay:false,timeAsPercent:false,timeDisplayDisabled:'--',onChange:function(position){this.updateVideo(position);},onError:function(error){this.detach();this.updateTime(false);this.knob.set('opacity',0.5);}},video:false,duration:0,startTime:0,initialize:function(element,knob,options)
{this.parent(element,knob,options);this.setOptions(options);this.video=$(this.options.video);this.detach();this.video.addEvent('timeupdate',function(an_event){this.updatePosition(this.video.get('currentTime'));}.bind(this));this.video.addEvent('seeked',function(an_event){this.updatePosition(this.video.get('currentTime'));}.bind(this));this.video.addEvent('durationchange',function(an_event){this.duration=this.video.get('duration');if(this.duration==0){this.fireEvent('error','Duration is zero');}}.bind(this));this.video.addEvent('loadstart',function(an_event){this.updatePosition(this.video.get('currentTime'));}.bind(this));this.video.addEvent('canplay',function(an_event){if($defined(this.video.get('startTime'))){this.startTime=this.video.get('startTime');}
this.updateTime(this.startTime);this.attach();}.bind(this));},updatePosition:function(time)
{this.updateTime(time);if(this.video.get('seeking')){return;}
position=this.toPosition(time/this.duration*this.range);this.knob.setStyle(this.property,position);},updateVideo:function(position)
{if(!position||this.duration==0)return;videotime=(position/this.range*this.duration);this.video.set('currentTime',videotime);},updateTime:function(time)
{if(this.options.timeDisplay){if(time===false||!$defined(time)){$(this.options.timeDisplay).set('html',this.options.timeDisplayDisabled);return;}
if(this.options.timeAsPercent){if(!this.duration||this.duration==0){$(this.options.timeDisplay).set('html',"0%");}
else{$(this.options.timeDisplay).set('html',(time/this.duration*100).toInt()+"%");}}
else{hr=Math.floor(time/3600);rem=time%3600;min=Math.floor(rem/60);sec=Math.floor(rem%60);if(hr>0){$(this.options.timeDisplay).set('html',hr+":"+(min<10?"0"+min:min)+":"+(sec<10?"0"+sec:sec));}
else{$(this.options.timeDisplay).set('html',min+":"+(sec<10?"0"+sec:sec));}}}}});
