// stars

function AddStarSlider(Element, Name, Style, Value, ChangeFunc){
		
		// html austauschen
		$(Element).setHTML('<div id="'+ Name +'"><div id="'+ Name +'_sa" style="width: 198px;"><div id="'+ Name +'_stars" class="strsshw_rate" style="width:0px;"><div id="'+ Name +'_mover" style="'+ Style +'" align="center"></div></div></div><input type="hidden" id="'+ Name +'_value" name="'+ Name +'_value"></div>');
	
		// slider erstellen
		var sSlider = new Slider($(Name+'_sa'), $(Name+'_mover'), {
			steps: 10,
			offset: 2,
			onChange: function(nStep){
				// Wertefeld
				$(Name+'_value').value=nStep;
				
				// Sterne
				$(Name+'_stars').style.width=(nStep*18) + 4 +'px';
				
				// Anzeigen
				$(Name+'_mover').innerHTML = nStep;
			},
			onComplete: function(nStep){
				// ausrichten
				this.set(this.step);
				try{
					ChangeFunc();
				}
				catch(e){}
			}
		}).set(Value);
		
		return sSlider;
}




/* ORIGINAL MOOTOOLS CODE HERE */

/*
Class: Slider
	Creates a slider with two elements: a knob and a container. Returns the values.
	
Note:
	The Slider requires an XHTML doctype.

Arguments:
	element - the knob container
	knob - the handle
	options - see Options below

Options:
	steps - the number of steps for your slider.
	mode - either 'horizontal' or 'vertical'. defaults to horizontal.
	offset - relative offset for knob position. default to 0.
	
Events:
	onChange - a function to fire when the value changes.
	onComplete - a function to fire when you're done dragging.
	onTick - optionally, you can alter the onTick behavior, for example displaying an effect of the knob moving to the desired position.
		Passes as parameter the new position.
*/

/*
Script: Slider.js
	Contains <Slider>

License:
	MIT-style license.
*/

var Slider = new Class({

	options: {
		onChange: Class.empty,
		onComplete: Class.empty,
		onTick: function(pos){
			this.knob.setStyle(this.p, pos);
		},
		mode: 'horizontal',
		steps: 100,
		offset: 0,
		enabled: true
	},

	initialize: function(el, knob, options){
		this.element = $(el);
		this.knob = $(knob);
		this.setOptions(options);
		this.previousChange = -1;
		this.previousEnd = -1;
		this.step = -1;
		this.element.addEvent('mousedown', this.clickedElement.bindWithEvent(this));
		var mod, offset;
		switch(this.options.mode){
			case 'horizontal':
				this.z = 'x';
				this.p = 'left';
				mod = {'x': 'left', 'y': false};
				offset = 'offsetWidth';
				break;
			case 'vertical':
				this.z = 'y';
				this.p = 'top';
				mod = {'x': false, 'y': 'top'};
				offset = 'offsetHeight';
		}
		this.max = this.element[offset] - this.knob[offset] + (this.options.offset * 2);
		this.half = this.knob[offset]/2;
		this.getPos = this.element['get' + this.p.capitalize()].bind(this.element);
		this.knob.setStyle('position', 'relative').setStyle(this.p, - this.options.offset);
		var lim = {};
		lim[this.z] = [- this.options.offset, this.max - this.options.offset];
		this.drag = new Drag.Base(this.knob, {
			limit: lim,
			modifiers: mod,
			snap: 1,
			onStart: function(){
				this.draggedKnob();
			}.bind(this),
			onDrag: function(){
				this.draggedKnob();
			}.bind(this),
			onComplete: function(){
				this.draggedKnob();
				this.end();
			}.bind(this)
		});
		if (this.options.initialize) this.options.initialize.call(this);
	},

	/*
	Property: set
		The slider will get the step you pass.

	Arguments:
		step - one integer
	*/

	set: function(step){
		this.step = step.limit(0, this.options.steps);
		this.checkStep();
		this.end();
		this.fireEvent('onTick', this.toPosition(this.step));
		return this;
	},

	clickedElement: function(event){
		if(this.enabled){
			var position = event.page[this.z] - this.getPos() - this.half;
			position = position.limit(-this.options.offset, this.max -this.options.offset);
			this.step = this.toStep(position);
			this.checkStep();
			this.end();
			this.fireEvent('onTick', position);
		}
	},

	draggedKnob: function(){
		this.step = this.toStep(this.drag.value.now[this.z]);
		this.checkStep();
	},

	checkStep: function(){
		if (this.previousChange != this.step){
			this.previousChange = this.step;
			this.fireEvent('onChange', this.step);
		}
	},

	end: function(){
		if (this.previousEnd !== this.step){
			this.previousEnd = this.step;
			this.fireEvent('onComplete', this.step + '');
		}
	},

	toStep: function(position){
		return Math.round((position + this.options.offset) / this.max * this.options.steps);
	},

	toPosition: function(step){
		return this.max * step / this.options.steps;
	},
	
	CJDeactivate: function(obj){
		this.element.setOpacity(0.7);
		
		// drag scheiße
		this.drag.detach();
		this.knob.innerHTML = obj;
		
		this.enabled = false
		
		//this.element.removeEvents('click');
		//this.element.removeEvents('mousedown');
	},
	
	CJActivate: function(obj){
		this.enabled=true;
		this.element.setOpacity(1);
		this.drag.attach();
		this.knob.innerHTML = obj;
	}

});

Slider.implement(new Events);
Slider.implement(new Options);