/*
 * Radio 3 Player
 * Copyright(c) 2008, Phil Rabin.
 */


CBCR3.namespace("CBCR3.Player");CBCR3.Player.PlaylistItemType={personal:"personal",favourite:"favourite"};CBCR3.Player.TimeCount={down:"down",up:"up"};CBCR3.Player.PlayerMode={stream:"stream",playlist:"playlist"};CBCR3.Player.ShuffleMode={shuffle:"shuffle",sequential:"sequential"};CBCR3.Player.PlayerStatus={playing:"playing",stopped:"stopped",inTransition:"inTransition"};CBCR3.Player.ThumbStatus={up:"up",down:"down",none:"none",disabled:"disabled"};CBCR3.Player.FavouriteStatus={clearToFavourite:"clearToFavourite",alreadyFavourited:"alreadyFavourited",disabled:"disabled"};CBCR3.Player.TrackType={track:"track",hostbreak:"hostbreak"};CBCR3.Player.StreamTrackStatus={playing:"playing",played:"played",scheduled:"scheduled"};

CBCR3.namespace("CBCR3.Player.Events");CBCR3.Player.Events.StateInitEvent={r3PlayerInit:"stateInitEvent:r3PlayerInit",playlistPlayerLoaded:"stateInitEvent:playlistPlayerLoaded",streamPlayerLoaded:"stateInitEvent:streamPlayerLoaded",streamAudioOnlyLoaded:"stateInitEvent:streamAudioOnlyLoaded"};CBCR3.Player.Events.StreamEvent={connecting:"streamEvent:connecting",connected:"streamEvent:connected",metaData:"streamEvent:metaData",metaDataConnected:"streamEvent:metaDataConnected",metaDataStreamError:"streamEvent:metaDataStreamError",streamSelected:"streamEvent:streamSelected",error:"streamEvent:error"};CBCR3.Player.Events.StreamErrorEvent={notFound:"streamErrorEvent:notFound",failed:"streamErrorEvent:failed"};CBCR3.Player.Events.PlaybackEvent={start:"playbackEvent:start",pause:"playbackEvent:pause",progress:"playbackEvent:progress",finished:"playbackEvent:finished",stream:"playbackEvent:stream",next:"playbackEvent:next",prev:"playbackEvent:prev"};CBCR3.Player.Events.LoadingEvent={ready:"loadingEvent:ready",open:"loadingEvent:open",progress:"loadingEvent:progress",complete:"loadingEvent:complete",buffering:"loadingEvent:buffering"};CBCR3.Player.Events.ServiceEvent={success:"serviceEvent:success",personalPlaylistsReceived:"serviceEvent:personalPlaylistsReceived",favouritePlaylistsReceived:"serviceEvent:favouritePlaylistsReceived",trackReceived:"serviceEvent:trackReceived",playlistReceived:"serviceEvent:playlistReceived",permalinkContextReceived:"serviceEvent:permalinkContextReceived",metaDataUpdated:"serviceEvent:metaDataUpdated",permalinkChanged:"serviceEvent:permalinkChanged"};CBCR3.Player.Events.PlaylistEvent={playlistSelected:"playlistEvent:playlistSelected",deletePlaylist:"playlistEvent:deletePlaylist",permalinkSelected:"playlistEvent:permalinkSelected",permalinkAdded:"playlistEvent:permalinkAdded",trackSelected:"playlistEvent:trackSelected",createNewPlaylist:"playlistEvent:createNewPlaylist",savePlaylist:"playlistEvent:savePlaylist",addPlaylistSelected:"playlistEvent:addPlaylistSelected",newPlaylistCreated:"playlistEvent:newPlaylistCreated"};CBCR3.Player.Events.PlaybackCommand={play:"playbackCommand:play",stop:"playbackCommand:stop",pause:"playbackCommand:pause",playPause:"playbackCommand:playPause",kill:"playbackCommand:kill"};CBCR3.Player.Events.ScrubCommand={playhead:"scrubCommand:playhead",playheadPercent:"scrubCommand:playheadPercent",volume:"scrubCommand:volume"};CBCR3.Player.Events.ScrubEvent={changed:"scrubEvent:changed",slide:"scrubEvent:slide"};CBCR3.Player.Events.PlayerErrorEvent={streamError:"playerErrorEvent:streamError",trackNotFound:"playerErrorEvent:trackNotFound",ioError:"playerErrorEvent:ioError"};CBCR3.Player.Events.RTMPStreamEvent={ready:"rtmpStreamEvent:ready",connecting:"rtmpStreamEvent:connecting",connected:"rtmpStreamEvent:connected",stopped:"rtmpStreamEvent:stopped",progress:"rtmpStreamEvent:progress",start:"rtmpStreamEvent:start",streaming:"rtmpStreamEvent:streaming",buffering:"rtmpStreamEvent:buffering",metaDataConnected:"rtmpStreamEvent:metaDataConnected",metaDataConnectionFailed:"rtmpStreamEvent:metaDataConnectionFailed",metaDataReceived:"rtmpStreamEvent:metaDataReceived",streamAudioOnly:"rtmpStreamEvent:streamAudioOnly",failed:"rtmpStreamEvent:failed"};CBCR3.Player.Events.StreamCommand={load:"streamCommand:load",play:"streamCommand:play",stop:"streamCommand:stop",pause:"streamCommand:pause",kill:"streamCommand:kill",playPause:"streamCommand:playPause",loadAudioOnly:"streamCommand:loadAudioOnly",volume:"streamCommand:volume"};CBCR3.Player.Events.ModeControlEvent={stream:"modeControlEvent:stream",playlist:"modeControlEvent:playlist",browseTracks:"modeControlEvent:browseTracks",browseStreams:"modeControlEvent:browseStreams"};CBCR3.Player.Events.PermalinkEvent={trackAdded:"permalinkEvent:trackAdded",favouriteRemoved:"permalinkEvent:favouriteRemoved",favouriteAdded:"permalinkEvent:favouriteAdded",favouriteStatus:"permalinkEvent:favouriteStatus",confirm:"permalinkEvent:confirm",cancel:"permalinkEvent:cancel",sendPermalink:"permalinkEvent:sendPermalink",retrievePermalinkContext:"permalinkEvent:retrievePermalinkContext"};CBCR3.Player.Events.SelectorEvent={scheduleReceived:"selectorEvent:scheduleReceived"};CBCR3.Player.Events.ShuffleEvent={changed:"shuffleEvent:changed"};CBCR3.Player.Events.ThumbEvent={received:"thumbEvent:received",registered:"thumbEvent:registered",changed:"thumbEvent:changed"};CBCR3.Player.Events.PanelEvent={remove:"panelEvent:remove",click:"panelEvent:click"};CBCR3.Player.Events.TrackTimeEvent={changed:"trackTimeEvent:changed"};CBCR3.Player.Events.ServiceExceptionEvent={userNotAuthenticated:"serviceExceptionEvent:userNotAuthenticated",duplicateTrack:"serviceExceptionEvent:duplicateTrack",emptyPlaylist:"serviceExceptionEvent:emptyPlaylist",endOfPlaylist:"serviceExceptionEvent:endOfPlaylist",nullPlaylist:"serviceExceptionEvent:nullPlaylist"};CBCR3.Player.Events.PlaybackExceptionEvent={emptyPlaylist:"playbackExceptionEvent:emptyPlaylist",endOfPlaylist:"playbackExceptionEvent:endOfPlaylist",nullPlaylist:"playbackExceptionEvent:nullPlaylist"};CBCR3.Player.Events.AuthenticationEvent={loginRequired:"authenticationEvent:loginRequired"};CBCR3.Player.Events.PageEvent={playPermalink:"pageEvent:playPermalink",addPermalink:"pageEvent:addPermalink",favouritePermalink:"pageEvent:favouritePermalink",permalink:"pageEvent:permalink",playStream:"pageEvent:playStream"};CBCR3.Player.Events.DisplayEvent={forceIERefresh:"displayEvent:forceIERefresh"};CBCR3.Player.Events.StatusEvent={notify:"statusEvent:notify"};

CBCR3.namespace("CBCR3.Player.Player");CBCR3.Player.Util={replaceClass:function(id,oldClass,newClass)
{$(id).removeClassName(oldClass);$(id).addClassName(newClass);},showLoading:function(element)
{var divId=element.id+"_loading";var loadingImage=new Element('div',{id:divId});loadingImage.addClassName("loading");element.appendChild(loadingImage);},hideLoading:function(element)
{var imageId=element.id+"_loading";if($(imageId))
$(imageId).remove();}};CBCR3.Player.Globals={playerId:"r3player",flashPlayerId:"flash",streamPlayerId:"quicktime",rtmpPlayerId:"rtmp",gatewayId:"flashGateway",meteorId:"meteorProxy",rtmpId:"rtmpGateway"};

