var Player = new Class({	

	initialize: function(name, controller, playerDiv, playlistDiv, url, enabled){
		this.name = name;
		this.c = controller;
		this.playerDiv = playerDiv;
		this.playlistDiv = playlistDiv;
		
		this.playlistFile = url //+ '?nothing=' + Math.floor(Math.random()*1000);//solves cache problem
		this.playerEnabled = enabled;
		
		this.playerObj = null;//needs to be instantiated globally!!!
		this.playlistButtons = [];
		this.activePlaylistButton = false;
		this.playlistObj = null;
		
		this.controllerFirst = false;
			
		this.attachPlayer();
		this.playerEnabled ? this.enablePlayer() : this.disablePlayer();
	},
	
	attachPlayer: function()
	{
		this.player = new SWFObject("player.swf", this.name, "425", "350", "8");
		
		//this.player.addParam("wmode", "transparent");
		this.player.addParam("allowfullscreen", "true");
		this.player.addVariable("width", "425");
		this.player.addVariable("height", "350");
		this.player.addVariable("autostart", this.playerEnabled);
		this.player.addVariable("volume", "50");
		this.player.addVariable("mute", "false");
		this.player.addVariable("controlbar", "over");
		this.player.addVariable("overstretch", "false");
		this.player.addVariable("file", this.playlistFile);
		this.player.addVariable("repeat", "always");
		
		this.writePlayer();
	},
	
	
	writePlayer: function()
	{
		this.player.write(this.playerDiv);
	},
	
	addEvents: function()
	{
		this.playerObj.addControllerListener('PLAYLIST','player.doPlaylist');	
		this.playerObj.addControllerListener('ITEM','player.doItem');//next in playlist
		this.playerObj.addControllerListener('MUTE','player.doMute');
		this.playerObj.addControllerListener('PLAY','player.doPlay');
		this.playerObj.addControllerListener('STOP','player.doPlay');
	},
	
	
	doItem: function(obj)
	{
		this.setActivePlaylistButton(this.playlistButtons[obj.index]);	
		this.miniPlayer.setTitle(obj.index);
	},
		
	doPlaylist: function(obj)
	{
		this.miniPlayer = new MiniPlayer($('mp'),this);
		//alert('hi')
		this.playlistObj = this.playerObj.getPlaylist();
		var i = 0;
		this.playlistObj.each
		(
			function(item)
			{
				
				var secVar0 = item.duration ;                            // The initial data, in seconds
				var minVar = Math.floor(secVar0/60);  // The minutes
				var secVar = secVar0 % 60;  
				var e = new Element
				(
					'div', 
					{
						'id': item.title,
						'html': '<small>' + item.type + '</small><br>' + item.author + '<br><b>' + item.title + '</b><br><small>duration: ' + minVar + ':' + secVar +'</small>',
						'styles':
						{
							'border-bottom': '1px solid silver',
							'padding-top': '10px',
							'padding-bottom': '10px',
							'background':'none'
						}
					}
				);
				
				e.inject($(this.playlistDiv));
				this.playlistButtons.include(new playlistButton(e, this.c, i));
				i++;
			}.bind(this)
		)
		
		
	},

	doMute:function()
	{
		//alert(this.controllerFirst);
		if(!this.controllerFirst)
		{
			this.miniPlayer.setMute();
		}else{
			this.controllerFirst = false;	
		}
	},
	
	doPlay:function(state)
	{
		
		if(!this.controllerFirst)
		{
			this.miniPlayer.setPlayFromFlash(state.state);
		}else{
			this.controllerFirst = false;	
		}
	},
	
	setActivePlaylistButton: function(b)
	{
		if(this.activePlaylistButton)
		{
				this.activePlaylistButton.e.setStyle('color','#3f3f3f');
		}
	
		this.activePlaylistButton = b;
		this.activePlaylistButton.e.setStyle('color','#00b8dc');
	},
	
	gotoAndPlay:function(b)
	{
		this.playerEnabled = true;
		this.playerObj.sendEvent("ITEM", b.number);
		this.miniPlayer.setTitle(b.number);
		this.setActivePlaylistButton(b);
		this.miniPlayer.setPlayCheck();
	},	

	togglePlayer: function(handle)
	{
		//this.playerObj.sendEvent("PLAY");
		this.playerEnabled ? this.disablePlayer() : this.enablePlayer();;
	},
	
	enablePlayer: function()
	{
		$(this.playerDiv).setStyle('visibility','visible');
		$(this.playlistDiv).setStyle('display','block');
	},
	
	disablePlayer: function()
	{
		$(this.playerDiv).setStyle('visibility','hidden');
		$(this.playlistDiv).setStyle('display','none');
	},
	
	afterMorph: function()
	{
		this.playerEnabled = this.playerEnabled ? false : true;	
	},
	
	toggleSound: function()
	{
		//this.playerObj.sendEvent("PLAY");
		this.controllerFirst = true;
		this.playerObj.sendEvent("MUTE");
		
	},
	
	togglePlay: function()
	{
		this.controllerFirst = true;
		this.playerObj.sendEvent("PLAY");
		
		//this.playerObj.sendEvent("MUTE");
	},
	
	previous: function()
	{
		this.playerObj.sendEvent("PREV");
		this.miniPlayer.setPlayCheck();
		//this.playerObj.sendEvent("MUTE", false);
		
	},
	
	next: function()
	{
		this.playerObj.sendEvent("NEXT");
		this.miniPlayer.setPlayCheck();
		//this.playerObj.sendEvent("MUTE", false);
	}
	
	
})

