$PWT.Class.create
(
	{
		$name:		'reviewmanager',
		$namespace:	'SC'
	}
)
(
	{
		action:		false,
		request:	false,
		window:		false,
		savePeriod:	false,
		
		json:		{},
		dirty:		false,
		
		clock:		null,
		statusBar:	null,
		editor:		null,
		checkbox:	null,
		
		clockPE:	null,
		savePE:		null,
		
		initialize: function()
		{
			Ext.BLANK_IMAGE_URL	=$EXT+'resources/images/default/s.gif';
			this.action			=SC.reviewmanager.action.init(this);
			this.request		=SC.reviewmanager.request.init(this);
			this.clock			=new Ext.Toolbar.TextItem('');
			this.statusBar		=new Ext.StatusBar({items:[this.clock]});
//			this.editor			=new Ext.form.HtmlEditor
//			(
//				{
//					width:				764,
//					height:				265,
//					hideLabel:			true,
//					enableSourceEdit:	false,
//					listeners:
//					{
//						sync: function()
//						{
//							this.dirty=true;
//							return;
//						}.bind(this)
//					}
//				}
//			);
			this.editor			=new Ext.form.TextArea
			(
				{
					width:				764,
					height:				225,
					hideLabel:			true,
					grow:				true,
					growMin:			225,
					growMax:			5000,
					enableKeyEvents:	true,
					listeners:
					{
						keypress: function()
						{
							this.dirty=true;
							return;
						}.bind(this)
					}
				}
			);
			this.checkbox=new Ext.form.Checkbox
			(
				{
					hideLabel:	true,
					boxLabel:	'Has Commercial Potential',
					checked:	false,
					listeners:
					{
						change: function()
						{
							this.dirty=true;
							return;
						}.bind(this)
					}
				}
			);
			this.window=new Ext.Window
			(
				{
					title:			'Review - ',
					iconCls:		'icon-window',
					width:			800,
					height:			500,
					modal:			true,
					resizable:		false,
					layout:			'border',
					tbar:
					[
						{
							id:		'review_button_file',
							text:	'File',
							menu:
							[
								{
									id:			'review_button_file_save',
									text:		'Save',
									iconCls:	'icon-save',
									handler:	this.action.save.bind(this.action)
								},
								{
									id:			'review_button_file_publish',
									text:		'Publish',
									iconCls:	'icon-pagego',
									handler:	this.action.publish.bind(this.action)
								},
								'-',
								{
									id:			'review_button_file_exit',
									text:		'Close',
									iconCls:	'icon-close',
									handler:	function()
									{
										this.window.close();
										return;
									}.bind(this)
								}
							]
						},
						'->'
					],
					items:
					[
						{
							region:		'center',
							autoScroll:	true,
							items:
							[
								this.editor,
								this.checkbox
							]
						},
						{
							region:			'south',
							title:			'Lyrics',
							height:			160,
							autoScroll:		true,
							collapsible:	true,
							split:			true,
							//collapsed:		true,
							//border:			false,
							html:			'<p id="review_version_lyrics" style="padding:5px;"></p>'
						}
					],
					bbar:	this.statusBar
				}
			);
			this.clockPE=new PeriodicalExecuter
			(
				function(pe)
				{
					Ext.fly(this.clock.getEl()).update(new Date().format('g:i:s A'));
					return;
				}.bind(this),
				1
			);
			if(this.savePeriod)
			{
				this.savePE=new PeriodicalExecuter
				(
					function(pe)
					{
						if (this.dirty)
						{
							this.statusBar.showBusy('Saving review...');
							this.action.save
							(
								function()
								{
									this.dirty=false;
									this.statusBar.setStatus
									(
										{
											text:		'Saved Review',
											iconCls:	'tick'
										}
									);
								}.bind(this)
							);
						}
						return;
					}.bind(this),
					this.savePeriod
				);
			}
			return;
		},
		show: function()
		{
			this.window.show();
			return;
		},
		hide: function()
		{
			this.window.hide();
			return;
		},
		error: function(text)
		{
			Ext.MessageBox.show
			(
				{
					title:		'Error!',
					msg:		text,
					icon:		Ext.MessageBox.ERROR,
					buttons:	Ext.MessageBox.OK
				}
			);
			return;
		},
		message: function(text)
		{
			Ext.MessageBox.show
			(
				{
					msg:		text,
					icon:		Ext.MessageBox.INFO,
					buttons:	Ext.MessageBox.OK
				}
			);
			return;
		}
	}
);
SC.reviewmanager.action=
{
	parent:		false,
	init: function(parent)
	{
		this.parent=parent;
		return;
	},
	open: function(reviewID)
	{
		this.parent.request.open
		(
			reviewID,
			function(review)
			{
				this.parent.json=review;
				this.parent.window.setTitle('Review - '+review.song_title);
				this.parent.editor.setValue(review.review_text);
				this.parent.checkbox.setValue(Boolean(Number(review.isProfessionalLevel)));
				$('review_version_lyrics').update(review.version_lyrics);
				this.parent.window.getTopToolbar().add
				(
					SC.playerlitePanel.init
					(
						{
							src:		review.id3.url,
							playIcon:	'icon-control_play_blue',
							pauseIcon:	'icon-control_pause_blue',
							stopIcon:	'icon-control_stop_blue'
						}
					)
				);
				return;
			}.bind(this)
		);
		return;
	},
	save: function(callback)
	{
		Ext.MessageBox.show
		(
			{
				title:			'Saving Review...',
//					msg:			'',
				progressText:	'Processing Request...',
				width:			300,
				wait:			true,
				waitConfig:		{interval:200}
			}
		);
		this.parent.request.save
		(
			function(response)
			{
				Ext.MessageBox.hide();
				if (Object.isFunction(callback))callback(response);
			}
		);
		return;
	},
	publish: function()
	{
		if(this.parent.json.reviewstatus_id < 5 && 
		confirm('Are you sure that you want to publish this review?\n\nNote that you will not be able to make changes once this review has been published!'))
		{
			Ext.MessageBox.show
			(
				{
					title:			'Publishing Review...',
//						msg:			'',
					progressText:	'Processing Request...',
					width:			300,
					wait:			true,
					waitConfig:		{interval:200}
				}
			);
			this.parent.request.publish
			(
				function(response)
				{
					Ext.MessageBox.hide();
					if(response && response.success)
					{
						// Set status ID and Title columns within grid window to "Published".
						this.parent.json.reviewstatus_id="5"; //so that disables future saves
						this.parent.json.reviewstatus_title="published";
						var row = window.sc_grid.getGrid().getStore().getById(this.parent.json.review_id);
						if(row)
						{
							row.json.reviewstatus_id="5"; //so that user is advised that cannot be modified (again)
							row.json.reviewstatus_title="published";
							window.sc_grid.getGrid().getView().refresh();
						}
						this.parent.window.close();
					}
				}.bind(this)
			); //function also saves content
		}
		return;
	}
}
SC.reviewmanager.request=
{
	parent:		false,
	ERROR:
	{
		JSON_FAIL:	'Unable to handle request at this time. Please try again later.'
	},
	init: function(parent)
	{
		this.parent=parent;
		return;
	},
	request: function(address,params,callback)
	{
		new Ajax.Request
		(
			address,
			{
				method:		'post',
				parameters:	params,
				onSuccess:	function(response)
				{
					if (!response.responseJSON.success)
					{
						this.parent.error(response.responseJSON.message);
					}
					else
					{
						if (Object.isFunction(callback))callback(response.responseJSON);
					}
				}.bind(this),
				onError: function(response)
				{
					this.parent.error(this.JSON_FAIL);
					return;
				}.bind(this)
			}
		);
	},
	save: function(callback)
	{
		if(this.parent.json.reviewstatus_id < 5)
		{
			this.request
			(
				$COREROOT+'data/review/save/',
				{
					review_id:				this.parent.json.review_id,
					reviewstatus_id:		this.parent.json.reviewstatus_id,
					review_text:			this.parent.editor.getValue(),
					isProfessionalLevel:	(this.parent.checkbox.getValue())?1:0
				},
				callback
			);
		}
		else if (Object.isFunction(callback))
		{
			callback();
		}
	},
//		saveAll: function()
//		{
//			this.parent.tabPanel.items.items.each
//			(
//				function(item)
//				{
//					this.request
//					(
//						$COREROOT+'data/review/save/',
//						{
//							review_id:				item.json.review_id,
//							review_text:			item.items.items[0].getValue(),
//							isProfessionalLevel:	0
//						},
//						callback
//					);
//				}.bind(this)
//			);
//		},
	publish: function(callback)
	{
		if(this.parent.json.reviewstatus_id < 5)
		{
			this.request
			(
				$COREROOT+'data/review/publish/',
				{
					review_id:				this.parent.json.review_id,
					reviewstatus_id:		this.parent.json.reviewstatus_id,
					review_text:			this.parent.editor.getValue(),
					isProfessionalLevel:	(this.parent.checkbox.getValue())?1:0
				},
				callback
			);
		}
		else if (Object.isFunction(callback))
		{
			callback();
		}
	},
	open: function(reviewID,callback)
	{
		this.request
		(
			$COREROOT+'data/review/detail/',
			{review_id:reviewID},
			callback
		);
		return;
	}
}

