var songcentral=
{
	audiocenter:		false,
	listmanager:		false,
	listManagerLoaded:	false,
	launchAudioCenterWindow: function(locationPrefix)
	{
		if (!this.audiocenter || this.audiocenter.closed)
		{
			this.audiocenter=window.open(locationPrefix+'/audiocenter/','audiocenter','width=800,height=600,resizable=1,scrollbars=0');
		}
		else
		{
			this.audiocenter.focus();
		}
		return this;
	},
	audioCenterIsOpen: function()
	{
		return (!this.audiocenter || this.audiocenter.closed)?false:true;
	},
	focusAudioCenter: function()
	{
		this.audiocenter.focus();
		return this;
	},
	blurAudioCenter: function()
	{
		this.audiocenter.blur();
		return this;
	},
	loadListManager: function(callback)
	{
		if (!Object.isFunction(callback))callback=Prototype.emptyFunction;
		if (!Prototype.Browser.IE && !Prototype.Browser.WebKit)
		{
			callback();
		}
		else
		{
			(function(){callback();}).delay(1);
		}
//		this.includeCSS($PUBLICROOT+'ext/resources/css/ext-all.css');
//		this.includeCSS($PUBLICROOT+'ext/resources/css/xtheme-slate.css');
//		this.includeCSS($PUBLICROOT+'scripts/listmanager/listmanager.css');
//		this.includeCSS($PUBLICROOT+'scripts/listmanager/column-tree.css');
//		this.includeCSS($PUBLICROOT+'scripts/listmanager/menus.css');
//		window.loader=new s3core.loader
//		(
//			{
//				info:		'Your web browser is currently downloading the necessary components to display the data on this page.',
//				message:	'Loading styles and images&#8230;'
//			}
//		);
//		window.lightscreen=new s3core.lightscreen({color:'#FFF'});
//		window.lightscreen.add(window.loader.toString());
//		window.loader.updateMessage('Loading Ext Core&#8230;');
//		s3core.include_once
//		(
//			[$PUBLICROOT+'ext/adapter/ext/ext-base.js',		function(){window.loader.updateMessage('Loading UI Components&#8230;');}],
//			[$PUBLICROOT+'ext/ext-all.js',					function(){window.loader.updateMessage('Loading Plugins&#8230;');}],
//			[$PUBLICROOT+'ext/plugins/rowExpander.js',		function(){window.loader.updateMessage('Loading List Manager&#8230;');}],
//			$PUBLICROOT+'scripts/listmanager/ColumnNodeUI.js',
//			$PUBLICROOT+'scripts/listmanager/listmanager.js',
//			$PUBLICROOT+'scripts/listmanager/listmanager.layout.js',
//			$PUBLICROOT+'scripts/listmanager/listmanager.request.js',
//			[
//				$PUBLICROOT+'scripts/listmanager/listmanager.action.js',
//				function()
//				{
//					window.loader.fade();
//					window.lightscreen.hide();
//					if (!Prototype.Browser.IE && !Prototype.Browser.WebKit)
//					{
//						callback();
//					}
//					else
//					{
//						(function(){callback();}).delay(1);
//					}
//					return;
//				}
//			]
//		);
	},
	openListManager: function(config)
	{
		if (!this.listManagerLoaded)
		{
			this.loadListManager
			(
				function()
				{
					this.listManagerLoaded=true;
					this.listmanager=new listmanager(config);
					return;
				}.bind(this)
			)
		}
		else
		{
			this.listmanager=new listmanager(config);
		}
		return;
	},
	includeCSS: function(url)
	{
		var newCSS=document.createElement('link');
			newCSS.rel='stylesheet';
			newCSS.type='text/css';
			newCSS.href=url;
			$$('head')[0].appendChild(newCSS);
		return this;
	},
	contactWindow: false,
	openContactWindow: function(parameters)
	{
		if (typeof parameters!='object')
		{
			parameters={to:parameters};
		}
		this.contactWindow=new Ext.Window
		(
			{
				title:		'Send Message',
				resizable:	false,
				modal:		true,
				width:		500,
				height:		300,
				items:
				{
					xtype:		'form',
					bodyStyle:	'padding:5px 5px 0',
					labelAlign:	'top',
					items:
					[
						{
							xtype:			'textfield',
							id:				'contact_subject',
							fieldLabel:		'Subject',
							labelStyle:		'font-weight:bold;',
							emptyText:		'Enter a Subject...',
							width:			470,
							allowBlank:		true
						},
						{
							xtype:			'textarea',
							id:				'contact_message',
							fieldLabel:		'Message',
							labelStyle:		'font-weight:bold;',
							emptyText:		'Enter your message...',
							width:			470,
							height:			150,
							allowBlank:		true
						}
					]
				},
				buttons:
				[
					{
						id:		'contact_button_send',
						text:	'Send',
						handler: function()
						{
							new Ajax.Request
							(
								$COREROOT+'data/message/send/?event=true',
								{
									parameters: Object.extend
									(
										{
											subject:	Ext.getCmp('contact_subject').getValue(),
											message:	Ext.getCmp('contact_message').getValue()
										},
										parameters
									),
									onSuccess: function(response)
									{
										if (response.responseJSON.success) 
										{
											Ext.getCmp('contact_subject').disable();
											Ext.getCmp('contact_message').disable();
											Ext.getCmp('contact_button_send').disable();
											Ext.getCmp('contact_button_cancel').setText('close');
											Ext.MessageBox.show
											(
												{
													title:		'Success!',
													msg:		response.responseJSON.message,
													buttons:	Ext.Msg.OK,
													icon:		Ext.MessageBox.INFO
												}
											);
										}
										else 
										{
											Ext.MessageBox.show
											(
												{
													title:		'Oops!',
													msg:		response.responseJSON.message,
													buttons:	Ext.Msg.OK,
													icon:		Ext.MessageBox.ERROR
												}
											);
										}
										return;
									}
								}
							);
						}.bind(this)
					},
					{
						id:		'contact_button_cancel',
						text:	'Cancel',
						handler: function()
						{
							this.contactWindow.close();
						}.bind(this)
					}
				]
			}
		);
		this.contactWindow.show();
		return this.contactWindow;
	},
	checkSongAgainstAlertsWatchList: function(versionID)
	{
		if(versionID && versionID>0)
		{
			new Ajax.Request($COREROOT+'data/song_alerts/watch/',{method:'post',parameters:{version_id:versionID}});
		}
		return true;
	},
	bound_playerInitiatedHandler: false
};
//Ext.onReady
//(
//	function()
//	{
//		s3core.include_once
//		(
//			$COREROOT+'direct/API/?event=true',
//			function()
//			{
//				Ext.Direct.addProvider.apply(Ext.Direct,new this.API.Direct().API);
//			}
//		);
//	}
//);
document.observe
(
	'dom:loaded',
	function()
	{
		s3core.formatDisabledFieldLables();
		if ($('launchListManager')!=null && !$('launchListManager').hasClassName('disabled'))
		{
			$('launchListManager').observe
			(
				'click',
				function()
				{
					this.openListManager
					(
					    {
				    		showTitle:	false,
				    		load:		true,
							callback:	function(listmanager)
							{
								//Set a callback function to be executed when the load button is clicked.
								listmanager.config.callback=function(listProperties,records)
								{
									try
									{
										Event.stopObserving('player:initiated',this.bound_playerInitiatedHandler);
									}
									catch(e){}
									//Bind a function to handle loading the records.
									this.bound_playerInitiatedHandler=function(listProperties,records)
									{
										var $this=this.audiocenter.player.playlist;
										$this.id=listProperties.list_id;
										$this.properties=listProperties;
										$this.player.panel_playlist.setTitle('Playlist ('+listProperties.list_name+')');
										//Clear the visual playlist.
										var root=$this.view.getRootNode();
										while (root.childNodes.length)
										{
											root.removeChild(root.childNodes[0]);
										}
										//Clear the player playlist.
										$this.player.removeAllRecords();
										//Load records into the playlist.
										for (var i=0; i<records.length; i++)
										{
											if (!i)
											{
												$this.addAndActivateRecord(records[i].id3);
												$this.player.togglePlayStatus();
											}
											else
											{
												$this.addRecord(records[i].id3);
											}
										}
										$this.setPlaylistStatus(true);
										window.listManagerWindow.close();
										return;
									}.bind(this,listProperties,records)
									document.observe
									(
										'player:initiated',
										this.bound_playerInitiatedHandler
									);
									//If the audio center is not open.
									if (!this.audioCenterIsOpen())
									{
										//Hmmm! Doesn't work... is blocked by browser - Fixed below with seperate event handler.
//										this.launchAudioCenterWindow('?URI=');
									}
									//Audio Center is open.
									else
									{
										//If the audio center has an unsaved playlist open, warn the user!
										if (!this.audiocenter.player.playlist.getPlaylistStatus())
										{
											Ext.MessageBox.show
											(
												{
													title:		'Player Warning!',
													msg:		'The audio center currently has a playlist open that has not been saved. Do you want to save it before loading the selected list?',
													icon:		Ext.MessageBox.WARNING,
													buttons:	Ext.MessageBox.YESNOCANCEL,
													fn:			function(button)
													{
														//Post-Save function...
//														var postSaveFunction=function()
//														{
//															this.bound_playerInitiatedHandler();
//															return;
//														}.bind(this);
														switch(button)
														{
															case 'yes':
															{
																if (this.id)
																{
																	this.savePlaylistWithoutDialog(songcentral.bound_playerInitiatedHandler);
																}
																else
																{
																	this.openSaveContext
																	(
																		function()
																		{
																			songcentral.bound_playerInitiatedHandler();
																			songcentral.audiocenter.listManagerWindow.close();
																			return;
																		}.bind(this)
																	);
																}
																songcentral.focusAudioCenter();
																break;
															}
															case 'no':
															{
																songcentral.bound_playerInitiatedHandler();
																songcentral.focusAudioCenter();
																break;
															}
															case 'cancel':{break;}//Closes dialog - does nothing.
														}
														return;
													}.bind(this.audiocenter.player.playlist)
												}
											);
										}
										else
										{
											this.bound_playerInitiatedHandler();
											this.focusAudioCenter();
										}
									}
									return;
								}.bind(this)
								//This has to be bound seperately to get around the browser's blocking it.
								Ext.getCmp('load_button_load').on
								(
									'click',
									function()
									{
										if (!songcentral.audioCenterIsOpen())songcentral.launchAudioCenterWindow('?URI=');
										return;
									}
								);
								window.listManagerWindow=new Ext.Window
								(
									{
										title:			'List Manager',
										width:			614,
										autoHeight:		true,
										modal:			true,
										resizable:		false,
										items:			listmanager.getShell(),
										buttonAlign:	'right',
										buttons:
										{
											id:			'save_button_save',
											text:		'Save',
											handler:	function()
											{
												window.listManagerWindow.close();
												return;
											}
										}
									}
								);
								window.listManagerWindow.show();
								listmanager.getCancelButton().on
								(
									'click',
									function()
									{
										window.listManagerWindow.close();
										return;
									}
								);
								return;
							}.bind(this)
						}
					)
				}.bind(this)
			);
			if ($('launchAudioCenter')!=null && !$('launchAudioCenter').hasClassName('disabled'))
			{
				$('launchAudioCenter').observe
				(
					'click',
					function()
					{
						this.launchAudioCenterWindow('?URI=');
						return;
					}.bind(this)
				);
			}
		}
		return;
	}.bind(songcentral)
);











//TODO: Implement properly.
//window.ExtLoadingMonitor=new PeriodicalExecuter
//(
//	function()
//	{
//		try
//		{
//			if (!Object.isUndefined(Ext))
//			{
//				Ext.BLANK_IMAGE_URL=$PUBLICROOT+'ext/resources/images/default/s.gif';
//				window.ExtLoadingMonitor.stop();
//			}
//		}
//		catch(e){}
//		return;
//	},
//	0.01
//);
