
/**
 * Collapses and add accordian-like events to sidebars
 */

RC.AccordianSidebar = {

	'triggerElementSelector'    : '.sideBarAccordian div ul li h3',

	mainContentDone: function() {
		this.addTriggerEvents();
		this.hideAll();
	},

	addTriggerEvents: function() {
		$(this.triggerElementSelector).each(function(index){
			var h3 = $(this);
			h3.css('cursor',  'pointer');
			h3.attr('title', 'Click to hide/show');

			h3.click(function(event){
				$(event.target).toggleClass('collapsible-plus').toggleClass('collapsible-minus');
				$(event.target).parent().next().slideToggle("slow", function(){
					return false;
				});
			});
		});
	},

	hideAll: function() {
		$(this.triggerElementSelector).each(function(index) {
			$(this).parent().next().slideToggle();
		});
	}
};
;
/**
 * Review Centre Forms Object
 * --------------------------
 *
 * Building a javascript constructor function (a class) which can be applied to forms to provide validation
 * and other functionality commonly associated with forms / data capture
 *
 * @author Mr Prichard <rob@reviewcentre.com>
 *
 * Dependencies:
 *
 * - jQuery v1.4.2+
 * - RC
 *
 */

RC.Form = function(form){

	/**
	 * a handle for the form element
	 */
	var form = form || {};

	/**
	 * an aggregate of field objects added to the form
	 */
	var fields = [];

	/**
	 * holds the validation status of the form, default valid - innocent until proven guilty!
	 */
	var valid = true;

	/**
	 * Add field
	 * @access public
	 * @param  object  a FormField object
	 */
	this.addField = function(FormField){

		fields[FormField.getId()] = FormField;

	};

	/**
	 * Get field
	 * @access public
	 * @param  int  the id of the field
	 */
	this.getField = function(field_id){

		return fields[field_id];

	};

	/**
	 * Delete field
	 * @access public
	 * @param  string  the field id
	 */
	this.deleteField = function(field_id){

		delete fields[field_id];

	};

	/**
	 * Validates the form
	 * @access public
	 */
	this.validate = function(){
		var valid = true;
		/* TEMP. Chat to Amir about this. Think this can be removed.
		for(field in fields){

			if(typeof fields[field] !== "function"){

				if(!fields[field].validate()){

					$(fields[field].getField()).addClass("RedBorder");
					valid = false;

				}else{

					$(fields[field].getField()).removeClass("RedBorder");

				}

			}

		}
		*/

		return valid;

	};

	/**
	 * Like a constructor method
	 * Sets the submit event listener when the Form object is instantiated
	 */
	(function(self){
		if (typeof form.submit != 'undefined') {
			$(form).submit(self.validate);
		}
	})(this);
}
;
/**
 * Review Centre FormField Object
 * ------------------------------
 *
 * Building a javascript constructor function (a class) which can be applied to forms to provide validation 
 * and other functionality commonly associated with forms / data capture
 *
 * @author Mr Prichard <rob@reviewcentre.com>
 *
 * Dependencies:
 *
 * - jQuery v1.4.2+
 * - RC 
 * 
 */

RC.FormField = function(field,options){

	/**
	 * a handle for the element
	 */
	var field = field || {};
	
	/**
	 * a temporary storage for click clear method, it saves the original field value here upon first click
	 */
	var default_text = null;
	
	/**
	 * holds the validation status of the form, default valid - innocent until proven guilty!
	 */
	var valid = true;
	
	/**
	 * an object / hash of default options
	 */
	var settings = {
	
		validateNotEmpty: false,
		validateNumeric: false,
		validateDate: false,
		validateEmail: false,
		validateIsChecked: false,
		clickClear: false,
		starRating: false
	
	};
	
	/**
	 * gets the id attribute of the field
	 * 
	 * @access public
	 */
	this.getId = function(){
	
		return field.id;
	
	};
	
	/**
	 * gets the value attribute of the field
	 * 
	 * @access public
	 */
	this.getValue = function(){
	
		return field.value;
	
	};
	
	/**
	 * returns the handle for the html element
	 * 
	 * @access public
	 */
	this.getField = function(){
	
		return field;
	
	};
	
	/**
	 * validates the field according to the options set
	 * 
	 * @access public
	 */
	this.validate = function(){
	
		if(settings.validateNotEmpty){
		
			valid = validateNotEmpty();
		
		}
		
		if(settings.validateNumeric){
		
			valid = validateNumeric();
		
		}
		
		if(settings.validateIsChecked){
		
			valid = validateIsChecked();
		
		}
		
		if(settings.validateEmail){
		
			valid = validateEmail();
		
		}
		
		if(settings.validateDate){
		
			// valid = validateDate();
		
		}
		
		// TODO: add more validation processes here ...
		
		return valid;
	
	};
	
	/**
	 * validates that the field is not empty or null
	 * 
	 * @access private
	 */
	var validateNotEmpty = function(){
	
		if(field.value !== '' && field.value !== null){
		
			return true;
			
		}else{
		
			return false;
		
		}
	
	};
	
	/**
	 * validates that the field is numeric
	 * 
	 * @access private
	 */
	var validateNumeric = function(){
	
		if(!isNaN(parseFloat(field.value)) && isFinite(field.value)){
		
			return true;	
		
		}else{
		
			return false;
		
		}
		
	};
	
	/**
	 * validates that the field is a date
	 * 
	 * @access private
	 */
	var validateDate = function(){
	
		// TODO: rex ex for date, may need to support different date ISO formats
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
		if(!filter.test(field.value)){
		
			return false;
			
		}else{
		
			return true;
		
		}
	
	};
	
	/**
	 * validates that the field is an email address
	 * 
	 * @access private
	 */
	var validateEmail = function(){
	
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if(!filter.test(field.value)){
		
			return false;
			
		}else{
		
			return true;
		
		}
	
	};
	
	/**
	 * validates that a checkbox is checked
	 * 
	 * @access private
	 */
	var validateIsChecked = function(){
	
		if(field.checked == true){
		
			return true;
			
		}else{
		
			return false;
		
		}
	
	};
	
	this.isClickClear = function(){
	
		if(settings.clickClear){
		
			return true;
			
		}else{
		
			return false;
		
		}
	
	};
	
	this.getSettings = function(){

		return settings;
	
	};
	
	/**
	 * clears the default value text from an element
	 * 
	 * @access private
	 */
	var clickClear = function(){
		
		default_text = field.value;
		field.value = '';
	
	};
	
	/**
	 * replaces the default value text of an element
	 * 
	 * @access private
	 */
	var clickRecall = function(){
	
		if(field.value == "" || field.value == default_text) {
		
			field.value = default_text;
			
		}else{
		
			$(field).unbind('focus');
			
		}

	};
	
	/**
	 * A self executing function that will run when the "class" is instantiated
	 * emulating a constructor function
	 */
	(function(self){
	
		// extend the default options with the options passed in
		settings = $.extend(settings,options);
		
		// apply click clear if set
		if(settings.clickClear){
		
			$(field).focus(clickClear).blur(clickRecall);
		
		}
		
		// apply starRating if set
		if(settings.starRating){
		
			var srField = new RC.StarRatingField(field);
			
		}
	
	})(this);

}
;

