var aStarRatings = stdClass.extend({
	//constructor
	constructor: function(el, settings) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			moduleName: null,
			json: {
				ver: '0.1',
				meta: {},
				data: {
					requests: []
				}
			},
			ulSelector: 'ul.rateable',
			starSelector: 'a',
			ratingTextSelector: '.rating_text',
			currentRatingSelector: '.current-rating'
			/* put extensions to collections here */
		});
		Object.extend(this.s, settings);
		// initialize nodes
		Object.extend(this.n, {
			container: el,
			ul: null,
			stars: [],
			ratingText: null,
			currentRating: null,
			jsonNode: document.createElement('div')
			/* put extensions to nodes here */
		});
		
		// initialize collections
		Object.extend(this.c, {
			requests: []
			/* put extensions to collections here */
		});
		this.parseClasses(this.n.container, true);
		/*if(!this.n.container.vars){
			alert('You need to add the class of "vars:{moduleName:theNameOfModule}" to your container!');
			return;
		}*/
		if(this.n.container.vars && this.n.container.vars.moduleName != ""){
			this.s.moduleName = this.n.container.vars.moduleName;
		}

		this._findElements();
	},
	starClicked: function(el){
		this.addRequest("changeRating", {
			newRating: el.vars.stars,
			uniqueID: this.n.ul.vars.uniqueID,
			type: this.n.ul.vars.type
		});

		this.JsonOut();
	},
	parseClasses: function(el, parseChildren){
		var classes = String(el.className).split(" ");
		var keepClasses = [];
		for(var x=0; x<classes.length; x++){
			if(classes[x].indexOf(':') != -1){
				var matches = classes[x].match(/^(\w*):{1,2}\{(.*)\}$/);
				var ob = {};
				var vars = matches[2].split(",");
				for(var y=0; y<vars.length; y++){
					vars[y] = vars[y].split(/::?/);
					if(!vars[y][1]) vars[y][1] = '';
					ob[vars[y][0]] = vars[y][1];
				}
				el[matches[1]] = ob;
			}else{
				keepClasses.push(classes[x]);
			}
		}
		el.className = keepClasses.join(" ");
		if(parseChildren){
			var eles = el.getElementsByTagName('*');
			for(var x=0; x<eles.length; x++){
				this.parseClasses(eles[x]);
			}
		}
	},
	addRequest: function(requestName, dataObject) {
		this.c.requests.push({
			id: this.c.requests.length,
			type: requestName,
			data: dataObject
		});
	},
	//json methods
	JsonOut: function() {
		//construct the post
		this.s.json.data.requests = this.c.requests;
		//this.setMeta('columns', '');
		var data = '__json=' + this.s.moduleName + '&data=' + toJSONString(this.s.json);
		this.c.requests = [];
		//send out the request
		if(this.s.moduleName == ''){
			var returnData = '{"meta":{},"data":{"html":"<div class=\'rating vars:{moduleName:AdminModule__rating}\'><ul class=\'rateable floatleft vars:{uniqueID=51820}\'><li class=\'current-rating\' style=\'width: ' + (this.s.json.data.requests[0].data.newRating*20) + '%;\'> </li><li><a href=\'javascript:void(0)\' title=\'1 star\' class=\'one-star vars:{stars:1}\'>1</a></li><li><a href=\'javascript:void(0)\' title=\'2 stars\' class=\'two-stars vars:{stars:2}\'>2</a></li><li><a href=\'javascript:void(0)\' title=\'3 stars\' class=\'three-stars vars:{stars:3}\'>3</a></li><li><a href=\'javascript:void(0)\' title=\'4 stars\' class=\'four-stars vars:{stars:4}\'>4</a></li><li><a href=\'javascript:void(0)\' title=\'5 stars\' class=\'five-stars vars:{stars:5}\'>5</a></li></ul><span class=\'rating_text\'>' + this.s.json.data.requests[0].data.newRating + '/5</span></div>"},"responses":[{"id":1,"type":"changeRating","data":{"success":"true"}}]}';
			this.JsonIn({responseText:returnData});
		}else{
			var myAjax = new Ajax.Request(window.location,
			{
				method: 'post',
				parameters: data,
				onSuccess: this.JsonIn.bind(this)
			});
		}
	},
	
	JsonIn: function(t) {
		//alert(t.responseText);
		var result = t.responseText.parseJSON();
		(result.responses.length).times(function(i) {
			if(result.responses[i].type == 'changeRating'){
				this.n.jsonNode.innerHTML = result.data.html;
				this.n.ratingText.innerHTML = document.getElementsBySelector(this.s.ratingTextSelector, this.n.jsonNode)[0].innerHTML;
				this.n.currentRating.style.width = document.getElementsBySelector(this.s.currentRatingSelector, this.n.jsonNode)[0].style.width;
				this._startScripts(this.n.container);
				var stars = document.getElementsBySelector(this.s.starSelector, this.n.ul);
				for(var x=0; x<stars.length; x++){
					stars[x].blur();
				}
			}
		}.bind(this));
	},
	_startScripts: function(el) {
		//empty for now...
	},
	_findElements: function(){
		this.n.ul = document.getElementsBySelector(this.s.ulSelector, this.n.container)[0];
		if(!this.n.ul){
			return;
		}
		this.n.currentRating = document.getElementsBySelector(this.s.currentRatingSelector, this.n.container)[0];
		var stars = document.getElementsBySelector(this.s.starSelector, this.n.ul);
		this.n.stars = [];
		for(var x=0; x<stars.length; x++){
			this.eObserve(stars[x], 'click', this.starClicked.bind(this, stars[x]));
			this.n.stars.push(stars[x]);
		}
		this.n.ratingText = document.getElementsBySelector(this.s.ratingTextSelector, this.n.container)[0];
	}
});
EventSelectors.register({
	'.rating': function(el, index) {
		new aStarRatings(el, {
			moduleName: 'AddRating'
		});
	}
}, true);