var BM = BM || {};

BM.Media = function() {
	
	return {
		
		target : null,
		player : null,
		tracker : null,
		
		initialize: function(){
			
			this.renderPlayer();
			this.initPlaylists();
			
		},
		
		renderPlayer: function(){
		
			this.target = $('player');
			var url = '/_components/flash/mediaplayer.swf';
			var id = 'mplayer';
			
			var mode = document.body.id;

			if (!this.target) {
				return;
			}
						
			// Render Player
			switch (mode) {
				
				case 'video':
					this.player = new SWFObject(url, id, 480, 360, 8, '#ffffff');
					this.player.addParam('allowscriptaccess', 'always');
					this.player.addVariable('usefullscreen', 'false');
					this.player.addVariable('width', 480);
					this.player.addVariable('height', 360);
					this.player.addVariable('javascriptid', id);
					this.player.addVariable('enablejs', 'true');
					this.player.addVariable('displaywidth', 480);
					this.player.addVariable('displayheight', 360);
					this.player.addVariable('autostart', 'true');
					this.player.addVariable('file', '');
					this.player.write(this.target);
					break;
					
				case 'audio':
					this.player = new SWFObject(url, id, 480, 20, 8, '#ffffff');
					this.player.addParam('allowscriptaccess', 'always');
					this.player.addVariable('width', 480);
					this.player.addVariable('height', 20);
					this.player.addVariable('javascriptid', id);
					this.player.addVariable('enablejs', 'true');
					this.player.addVariable('usefullscreen', 'false');
					this.player.addVariable('file', '');
					this.player.addVariable('autostart', 'true');
					this.player.write(this.target);				
					break;
			}

			if (!Browser.Engine.trident) {
				this.target.setStyle('position', 'relative');
				this.target.player = this.target.getElement('embed');
				this.target.dummy = new Element('div').setStyles(this.target.player.getCoordinates());
				this.target.dummy.setStyles({
					'position' : 'absolute',
					'background-color' : '#000000',
					'top' : 0,
					'left' : 0
				});
				this.target.adopt(this.target.dummy);
				this.target.player.setStyle('visibility', 'hidden');
				
				this.target.slide = new Fx.Slide(this.target).hide();
			} else {
				this.target.addClass('hidden');
			}
			
		},
		
		initPlaylists: function(){
			
			if (this.player == null) {
				return;
			}
			
			var that = this;
			var t = $('trackTitle');
			var headers = $$('#playlists h3');
			var lists = $$('#playlists .list');
			
			window.skroll = new Fx.Scroll(window, {
				duration : 250
			});
			
			headers.each(function(h){
				h.addEvents({
					'mouseover' : function() {
						
						this.addClass('hover');
						
						if (Browser.Engine.trident4) {
							$$('select').each(function(el){
								el.set('styles',{
									'visibility' : 'hidden'
								});
							});
						}
					},
					'mouseout' : function() {
						
						this.removeClass('hover');
						
						if (Browser.Engine.trident4) {
							$$('select').each(function(el){
								el.set('styles',{
									'visibility' : 'visible'
								});
							});
						}
					}
				});
			});
			
			var tips = new Tips(headers, {
				maxTitleChars : 60,
				fixed : true,
				offsets : {
					x : -198,
					y : -25
				}
			});
			
			var a = new Accordion(headers, lists, {
				duration : 200,
				alwaysHide : true
			});
			
			$$('#playlists a').each(function(link){
				link.addEvent('click', function(e){
					(new Event(e)).stop();
					
					var name = this.get('text');
					
					t.set('text', name);
					window.skroll.toTop();
					
					that.loadClip(this.href);
					
					this.setAsActive.bind(this)();
					
					// Tracking
					that.tracker.src = (document.location.pathname.replace(/^\/(audio|video)(.*)/,'/$1/') + 'tracker.php?clip=' + encodeURIComponent(name));
					
				});
				
				link.setAsActive = function() {
					$$('#playlists a').each(function(l){
						if (l !== this) {
							l.removeClass('active');
						}
					});	
					this.addClass('active');
				};
			});
			
			// Set up tracker
			this.tracker = this.tracker || new Element('iframe',{
				'width' : 0,
				'height' : 0
			}).inject(document.body);
			
			
		},
		
		loadClip: function(clip) {
			
			if (!this.player) {
				return;
			}
			
			if (!Browser.Engine.trident && !this.target.slide.open) {
				this.target.slide.slideIn().chain(function(){
					this.target.dummy.destroy();
					this.target.player.setStyle('visibility', 'visible');
					delete this.target.dummy;
				}.bind(this));
				this.target.set('styles', {
					'margin-left' : 'auto',
					'margin-right' : 'auto'
				});
			} else {
				this.target.removeClass('hidden');
			};
			
			try{
				document.getElementById('mplayer').loadFile({'file' : clip});
			} catch(e) {
				if (Browser.Engine.trident4) {
					this.target.empty();
					this.target.setHTML("<h4>Sorry - there's a problem</h4><p>These " + document.body.id + " clips don't work well in Internet Explorer 6.  Please try a more capable browser, like <a href='http://www.mozilla.com'>Firefox</a>.</p>");
				}
			}
			
		}
		
	};
	
}();

window.addEvent('domready',BM.Media.initialize.bindWithEvent(BM.Media));