/*
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'
	},

	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();
	},

	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();
	},

	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);
					}).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);
					}).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);
					}).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);
					}).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);
					}).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();
	},

	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);