/**
 * Review Centre Facebook Object
 * -------------------------------
 *
 * @version 1 (!)
 *
 * @author Nick G. Lavrik <nick.lavrik@gmail.com>
 *
 * Used to load facebook module async to mainpage
 *
 * Dependencies:
 *
 * - jQuery v1.4.2+
 * - RC
 *
 */

RC.Facebook = {
	pageLoad : function() {
		return RC.Facebook.init();
	},

	// loads facebook right panel
	init : function() {
		/**
		 * Social Bookmarks
		 */
		FB.init({
			appId: '166680780047811',
			status: true,
			cookie: true,
			xfbml:  true,
			channelUrl: document.location.protocol+'//'+document.location.host+'/channel.html'
		});

	}
};


;

/**
 * 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 Google Analytics Handlers Object
 * ------------------------------
 *
 * Adds Google Analytics handlers
 *
 * @author Dmitriy Dorosh <dmdo@reviewcentre.com>
 *
 * Dependencies:
 *
 * - jQuery v1.4.2+
 * - RC
 *
 */

RC = RC || {};
RC.GoogleAnalyticsHandlers = {
	/**
	 * The object constructor, sets all the primary listeners
	 *
	 * @return void
	 */
	mainContentDone: function(mainContent) {
		$('.writeYourOwnReview', mainContent).click(this.handleWriteReviewButton);
		$('.AddCommentButton', mainContent).click(this.handleCommentReviewButton);
		$('.placeholder-sidebar.BTQCompareButton A', mainContent).click(this.handlerBTQCompareButtonSidebar);
		$('.placeholder-review.BTQCompareButton A', mainContent).click(this.handlerBTQCompareButtonReview);

		// Tab on the left of hand side of every page.
		$('.WriteReviewButton' , mainContent).click(this.handlerWriteAReviewButton);

		// Tab below the item graphic on the Overview, Reviews, Prices, Media Gallery, Verdict, Specification etc. There are two types.
		// The normal one in websites.
		$('#itemBoxToolbar A[href^="/add-"]:not(.Large)', mainContent).click(this.handlerWarItemPageRegular);

		// Tab below the item graphic on the Overview, Reviews, Prices, Media Gallery, Verdict, Specification etc. There are two types.
		// The extra large in websites.
		$('#itemBoxToolbar A[href^="/add-"].Large', mainContent).click(this.handlerWarItemPageLarge);

		// Tab at the bottom of the review card on the item pages plus the featured reviews on the topic pages and the individual review page.
		$('.ReviewCommentWrapper A.writeYourOwnReview', mainContent).click(this.handlerWarReviewCard);

		// Button on the small card on the category page
		$('.ItemCardSlim .WriteReview A[href^="/add-"]', mainContent).click(this.handlerWarCategoryPage);

		// Write another review button at the end of the write a review process
		$('#WriteAReview .ThankyouArea A[href="/add_review.html"]', mainContent).click(this.handlerWarAnotherReview);

		if(RC.Observer instanceof Object) {
			RC.Observer.bind('RC.WriteReviewPage.ReviewPublishClick', this.triggerReviewPublishClick);
		};
		// $('input[type="submit"][value="Agree & Submit"]')
		/*
		$('input[type="submit"].ReviewPublish')
			.click(this.ReviewPublishClick)
		;
		*/
	},

 	/**
	* google analitics tracking (Write Review)
	*/
	triggerReviewPublishClick : function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackEvent', 'Write A Review', 'Agree and Submit Review', ''], event);
	},


	trackGA : function(info, event) {
		var result = false;
		if(_gaq instanceof Object && _gaq['push']) {
			// if(!location.href.match(/lavrik/) || confirm('Send GoogleAnalitics\n\n    * '+Array.prototype.join.call(info,'\n    * '))) {
				_gaq.push(info);
				result = true;
			// };
		};

		if(!result) {
			event.preventDefault();
			event.stopPropagation();
		};

		return result;
	},

	/**
	 * adds GA tracker to "write your own review" button
	 *
	 * @return boolean
	 */
	handleWriteReviewButton: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackEvent', 'Review Card Buttons', 'Write your own review', ''], event);
	},

	/**
	 * adds GA tracker to "comment on this review" button
	 *
	 * @return boolean
	 */
	handleCommentReviewButton: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackEvent', 'Review Card Buttons', 'Comment on this review', ''], event);
	},

	/**
	 * adds GA tracker to "Write Own Review" button
	 * left on hand side
	 *
	 * @param event
	 * @scope DOMElement
	 * @return boolean
	 */
	handlerWriteAReviewButton: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackPageview', '/WAR/LHS'], event);
	},

	/**
	 * adds GA tracker to "Write a Review" button
	 * in itemPage
	 *
	 * @param event
	 * @scope DOMElement
	 * @return boolean
	 */
	handlerWarItemPageRegular: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackPageview', '/WAR/ItemPageRegular'], event);
	},

	/**
	 * adds GA tracker to "Write a Review" button
	 * in itemPage
	 *
	 * @param event
	 * @scope DOMElement
	 * @return boolean
	 */
	handlerWarItemPageLarge: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackPageview', '/WAR/ItemPageLarge'], event);
	},

	/**
	 * adds GA tracker to "Write your own review" button
	 * in review block
	 *
	 * @param event
	 * @scope DOMElement
	 * @return boolean
	 */
	handlerWarReviewCard: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackPageview', '/WAR/ReviewCard'], event);
	},

	/**
	 * adds GA tracker to "Write a review" link
	 * in category page
	 *
	 * @param event
	 * @scope DOMElement
	 * @return boolean
	 */
	handlerWarCategoryPage: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackPageview', '/WAR/CategoryPage'], event);
	},

	/**
	 * Write another review button at the end of the write a review process
	 *
	 * @param event
	 * @scope DOMElement
	 * @return boolean
	 */
	handlerWarAnotherReview: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackPageview', '/WAR/WriteAnotherReview'], event);
	},

	/**
	 * adds GA tracker to "Compare BTQ Button" from Right Column
	 *
	 * @return boolean
	 */
	handlerBTQCompareButtonSidebar: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackEvent', 'BTQ Compare', $(this).attr('title'), 'Top Right Column'], event);
	},

	/**
	 * adds GA tracker to "Compare BTQ Button" from Review Card
	 *
	 * @return void
	 */
	handlerBTQCompareButtonReview: function(event) {
		return RC.GoogleAnalyticsHandlers.trackGA(['t1._trackEvent', 'BTQ Compare',  $(this).attr('title') , 'Review Card'], event);
	}
};


;
/**
 * 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);

}
;