CBCR3.namespace("CBCR3.Player.Exceptions");CBCR3.Player.Exceptions.Exception=Class.create({initialize:function()
{this.type="";this.message="";this.stackTrace="";this.statusCode="";this.timedOut="";}});

CBCR3.namespace("CBCR3.Player.Exceptions");CBCR3.Player.Exceptions.SoapExceptionFactory=Class.create({initialize:function()
{},createException:function(soapException)
{var exception=new CBCR3.Player.Exceptions.Exception();exception.type=soapException.get_exceptionType();exception.message=soapException.get_message();exception.stackTrace=soapException.get_stackTrace();exception.timedOut=soapException.get_timedOut();exception.statusCode=soapException.get_statusCode();return exception;}});

CBCR3.namespace("CBCR3.Player.Exceptions");CBCR3.Player.Exceptions.SoapExceptionLookup=Class.create({exceptionMap:null,initialize:function()
{this.populateMap();},populateMap:function()
{this.exceptionMap=new Hash();this.exceptionMap.set("CBC.Radio3.Player.Exceptions.UserNotAuthenticatedException",CBCR3.Player.Events.ServiceExceptionEvent.userNotAuthenticated);this.exceptionMap.set("CBC.Radio3.Player.Exceptions.DuplicateTrackException",CBCR3.Player.Events.ServiceExceptionEvent.duplicateTrack);this.exceptionMap.set("CBC.Radio3.Player.Exceptions.EmptyPlaylistException",CBCR3.Player.Events.ServiceExceptionEvent.emptyPlaylist);this.exceptionMap.set("CBC.Radio3.Player.Exceptions.EndOfPlaylistException",CBCR3.Player.Events.ServiceExceptionEvent.endOfPlaylist);this.exceptionMap.set("CBC.Radio3.Player.Exceptions.NullPlaylistException",CBCR3.Player.Events.ServiceExceptionEvent.nullPlaylist);},map:function(soapExceptionType)
{var exceptionType=this.exceptionMap.get(soapExceptionType);if(exceptionType==null)
Trace.write("R3Player Unhandled exception: "+soapExceptionType+"\n Exception type not found");return exceptionType;}});

CBCR3.namespace("CBCR3.Player.Util");CBCR3.Player.Util.VolumeService=Class.create({provider:null,initialize:function()
{this.provider=new CBCR3.Player.Util.VolumeCookieProvider(50);},setVolume:function(value)
{this.provider.setVolume(value);},getVolume:function()
{return this.provider.getVolume();}});CBCR3.Player.Util.VolumeCookieProvider=Class.create({defaultVolume:0,initialize:function(defaultVolume)
{this.defaultVolume=defaultVolume;if(!CBCR3.Commons.Cookies)
alert("cookies class not found");},setVolume:function(value)
{CBCR3.Commons.Cookies.create(CBCR3.Player.Configuration.cookieName,value,30);},getVolume:function()
{var volume=CBCR3.Commons.Cookies.getValue(CBCR3.Player.Configuration.cookieName);if(volume!=null&&volume>=0&&volume<=100)
return volume;return this.defaultVolume;}});

CBCR3.namespace("CBCR3.Player.Models");CBCR3.Player.Models.Track=Class.create({initialize:function(properties)
{if(!properties)
properties={};this.title=properties.title||"";this.artist=properties.artist||"";this.infoLink=properties.infoLink||"";this.permalink=properties.permalink||"";this.type=properties.type||"";this.iTunesLink=properties.iTunesLink||"";}});

CBCR3.namespace("CBCR3.Player.Models");CBCR3.Player.Models.PlaylistTrack=Class.create(CBCR3.Player.Models.Track,{initialize:function($super,properties)
{$super(properties);if(!properties)
properties={};this.duration=properties.duration||0;this.audioUrl=properties.audioUrl||"";this.sequence=properties.sequence||"";this.thumb=properties.thumb||CBCR3.Player.ThumbStatus.none;}});

CBCR3.namespace("CBCR3.Player.Services");CBCR3.Player.Services.MusicService=Class.create(CBCR3.Commons.EventDispatcher,{initialize:function($super)
{$super();},getPlaylist:function(playlistPermalink)
{CBC.Radio3.Player.Web.PlayerWebService.GetPlaylist(playlistPermalink,this.getPlaylist_Success.bind(this),this.webService_Failed.bind(this));},getTrack:function(permalink,sequence)
{CBC.Radio3.Player.Web.PlayerWebService.GetTrack(permalink,sequence,this.getTrack_Success.bind(this),this.webService_Failed.bind(this));},getNextTrack:function(permalink,sequence,isShuffle)
{CBC.Radio3.Player.Web.PlayerWebService.GetNextTrack(permalink,sequence,isShuffle,this.getTrack_Success.bind(this),this.webService_Failed.bind(this));},getPrevTrack:function(permalink,sequence,isShuffle)
{CBC.Radio3.Player.Web.PlayerWebService.GetPreviousTrack(permalink,sequence,isShuffle,this.getTrack_Success.bind(this),this.webService_Failed.bind(this));},getTrack_Success:function(response)
{var track=new CBCR3.Player.Mappers.PlaylistTrackDtoMapper().mapFrom(response);this.dispatchEvent(CBCR3.Player.Events.ServiceEvent.trackReceived,{track:track});},getPlaylist_Success:function(response)
{var playlist=this.parsePlaylist(response);this.dispatchEvent(CBCR3.Player.Events.ServiceEvent.playlistReceived,{playlist:playlist});},parsePlaylist:function(playlistDto)
{return new CBCR3.Player.Mappers.PlaylistDtoMapper().mapFrom(playlistDto);},webService_Failed:function(errorResponse)
{var exception=new CBCR3.Player.Exceptions.SoapExceptionFactory().createException(errorResponse);var type=new CBCR3.Player.Exceptions.SoapExceptionLookup().map(exception.type);this.dispatchEvent(type,{exception:exception});}});

CBCR3.namespace("CBCR3.Player.Services");CBCR3.Player.Services.PermalinkService=Class.create(CBCR3.Commons.EventDispatcher,{initialize:function($super)
{$super();},addTrackToPlaylist:function(trackPermalink,playlistPermalink,allowDuplicates)
{CBC.Radio3.Player.Web.PlayerWebService.AddTrackToPlaylist(trackPermalink,playlistPermalink,allowDuplicates,this.addTrackToPlaylist_Success.bind(this),this.webService_Failed.bind(this),{trackPermalink:trackPermalink,playlistPermalink:playlistPermalink});},addTrackToPlaylist_Success:function(response,userContext)
{this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.trackAdded,{trackPermalink:userContext.trackPermalink,playlistPermalink:userContext.playlistPermalink,sequence:response});},addTrackToPlaylist_failed:function(response,userContext)
{var exParser=new CBCR3.Player.Exceptions.SoapExceptionParser();var exception=exParser.parseException(response);this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.trackExists,{elementId:userContext.elementId,playlistPermalink:userContext.playlistPermalink});},addPlaylistToFavourites:function(playlistPermalink)
{CBC.Radio3.Player.Web.PlayerWebService.AddPlaylistToFavourites(playlistPermalink,this.addPlaylistToFavourites_Success.bind(this),this.webService_Failed.bind(this));},addPlaylistToFavourites_Success:function(response,userContext)
{this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.favouriteAdded);},getFavouriteStatus:function(playlistPermalink)
{CBC.Radio3.Player.Web.PlayerWebService.GetFavouriteStatus(playlistPermalink,this.getFavouriteStatus_Success.bind(this),this.webService_Failed.bind(this));},getFavouriteStatus_Success:function(response,userContext)
{this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.favouriteStatus,{favouriteStatus:response});},removePlaylistFromFavourites:function(playlistPermalink,elementId)
{CBC.Radio3.Player.Web.PlayerWebService.RemovePlaylistFromFavourites(playlistPermalink,this.removePlaylistFromFavourites_Success.bind(this),this.webService_Failed.bind(this),{elementId:elementId,playlistPermalink:playlistPermalink});},removePlaylistFromFavourites_Success:function(response,context)
{var elementId=context.elementId;var playlistPermalink=context.playlistPermalink;this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.favouriteRemoved,{id:elementId,playlistPermalink:playlistPermalink});},getPermalinkContext:function(playlistPermalink)
{CBC.Radio3.Player.Web.PlayerWebService.GetPermalinkContext(playlistPermalink,this.getPermalinkContext_Success.bind(this),this.webService_Failed.bind(this));},getPermalinkContext_Success:function(response,context)
{var playlistContext=new CBCR3.Player.Models.PlaylistPermalinkContext({title:response.Title,infoLink:response.InfoLink,details:response.Details,detailLink:response.DetailLink});this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,{permalinkContext:playlistContext});},webService_Failed:function(errorResponse)
{var exception=new CBCR3.Player.Exceptions.SoapExceptionFactory().createException(errorResponse);var type=new CBCR3.Player.Exceptions.SoapExceptionLookup().map(exception.type);this.dispatchEvent(type,{exception:exception});}});

