/*
# "VOTItaly" Plugin for Joomla! 1.5.x - Version 1.2
# License: http://www.gnu.org/copyleft/gpl.html
# Authors: Luca Scarpa & Silvio Zennaro
# Copyright (c) 2006 - 2009 Siloos snc - http://www.siloos.it
# Project page at http://www.joomitaly.com - Demos at http://demo.joomitaly.com
# ***Last update: Aug 27th, 2009***
*/
var VotitalyPlugin = new Class ({
	options: {
    submiturl: '/plugins/content/ji_votitaly_ajax.php',
	loadingimg: '/plugins/content/ji_votitaly/images/loading.gif',
	show_stars: true,
	star_description: 'undefined',
		language: {
			updating: 'undefined',
			thanks: 'undefined',
			already_vote: 'undefined',
			votes: 'undefined',
			vote: 'undefined',
			average: 'undefined',
			outof: 'undefined',
			error1: 'undefined',
			error2: 'undefined',
			error3: 'undefined',
			error4: 'undefined',
			error5: 'undefined'
		}
  },
	
  initialize: function(options){
    this.setOptions(options);

		this.elements = [];
		this.logmessages = [];
		
		this.build();
	},
	
	build: function () {		
		function getElementByClass(element_n, class_n)
		{
			var arrdiv = $$(element_n);
			for(var i = 0; i < arrdiv.length; i++)
			{
				var element = arrdiv[i].get('class');
				if(element == class_n)
				{
					return(arrdiv[i]);
				}
			}
		}
		
		function getElementsByClass(element_n, class_n)
		{
			var arrdiv = $$(element_n);
			var returnValues = new Array();
			
			for(var i = 0; i < arrdiv.length; i++)
			{
				var element = arrdiv[i].get('class');
				if(element.split(" ").contains(class_n))
				{
					returnValues.push(arrdiv[i]);
				}
			}
			return returnValues;
		}
		
		var arrOfElements = getElementsByClass('div','votitaly-inline-rating'); 
		
		if ($type(arrOfElements) != 'array') {
			this.log('Parametri di inizializzazione errati!');
			return false;
		}
		if (!arrOfElements.length) {
			this.log('Nessun elemento di inizializzazione configurato!');
			return false;			
		}

		// analisi degli elementi passati come parametro
		var _class = this;
		var container_fx = box_fx = [];
		arrOfElements.each(function (el) {
			var actual_el_id = getElementByClass('div', 'votitaly-get-id').get('text');
			var actual_id = el.id;
			var togglers = getElementsByClass('a', 'votitaly-toggler');

			container_fx[actual_el_id] = new Fx.Morph(actual_id, {duration:1000, wait:false});
			box_fx[actual_el_id] = new Fx.Morph(getElementsByClass('div', 'votitaly-box'), {property:'opacity',duration:1000, delay:4000});
			
			// foreach star to click
			togglers.each(function(tog) {
				tog.addEvent('click', function (e) {
					var container_id = actual_id;
					var element_id = actual_el_id;
					container_fx[element_id].start({
						'opacity': [100,30]
					});
					// increasing performance:
					getElementByClass('div', 'votitaly-box').set('html','<img src="'+_class.options.loadingimg+'" class="loading" /> '+_class.options.language.updating);
					var request = new Request.JSON({
						url : _class.options.submiturl,
						onSuccess : function(req_response) {
								var cont_el = $(container_id);
								// modifica width li.current-rating
								var el = getElementByClass($$('li'), 'current-rating');
								var width = req_response.width.replace(/"/gi, ' ');
								el.setStyle('width', width);
								
								// opacity 1 su tutto il div
								container_fx[element_id].start({
									'opacity': [30, 100]
								});
								// effetto sul box per ringraziamenti
								var chain = box_fx[element_id].start(0)
								.chain(function() {
									getElementByClass($$('div'), 'votitaly-box').set('html', (req_response.success ? _class.options.language.thanks : _class._getErrorString(req_response.return_code)));
								}).start(100).start(0)
								.chain(function() {
									_class._update_voting_description(
											getElementByClass($$('div'), 'votitaly-box'),
											req_response.num_votes, // #totale voti
											req_response.average,		// media voti
											req_response.out_of			// massimo scala votazione
									)
								}).start(100);
								while(chain.callChain() !== false) {}
						}
					}).post({
						'task' : 'vote',
						'cid' : element_id,
						'rating' : tog.get('text')
					});
				});
			});
		});
		
	},

	_update_voting_description: function (box_el, num_votes, average, out_of) {
		var string = this.options.star_description;
		string = string.replace(/{num_votes}/ig, num_votes)
							.replace(/{num_average}/ig, average)
							.replace(/{num_outof}/ig, out_of)
							.replace(/#LANG_VOTES/ig, num_votes == 1 ? this.options.language.vote : this.options.language.votes )
							.replace(/#LANG_AVERAGE/ig, this.options.language.average )
							.replace(/#LANG_OUTOF/ig, this.options.language.outof );
		box_el.set('html',string);
	},

	_getErrorString: function (errno) {
		switch (errno) {
			case 1: return this.options.language.error1;
			case 2: return this.options.language.error2;
			case 3: return this.options.language.error3;
			case 4: return this.options.language.error4;
			case 5: return this.options.language.error5;
			default: return 'undefined';
		}
	},

	//-- error logs methods ---------------//
	log: function (string) {			
		this.logmessages.include(string);
	},
	showLogs: function () {
		this.logmessages.each(function (message) {
			console.log(message);
		});
	},
	hasLogs: function () {
		return this.logmessages.length > 0;
	},
	emptyLogs: function () {
		this.logmessages = [];
	}
});
VotitalyPlugin.implement(new Options);
