/*
Script: rating.js
	Contains <rating>

Author:
	Alan Roemen

Class: rating
	A 5 star link rating system

Options:
	starImgOn : Star image used on mouseover or successfull vote. Default: false
	starImgOff : Star image used on load and to show empty rated star. Default: false
	baseURL : Page location. Default: false
	className : CSS class name. Default: 'LINKRating'
*/
var LinkRating = new Class({

	options: {
		starImgOn : false,
		starImgOff : false,
		baseURL : false,
		className : 'LINKRating',
		scriptName: 'rating',
		autoFillEmail: true
	},

	initialize: function(field, options){
		var nodes, el, temp = '';
		this.setOptions(options);
		this.field = $(field);
		if(!this.field) return;
		this.field.className = this.options.className;

		// Get script base path
		if (!this.options.baseURL) {
			var elements = document.getElementsByTagName('script');
			for (var i=0; i<elements.length; i++) {
				if (elements[i].src && (elements[i].src.indexOf(this.options.scriptName+'.js') != -1)) {
					var src = elements[i].src;
					this.options.baseURL = src.substring(0, src.lastIndexOf('/'));
					break;
				}
			}
			// Get document base path
			this.documentBasePath = document.location.href;
			if (this.documentBasePath.indexOf('?') != -1)
				this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.indexOf('?'));
			this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.lastIndexOf('/'));
			if (this.options.baseURL.indexOf('://') == -1 && this.options.baseURL.charAt(0) != '/')
				this.options.baseURL = this.documentBasePath + "/" + this.options.baseURL;
		}

		// Adds Stylesheet
		new Asset.css(this.options.baseURL + '/' + this.options.scriptName + '.css');

		// Setup Ajax
		this.link = this.field.innerHTML;this.field.setHTML('');
		this.url = this.options.baseURL + '/'+this.options.scriptName+'.php';

		// Set Month Background
		if(!this.options.starImgOn) this.options.starImgOn = this.options.baseURL+'/star.png';
		if(!this.options.starImgOff) this.options.starImgOff = this.options.baseURL+'/star_grey.png';

		this.voted = false;
		this.start();
		if (this.options.autoFillEmail == true) {
			this.getEmail();
		}
	},

	start: function(){
		new Ajax(this.url, {
			method: 'post',
			data: 'link='+this.link+'&get_vote=true',
			onComplete: function(voted){
				this.build();
				if(voted!='false'){
					this.vote(voted,false);
					this.complete('true');
				}
			}.bind(this)
		}).request();
	},

	getEmail: function(){
		new Ajax(this.url, {
			method: 'get',
			data: 'get_email=true',
			onComplete: function(response){
				if (response != 'false'){
					this.user_email=response
				} else {
					this.user_email=''
				}
			}.bind(this)
		}).request()
	},

	build: function(){
		this.star = new Array();
		this.star[0] = new Element('img',{
				'src':this.options.starImgOff,
				'styles': {
					'cursor': 'pointer'
				},
				'events': {
					'mouseover': (function(){
						if(this.voted) return;
						this.rate(1);
						this.star[0].src = this.options.starImgOn;
					}).bindWithEvent(this),
					'mouseout': (function(){
						if(this.voted) return;
						this.rate(false);
						this.star[0].src = this.options.starImgOff;
					}).bindWithEvent(this),
					'click': (function(){
						if(this.voted) return;
						this.vote(1);
						this.show();
					}).bindWithEvent(this)}
				}).injectInside(this.field);
		this.star[1] = new Element('img',{
				'src':this.options.starImgOff,
				'styles': {
					'cursor': 'pointer'
				},
				'events': {
					'mouseover': (function(){
						if(this.voted) return;
						this.rate(2);
						this.star[0].src = this.options.starImgOn;
						this.star[1].src = this.options.starImgOn;
					}).bindWithEvent(this),
					'mouseout': (function(){
						if(this.voted) return;
						this.rate(false);
						this.star[0].src = this.options.starImgOff;
						this.star[1].src = this.options.starImgOff;
					}).bindWithEvent(this),
					'click': (function(){
						if(this.voted) return;
						this.vote(2);
						this.show();
					}).bindWithEvent(this)}
				}).injectInside(this.field);
		this.star[2] = new Element('img',{
				'src':this.options.starImgOff,
				'styles': {
					'cursor': 'pointer'
				},
				'events': {
					'mouseover': (function(){
						if(this.voted) return;
						this.rate(3);
						this.star[0].src = this.options.starImgOn;
						this.star[1].src = this.options.starImgOn;
						this.star[2].src = this.options.starImgOn;
					}).bindWithEvent(this),
					'mouseout': (function(){
						if(this.voted) return;
						this.rate(false);
						this.star[0].src = this.options.starImgOff;
						this.star[1].src = this.options.starImgOff;
						this.star[2].src = this.options.starImgOff;
					}).bindWithEvent(this),
					'click': (function(){
						if(this.voted) return;
						this.vote(3);
						this.show();
					}).bindWithEvent(this)}
				}).injectInside(this.field);
		this.star[3] = new Element('img',{
				'src':this.options.starImgOff,
				'styles': {
					'cursor': 'pointer'
				},
				'events': {
					'mouseover': (function(){
						if(this.voted) return;
						this.rate(4);
						this.star[0].src = this.options.starImgOn;
						this.star[1].src = this.options.starImgOn;
						this.star[2].src = this.options.starImgOn;
						this.star[3].src = this.options.starImgOn;
					}).bindWithEvent(this),
					'mouseout': (function(){
						if(this.voted) return;
						this.rate(false);
						this.star[0].src = this.options.starImgOff;
						this.star[1].src = this.options.starImgOff;
						this.star[2].src = this.options.starImgOff;
						this.star[3].src = this.options.starImgOff;
					}).bindWithEvent(this),
					'click': (function(){
						if(this.voted) return;
						this.vote(4);
						this.show()
					}).bindWithEvent(this)}
				}).injectInside(this.field);
		this.star[4] = new Element('img',{
				'src':this.options.starImgOff,
				'styles': {
					'cursor': 'pointer'
				},
				'events': {
					'mouseover': (function(){
						if(this.voted) return;
						this.rate(5);
						this.star[0].src = this.options.starImgOn;
						this.star[1].src = this.options.starImgOn;
						this.star[2].src = this.options.starImgOn;
						this.star[3].src = this.options.starImgOn;
						this.star[4].src = this.options.starImgOn;
					}).bindWithEvent(this),
					'mouseout': (function(){
						if(this.voted) return;
						this.rate(false);
						this.star[0].src = this.options.starImgOff;
						this.star[1].src = this.options.starImgOff;
						this.star[2].src = this.options.starImgOff;
						this.star[3].src = this.options.starImgOff;
						this.star[4].src = this.options.starImgOff;
					}).bindWithEvent(this),
					'click': (function(){
						if(this.voted) return;
						this.vote(5);
						this.show();
					}).bindWithEvent(this)}
				}).injectInside(this.field);
		this.txt = new Element('span').injectInside(this.field);
	},

	rate: function(val){
		if(val === false) this.txt.setHTML('');
		else this.txt.setHTML(val+' of 5 Stars');
	},

	vote: function(val, save){
		if(save!==false) save=true;
		this.voted = true;
		this.txt.setHTML('You voted '+val+' star'+(val==1?'':'s'));
		for(var i=0; i<val; i++) this.star[i].src = this.options.starImgOn;

		if(!save) return;
		new Ajax(this.url, {
			method: 'post',
			data: 'link='+this.link+'&save_rating='+val,
			onComplete: function(response){
				this.complete(response);
			}.bind(this)
		}).request();
	},

	comment:function(A,B){
		new Ajax(this.url,{
			method:"post",
			data:"email="+A+"&comment="+B+"&link="+this.link+"&save_comment=true",
			onComplete:function(C){
				this.complete(C)
				}.bind(this)
			}).request()
		},

	show:function(){
		this.background = new Element('div',{
			'class': 'background',
			'styles': {
				'width': '100%',
				'height': '100%',
				'opacity': '0.7',
				'background': '#000000',
				'position': 'fixed',
				'top': '0',
				'left': '0'
			},
			'events': {
				'click':(function(e){
					new Event(e).stop();
					this.hide();
				}).bindWithEvent(this)
			}
		});
		this.container=new Element('div', { 'class': this.options.className + 'popUP' });
		new Element('p').setHTML('<strong> Thank you for ranking this page! Your opinion is valuable.</strong>').injectInside(this.container);
		new Element('p').setHTML('If you\'d like to send us a comment about this page, write a message below and click submit.').injectInside(this.container);
		new Element('p').setHTML('If you have a question regarding products or services, please include your e-mail address below for a personal response.').injectInside(this.container);
		var email_container = new Element('div').setHTML('<label>E-mail address (Optional):</label>').injectInside(this.container);
		this.email=new Element('input').setProperties({
			'type': 'text',
			'name': 'rating_email',
			'style': 'width: 218px;',
			'value': this.user_email
		}).injectInside(email_container);
		new Element('label').setStyle('font-style','italic').setHTML('Your e-mail address will be used solely for responses to your comments.').injectInside(this.container);
		var comment_container=new Element('div').injectInside(this.container);
		this.user_comment = new Element('textarea').setProperties({
			'name': 'rating_comment',
			'cols': '42',
			'rows': '4'
		}).injectInside(comment_container);
		new Element('input',{
			'events':{
				'click': (function(e){
					new Event(e).stop();
					this.comment(this.email.getProperty('value'), this.user_comment.getProperty('value'));
					this.hide();
				}).bindWithEvent(this)
			}
		}).setProperties({
			'type': 'image',
			'src': '/images/button_submit.gif',
			'value': 'Submit'
		}).injectInside(comment_container);
		new Element('input',{
			'events': {
				'click':(function(e){
					new Event(e).stop();
					this.hide();
				}).bindWithEvent(this)
			}
		}).setProperties({
			'type': 'image',
			'src': '/images/button_cancel.gif',
			'value': 'Cancel'
		}).injectInside(comment_container);
	
		var close_element = this.container.getElement(".close");
		if(close_element) {
			close_element.addEvent('click', function(e){
				new Event(e).stop();
				this.hide();
			}.bind(this));
		}
		this.background.inject($$('body')[0]);
		this.container.inject($$('body')[0]);
		var csize=this.container.getSize().size;
		this.container.setStyles({
			'margin-top':(((csize.y/2).toInt())*-1)+'px',
			'margin-left':(((csize.x/2).toInt())*-1)+'px'
		});
		if(window.ie6) {
			new Ajax(this.options.baseURL+'/ie7-fixed.js',{
				'method': 'get',
				'evalScripts': true
			}).request();
		}
		this.fireEvent('onLoad');
	},

	hide:function(){
		this.container.remove();
		this.background.remove();
		this.active=false
	},

	complete: function(response){
		if(response == 'true') { for(var i=0; i<this.star.length; i++) this.star[i].setStyle('cursor','auto'); return; }
		this.voted = false;
		this.field.empty();
		this.build();
		alert(response);
	}
});

LinkRating.implement(new Events, new Options);
