// Slider fuer den Preis.
// Verwendet Funktionen aus dem slider.js Script und muss daher mit Diesem zusammen eingebunden werden.
// Date: 24.04.2007
// Author: Markus Hanses
 
function sliderPrice (a_init, a_tpl) {
	this.active = 'true';
	this.sliderListener='';
	this.oldValue='';
	this.newValue='';
	this.f_setValue  = f_sliderSetValue4Price;
	this.f_getPos    = f_sliderGetPos;
	this.setSliderListener = setSliderListener;
	this.notifySliderListener = notifySliderListener;
	this.getInput = getInput;
	// fuer die Aktualisierung
	this.setFlightContent = setFlightContent;
	this.f_sliderSetValue = f_sliderSetValue;
	//
	this.setSliderInactive = setSliderInactive;
	this.setSliderActive = setSliderActive;
	this.resetSlider = resetSlider;
	this.unsetSlider = unsetSlider;
	// register in the global collection	
	if (!window.A_SLIDERS)
		window.A_SLIDERS = [];
	this.n_id = window.A_SLIDERS.length;
	window.A_SLIDERS[this.n_id] = this;

	// save config parameters in the slider object
	var s_key;
	if (a_tpl)
		for (s_key in a_tpl)
			this[s_key] = a_tpl[s_key];
	for (s_key in a_init)
		this[s_key] = a_init[s_key];

	this.n_pix2value = this.n_pathLength / (this.n_maxValue - this.n_minValue);
	if (this.n_value == null)
		this.n_value = this.n_minValue;
	
	// Absolute Werte fuer den Reset
	this.firstMaxValue = this.n_maxValue;
	this.firstMinValue = this.n_minValue;
	
	// generate the control's HTML
	document.write(
		'<div style="width:' + this.n_controlWidth + 
    'px;height:' + this.n_controlHeight + 'px;border:0; background-image:url(' + 
    this.s_imgControl + ')" id="sl' + this.n_id + 'base">' +
		
    '<img src="' + this.s_imgSlider + '" width="' + 
    this.n_sliderWidth + '" height="' + this.n_sliderHeight + 
    '" border="0" style="position:relative;left:' + 
    this.n_pathLeft + 'px;top:' + this.n_pathTop + 
    'px;z-index:' + this.n_zIndex + 
    ';cursor:hand;visibility:hidden;" name="sl' + 
    this.n_id + 'slider" id="sl' + this.n_id + 
    'slider" onmousedown="return f_sliderMouseDown(' + this.n_id + ')"/></div>'
	);
	this.e_base   = get_element('sl' + this.n_id + 'base');
	this.e_slider = get_element('sl' + this.n_id + 'slider');
	
	// safely hook document/window events
	if (document.onmousemove != f_sliderMouseMove) {
		window.f_savedMouseMove = document.onmousemove;
		document.onmousemove = f_sliderMouseMove;
		//alert("Slider onmousemove="+document.onmousemove);
	}
	if (document.onmouseup != f_sliderMouseUp) {
		window.f_savedMouseUp = document.onmouseup;
		document.onmouseup = f_sliderMouseUp;
	}
	// preset to the value in the input box if available
	var e_input = this.s_form == null
		? get_element(this.s_name)
		: document.forms[this.s_form]
			? document.forms[this.s_form].elements[this.s_name]
			: null;
	this.f_setValue(e_input && e_input.value != '' ? e_input.value : null, 1);
	this.e_slider.style.visibility = 'visible';
}

function f_sliderSetValue4Price (n_value, b_noInputCheck) {
	if (n_value == null)
		n_value = this.n_value == null ? this.n_minValue : this.n_value;
	if (isNaN(n_value))
		return false;
	// round to closest multiple if step is specified
	if (this.n_step)
		n_value = Math.round((n_value - this.n_minValue) / this.n_step) * this.n_step + this.n_minValue;
	// smooth out the result
	if (n_value % 1)
		n_value = Math.round(n_value * 1e5) / 1e5;

	if (n_value < this.n_minValue)
		n_value = this.n_minValue;
	if (n_value > this.n_maxValue)
		n_value = this.n_maxValue;
		
	// Realisiert Spruenge in 5er Schritten	
	var div_value = 5;
	if (n_value != this.n_minValue && n_value != this.n_maxValue && (n_value % div_value) != 0)
		n_value = (n_value % div_value) > 2 ? n_value + (div_value - (n_value % div_value)) : n_value - (n_value % div_value);
	
	this.n_value = n_value;

	// move the slider
	if (this.b_vertical)
		this.e_slider.style.top  = (this.n_pathTop + this.n_pathLength - Math.round((n_value - this.n_minValue) * this.n_pix2value)) + 'px';
	else
		this.e_slider.style.left = (this.n_pathLeft + Math.round((n_value - this.n_minValue) * this.n_pix2value)) + 'px';

	// save new value
	var e_input;
	if (this.s_form == null) {
		e_input = get_element(this.s_name);
		if (!e_input)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the input with ID='" + this.s_name + "'.");
	}
	else {
		var e_form = document.forms[this.s_form];
		if (!e_form)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the form with NAME='" + this.s_form + "'.");
		e_input = e_form.elements[this.s_name];
		if (!e_input)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the input with NAME='" + this.s_name + "'.");
	}
	
  this.oldValue = e_input.value;
	this.newValue = n_value;
  var theSliderEvent = new SliderEvent(this.oldValue, this.newValue, this);
  e_input.value = this.newValue;
}

// aktualisiert die Werte des Sliders
function setFlightContent (aContentModel)
{
	if (aContentModel.getHighestOfferPrice() > 0) {
		// setzen der neuen Werte
		
		this.n_maxValue = aContentModel.getHighestPrice();
		this.n_minValue = aContentModel.getLowestOfferPrice();
		var divisor = (this.n_maxValue - this.n_minValue);
		if (divisor == 0) 
		{
			this.setSliderInactive();
			this.n_minValue = 0;	
		} else {
			if (this.active == false)
				 this.setSliderActive();			
		}				
		this.n_pix2value = this.n_pathLength / (this.n_maxValue - this.n_minValue);
			// aktualisieren der Anzeige			
		if (this.n_maxValue < this.n_newValue) 
		{
			this.n_newValue = this.n_maxValue;			
		}	
		this.f_sliderSetValue (this.n_newValue, null);
	}
}