Ext.namespace('cc');
Ext.namespace('cc.re');
Ext.namespace('cc.re.reviews');

Ext.onReady(function(){
	/**
	* Shows the review window
	*/
	cc.re.reviews.show_review_win = function(m, id)
	{
		if (cc.re.member_only_rate && !cc.re.authenticated)
		{
			Ext.MessageBox.alert('Membership Required!', 'Only members can write reviews.  If you already registered, login to your member account before writing a review.  If you do not have an account, registration is quick, easy and FREE!');
			return;
		}
		
		cc.re.reviews.review_win.getUpdater().update({
			url: '../admin/modules/reviews/site_provider.php?act=rev_win_layout',
			params: {
				model: m,
				id: id
			}
		});
		
		cc.re.reviews.review_win.show();
	}
	
	/**
	* Cancels review entry
	*/
	cc.re.reviews.cancel = function()
	{
		cc.re.reviews.review_win.hide();
	}
	
	/**
	* Submits a review
	*/
	cc.re.reviews.submit = function()
	{
		var sf = new Ext.form.BasicForm('review_form', {
			method: 'POST',
			url: '../admin/modules/reviews/site_provider.php?act=save_rev'
		});
		
		fields = sf.getValues();
		// Validate the values
		if (fields.email == '' || fields.nickname == '' || fields.review == '' || fields.title == '')
		{
			Ext.MessageBox.alert('Incomplete review!', 'You must fill out all fields to submit a review!');
			return false;
		}
		else if (typeof fields.rate == 'undefined' || fields.rate == '')
		{
			Ext.MessageBox.alert('Incomplete review!', 'You must choose a rating!');
			return false;
		}
		
		sf.submit({
			success: function(form, action)
			{
				if (cc.re.moderate_reviews)
				{
					Ext.MessageBox.alert(
						'Success!',
						'Your review was sucessfully received and will appear on the site once a moderator has approved it.',
						function() { document.location.reload(); }
					);
				}
				else
				{
					document.location.reload();
				}
				cc.re.reviews.review_win.hide();
			},
			failure: function(form, action)
			{
				Ext.MessageBox.alert('Connection error!', 'An error was encountered while communicating with the server.');
			}
		});
		return true;
	}
	
	/**
	* The review window
	*/
	cc.re.reviews.review_win = new Ext.Window({
		width: 640,
		height: 480,
		autoLoad: false,
		bodyStyle: 'padding: 25px;',
		closable: true,
		closeAction: 'hide',
		constrain: true,
		draggable: true,
		hideModel: 'display',
		layout: 'fit',
		maximizable: false,
		modal: true,
		renderTo: 'body',
		title: 'Write Review',
		resizable: false,
		tools: [
			{
				id: 'refresh',
				handler: function (event, toolEl, Panel)
				{
					Panel.getUpdater().refresh();
				}
			}
		]
	});
	
	
	/* ----------------------------------------------------------------------- */
	/**
	* Shows the review window
	*/
	cc.re.reviews.show_rate_win = function(m, id)
	{
		if (cc.re.member_only_review && !cc.re.authenticated)
		{
			Ext.MessageBox.alert('Membership Required!', 'Only members can submit ratings.  If you already registered, login to your member account before giving a rating.  If you do not have an account, registration is quick, easy and FREE!');
			return;
		}
		
		cc.re.reviews.rate_win.getUpdater().update({
			url: '../admin/modules/reviews/site_provider.php?act=rate_win_layout',
			params: {
				model: m,
				id: id
			}
		});
		
		cc.re.reviews.rate_win.show();
	}
	
	/**
	* Cancels review entry
	*/
	cc.re.reviews.rate_cancel = function()
	{
		cc.re.reviews.rate_win.hide();
	}
	
	/**
	* Submits a review
	*/
	cc.re.reviews.rate_submit = function()
	{
		var sf = new Ext.form.BasicForm('rate_form', {
			method: 'POST',
			url: '../admin/modules/reviews/site_provider.php?act=save_rate'
		});
		
		fields = sf.getValues();
		// Validate the values
		if (typeof fields.rate == 'undefined' || fields.rate == '')
		{
			Ext.MessageBox.alert('Incomplete rating!', 'You must choose a rating!  If you do not wish to submit a rating, click Cancel.');
			return false;
		}
		
		sf.submit({
			success: function(form, action)
			{
				Ext.MessageBox.alert(
					'Success!',
					'Your rating was sucessfully received.',
					function() { document.location.reload(); }
				);
				cc.re.reviews.rate_win.hide();
			},
			failure: function(form, action)
			{
				Ext.MessageBox.alert('Connection error!', 'An error was encountered while communicating with the server.');
			}
		});
		return true;
	}
	
	/**
	* The review window
	*/
	cc.re.reviews.rate_win = new Ext.Window({
		width: 640,
		height: 280,
		autoLoad: false,
		bodyStyle: 'padding: 25px;',
		closable: true,
		closeAction: 'hide',
		constrain: true,
		draggable: true,
		hideModel: 'display',
		layout: 'fit',
		maximizable: false,
		modal: true,
		renderTo: 'body',
		title: 'Rate It!',
		resizable: false,
		tools: [
			{
				id: 'refresh',
				handler: function (event, toolEl, Panel)
				{
					Panel.getUpdater().refresh();
				}
			}
		]
	});
});