var RadioPlayer = function() {
	
	var $U = CBC.UTIL;
	var $ = $U.getElementsById;
	var _config = new Object();
	var _streams = new Object();
	var _currentStreamId;
	var _selectorElem = $('channelselector');
	var _popUrl = '/radio2/channels/popup.html';
	var _playerElem;
	var _closeLink = YAHOO.util.Dom.getElementsByClassName('close', 'a', _selectorElem)[0];
	var _selectLink = YAHOO.util.Dom.getElementsByClassName('select', 'a', 'playerhead')[0];
	var _firstPlay = false;
	
	var _noFlashMsg = '<p class="noflash">To listen to the Radio2 channels, you need the most recent Flash player. You can download it <a href="http://get.adobe.com/flashplayer" target="_blank">here</a>.</p>';
	
	return {
		init: function(initObj) {
			_config = initObj;
			_playerElem = $(_config.playerDiv);
			_streams = {
				/* Backup player
				classical:	{swf:"/radio2/channels/radioplayer.swf",layoutType:1,name:"Classical",streamUrl:"http://asx.abacast.com/cbc-classicalhi-192.pls",metaUrl:"/radio2/streams/feeds/classical.xml"},
				jazz: 		{swf:"/radio2/channels/radioplayer.swf",layoutType:2,name:"Jazz",streamUrl:"http://asx.abacast.com/cbc-jazzhi-192.pls",metaUrl:"/radio2/streams/feeds/jazz.xml"},
				songwriters:{swf:"/radio2/channels/radioplayer.swf",layoutType:2,name:"Canadian Songwriters",streamUrl:"http://asx.abacast.com/cbc-songwriterhi-192.pls",metaUrl:"/radio2/streams/feeds/songwriter.xml"},
				composers:	{swf:"/radio2/channels/radioplayer.swf",layoutType:1,name:"Canadian Composers",streamUrl:"	http://asx.abacast.com/cbc-canadianclassicalhi-192.pls",metaUrl:"/radio2/streams/feeds/canadianclassical.xml"}
				*/
				classical:	{swf:"http://cbcca.a.mms.mavenapps.net/mms/rt/1/site/cbcca-cbc-radio-pub01-live/current/launch.swf?playerId=classical"},
				jazz:		{swf:"http://cbcca.a.mms.mavenapps.net/mms/rt/1/site/cbcca-cbc-radio-pub01-live/current/launch.swf?playerId=jazz"},
				songwriters:{swf:"http://cbcca.a.mms.mavenapps.net/mms/rt/1/site/cbcca-cbc-radio-pub01-live/current/launch.swf?playerId=canadiansingersongwriter"},
				composers:	{swf:"http://cbcca.a.mms.mavenapps.net/mms/rt/1/site/cbcca-cbc-radio-pub01-live/current/launch.swf?playerId=canadiancomposer"}
			};
			
			this.initSelector();
			
			// if a specific stream is requested, load it
			if (_streams[_config.stream] != undefined) {
				this.initStream();
			// else, show the channel selector
			} else {
				this.toggleVisibility(_selectorElem, _playerElem);
			}
		},
		
		initStream: function() {
			this.toggleVisibility(_playerElem, _selectorElem);
			this.loadStream(_config.stream);
		},
		
		initSelector: function() {
			var streamLinks = $('channels').getElementsByTagName('dt');
			var streamDescriptions = $('channels').getElementsByTagName('dd');
			for (var i=0; i<streamLinks.length; i++) {
				YAHOO.util.Event.on(streamLinks[i], 'click', this.streamClick, {btn:streamLinks[i], desc:streamDescriptions[i], streamId:streamLinks[i].className}, this);
				YAHOO.util.Event.on(streamLinks[i], 'mouseover', this.streamOver, {btn:streamLinks[i], desc:streamDescriptions[i]}, this);
				YAHOO.util.Event.on(streamLinks[i], 'mouseout', this.streamOut, {btn:streamLinks[i], desc:streamDescriptions[i]}, this);
			}
		},
		
		streamClick: function(ev, obj) {
			this.loadStream(obj.streamId);
			this.streamOut(null, obj);
		},
		streamOver: function(ev, obj) {
			YAHOO.util.Dom.addClass(obj.btn, 'on');
			YAHOO.util.Dom.addClass(obj.desc, 'on');
		},
		streamOut: function(ev, obj) {
			YAHOO.util.Dom.removeClass(obj.btn, 'on');
			YAHOO.util.Dom.removeClass(obj.desc, 'on');

		},
		
		
		openSelect: function() {
			YAHOO.util.Dom.addClass(_selectorElem, 'on');
		},
		closeSelect: function() {
			YAHOO.util.Dom.removeClass(_selectorElem, 'on');
		},
		
		toggleVisibility: function(module1, module2) {
			YAHOO.util.Dom.addClass(module1, 'on');
			YAHOO.util.Dom.removeClass(module2, 'on');
		},
		
		loadStream: function(streamId) {
			_currentStreamId = streamId;
			this.toggleVisibility(_playerElem, _selectorElem);
			if (!_firstPlay) {
				YAHOO.util.Dom.addClass(_closeLink, 'on');
				YAHOO.util.Dom.addClass(_selectLink, 'on');
			}
			
			var so = new SWFObject(_streams[streamId].swf, "mavenswf", "467", "206", "8", "#FFFFFF" );
			so.addParam("allowScriptAccess", "always");
			so.addParam("wmode","opaque");
			//so.addVariable("streamName",_streams[streamId].name);
			//so.addVariable("layoutType",_streams[streamId].layoutType);
			//so.addVariable("streamUrl",_streams[streamId].streamUrl);
			//so.addVariable("metaUrl",_streams[streamId].metaUrl);
			var userHasFlash = so.write(_config.playerDiv);
			if (!userHasFlash) {
				$(_config.playerDiv).innerHTML = _noFlashMsg;
			}
			
			_firstPlay = true;
		},
		stop: function() {
			YAHOO.util.Dom.removeClass(_closeLink, 'on');
			this.toggleVisibility(_selectorElem, _playerElem);
			_playerElem.innerHTML = "";
			_currentStreamId = null;
			_firstPlay = false;
		},
		
		popPlayer: function(streamId) {
			var popUrl = _popUrl;
			if (streamId) {
				popUrl+= '?stream='+streamId;
			} else if (_currentStreamId) {
				popUrl+= '?stream='+_currentStreamId;
			}
			this.stop();
			window.open(popUrl, 'r2pop', 'width=487,height=352,scrollbars=0,resizable=1').focus();
		}


	}
}