/*
songcentral.reviewer=
{
	reviewDialog: Class.create
	(
		{
			window:					false,
			id:						0,
			initialize: function()
			{
				Ext.BLANK_IMAGE_URL=$EXT+'resources/images/default/s.gif';
				this.tabPanel=new Ext.TabPanel
				(
					{
						height:				324,
						enableTabScroll:	true,
						layoutOnTabChange:	true,
						defaults:
						{
							iconCls:	'icon-page'
						}
					}
				);
				this.window=new Ext.Window
				(
					{
						title:		'Reviewer',
						iconCls:	'icon-window',
						width:		800,
						height:		400,
						modal:		true,
						resizable:	false,
						tbar:
						[
							{
								id:		'review_button_file',
								text:	'File',
								menu:
								[
									{
										id:			'review_button_file_new',
										text:		'New',
										iconCls:	'icon-new',
										handler:	Prototype.emptyFunction
									},
									{
										id:			'review_button_file_open',
										text:		'Open',
										iconCls:	'icon-open',
										handler:	Prototype.emptyFunction
									},
									'-',
									{
										id:			'review_button_file_save',
										text:		'Save',
										iconCls:	'icon-save',
										handler:	Prototype.emptyFunction
									},
									{
										id:			'review_button_file_saveAs',
										text:		'Save As...',
										iconCls:	'icon-saveAs',
										handler:	Prototype.emptyFunction
									},
									'-',
									{
										id:			'review_button_file_close',
										text:		'Close',
										//iconCls:	'icon-new',
										handler:	Prototype.emptyFunction
									},
									{
										id:			'review_button_file_closeAll',
										text:		'close All',
										//iconCls:	'icon-new',
										handler:	Prototype.emptyFunction
									},
									'-',
									{
										id:			'review_button_file_exit',
										text:		'Exit',
										iconCls:	'icon-exit',
										handler:	function()
										{
											this.window.close();
											return;
										}.bind(this)
									}
								]
							}
						],
						items: this.tabPanel,
						listeners:
						{
							render: function()
							{
//								// Add a class to the parent TD of each text item so we can give them
//								// some custom inset box styling:
//								Ext.fly(this.wordCount.getEl()	.parentNode).addClass('x-status-text-panel');
//								Ext.fly(this.charCount.getEl()	.parentNode).addClass('x-status-text-panel');
//								Ext.fly(this.clock.getEl()		.parentNode).addClass('x-status-text-panel');
//								// Kick off the clock timer that updates the clock el every second:
//								Ext.TaskMgr.start
//								(
//									{
//										run: function()
//										{
//											Ext.fly(this.clock.getEl()).update(new Date().format('g:i:s A'));
//											return;
//										}.bind(this),
//										interval: 1000
//									}
//								);
								return;
							}.bind(this)
						}
					}
				);
				this.window.show();
				this.addTab('Test 1');
				this.addTab('Test 2');
				this.addTab('Test 3');
				return;
			}
		}
	)
}
*/