CBCR3.namespace("CBCR3.Player.Services");CBCR3.Player.Services.ThumbWebLookupService=Class.create(CBCR3.Commons.EventDispatcher,{permalink:"",initialize:function($super)
{$super();},getThumbStatus:function(permalink)
{CBC.Radio3.Player.Web.PlayerWebService.GetThumbStatus(permalink,this.getThumbStatus_Success.bind(this),this.getThumbStatus_Failed.bind(this));},parseThumbStatus:function(dto)
{return dto;},getThumbStatus_Success:function(response)
{var thumbStatus=this.parseThumbStatus(response);this.dispatchEvent(CBCR3.Player.Events.ThumbEvent.received,{thumbStatus:thumbStatus});},getThumbStatus_Failed:function(errorResponse)
{var exception=new CBCR3.Player.Exceptions.SoapExceptionFactory().createException(errorResponse);var type=new CBCR3.Player.Exceptions.SoapExceptionLookup().map(exception.type);this.dispatchEvent(type,{exception:exception});}});

CBCR3.namespace("CBCR3.Player.External");CBCR3.Player.External.RTMPGateway=Class.create(CBCR3.Commons.EventDispatcher,{initialize:function($super){$super();},sendStreamEvent:function(event){Trace.write("^^^ rtmpgateway: "+event.type);this.dispatchEvent(event.type,event);},sendMetaDataEvent:function(event){var metaData=new CBCR3.Player.Mappers.StreamMetaDataDtoMapper().mapCollection(event.metaData);Trace.write("** Meta Data Received in RTMP Gateway**");Trace.write(metaData);metaData.get=function(permalink){for(var i=0;i<this.length;i++)
if(this[i].permalink==permalink)
return this[i];return null;};this.dispatchEvent(CBCR3.Player.Events.RTMPStreamEvent.metaDataReceived,{metaData:metaData});},sendStreamErrorEvent:function(event){Trace.write(event);alert("There was an error with the stream, please try refreshing the page");},sendStreamCommand:function(commandName,commandArgs){Trace.write(" in rtmpgateway.js - > sendStreamCommand() Command Name:"+commandName+" args:"+commandArgs);$(CBCR3.Player.Globals.rtmpPlayerId).streamCommand(commandName,commandArgs);}});

CBCR3.namespace("CBCR3.Player.External");CBCR3.Player.External.PlaylistGateway=Class.create(CBCR3.Commons.EventDispatcher,{initialize:function($super){$super()},sendLoadingEvent:function(event){this.dispatchEvent(event.type,event);},sendPlaybackEvent:function(event){this.dispatchEvent(event.type,event);},sendPlayerErrorEvent:function(event){this.dispatchEvent(event.type,event)},sendPlaybackCommand:function(commandName){Trace.write("in playlistgateway.js -> sendPlaybackCommand() commandName: "+commandName);$(CBCR3.Player.Globals.flashPlayerId).playbackCommand(commandName);},sendScrubCommand:function(commandName,position){$(CBCR3.Player.Globals.flashPlayerId).scrubCommand(commandName,position);},sendLoadCommand:function(r3Track){$(CBCR3.Player.Globals.flashPlayerId).loadCommand(r3Track);}});

CBCR3.namespace("CBCR3.Player.Players");CBCR3.Player.Players.RTMPPlayer=Class.create(CBCR3.Commons.EventDispatcher,{isPlaying:false,service:null,rtmpGateway:null,metaData:null,streamPermalink:null,mediaTracker:null,currentShow:"",initialize:function($super,rtmpGateway,mediaTracker){$super();this.rtmpGateway=rtmpGateway;this.mediaTracker=mediaTracker;this.init();},init:function(){this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.metaDataReceived,this.rtmpMetaDataReceivedHandler.bind(this));this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.ready,this.streamReadyHandler.bind(this));this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.connecting,this.streamConnectingHandler.bind(this));this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.progress,this.streamProgressHandler.bind(this));this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.streaming,this.streamStreamingHandler.bind(this));this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.connected,this.streamConnectedHandler.bind(this));this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.stopped,this.streamStoppedHandler.bind(this));this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.metaDataConnected,this.rtmpMetaDataConnectedHandler.bind(this));this.rtmpGateway.addEventListener(CBCR3.Player.Events.RTMPStreamEvent.failed,this.rtmpMetaDataFailedHandler.bind(this));},load:function(initialVolume){var swfBuilder=new CBCR3.Player.Players.RtmpSwfBuilder();swfBuilder.createSwf(initialVolume);},streamProgressHandler:function(event){Trace.write("^^^ STREAM Progress");Trace.write(event);this.mediaTracker.sendInfo({eventType:this.mediaTracker.eventMid,timePassed:event.data.interval,timeOffset:event.data.position});},streamStoppedHandler:function(event){Trace.write("^^^ STREAM Stopped");Trace.write(event);this.mediaTracker.sendInfo({eventType:this.mediaTracker.eventClose,timePassed:event.data.interval,timeOffset:event.data.position});},streamReadyHandler:function(event){Trace.write("STREAM READY");this.dispatchEvent(CBCR3.Player.Events.StateInitEvent.streamPlayerLoaded);},streamConnectingHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.StreamEvent.connecting);},streamConnectedHandler:function(event){try{Trace.write("### STREAM CONNECTED");var trackInfo=this.getTrack();if(this.streamPermalink==null)
return;Trace.write("### streamConnectedHandler:"+this.streamPermalink);var streamInfo=this.getStreamByPermalink(this.streamPermalink);Trace.write(streamInfo);Trace.write(event.data.timestamp);this.mediaTracker.initVars({category:"Radio",streamName:this.getStreamNameInfo(trackInfo,streamInfo),itemMetaData:this.getMetaDataInfo(trackInfo,streamInfo),timePassed:0,audioVideo:"Audio",playerpage:"radio:radio3:music:"+streamInfo.name,type:"Continuous",showName:streamInfo.name,playTimestamp:event.data.timestamp.strftime("%Y-%m-%d %H:%M:%S"),itemURL:this.streamPermalink,LiveOrDemand:"Live",totalTime:this.mediaTracker.STREAM_MAX_TIME});this.mediaTracker.sendInfo({eventType:this.mediaTracker.eventPlay,timeOffset:event.data.position,timePassed:0,playTimestamp:event.data.timestamp});this.dispatchEvent(CBCR3.Player.Events.StreamEvent.connected);}catch(err){Trace.write("*** ERROR - RTMP Stream connected error:"+err);}},rtmpMetaDataConnectedHandler:function(event){Trace.write("RTMP META DATA CONNECTED");this.dispatchEvent(CBCR3.Player.Events.StreamEvent.metaDataConnected);},rtmpMetaDataReceivedHandler:function(event){try{Trace.write("RTMP Meta Data Received in RTMP Player");Trace.write(event.data.metaData);this.metaData=event.data.metaData;this.dispatchEvent(CBCR3.Player.Events.StreamEvent.metaData,{metaData:this.metaData});if(this.streamPermalink!=null)
this.dispatchEvent(CBCR3.Player.Events.ServiceEvent.trackReceived,{track:this.getTrack()});}catch(error){Trace.write("rtmpMetaDataReceivedHandler Error:"+error);Trace.write(event);}},rtmpMetaDataFailedHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.StreamEvent.error);},getMetaData:function(){return this.metaData;},streamStreamingHandler:function(event){this.isPlaying=true;this.dispatchEvent(CBCR3.Player.Events.ServiceEvent.trackReceived,{track:this.getTrack()});},getStreamNameInfo:function(track,stream){return"Radio|Stream|"+stream.name+"|title";},getMetaDataInfo:function(track,stream){Trace.write("rtmpplayer.js -> setCurrentShow()");Trace.write("Tracktype: "+track.type);Trace.write(track);var tmp=this.getStreamNameInfo(track,stream)+"|Continuous|Audio|Live";Trace.write(tmp);return tmp;},getIsPlaying:function(){return this.isPlaying;},setIsPlaying:function(isPlaying){this.isPlaying=isPlaying;},play:function(){if(this.isPlaying)
this.rtmpGateway.sendStreamCommand(CBCR3.Player.Events.StreamCommand.play);else
this.loadStream(this.streamPermalink);},pause:function(){this.isPlaying=false;this.rtmpGateway.sendStreamCommand(CBCR3.Player.Events.StreamCommand.pause);},setPermalink:function(permalink){Trace.write("*** setPermalink: "+permalink);this.streamPermalink=permalink;},getPermalink:function(){return this.streamPermalink;},clearTrack:function(){},getTrack:function(){if(!this.streamPermalink)return;return this.getStreamByPermalink(this.streamPermalink).getNowPlaying();},setShuffleMode:function(shuffleMode){throw("Can't set shuffle mode on rmtp player");},getShuffleMode:function(){throw("Can't get shuffle mode on rmtp player");},setTimeCount:function(timeCount){throw("can't set time count in rtmp player");},getTimeCount:function(){throw("can't set time count in rtmp player");},prev:function(){throw("Previous not implemented");},next:function(){throw("Next not implemented");},playTrack:function(sequence){throw("Play track not implemented");},getProgress:function(){throw("progress not implemented");},getPercentLoaded:function(){throw("percent loaded not implemented");},scrubTo:function(value){throw("Srubbing not implemented in RTMP Player");},stop:function(){Trace.write("^^^ rtmpplayer stop()");this.isPlaying=false;this.rtmpGateway.sendStreamCommand(CBCR3.Player.Events.StreamCommand.stop);},kill:function(){Trace.write("^^^ rtmpplayer kill()");this.isPlaying=false;this.rtmpGateway.sendStreamCommand(CBCR3.Player.Events.StreamCommand.kill);},loadStream:function(streamPermalink){Trace.write("*** loadStream:"+this.streamPermalink);var stream=this.getStreamByPermalink(streamPermalink);this.rtmpGateway.sendStreamCommand(CBCR3.Player.Events.StreamCommand.load,{rtmpUrl:stream.rtmpUrl,rtmpName:stream.rtmpName});},setVolume:function(percent){this.rtmpGateway.sendStreamCommand(CBCR3.Player.Events.StreamCommand.volume,percent);},getStreamByPermalink:function(streamPermalink){for(var i=0;i<this.metaData.length;i++){if(this.metaData[i].permalink==streamPermalink)
return this.metaData[i];}
throw"Current stream not found for this permalink: "+streamPermalink;},retrievePermalinkContext:function(){Trace.write(!this.metaData);if(!this.metaData)
return;var currentStream=this.metaData.get(this.streamPermalink);if(!currentStream)
return;var context=new CBCR3.Player.Models.StreamPermalinkContext({title:currentStream.name});this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,{permalinkContext:context});}});

