// Slider fuer den Zeitraum mit zwei Reglern.
// Verwendet Funktionen aus dem slider.js Script und muss daher mit diesem zusammen eingebunden werden.
// Date: 24.04.2007
// Author: Markus Hanses
/**
 * Realisiert einen Regler fuer die Uhrzeiten.
 * 
 * @author Markus Hanses
 * @param {Object} a_init
 * @param {Object} a_tpl
 */
function sliderTime (a_init, a_tpl) {	
	this.active = 'true';
	this.isSlider4Time = 'true';
	this.selectedId = null;
  	this.sliderListener='';
  	this.oldStartValue='';
  	this.newStartValue='';
	this.oldEndValue='';
  	this.newEndValue='';  
	this.f_setValue = f_sliderSetValue4Time;
	this.f_getPos = f_sliderGetPos;
	this.setSliderListener = setSliderListener;
	this.notifySliderListener = notifySliderListener;
	this.getInput = getInput4Time;	
	this.checkDistance4Time = checkDistance4Time;
	this.setSliderInactive = setSliderTimeInactive;
	this.setSliderActive = setSliderTimeActive;
	this.resetSlider = resetSliderTime;
	this.unsetSlider = resetSliderTime;
	// 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_startValue == null)
		this.n_startValue = this.n_minValue;
		
	if (this.n_endValue == null)
		this.n_endValue = 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:absolute;left:' + 
    this.n_pathLeft + 'px;top:' + this.n_pathTop + 
    'px;z-index:' + this.n_zIndex + 
    ';cursor:hand;visibility:hidden;" name="sl' + 
    this.n_id + 'sliderStart" id="sl' + this.n_id + 
    'sliderStart" onmousedown="return f_sliderMouseDown(' + this.n_id + ')"/>' 
	+
	'<img src="' + this.s_imgSlider + '" width="' + 
    this.n_sliderWidth + '" height="' + this.n_sliderHeight + 
    '" border="0" style="position:absolute;left:' + 
    this.n_pathLeft + 'px;top:' + this.n_pathTop + 
    'px;z-index:' + this.n_zIndex + 
    ';cursor:hand;visibility:hidden;" name="sl' + 
    this.n_id + 'sliderEnd" id="sl' + this.n_id + 
    'sliderEnd" onmousedown="return f_sliderMouseDown(' + this.n_id + ')"/>'
	+	
	'</div>'
	);
	this.e_base   = get_element('sl' + this.n_id + 'base');	
	this.e_sliderStart = get_element('sl' + this.n_id + 'sliderStart');
	this.e_sliderEnd = get_element('sl' + this.n_id + 'sliderEnd');
	
	if (document.onmousemove != f_sliderMouseMove) {
		window.f_savedMouseMove = document.onmousemove;
		document.onmousemove = f_sliderMouseMove;
	}
	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_startInput = this.s_form == null
		? get_element(this.s_name + 'Start')
		: document.forms[this.s_form]
			? document.forms[this.s_form].elements[this.s_name + 'Start']
			: null;				
	var e_endInput = this.s_form == null
		? get_element(this.s_name + 'End')
		: document.forms[this.s_form]
			? document.forms[this.s_form].elements[this.s_name + 'End']
			: null;						
	this.f_setValue(
		e_startInput && e_startInput.value != '' ? e_startInput.value : null, 
		e_endInput && e_endInput.value != '' ? e_endInput.value : null, 
		1);
	this.e_sliderStart.style.visibility = 'visible';
	this.e_sliderEnd.style.visibility = 'visible';
}

function getInput4Time(e_input_name)
{
	alert("getInput4Time wurde aufgerufen");
}
/**
 * Setzt die Werte der WebUI, welche mit dem Regler eingestellt wurden.
 * 
 * @author Markus Hanses
 * @param {Object} n_startValue
 * @param {Object} n_endValue
 * @param {Object} b_noInputCheck
 */
