/* 
	This code utilizes the PlotKit methods and provides some helper methods 
	for creating slider controls 
	
	3 methods are used to accomplish slider behavior on the page
	addToSliderArray - used to add a new slider object to the global slider array
	recalculateSliderArray - runs the recalculate method on all slider objects in the global slider array
	initializeSlider - creates a slider object, ties slider to input field and label, adds slider to global slider array

	NOTE:	The sliders on the page need to be recalculated after being hidden
			A global reference is required to run the recalculate method
	
*/

var _sliderArray = []; // global container for any sliders on the page

function addToSliderArray(newSlider){
	_sliderArray[_sliderArray.length] = newSlider;
}

// For the sliders to show the correct value after the parent div is 
// hidden and then displayed they need to run the recalculate method
function recalculateSliderArray(){
	for(var i = 0; i < _sliderArray.length; i++){
		_sliderArray[i].recalculate();
	}
}

// Create a slider object
// Ties the slider object to sliderDataInputId and dataLabelId so that a change to the slider
// or a keyed in change to the sliderDataInputId field will update the other
// additionally the slider is tied to a hidden field whose onchange event can be used to update data with 
// or call another method
function initializeSlider(sliderDivId, sliderInputId, sliderDataInputId, dataHiddenElementId, dataLabelId){
	var ie = (navigator.appName =="Microsoft Internet Explorer");
	var s = new Slider(document.getElementById(sliderDivId), document.getElementById(sliderInputId));
	var dataInput = document.getElementById(sliderDataInputId);
	s.onchange= function(){
		dataInput.value = this.getValue();
		setPageElementToValue(dataLabelId,dataInput.value);
		setPageElementToValue(dataHiddenElementId,dataInput.value);
		if(!ie){
			document.getElementById(dataHiddenElementId).onchange();
		}
	}
	dataInput.onkeyup = function(){
		s.setValue(this.value);
		setPageElementToValue(dataLabelId,this.value);
		setPageElementToValue(dataHiddenElementId,this.value);
		document.getElementById(dataHiddenElementId).onchange();
	}
	
	
	if(ie){
		document.getElementById(sliderDivId).onmouseup = function (){document.getElementById(dataHiddenElementId).onchange();};
	}
	
	addToSliderArray(s);
}

// convienence method.  can be used as long as elements follow naming standards
function initializeSliderByStandardNames(sliderTd){
	initializeSlider(sliderTd + 'Div', sliderTd + 'BackingInput', sliderTd + 'Input', sliderTd + 'InputHidden', sliderTd + 'Label');
}