CBCR3.namespace("CBCR3.Player.Players");CBCR3.Player.Players.PlaylistPlayer=Class.create(CBCR3.Commons.EventDispatcher,{isPlaying:false,controller:null,permalink:null,shuffleMode:CBCR3.Player.ShuffleMode.sequential,track:null,timeCount:CBCR3.Player.TimeCount.up,thumbStatus:CBCR3.Player.ThumbStatus.none,percentLoaded:0,progress:{},logPoint:0,trackLogged:false,mediaTracker:null,SEND_INFO_INTERVAL_COUNTER:120,musicService:null,permalinkService:null,playlistGateway:null,loggingService:null,initialize:function($super,playlistGateway,musicService,loggingService,permalinkService,mediaTracker){$super();this.playlistGateway=playlistGateway;this.musicService=musicService;this.loggingService=loggingService;this.permalinkService=permalinkService;this.mediaTracker=mediaTracker;this.init();},init:function(){this.registerEvents();},registerEvents:function(){this.musicService.addEventListener(CBCR3.Player.Events.ServiceEvent.trackReceived,this.trackLoadedHandler.bind(this));this.musicService.addEventListener(CBCR3.Player.Events.ServiceExceptionEvent.emptyPlaylist,this.emptyPlaylistHandler.bind(this));this.musicService.addEventListener(CBCR3.Player.Events.ServiceExceptionEvent.endOfPlaylist,this.endOfPlaylistHandler.bind(this));this.musicService.addEventListener(CBCR3.Player.Events.ServiceExceptionEvent.nullPlaylist,this.nullPlaylistHandler.bind(this));this.permalinkService.addEventListener(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,this.permalinkContextReceivedHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.LoadingEvent.ready,this.loadingReadyHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.LoadingEvent.open,this.loadingOpenHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.LoadingEvent.complete,this.loadingCompleteHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.LoadingEvent.progress,this.loadingProgressHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.LoadingEvent.buffering,this.loadingBufferingHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.PlaybackEvent.start,this.playbackStartHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.PlaybackEvent.pause,this.playbackPauseHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.PlaybackEvent.progress,this.playbackProgressHandler.bind(this));this.playlistGateway.addEventListener(CBCR3.Player.Events.PlaybackEvent.finished,this.playbackFinishedHandler.bind(this));},load:function(initialVolume){var swfBuilder=new CBCR3.Player.Players.PlaylistSwfBuilder();swfBuilder.createSwf(initialVolume);},loadingReadyHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.StateInitEvent.playlistPlayerLoaded);},loadingOpenHandler:function(event){this.trackLogged=false;this.logPoint=(Math.random()*this.track.duration).round();this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.open);},loadingProgressHandler:function(event){this.percentLoaded=event.data.percentLoaded;this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.progress,{percentLoaded:this.percentLoaded});},loadingBufferingHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.buffering);},loadingCompleteHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.complete);},playbackStartHandler:function(event){try{Trace.write("playlistplayer.js -> playbackStartHandler()");Trace.write(event);this.progress=event.data;this.setCurrentPoint(event.data.position);this.mediaTracker.sendInfo({eventType:this.mediaTracker.eventPlay,timePassed:this.mediaTracker.getProgressTime(this.currentPoint),timeOffset:this.currentPoint,playTimestamp:new Date()});this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.start,{progress:this.progress});}catch(error){Trace.write("Error - in playlistplayer.js playbackStartHandler() "+error);}},playbackPauseHandler:function(event){Trace.write("*** in playlistplayer.js -> playbackPauseHandler() - event.type: "+event.type+" event"+event);Trace.write(event);this.setCurrentPoint(event.data.position);this.mediaTracker.sendInfo({eventType:this.mediaTracker.eventPause,timePassed:this.mediaTracker.getProgressTime(this.currentPoint),timeOffset:this.currentPoint});},playbackProgressHandler:function(event){try{this.mediaTracker.progressCounter++;this.progress=event.data;this.setCurrentPoint(this.progress.position);if(this.currentPoint==this.logPoint&&!this.trackLogged){this.loggingService.logTrackPlay(this.track.permalink);this.trackLogged=true;}
if(this.mediaTracker.progressCounter==this.SEND_INFO_INTERVAL_COUNTER)
{Trace.write("progress() counter:"+this.mediaTracker.progressCounter);Trace.write(event.data);this.mediaTracker.sendInfo({eventType:this.mediaTracker.eventMid,timePassed:this.mediaTracker.getProgressTime(this.currentPoint),timeOffset:this.currentPoint});this.mediaTracker.resetProgress();}
this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.progress,{progress:this.progress});}catch(error){Trace.write("Error in playlistplayer - "+error);}},setCurrentPoint:function(position){this.currentPoint=(position==0)?0:(position/1000).round();},playbackFinishedHandler:function(event){Trace.write("*** in playbackFinishedHandler");Trace.write(event);this.progress=event.data;this.setCurrentPoint(this.progress.position);this.mediaTracker.sendInfo({eventType:this.mediaTracker.eventClose,timePassed:this.mediaTracker.getProgressTime(this.currentPoint),timeOffset:this.currentPoint});this.mediaTracker.resetProgress();this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.finished,{progress:this.progress});},getIsPlaying:function(){return this.isPlaying;},setIsPlaying:function(isPlaying){this.isPlaying=isPlaying;},setPermalink:function(permalink){this.permalink=permalink;},getPermalink:function(){return this.permalink;},getTrack:function(){return this.track;},clearTrack:function(){this.track=null;},setShuffleMode:function(shuffleMode){this.shuffleMode=shuffleMode;},getShuffleMode:function(){return this.shuffleMode;},setTimeCount:function(timeCount){this.timeCount=timeCount;},getTimeCount:function(){return this.timeCount;},play:function(){if(this.track==null&&!this.permalink.blank()){this.next();return;}
Trace.write("Play again");this.isPlaying=true;this.playlistGateway.sendPlaybackCommand(CBCR3.Player.Events.PlaybackCommand.play);},pause:function(){this.isPlaying=false;this.playlistGateway.sendPlaybackCommand(CBCR3.Player.Events.PlaybackCommand.pause);},prev:function(){var isShuffle=(this.shuffleMode==CBCR3.Player.ShuffleMode.shuffle);this.kill();var sequence=0;if(this.track)
sequence=this.track.sequence;this.musicService.getPrevTrack(this.permalink,sequence,isShuffle);},next:function(){var isShuffle=(this.shuffleMode==CBCR3.Player.ShuffleMode.shuffle);this.kill();var sequence=0;if(this.track)
sequence=this.track.sequence;Trace.write("in Next -> playlistplayer.js");this.musicService.getNextTrack(this.permalink,sequence,isShuffle);},playTrack:function(sequence){this.musicService.getTrack(this.permalink,sequence);},stop:function(){this.isPlaying=false;this.playlistGateway.sendPlaybackCommand(CBCR3.Player.Events.PlaybackCommand.stop);},kill:function(){this.isPlaying=false;this.playlistGateway.sendPlaybackCommand(CBCR3.Player.Events.PlaybackCommand.kill);},setVolume:function(percent){var value=percent/100;this.playlistGateway.sendScrubCommand(CBCR3.Player.Events.ScrubCommand.volume,value);},getProgress:function(){return this.progress;},getPercentLoaded:function(){return this.percentLoaded;},setProgress:function(progress){this.progress=progress;},scrubTo:function(value){this.playlistGateway.sendScrubCommand(CBCR3.Player.Events.ScrubCommand.playheadPercent,value);},trackLoadedHandler:function(event){try{this.track=event.data.track;this.isPlaying=true;Trace.write("playlistPlayer trackLoadedHandler: ");Trace.write(this.track);var playtimestamp=new Date();Trace.write("^^* initVars in playlistPlayer.js");this.mediaTracker.initVars({category:"Radio",streamName:"Radio|"+this.track.artist+"|"+this.track.title,itemMetaData:"Radio|"+this.track.artist+"|album|"+this.track.title+"|"+this.track.duration+"|playlist|audio|on-demand",timePassed:0,audioVideo:"Audio",playerpage:"radio:radio3:music:"+this.track.artist,type:"playlist",showName:this.getShowType(this.track.permalink),playTimestamp:playtimestamp.strftime("%Y-%m-%d %H:%M:%S"),itemURL:this.track.permalink,LiveOrDemand:"On-Demand",totalTime:this.track.duration});Trace.write("playlistplayer.js -> trackLoadedHandler()");Trace.write(event);this.mediaTracker.resetProgress();this.mediaTracker.sendInfo({eventType:this.mediaTracker.eventPlay,timePassed:0,timeOffset:0,playTimestamp:new Date()});this.playlistGateway.sendLoadCommand(this.track);this.dispatchEvent(CBCR3.Player.Events.ServiceEvent.trackReceived,{track:this.track});}catch(error){Trace.write("***ERROR:"+error);}},retrievePermalinkContext:function(){this.permalinkService.getPermalinkContext(this.permalink);},permalinkContextReceivedHandler:function(event){Trace.write("** Permalink Context Received **");Trace.write(event.data.permalinkContext);this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,{permalinkContext:event.data.permalinkContext});},getShowType:function(permalink){if(permalink==null)
return"";if(permalink.toLowerCase().indexOf("/concert/")>=0)
return"concert";else
return"playlist";},emptyPlaylistHandler:function(){this.isPlaying=false;this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.emptyPlaylist,{message:"This playlist is empty"});},endOfPlaylistHandler:function(){this.isPlaying=false;this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.endOfPlaylist,{message:"End of playlist"});},nullPlaylistHandler:function(){this.isPlaying=false;this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.nullPlaylist,{message:"This playlist does not exist."});}});

