/* AL Ratings (C) 2010 Adrian Levano. All rights reserved.

Version 1.0.1 (Mootools 1.11)
AL Ratings is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
var ALRatings = new Class( {
	options: {
			url:'',
			container:'.alratings_container',
	},
	container:	null,
	initialize : function(options) {
		this.setOptions(options);
		this.container = $$(this.options.container);
		this.url = this.options.url;
		
		this.container.each(function(cont){
			var url 	= this.url;
			var ogroup	= cont.getElement('.alratings_ogroup');
			var okeyid	= cont.getElement('.alratings_okeyid');
			var otitle	= cont.getElement('.alratings_otitle');
			var olink	= cont.getElement('.alratings_olink');
			var text	= cont.getElement('.alratings_text');
			var current	= cont.getElement('.alratings_current');
			var stars   = cont.getElements('.alratings_stars a');
			
			stars.addEvent('click', function(e){
				e = new Event(e).stop();
				var rate = this.getText();
				text.empty().addClass('alratings_loading');
				
				new Ajax(url, {
					method:"GET",
					data:{
						task:'vote',
						format:'raw',
						rating: rate,
						ogroup: ogroup.getValue(),
						okeyid: okeyid.getValue(),
						otitle: otitle.getValue(),
						olink: olink.getValue()
					},
					onFailure: function(response){
						text.removeClass('alratings_loading');
						text.setHTML('error....'+response.statusText);	
					},
					onComplete: function(response){
						var json = Json.evaluate(response);
						text.removeClass('alratings_loading');
						text.setHTML(json.message);	
						current.setStyle('width', json.width);
						setTimeout(function(){
							text.setHTML(json.vote_text);
						}, 1500);
					}
				}).request();
				
			});
		}.bind(this));
	},
	refreshRating : function(pgroup,pkeyid) {
		this.container.each(function(cont){
			var url 	= this.url;
			var ogroup	= cont.getElement('.alratings_ogroup');
			var okeyid	= cont.getElement('.alratings_okeyid');
			var otitle	= cont.getElement('.alratings_otitle');
			var olink	= cont.getElement('.alratings_olink');
			var text	= cont.getElement('.alratings_text');
			var current	= cont.getElement('.alratings_current');

			if((ogroup.getValue()==pgroup)&&(okeyid.getValue()==pkeyid)){
				
				new Ajax(url, {
					method:"GET",
					data:{
						task:'refresh',
						format:'raw',
						ogroup: ogroup.getValue(),
						okeyid: okeyid.getValue(),
						otitle: otitle.getValue(),
						olink: olink.getValue()
					},
					onFailure: function(response){
						text.setHTML('error....'+response.statusText);	
					},
					onComplete: function(response){
						var json = Json.evaluate(response);
						current.setStyle('width', json.width);
						text.setHTML(json.vote_text);
					}
				}).request();
			}
			
		}.bind(this));

	}
});

ALRatings.implement(new Options);

