/*
Script: productreview.js
	Contains <ProductReview>

Author:
	Walter Enriquez
	June 15, 2010

Class: ProductReview


Options:
*/
var ProductReview = new Class({

	options: {
		rating: 0
	},

	initialize: function(options){

		this.options = options;
		this.options.rating = 0;

		options.reviewbutton.addEvent('click', function(e) {
			new Event(e).stop();
			this.options.overlay = new Element('div', {
				'styles': {
					'background-color': '#1A1919',
					'top': '0',
					'left': '0',
					'width': '100%',
					'height': '100%',
					'position': 'fixed',
					'index': '9998',
					'opacity': '0.5'
				}
			}).inject(document.body);

			this.options.target = new Element('div').inject(document.body);

			new Ajax(this.options.body, {
				method: 'get',
				update: this.options.target,
				onComplete: function(){
					this.postReview()
				}.bind(this)
			}).request();

		}.bind(this));

		if ($$(this.options.readmore)) $$(this.options.readmore).each(function(e){
			e = $$(e);
			e.addEvent('click', function(el){
				new Event(el).stop();
				if ('none' == e.getPrevious().getStyle('display')) {
					e.getPrevious().setStyle('display', 'inline');
					e.setText('');
				} 
			});
		});

		if ($$(this.options.getmore)) $$(this.options.getmore).addEvent('click', function(e){
			new Event(e).stop();
			i=0;
			$$('div.review-hidden').each(function(e){
				i++;
				$$(e).removeClass('review-hidden');
				$$(e).addClass('review');
				if (5>=i) return;
			});
			if ($$('div.review-hidden').length <= i) $('review-more-button').setStyle('display', 'none');
		});

	},

	postReview: function(){
		$$('#rate a.star').addEvents({
			'click': this.updateStar.bindWithEvent(this, true),
			'mouseover': this.updateStar.bindWithEvent(this, false),
			'mouseout': this.updateStar.bindWithEvent(this, false)
		});

		$(this.options.close).addEvent('click', function(e){
			new Event(e).stop();
			this.options.target.empty();
			this.options.overlay.remove();
		}.bind(this));

		$(this.options.submit).addEvent('click', function(e){
			new Event(e).stop();
			submit = true;
			$$('p.fl').removeClass('valid-error');
			$$('label').removeClass('valid-error');
			$(this.options.form).getElements('input[id^=ff_]').setProperty('class', 'field');
			if (0 == this.options.rating) { $$('p.fl').addClass('valid-error'); submit = false; }
			if ($('ff_headline').value.replace(/\s/g,"") == "") { $$('label[for=headline]')[0].setProperty('class', 'valid-error'); submit = false; }
			if ($('ff_comment').value.replace(/\s/g,"") == "") { $$('label[for=comment]')[0].setProperty('class', 'valid-error'); submit = false; }
			if (!submit) {
				$$('p.error-headline').setStyle('display', 'block');
				return;
			}
			var str = new Array();
			var query = {
				prod_code: {name: 'prod_code', value: this.options.item},
				rating: {name: 'rating', value: this.options.rating},
				headline: {name: 'headline', value: $(this.options.form).headline.value},
				comment: {name: 'comment', value: $(this.options.form).comment.value},
				action: {name: 'action', value: this.options.action},
				sid: {name: 'sid', value: $(this.options.form).sid.value}
			}
			for (name in query) str.push(name+'='+encodeURIComponent(query[name].value));
			str = str.join('&');
			new Ajax(this.options.url, {
				method: 'post',
				data: str
			}).request();
			$$('.dialog-content')[0].empty();
			$('review-popup').setStyle('height', '145px');
			$$('.dialog-content')[0].setHTML('<p style="margin-top: 25px" class="dialog-title">Thank you. Your review has been received and is pending approval.</p><a class="button button-secondary" style="float: right" href="javascript:location.reload(true)">Close<span></span></a>');
			$('dialog-close').removeEvents().addEvent('click', function(e){
				new Event(e).stop();
				location.reload(true);
			});
		}.bind(this));
	},

	updateStar: function(e, save){
		e = new Event(e); e.stop();
		// had to add the following line, because IE did not create e as a mootools element.
		e.target = $(e.target);
		if ('img' == e.target.getTag()) e.target = e.target.getParent();
		if (save) this.options.rating = e.target.getProperty('href').match(/rating\-(\d+)/)[1];
		$('rate').getElements('a[href^=/search/]').each(function(e){
			var linkRating = e.getProperty('href').match(/rating\-(\d+)/)[1];
			e = e.getElement('img');
			if (linkRating <= this.options.rating) e.setProperty('src', '/images/star.png');
			else e.setProperty('src', '/images/empty-star.png');
		}.bind(this));
	}

});

ProductReview.implement(new Options, new Events);