CBCR3.namespace("CBCR3.Player.Players");CBCR3.Player.Players.MasterPlayer=Class.create(CBCR3.Commons.EventDispatcher,{playerState:null,permalinkService:null,streamPlayer:null,playlistPlayer:null,volumeService:null,initialize:function($super,streamPlayer,playlistPlayer,volumeService){Trace.write("*#* MasterPlayer INIT");$super();this.streamPlayer=streamPlayer;this.playlistPlayer=playlistPlayer;this.volumeService=volumeService;this.playerState=new CBCR3.Player.Models.PlayerState();this._init();},_init:function(){this._registerEvents();this._loadPlayers();},_registerEvents:function(){this.streamPlayer.addEventListener(CBCR3.Player.Events.StateInitEvent.streamPlayerLoaded,this._streamPlayerLoadedHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.StateInitEvent.playlistPlayerLoaded,this._playlistPlayerLoadedHandler.bind(this));this.permalinkService=new CBCR3.Player.Services.PermalinkService();this.permalinkService.addEventListener(CBCR3.Player.Events.PermalinkEvent.favouriteStatus,this._favouriteStatusHandler.bind(this));this.streamPlayer.addEventListener(CBCR3.Player.Events.ServiceEvent.trackReceived,this._trackReceivedHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.ServiceEvent.trackReceived,this._trackReceivedHandler.bind(this));this.streamPlayer.addEventListener(CBCR3.Player.Events.StreamEvent.connecting,this._streamConnectingHandler.bind(this));this.streamPlayer.addEventListener(CBCR3.Player.Events.StreamEvent.connected,this._streamConnectedHandler.bind(this));this.streamPlayer.addEventListener(CBCR3.Player.Events.StreamEvent.metaDataConnected,this._metaDataConnectedHandler.bind(this));this.streamPlayer.addEventListener(CBCR3.Player.Events.StreamEvent.metaData,this._streamMetaDataReceivedHandler.bind(this));this.streamPlayer.addEventListener(CBCR3.Player.Events.StreamEvent.error,this._streamErrorHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.LoadingEvent.open,this._loadingOpenHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.LoadingEvent.progress,this._loadingProgressHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.LoadingEvent.buffering,this._loadingBufferingHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.PlaybackEvent.start,this._playbackStartHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.PlaybackEvent.progress,this._playbackProgressHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.PlaybackEvent.finished,this._playbackFinishedHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.PlaybackExceptionEvent.emptyPlaylist,this._emptyPlaylistHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.PlaybackExceptionEvent.endOfPlaylist,this._endOfPlaylistHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.PlaybackExceptionEvent.nullPlaylist,this._nullPlaylistHandler.bind(this));this.playlistPlayer.addEventListener(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,this._permalinkContextReceivedHandler.bind(this));this.streamPlayer.addEventListener(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,this._permalinkContextReceivedHandler.bind(this));},_loadPlayers:function(){var initialVolume=this.volumeService.getVolume();this.streamPlayer.load(initialVolume);this.playlistPlayer.load(initialVolume);},_streamPlayerLoadedHandler:function(event){Trace.write("^^^ masterplayer.js -> _streamPlayerLoadedHandler()");this.dispatchEvent(CBCR3.Player.Events.StateInitEvent.streamPlayerLoaded);},_playlistPlayerLoadedHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.StateInitEvent.playlistPlayerLoaded);},_streamConnectingHandler:function(event){this.playerState.set({message:"Stream is connecting...",details:"Please wait",status:CBCR3.Player.PlayerStatus.inTransition});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.buffering);},_streamConnectedHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.StreamEvent.connected);},_metaDataConnectedHandler:function(event){if(this.playerState.mode==CBCR3.Player.PlayerMode.playlist)
return;this.playerState.set({message:"",details:"",status:CBCR3.Player.PlayerStatus.stopped});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.StreamEvent.metaDataConnected);},_streamMetaDataReceivedHandler:function(event){this.playerState.set({streamMetaData:event.data.metaData});this.dispatchEvent(CBCR3.Player.Events.StreamEvent.metaData,{metaData:event.data.metaData});},_streamErrorHandler:function(event){Trace.write("STREAM ERROR");Trace.write(event);this.playerState.set({message:"The R3 Player could not connect!",details:"Please try refreshing the page or visit our help section",status:CBCR3.Player.PlayerStatus.stopped});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.StreamEvent.error);},_loadingOpenHandler:function(event){this.playerState.set({message:"Loading track...",details:"Please wait"});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.open,event.data);},_loadingProgressHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.progress,event.data);},_loadingBufferingHandler:function(event){this.playerState.set({message:"Buffering...",details:"Please wait",status:CBCR3.Player.PlayerStatus.inTransition});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.buffering,event.data);},_playbackStartHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.start,event.data);},_playbackProgressHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.progress,event.data);},_playbackFinishedHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.finished,event.data);},getPlayerState:function(){return this.playerState;},setPlayerState:function(properties){this.playerState.set(properties);},activePlayer:function(){if(this.playerState.mode==CBCR3.Player.PlayerMode.stream)
return this.streamPlayer;return this.playlistPlayer;},setPermalink:function(permalink){Trace.write("### setPermalink() : "+permalink);this.activePlayer().setPermalink(permalink);},getPermalink:function(){return this.activePlayer().getPermalink();},getStreamMetaData:function(){return this.streamPlayer.getMetaData();},play:function(){this.activePlayer().play();},pause:function(){this.activePlayer().pause();},prev:function(){this.activePlayer().prev();},next:function(){this.activePlayer().next();},playTrack:function(sequence){this.activePlayer().playTrack(sequence);},stop:function(){this.activePlayer().stop();},kill:function(){this.activePlayer().kill();},getIsPlaying:function(){return this.activePlayer().getIsPlaying();},setIsPlaying:function(isPlaying){this.activePlayer().setIsPlaying(isPlaying);},setVolume:function(percent){this.activePlayer().setVolume(percent);},getVolume:function(){return this.volumeService.getVolume();},saveVolume:function(percent){this.volumeService.setVolume(percent);},getPercentLoaded:function(){return this.activePlayer().getPercentLoaded();},getProgress:function(){return this.activePlayer().getProgress();},scrubTo:function(value){this.activePlayer().scrubTo(value);},getTrack:function(){return this.activePlayer().getTrack();},_trackReceivedHandler:function(event){Trace.write("*#* Masterplayer.js -> _trackReceivedHandler()");Trace.write(event);this.playerState.set({status:CBCR3.Player.PlayerStatus.playing});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.stream);},clearTrack:function(){this.activePlayer().clearTrack();},getFavouriteStatus:function(){return this.playerState.favouriteStatus;},setFavouriteStatus:function(favouriteStatus){this.playerState.set({favouriteStatus:favouriteStatus});},retrieveFavouriteStatus:function(){this.permalinkService.getFavouriteStatus(this.getPermalink());},_favouriteStatusHandler:function(event){this.playerState.set({favouriteStatus:event.data.favouriteStatus});this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.favouriteStatus,{favouriteStatus:event.data.favouriteStatus});},setShuffleMode:function(shuffleMode){this.activePlayer().setShuffleMode(shuffleMode);},getShuffleMode:function(){return this.activePlayer().getShuffleMode();},setTimeCount:function(timeCount){this.activePlayer().setTimeCount(timeCount);},getTimeCount:function(){return this.activePlayer().getTimeCount();},retrievePermalinkContext:function(){this.activePlayer().retrievePermalinkContext();},_permalinkContextReceivedHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,{permalinkContext:event.data.permalinkContext});},_emptyPlaylistHandler:function(event){this.playerState.set({message:"This playlist is empty.",details:"Please select another playlist.",status:CBCR3.Player.PlayerStatus.stopped});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.emptyPlaylist);},_endOfPlaylistHandler:function(event){this.playerState.set({message:"Stopped",details:"",status:CBCR3.Player.PlayerStatus.stopped});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.endOfPlaylist);},_nullPlaylistHandler:function(event){this.playerState.set({message:"This playlist or track does not exist.",details:"Please select another one.",status:CBCR3.Player.PlayerStatus.stopped});this.dispatchEvent(CBCR3.Player.Events.StatusEvent.notify,this.playerState);this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.nullPlaylist);}});

