/* Sternerating, written by SK */

var counter = 0; 

var Sternerating = Class.create();
Sternerating.prototype = {
	initialize: function(groupname) {
		
		this.options = Object.extend({
      		activebild: "../bilder/d5/stern1.gif",
      		inactivebild: "../bilder/d5/stern_grau2.gif",
                already_set: -1,
			value_suffix: "_value",
			ausgabe_suffix: "_ausgabe",
			img_suffix: "_img",
			werte: new Array("sehr schlecht","schlecht","befriedigend","gut","sehr gut"),
      		anzahl: 5
    		}, arguments[1] || {}
		);
		this.elements = new Array();
		this.activelement = -1;
		this.elementsevents = new Array();
		this.groupname = groupname;
		for (i = 0; i < this.options.anzahl;i++) {
			this.elements[i] = $(groupname+this.options.img_suffix+i);
			this.registerEvents(i);
		}
                if (this.options.already_set >= 0) {
                      this.setbewertung(this,this.options.already_set);
                }
    },
    
    registerEvents: function(cnt) {
		this.elementsevents["mouseover"+cnt] = this.makeactive.bindAsEventListener(this,cnt);
		this.elementsevents["mouseout"+cnt] = this.makeinactive.bindAsEventListener(this,cnt);
		this.elementsevents["click"+cnt] = this.setbewertung.bindAsEventListener(this,cnt);
    	Event.observe(this.elements[cnt], "mouseover",this.elementsevents["mouseover"+cnt]);
    	Event.observe(this.elements[cnt], "mouseout", this.elementsevents["mouseout"+cnt]);
    	Event.observe(this.elements[cnt], "click", this.elementsevents["click"+cnt]);
  	},
  	
  	unregisterEvents: function(cnt) {
		Event.stopObserving(this.elements[cnt], "mouseover", this.elementsevents["mouseover"+cnt]);
		Event.stopObserving(this.elements[cnt], "mouseout", this.elementsevents["mouseout"+cnt]); 
		Event.stopObserving(this.elements[cnt], "click", this.elementsevents["click"+cnt]);	   
	},
	    
	makeactive: function(e) {
	    cnt = $A(arguments)[1];
		this.elements[cnt].src = this.options.activebild;
		$(this.groupname+this.options.ausgabe_suffix).innerHTML = this.options.werte[cnt]; 
	},
	
	makeinactive: function(e) {
	    cnt = $A(arguments)[1];
		this.elements[cnt].src = this.options.inactivebild;
		$(this.groupname+this.options.ausgabe_suffix).innerHTML = "";
		if (this.activelement >= 0) {
			$(this.groupname+this.options.ausgabe_suffix).innerHTML = this.options.werte[this.activelement]; 
		}
	},

	setbewertung: function(e) {
		cnt = $A(arguments)[1];	
		this.unregisterEvents(cnt);
		if (this.activelement >= 0){
			this.registerEvents(this.activelement);	
			this.elements[this.activelement].src = this.options.inactivebild;
		}
                else {
                        counter++;
                }
		this.activelement = cnt;
		this.elements[cnt].src = this.options.activebild;
		$(this.groupname+this.options.ausgabe_suffix).innerHTML = this.options.werte[cnt];
		$(this.groupname+this.options.value_suffix).value = (cnt+1);
		this.check_submit_button();
	},
	
	check_submit_button: function(){
		if (counter >= $("bewertcount").value && this.options.already_set == -1) {
       		$("pr_bewertung_submit").disabled = "";
    	}
	}     
}
