/**
 * See comments engine
 * 
 * @author Ollie Maitland
 * @copyright Byng Systems LLP
 * @requires Mootools-1.2
 */
Se.comments = new Class(
{
	/**
	 * Implement Options and ByngView
	 *  
	 */
	Implements: [Options, ByngView],
	
	/**
	 * Message submitted
	 * 
	 * @type String
	 */
	MSG_COMMENT_FEEDBACK_1 : 	'<p>Thank you for taking the time to provide your comment and rating. They have been sent automatically to the company and copied to SEE Ltd.</p>',
	MSG_COMMENT_FEEDBACK_2 : 	'<p>Thank you for alerting SEE Ltd to the fact that you regard this company\'s information as inaccurate, misleading and/or incomplete. On the assumption that your challenge has met the basic requirements as set out <a href="/legal/licensing_terms#12" target="_blank">here</a>, we will initiate an investigation right away.</p>',
	
	/**
	 * Holds meta data for ByngView
	 * 
	 */
	meta: {
		file    : 'se.comments.js',
		version : '0.1',
		gateway : {'package' : 'site', 'module' : 'comments'}
	},

	ratings : {'policy' : null, 'clarity' : null},
	stars  : {},

	initialize : function ( compare )
	{
		if (compare) {
			this.compare = compare;
			this.companyId 	= compare.companyId;
			this.questionId = compare.questionId;
		}
	},

	/**
	 * Attach behaviours to the comment block
	 * 
	 * @param Element comments
	 */
	attachBehaviours : function ( comments )
	{
		this.stars  = {'policy' : [], 'clarity' : []};
		
		comments.getElement('#se-comment_form').addEvent('submit', this.submit.bind(this));
		comments.getElements('.se-rating li a').each(function(star){
			if (star.hasClass('policy') ) {
				this.stars.policy.push(star);
			} else {
				this.stars.clarity.push(star);
			}
			star.addEvent('click',  this.selectStar.bind(this));
			
			// add mouseover events
			star.addEvent('mouseover', function(e) {
				if (e.target.hasClass('policy') ) {
					var selected = this.ratings.policy;
					var stars = this.stars.policy;
				} else {
					var selected = this.ratings.clarity;
					var stars = this.stars.clarity;
				}
				
				var rating = e.target.getProperty('accesskey');
				
				if (!selected || rating == selected) {
					stars.each(function(star) {
						if (star.getProperty('accesskey') <= rating) {
							star.addClass('on');
						} else {
							star.removeClass('on');
						}
					});
				}
				
			}.bind(this));
			
			star.addEvent('mouseout', function(e) {
				if (e.target.hasClass('policy') ) {
					var selected = this.ratings.policy;
					var stars = this.stars.policy;
				} else {
					var selected = this.ratings.clarity;
					var stars = this.stars.clarity;
				}
				
				var rating = e.target.getProperty('accesskey');
				
				if (!selected || rating == selected) {
					
					stars.each(function(star) {
					if (star.getProperty('accesskey') > rating) {
						star.removeClass('on');
					} else {
						star.addClass('on');						
					}
					});
				}
				
			}.bind(this));
			
		}.bind(this));
		
		comments.getElements('#se-comment_challenge_confirm').addEvent('click',  this.confirmChallenging.bind(this));
		comments.getElements('.se-comment_back a.back').addEvent('click',  this.returnBack.bind(this));
		this.form = comments.getElement('form'); 
		return this;
	},
	
	confirmChallenging : function (e)
	{
		Byng.ui.toggle( $('se-comment_contact') );
	},
	
	returnBack : function ( e ) 
	{
		e.stop();
		SqueezeBox.setContent('ajax', this.compare.getCompareRequest().composeAsString());		
	},
	
	/**
	 * Select a star
	 * 
	 * @param Event e
	 */
	selectStar : function ( e )
	{
		e.stop();
		// remove the other highlights
		e.target.getParent('ul').getElements('a').removeClass('on');
		// add this toggle
		e.target.fireEvent('mouseover', [e]);
		
		var rating = e.target.getProperty('accesskey');

		if ( e.target.hasClass('policy') ) {
			this.ratings.policy = rating;
			$(this.form['rating[policy]']).set('value', rating);
		} else {
			this.ratings.clarity = rating;
			$(this.form['rating[clarity]']).set('value', rating);
		}
		e.target.blur();
	},
	
	/**
	 * Submit a comment
	 * 
	 * @param Event e
	 */
	submit : function ( e )
	{
//		e.stop();
//		var request = this.getRequest().addParam('companyId',this.companyId).addParam('questionId', this.questionId).setAction('submit');
//		Byng.transit.post({'form' : this.form, 'request' : request, 'onComplete' : this.submitted.bind(this)});
	},
	
	/**
	 * Comment submitted
	 * 
	 * @param Object xml
	 */
	submitted : function ( xml )
	{
		var resp = new ByngXml ( xml );
		if (resp.getCode() == 0) {
			alert (resp.getChildContent('error'));
		} else {
			
			if (resp.getParam('challenged') == true) {
				var feedback = this.MSG_COMMENT_FEEDBACK_2;
			} else {
				var feedback = this.MSG_COMMENT_FEEDBACK_1;
			}
			
			var back = this.form.getParent('.sbox-content-ajax')
								.getElement('.se-comment_back').clone()
								.addEvent('click',  this.returnBack.bind(this));
								
			new Element('div',{'class' : 'se-comment_feedback'})
						.set('html', feedback)
						.inject(this.form.getParent('.sbox-content-ajax').empty())
						.adopt(back);
		}
	}
	
	
});

window.addEvent('domready', function() 
{
	Byng.app.comments = new Se.comments;
});