/*

	ReviewInterface.js
	
*/

var ReviewInterface = new Class({
	options: { 
		Wrapper: '/Classes.cfm',
		ReviewForm: Class.empty, 
		SubmitBtn: Class.empty,
		ReviewDiv: Class.empty
	}, 
	
	initialize: function(options) {
		// Start-Up Code	
		this.setOptions(options);
		
		this.Form = $(this.options.ReviewForm); 
		this.Btn  = $(this.options.SubmitBtn);
		this.Div  = $(this.options.ReviewDiv);
		
		if( this.Btn )
			this.Btn.addEvent('click', this.SubmitForm.bindWithEvent( this ));
	},
	
	/*
		sAttributes : Attributes in the form of /Page/1/Request/Test Request Response or ?Page=1&Request=Test Request Response
		oContainer  : Container in the form of 'test'
		postFunction: Function to execute after the response has been received.
	*/	
	LoadContent: function(sAttributes, oContainer, postFunction) {
		if( postFunction != null )
			new Ajax( this.options.Wrapper + sAttributes, { method: 'get', update: oContainer, onComplete: postFunction}).request();
		else
			new Ajax( this.options.Wrapper + sAttributes, { method: 'get', update: oContainer}).request();
	},
	
	SubmitForm: function(event)	{
		// Update FCK 
		this.UpdateEditorFormValue();
		// Form Checking
		var ReviewMessage = this.Form.ReviewMessage.value.replace( /\//g, "slash_" ); // Need to take out / for attributes.
		var Rating        = this.Form.RatingNum.value;
		var ClassID       = this.Form.RatingClass.value;
				
		if( ReviewMessage == "" )
		{
			this.Echo( 'Please enter your review.' );
		} else if( Rating == 0 ) {
			this.Echo( 'Please select a star rating.' );
		} else {
			var Attributes = '/Review/' + ClassID + '/Rating/' + Rating + '/Message/' + ReviewMessage;
			this.LoadContent( Attributes, this.Form, null );
		}
		event.stop();
	},
	
	UpdateEditorFormValue: function()
	{
		// Udates FCK Form Values
		for ( i = 0; i < parent.frames.length; ++i )
				if ( parent.frames[i].FCK )
						parent.frames[i].FCK.UpdateLinkedField();
	},
	
	Echo: function( value ) {
		alert( value );
	}
});

ReviewInterface.implement(new Options);

window.addEvent('domready', function(){
	var Review = new ReviewInterface({
		Wrapper: '/Classes.cfm',
		ReviewForm: 'review_form', 
		SubmitBtn: 'SubmitReview',
		ReviewDiv: 'ReviewForm'
	});
});