CBCR3.namespace("CBCR3.Player.Players");CBCR3.Player.Players.Html5PlaylistPlayer=Class.create(CBCR3.Commons.EventDispatcher,{isPlaying:false,controller:null,permalink:null,shuffleMode:CBCR3.Player.ShuffleMode.sequential,track:null,timeCount:CBCR3.Player.TimeCount.up,thumbStatus:CBCR3.Player.ThumbStatus.none,percentLoaded:0,progress:{},logPoint:0,trackLogged:false,initialVolume:0,musicService:null,permalinkService:null,loggingService:null,audio:null,initialize:function($super,musicService,loggingService,permalinkService){Trace.write(">>> New HTML 5 Player");$super();this.musicService=musicService;this.loggingService=loggingService;this.permalinkService=permalinkService;this.init();this.dispatchEvent(CBCR3.Player.Events.StateInitEvent.playlistPlayerLoaded);},init:function(){this.registerEvents();this.createNewAudio();},registerEvents:function(){this.musicService.addEventListener(CBCR3.Player.Events.ServiceEvent.trackReceived,this.trackLoadedHandler.bind(this));this.musicService.addEventListener(CBCR3.Player.Events.ServiceExceptionEvent.emptyPlaylist,this.emptyPlaylistHandler.bind(this));this.musicService.addEventListener(CBCR3.Player.Events.ServiceExceptionEvent.endOfPlaylist,this.endOfPlaylistHandler.bind(this));this.musicService.addEventListener(CBCR3.Player.Events.ServiceExceptionEvent.nullPlaylist,this.nullPlaylistHandler.bind(this));this.permalinkService.addEventListener(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,this.permalinkContextReceivedHandler.bind(this));},createNewAudio:function()
{if(this.audio)
this.audio.remove();this.audio=new Audio();$(document.body).insert(this.audio);this.audio.autoplay=true;this.audio.autobuffer=true;this.audio.volume=this.initialVolume;this.audio.controls=true;this.audio.addEventListener("loadstart",this.onAudioLoadStart.bind(this));this.audio.addEventListener("load",this.onAudioLoad.bind(this));this.audio.addEventListener("pause",this.onAudioPause.bind(this));this.audio.addEventListener("play",this.onAudioPlay.bind(this));this.audio.addEventListener("playing",this.onAudioPlaying.bind(this));this.audio.addEventListener("ended",this.onAudioEnded.bind(this));this.audio.addEventListener("empty",this.onAudioEmpty.bind(this));this.audio.addEventListener("emptied",this.onAudioEmptied.bind(this));this.audio.addEventListener("progress",this.onAudioProgress.bind(this));this.audio.addEventListener("timeupdate",this.onAudioTimeUpdate.bind(this));},load:function(initialVolume){this.initialVolume=initialVolume/100;},loadingBufferingHandler:function(event){this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.buffering);},onAudioLoadStart:function(event)
{Trace.write(">>> load start");Trace.write(this.progress);this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.open);},onAudioLoad:function(event)
{Trace.write(">>> load");this.trackLogged=false;this.logPoint=(Math.random()*this.track.duration).round();this.percentLoaded=100;this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.progress,{percentLoaded:this.percentLoaded});this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.complete);},onAudioProgress:function(event)
{this.percentLoaded=event.loaded/event.total*100;this.dispatchEvent(CBCR3.Player.Events.LoadingEvent.progress,{percentLoaded:this.percentLoaded});},onAudioPlay:function(event)
{Trace.write(">>> play");Trace.write(event)},onAudioPlaying:function(event)
{this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.start,{progress:this.progress});},onAudioPause:function(event)
{Trace.write(">>> pause");Trace.write(event)},onAudioEnded:function(event)
{this.progress.position=this.audio.currentTime*1000;this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.finished,{progress:this.progress});},onAudioEmpty:function(event)
{Trace.write(">>> empty");Trace.write(event)},onAudioEmptied:function(event)
{Trace.write(">>> empty");Trace.write(event)},onAudioTimeUpdate:function(event)
{this.progress.position=this.audio.currentTime*1000;this.currentPoint=this.progress.position.round();if(this.currentPoint==this.logPoint&&!this.trackLogged){this.loggingService.logTrackPlay(this.track.permalink);this.trackLogged=true;}
this.dispatchEvent(CBCR3.Player.Events.PlaybackEvent.progress,{progress:this.progress});},getIsPlaying:function(){return this.isPlaying;},setIsPlaying:function(isPlaying){this.isPlaying=isPlaying;},setPermalink:function(permalink){this.permalink=permalink;},getPermalink:function(){return this.permalink;},getTrack:function(){return this.track;},clearTrack:function(){this.track=null;},setShuffleMode:function(shuffleMode){this.shuffleMode=shuffleMode;},getShuffleMode:function(){return this.shuffleMode;},setTimeCount:function(timeCount){this.timeCount=timeCount;},getTimeCount:function(){return this.timeCount;},play:function()
{Trace.write(">>> play");if(this.track==null&&!this.permalink.blank()){this.next();return;}
this.isPlaying=true;if(this.audio)
this.audio.play();},pause:function(){Trace.write(">>> pause");this.isPlaying=false;if(this.audio)
this.audio.pause();},prev:function()
{var isShuffle=(this.shuffleMode==CBCR3.Player.ShuffleMode.shuffle);this.kill();var sequence=0;if(this.track)
sequence=this.track.sequence;this.musicService.getPrevTrack(this.permalink,sequence,isShuffle);},next:function()
{var isShuffle=(this.shuffleMode==CBCR3.Player.ShuffleMode.shuffle);this.kill();var sequence=0;if(this.track)
sequence=this.track.sequence;this.musicService.getNextTrack(this.permalink,sequence,isShuffle);},playTrack:function(sequence){Trace.write("play track at "+sequence);this.musicService.getTrack(this.permalink,sequence);},stop:function(){this.isPlaying=false;this.audio.pause();},kill:function(){Trace.write(">>> kill");this.isPlaying=false;if(this.audio&&!this.audio.paused){this.audio.pause();this.audio.currentTime=0;}
Trace.write(">>> killed");},setVolume:function(percent){this.audio.volume=percent/100;},getProgress:function(){return this.progress;},getPercentLoaded:function(){return this.percentLoaded;},setProgress:function(progress){this.progress=progress;},scrubTo:function(value){Trace.write(">>> scrub to "+(this.track.duration*value));this.audio.currentTime=this.track.duration*value;},trackLoadedHandler:function(event){this.kill();this.createNewAudio();this.track=event.data.track;this.isPlaying=true;this.audio.src="/radio3/dawn/nmc/0/40/tracks/The-Light-is-You.mp3";this.play();this.progress={position:0,end:this.track.duration*1000};this.dispatchEvent(CBCR3.Player.Events.ServiceEvent.trackReceived,{track:this.track});},fakeClick:function(fn)
{Trace.write(">>> flake click");var anchor=new Element("a",{id:"fakeClick",href:"#"});anchor.observe("click",function(event){Trace.write(">>> clicked!!");event.stop();fn();});$(document.body).insert({bottom:anchor});anchor.simulate("click");anchor.remove();},retrievePermalinkContext:function(){this.permalinkService.getPermalinkContext(this.permalink);},permalinkContextReceivedHandler:function(event)
{Trace.write(">>> ** Permalink Context Received **");Trace.write(event.data.permalinkContext);this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,{permalinkContext:event.data.permalinkContext});},emptyPlaylistHandler:function(){this.isPlaying=false;this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.emptyPlaylist,{message:"This playlist is empty"});},endOfPlaylistHandler:function(){this.isPlaying=false;this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.endOfPlaylist,{message:"End of playlist"});},nullPlaylistHandler:function(){this.isPlaying=false;this.dispatchEvent(CBCR3.Player.Events.PlaybackExceptionEvent.nullPlaylist,{message:"This playlist does not exist."});}});(function(){var eventMatchers={'HTMLEvents':/^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,'MouseEvents':/^(?:click|mouse(?:down|up|over|move|out))$/};var defaultOptions={pointerX:0,pointerY:0,button:0,ctrlKey:false,altKey:false,shiftKey:false,metaKey:false,bubbles:true,cancelable:true};Event.simulate=function(element,eventName){var options=Object.extend(Object.clone(defaultOptions),arguments[2]||{});var oEvent,eventType=null;element=$(element);for(var name in eventMatchers){if(eventMatchers[name].test(eventName)){eventType=name;break;}}
if(!eventType)
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');if(document.createEvent){oEvent=document.createEvent(eventType);if(eventType=='HTMLEvents'){oEvent.initEvent(eventName,options.bubbles,options.cancelable);}
else{oEvent.initMouseEvent(eventName,options.bubbles,options.cancelable,document.defaultView,options.button,options.pointerX,options.pointerY,options.pointerX,options.pointerY,options.ctrlKey,options.altKey,options.shiftKey,options.metaKey,options.button,element);}
element.dispatchEvent(oEvent);}
else{options.clientX=options.pointerX;options.clientY=options.pointerY;oEvent=Object.extend(document.createEventObject(),options);element.fireEvent('on'+eventName,oEvent);}
return element;};Element.addMethods({simulate:Event.simulate});})();