function f_sliderSetValue4Time (n_startValue, n_endValue, b_noInputCheck) {
	if (n_startValue == null)
		n_startValue = this.n_startValue == null ? this.n_minValue : this.n_startValue;
	if (isNaN(n_startValue))
		return false;
	if (n_endValue == null)
		n_endValue = this.n_endValue == null ? this.n_maxValue : this.n_endValue;
	if (isNaN(n_endValue))
		return false;
	// round to closest multiple if step is specified
	if (this.n_step) 
	{
		n_startValue = Math.round((n_startValue - this.n_minValue) / this.n_step) * this.n_step + this.n_minValue;
		n_endValue = Math.round((n_endValue - this.n_maxValue) / this.n_step) * this.n_step + this.n_maxValue;
	}
	// smooth out the result
	if (n_startValue % 1)
		n_startValue = Math.round(n_startValue * 1e5) / 1e5;
	if (n_endValue % 1)
		n_endValue = Math.round(n_endValue * 1e5) / 1e5;

	if (n_startValue < this.n_minValue)
		n_startValue = this.n_minValue;
	if (n_startValue > this.n_maxValue)
		n_startValue = this.n_maxValue - 1;
	
		
	if (n_endValue < this.n_minValue)
		n_endValue = this.n_minValue + 1;
	if (n_endValue > this.n_maxValue)
		n_endValue = this.n_maxValue;

	this.n_startValue = n_startValue;
	this.n_endValue = n_endValue;

	// move the slider
	if (this.b_vertical) 
	{
		this.e_sliderStart.style.top  = (this.n_pathTop + this.n_pathLength - Math.round((n_startValue - this.n_minValue) * this.n_pix2value)) + 'px';
		this.e_sliderEnd.style.top  = (this.n_pathTop + this.n_pathLength - Math.round((n_endValue - this.n_minValue) * this.n_pix2value)) + 'px';
	} else {
		this.e_sliderStart.style.left = (this.n_pathLeft + Math.round((n_startValue - this.n_minValue) * this.n_pix2value)) + 'px';
		this.e_sliderEnd.style.left = (this.n_pathLeft + Math.round((n_endValue - this.n_minValue) * this.n_pix2value)) + 'px';
	}
	// save new value
	var e_startInput;
	var e_endInput;
	if (this.s_form == null) {
		e_startInput = get_element(this.s_name + "Start");
		e_endInput = get_element(this.s_name + "End");
		if (!e_startInput)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the input with ID='" + this.s_name + 'Start' + "'.");
		if (!e_endInput)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the input with ID='" + this.s_name + 'End' + "'.");	
	}
	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_startInput = e_form.elements[this.s_name + 'Start'];
		e_endInput = e_form.elements[this.s_name + 'End'];
		if (!e_startInput)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the input with NAME='" + this.s_name + 'Start' + "'.");
		if (!e_endInput)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the input with NAME='" + this.s_name + 'End' + "'.");	
	}
	
  	this.oldStartValue = e_startInput.value;
	this.newStartValue = n_startValue;
	this.oldEndValue = e_endInput.value;
	this.newEndValue = n_endValue;
  	var theSliderEvent = new SliderEvent(this.oldStartValue, this.newStartValue, this);
	var theSliderEvent = new SliderEvent(this.oldEndValue, this.newEndValue, this);
  	e_startInput.value = this.newStartValue >= 24 ? 24 : this.newStartValue;
	e_endInput.value = this.newEndValue >= 24 ? 24 : this.newEndValue;	  	
}
/**
 * Erzeugt ein Event Objekt, welches an den Listener weiter gereicht wird.
 * 
 * @author Markus Hanses
 * @param {Object} oldStartValue
 * @param {Object} newStartValue
 * @param {Object} oldEndValue
 * @param {Object} newEndValue
 * @param {Object} aslider
 */
function SliderEvent4Time (oldStartValue, newStartValue, oldEndValue, newEndValue, aslider)
{
  	this.oldStartValue = this.createRealTime(oldStartValue += '');
	this.newStartValue = this.createRealTime(newStartValue += ''); 
	this.oldEndValue = this.createRealTime(oldEndValue +=''); 
	this.newEndValue = this.createRealTime(newEndValue +=''); 
	this.aslider = aslider;
}
/**
 * Erzeugt aus der Stundenanzeige eine Uhrzeit.
 * @author Markus Hanses 
 * @param {Object} hour
 */
SliderEvent4Time.prototype.createRealTime=function(hour)
{
	var realTime = hour.length > 1 ? hour + ':00' : '0'+ hour +':00';
	if (realTime == '24:00') 
	{
		realTime = '23:59';
	} 
	return realTime;
}
/**
 * Testet, ob der Abstand zwischen den beiden Reglern ausreichend Gross ist.
 * @author Markus Hanses
 * @param {Object} n_startValue
 * @param {Object} n_endValue
 */
function checkDistance4Time (n_startValue, n_endValue) 
{
	return (n_endValue - n_startValue >= 3) ? true : false; 		
}
/**
 * Setzt den Zeit-Regler auf inaktiv.
 * @author Markus Hanses
 */
function setSliderTimeInactive()
{
		this.e_base.src = this.s_imgControl_inactive;
		this.e_sliderStart.src = this.s_imgSlider_inactive;
		this.e_sliderEnd.src = this.s_imgSlider_inactive;;		
		var e_startInput = this.s_form == null
			? get_element(this.s_name + 'Start')
			: document.forms[this.s_form]
			? document.forms[this.s_form].elements[this.s_name + 'Start']
			: null;				
		var e_endInput = this.s_form == null
			? get_element(this.s_name + 'End')
			: document.forms[this.s_form]
			? document.forms[this.s_form].elements[this.s_name + 'End']
			: null;	
		e_startInput.style.color='#aaaaaa';
		e_endInput.style.color='#aaaaaa';		
		this.active = 'false';
}
/**
 * Setzt den Zeit-Regler auf aktiv.
 * @author Markus Hanses
 */
function setSliderTimeActive()
{
		this.e_base.src = this.s_imgControl;
		this.e_sliderStart.src = this.s_imgSlider;
		this.e_sliderEnd.src = this.s_imgSlider;
		var e_startInput = this.s_form == null
			? get_element(this.s_name + 'Start')
			: document.forms[this.s_form]
			? document.forms[this.s_form].elements[this.s_name + 'Start']
			: null;				
		var e_endInput = this.s_form == null
			? get_element(this.s_name + 'End')
			: document.forms[this.s_form]
			? document.forms[this.s_form].elements[this.s_name + 'End']
			: null;	
		e_startInput.style.color='#000000';
		e_endInput.style.color='#000000';
		this.active = 'true';
}
/**
 * Setzt den Regler auf die Uhrsprungswerte zurueck.
 * @author Markus Hanses
 */
function resetSliderTime()
{
	this.f_setValue(this.n_minValue, this.n_maxValue, 1);
}