var RatingStars = Class.create();
RatingStars.prototype = {
	initialize: function(element_id, object_id, object_type, rating, rating_count) {    		
		this.element_id = element_id;
		this.btn_class = "rating_but";
		this.count_class = "rating_count";
		
		$(element_id).style.display = "block";
		
		this.height = $(element_id).offsetHeight;
		
		this.object_id = object_id;
		this.object_type = object_type;
		
		this.current_points = rating;
		this.current_count = rating_count;
		
		//Buttons -----
		this.init_buttons();
		
		//Set - Init ---------
		this.showStarValue(this.current_points);
		this.showCount(this.current_count);

		
  	},
  
  	init_buttons: function() {
  		var items = $$("#"+this.element_id+" ."+this.btn_class);
		for(z=0;z<items.length;z++) {
			Event.observe(items[z], 'click', this.but_click.bindAsEventListener(this));
			Event.observe(items[z], 'mouseover', this.but_over.bindAsEventListener(this));
			Event.observe(items[z], 'mouseout', this.but_out.bindAsEventListener(this));
		}
  	},
  
  	but_click: function(e) {
		Event.stop(e);
		var element = Event.findElement(e, 'a');	
		var value = this.getIdFromItem(element);	
		
		this.click_x = Event.pointerX(e);
		this.click_y = Event.pointerY(e);
		
		this.addValue_request(value);
  	},
	
	but_over: function(e) {
		Event.stop(e);
		var element = Event.findElement(e, 'a');	
		
		var value = this.getIdFromItem(element);
		this.showStarValue(value);
  	},
	
	but_out: function(e) {
		Event.stop(e);
		var element = Event.findElement(e, 'a');	

		this.showStarValue(this.current_points);
  	},
	
	
	
	hasClassNameCheck: function (el, c_check) {
		var c = el.className.split(" ");
		var found = false;
		for (i=0;i<c.length;i++) {
			if (c[i] == c_check) {
				found = true;
			}
		}
		return found;
	},
	
	getIdFromItem: function (li) {
		var base = li.className.split(" ");
		var id = "";
		base.each(function(part) {
					var start = part.indexOf("value_");
					if (start != -1) {
						id = part.substring(start+6, part.length);
					}
		});
		return id;
	},
	
	showStarValue: function (value) {
		if (typeof (this) == "object" ) var myself = this;
		else var myself = this.myself;		
		
		var pos = myself.height * Math.round(value);
		$(myself.element_id).style.backgroundPosition = "0px "+(-1)*pos+"px";

	},
	
	showCount: function (value) {
		if (typeof (this) == "object" ) var myself = this;
		else var myself = this.myself;
		
		var el = $$("#"+myself.element_id+" ."+myself.count_class);
		el[0].innerHTML = "("+value+")";
	},
	
	
  
  	addValue_request: function (value) {
		var url = 'index.php?saveRating=1&value='+value+'&id='+this.object_id+'&object_type='+this.object_type;
		var req = new AjaxAction(url, this.addValue_request_callback);
		req.saveObject(this);
		req.sendRequest();
	},
	
	addValue_request_callback: function(response, myself) { //myself hat referenz auf this
		var vars = BasicLib.parseGetString(response); //output

		if (vars.status == "ok") {
			BasicLib.show_overlay_msg("Saved", (myself.click_x-20), (myself.click_y-20));
					
			myself.current_points = vars.rating;
			myself.current_count = vars.count;
			
			myself.showStarValue(myself.current_points);
			myself.showCount(myself.current_count);
		} else {
			if (vars.msg == "doppelvote") BasicLib.show_overlay_msg("already rated", (myself.click_x-20), (myself.click_y-20));
			else BasicLib.show_overlay_msg("Something wrong", (myself.click_x-20), (myself.click_y-20));
		}
	}
		
}; // end class