CBCR3.namespace("CBCR3.Player.Players");CBCR3.Player.Players.Html5StreamPlayer=Class.create(CBCR3.Commons.EventDispatcher,{isPlaying:false,service:null,rtmpGateway:null,metaData:null,streamPermalink:null,initialize:function($super)
{$super();},load:function(initialVolume)
{this.dispatchEvent(CBCR3.Player.Events.StateInitEvent.streamPlayerLoaded);},streamReadyHandler:function(event)
{Trace.write("STREAM READY");},streamConnectingHandler:function(event)
{this.dispatchEvent(CBCR3.Player.Events.StreamEvent.connecting);},streamConnectedHandler:function(event)
{Trace.write("STREAM CONNECTED");this.dispatchEvent(CBCR3.Player.Events.StreamEvent.connected);},rtmpMetaDataConnectedHandler:function(event)
{Trace.write("RTMP META DATA CONNECTED");this.dispatchEvent(CBCR3.Player.Events.StreamEvent.metaDataConnected);},rtmpMetaDataReceivedHandler:function(event)
{Trace.write("RTMP Meta Data Recieved in RTMP Player")
this.metaData=event.data.metaData;this.dispatchEvent(CBCR3.Player.Events.StreamEvent.metaData,{metaData:this.metaData});this.dispatchEvent(CBCR3.Player.Events.ServiceEvent.trackReceived,{track:this.getTrack()});},rtmpMetaDataFailedHandler:function(event)
{this.dispatchEvent(CBCR3.Player.Events.StreamEvent.error);},getMetaData:function()
{return this.metaData;},streamStreamingHandler:function(event)
{this.isPlaying=true;this.dispatchEvent(CBCR3.Player.Events.ServiceEvent.trackReceived,{track:this.getTrack()});},getIsPlaying:function()
{return this.isPlaying;},setIsPlaying:function(isPlaying)
{this.isPlaying=isPlaying;},play:function()
{Trace.write("--- play stream ");},pause:function()
{this.isPlaying=false;Trace.write("--- pause stream ");},setPermalink:function(permalink)
{this.streamPermalink=permalink;},getPermalink:function()
{return this.streamPermalink;},clearTrack:function()
{},getTrack:function()
{Trace.write("--- get track ");},setShuffleMode:function(shuffleMode)
{throw("Can't set shuffle mode on rmtp player");},getShuffleMode:function()
{throw("Can't get shuffle mode on rmtp player");},setTimeCount:function(timeCount)
{throw("can't set time count in rtmp player");},getTimeCount:function()
{throw("can't set time count in rtmp player");},prev:function()
{throw("Previous not implemented");},next:function()
{throw("Next not implemented");},playTrack:function(sequence)
{throw("Play track not implemented");},getProgress:function()
{throw("progress not implemented");},getPercentLoaded:function()
{throw("percent loaded not implemented");},scrubTo:function(value)
{throw("Srubbing not implemented in RTMP Player");},stop:function()
{this.isPlaying=false;Trace.write("--- stop stream ");},kill:function()
{this.isPlaying=false;Trace.write("--- kill stream ");},loadStream:function(streamPermalink)
{Trace.write("--- load stream "+streamPermalink);},setVolume:function(percent)
{Trace.write("--- set volume ");},retrievePermalinkContext:function()
{Trace.write(!this.metaData);if(!this.metaData)
return;var currentStream=this.metaData.get(this.streamPermalink);if(!currentStream)
return;var context=new CBCR3.Player.Models.StreamPermalinkContext({title:currentStream.name});this.dispatchEvent(CBCR3.Player.Events.PermalinkEvent.permalinkContextReceived,{permalinkContext:context});}});

CBCR3.namespace("CBCR3.Player.Application");var iocConfig={rtmpGateway:CBCR3.Player.External.RTMPGateway,playlistGateway:CBCR3.Player.External.PlaylistGateway};var staticContainer={};CBCR3.Player.Application.IoC={getInstanceOf:function(serviceType){if(iocConfig[serviceType]==null)
alert("Unknown service type in IoC configuration: "+serviceType);if(staticContainer[serviceType]==null)
staticContainer[serviceType]=new iocConfig[serviceType]();return staticContainer[serviceType];},getMasterPlayerInstance:function(){var mediaTracker=CBCR3.Stats.UI.getMediaTracker();var rtmpGateway=CBCR3.Player.Application.IoC.getInstanceOf("rtmpGateway");var rtmpPlayer=new CBCR3.Player.Players.RTMPPlayer(rtmpGateway,mediaTracker);var playlistGateway=CBCR3.Player.Application.IoC.getInstanceOf("playlistGateway");var musicService=new CBCR3.Player.Services.MusicService();var loggingService=new CBCR3.Player.Services.LoggingService();var permalinkService=new CBCR3.Player.Services.PermalinkService();var playlistPlayer=new CBCR3.Player.Players.PlaylistPlayer(playlistGateway,musicService,loggingService,permalinkService,mediaTracker);var volumeService=new CBCR3.Player.Util.VolumeService();return new CBCR3.Player.Players.MasterPlayer(rtmpPlayer,playlistPlayer,volumeService);}};

CBCR3.namespace("CBCR3.Player.Services");CBCR3.Player.Services.LoggingService=Class.create(CBCR3.Commons.EventDispatcher,{initialize:function($super)
{$super();},logTrackPlay:function(trackPermalink)
{CBC.Radio3.Player.Web.PlayerWebService.LogTrackPlay(trackPermalink,this.logTrackPlay_Success.bind(this),this.webService_Failed.bind(this));},logTrackPlay_Success:function(response,context)
{},webService_Failed:function(errorResponse)
{var exception=new CBCR3.Player.Exceptions.SoapExceptionFactory().createException(errorResponse);var type=new CBCR3.Player.Exceptions.SoapExceptionLookup().map(exception.type);this.dispatchEvent(type,{exception:exception});}});

CBCR3.namespace("CBCR3.Player.Players");CBCR3.Player.Players.PlaylistSwfBuilder=Class.create({containerId:"playlistContainer",initialize:function()
{var container=new Element("div",{id:this.containerId});$(document.body).appendChild(container);},createSwf:function(volume)
{var volumeLevel=volume/100;var flashvars={volume:volumeLevel};var params={menu:'false',allowScriptAccess:'always'};var attributes={id:CBCR3.Player.Globals.flashPlayerId};swfobject.embedSWF(CBCR3.Player.Configuration.flashPath+"R3Player.swf?v="+CBCR3.Application.Config.svnRevision,this.containerId,"1","1",CBCR3.Player.Configuration.flashVersion.full,null,flashvars,params,attributes);}});

CBCR3.namespace("CBCR3.Player.Players");CBCR3.Player.Players.RtmpSwfBuilder=Class.create({containerId:"rtmpContainer",initialize:function()
{var container=new Element('div',{id:this.containerId});$(document.body).appendChild(container);},createSwf:function(volume)
{var volumeLevel=volume/100;var flashvars={volume:volumeLevel,connectionStrings:CBCR3.Player.Configuration.connectionStrings};var params={menu:'false',allowScriptAccess:'always'};var attributes={id:CBCR3.Player.Globals.rtmpPlayerId};swfobject.embedSWF(CBCR3.Player.Configuration.flashPath+"RTMPPlayer.swf?v="+CBCR3.Application.Config.svnRevision,this.containerId,"1","1",CBCR3.Player.Configuration.flashVersion.full,null,flashvars,params,attributes);}});

CBCR3.namespace("CBCR3.Player.Models");CBCR3.Player.Models.PlayerState=Class.create({initialize:function(properties)
{if(!properties)
properties={};this.favouriteStatus=properties.favouriteStatus||CBCR3.Player.FavouriteStatus.disabled,this.isPlaying=properties.isPlaying||false;this.mode=properties.mode||"";this.status=properties.status||CBCR3.Player.PlayerStatus.stopped;this.message=properties.message||"";this.details=properties.details||"";this.permalink=properties.permalink||"";this.streamMetaData=properties.streamMetaData||null;},set:function(properties)
{for(var prop in properties)
{if(this[prop]===undefined)
throw"Tried to set undefined Player State property: "+prop;this[prop]=properties[prop];}}});

CBCR3.namespace("CBCR3.Player.Models");CBCR3.Player.Models.StreamMetaData=Class.create({initialize:function(properties)
{if(!properties)
properties={};this.name=properties.name||"";this.permalink=properties.permalink||"";this.rtmpName=properties.rtmpName||"";this.rtmpUrl=properties.rtmpUrl||"";this.shoutcastUrl=properties.shoutcastUrl||"";this.tracks=properties.tracks||"";},getNowPlaying:function()
{for(var i=0;i<this.tracks.length;i++)
{if(this.tracks[i].status==CBCR3.Player.StreamTrackStatus.playing)
return this.tracks[i];}
throw"COULD NOT FIND A NOW PLAYING STREAM FOR: "+this.name;},getScheduledTracks:function()
{return this._getCollectionByStatus(CBCR3.Player.StreamTrackStatus.scheduled);},getPlayedTracks:function()
{return this._getCollectionByStatus(CBCR3.Player.StreamTrackStatus.played).reverse();},_getCollectionByStatus:function(status)
{var results=new Array();for(var i=0;i<this.tracks.length;i++)
{if(this.tracks[i].status==status)
results.push(this.tracks[i]);}
return results;}});

CBCR3.namespace("CBCR3.Player.Models");CBCR3.Player.Models.TrackMetaData=Class.create(CBCR3.Player.Models.Track,{initialize:function($super,properties)
{$super(properties);if(!properties)
properties={};this.status=properties.status||"";this.thumbnail=properties.thumbnail||"";}});

CBCR3.namespace("CBCR3.Player.Models");CBCR3.Player.Models.AbstractPermalinkContext=Class.create({initialize:function(properties)
{if(!properties)
properties={};this.title=properties.title||"";this.infoLink=properties.infoLink||"";this.details=properties.details||"";this.detailLink=properties.detailLink||"";},render:function()
{throw"render method must be overridden by sub class";}});

CBCR3.namespace("CBCR3.Player.Models");CBCR3.Player.Models.StreamPermalinkContext=Class.create(CBCR3.Player.Models.AbstractPermalinkContext,{render:function()
{return this.getTemplate().evaluate(this);},getTemplate:function()
{var markup="<div class='streamContext'>"+"RADIO STREAM: <strong>#{title}</strong> "+"</div>";return new Template(markup);}});

CBCR3.namespace("CBCR3.Player.Models");CBCR3.Player.Models.PlaylistPermalinkContext=Class.create(CBCR3.Player.Models.AbstractPermalinkContext,{render:function()
{return this.getTemplate().evaluate(this);},getTemplate:function()
{var markup="<div class='playlistContext'>"+"<div class='title'>"+"<a href='#{infoLink}' title=\"Go to this playlist's page\">PLAYLIST: #{title}</a>"+"</div>"+"<div class='subTitle'>"+"created by: <a href='#{detailLink}' title=\"Navigation to user's page\">#{details}</a>"+"</div>"+"</div>";return new Template(markup);}});

CBCR3.namespace("CBCR3.Player.Mappers");CBCR3.Player.Mappers.TrackMetaDataDtoMapper=Class.create(CBCR3.Commons.MapperBase,{mapFrom:function(dto)
{return new CBCR3.Player.Models.TrackMetaData({artist:dto.artist.blank()?"CBC Radio 3":dto.artist,infoLink:dto.infoLink,permalink:dto.permalink,status:dto.status.toLowerCase(),thumbnail:dto.thumbnail,title:dto.title,type:dto.type.toLowerCase(),iTunesLink:dto.iTunesLink});}});

CBCR3.namespace("CBCR3.Player.Mappers");CBCR3.Player.Mappers.StreamMetaDataDtoMapper=Class.create(CBCR3.Commons.MapperBase,{trackMapper:null,initialize:function($super)
{$super();this.trackMapper=new CBCR3.Player.Mappers.TrackMetaDataDtoMapper();},mapFrom:function(dto)
{return new CBCR3.Player.Models.StreamMetaData({name:dto.name,permalink:dto.permalink,rtmpName:dto.rtmpName,rtmpUrl:dto.rtmpUrl,shoutcastUrl:dto.shoutcastUrl,tracks:this.trackMapper.mapCollection(dto.tracks)});}});

CBCR3.namespace("CBCR3.Player.Mappers");CBCR3.Player.Mappers.PlaylistTrackDtoMapper=Class.create(CBCR3.Commons.MapperBase,{mapFrom:function(dto)
{return new CBCR3.Player.Models.PlaylistTrack({title:dto.Title,artist:dto.Artist,sequence:dto.Sequence,type:dto.Type,duration:dto.LengthInSeconds,audioUrl:dto.AudioUrl,infoLink:dto.InfoLink,permalink:dto.Permalink,thumb:dto.Thumb,iTunesLink:dto.iTunesLink});}});

CBCR3.namespace("CBCR3.Player.Services");CBCR3.Player.Services.StatsService={trackLink:function(event,options){var opts=options||{};if(!opts.linkPos)
opts.linkPos="R3Player";CBCR3.Stats.UI.trackLink(event,opts);}};

