

 var ForioSlider = function() 
 { 
	var Event = YAHOO.util.Event, //initialize the YUI event class
		Dom   = YAHOO.util.Dom,   //initialize the YUI dom class (for easy access to the interface DOM tree)
		lang  = YAHOO.lang; 
	
	var slider;
		
	var sliderBgId;
	var sliderThumbId;
	var sliderConvertedValueId;

	// The slider can move 0 pixels up (it's a good idea to keep this at zero) 
	var topConstraint = 0; 

	// The slider can move 35 pixels down 
	var bottomConstraint = 35; 
	
	// set the amount of pixels for the slider to move left and righ
	var pixelWidth = bottomConstraint - topConstraint;
	
	// The amount the slider moves when the value is changed with the arrow 
	// keys 
	var keyIncrement = 1; 

	// if you're showing ticks on the slider (i.e. if you're using the graphics provided by the YUI)
	// this will be the space in between the tick marks on the slider
	var tickSize = 5; 

	var minValue;
	var maxValue;
	var numberFormat;
	var initValue;
	var nameValue;
	
	// create a var that we will set the value of to "this", enabling
	// the use of this.function()
	var that;

	return {
	
		// object with params: sliderBgId, sliderThumbId, sliderConvertedValueId, minValue, maxValue, numberFormat, initValue
		init: function(config)
		{
			try
			{
				that = this;
   				
				sliderBgId = config.sliderBg;
				sliderThumbId = config.sliderThumb;
				sliderConvertedValueId = config.sliderValue;
				nameValue = config.nameValue;
				minValue = config.minValue;
				maxValue = config.maxValue;
   				numberFormat = config.numberFormat; 
				initValue = config.initValue; 
				 
				// set the name of the text field where the value sits
				Dom.get(sliderConvertedValueId).name = "D_" + nameValue;
				 
				// initialize the horizontal slider  
				slider = YAHOO.widget.Slider.getHorizSlider(sliderBgId,  
								 sliderThumbId, topConstraint, bottomConstraint, 1); 
                
				// get the actual value from handleGetRealValue method
				slider.getRealValue = that.handleGetRealValue;
   
				// slider change? do this:
				slider.subscribe("change", that.handleSliderChange);
				
				// text field change? do this:
				Event.on(sliderConvertedValueId, "keydown", function(e)
				{
					that.handleTextFieldChange(e); 
				});
				
				// put this value in the text field
   				slider.setValue(this.convertValueToPixels(initValue), false); //false here means to animate if possible
				document.getElementById(sliderConvertedValueId).value = FD_formatNumber(initValue, numberFormat);
			
			} catch (e)
			{
				alert(e);
			}

		},

        // take the value and convert it into pixels
		convertValueToPixels: function(value)
		{
				
				var pixValue = Math.round((value - minValue)*pixelWidth/(maxValue - minValue));
				return pixValue;
			
		},
		
		// call this whenever the slider is changed
		handleSliderChange: function(offsetFromStart) 
		{ 
				var fld = Dom.get(sliderConvertedValueId); 
				
				// use the scale factor to convert the pixel offset into a real 
				// value 
				var actualValue = slider.getRealValue(); 
	
				// update the text box with the actual value 
				fld.value = actualValue; 
				
				// Update the title attribute on the background.  This helps assistive 
				// technology to communicate the state change 
				Dom.get(sliderBgId).title = "slider value = " + actualValue; 
		},

		// use this when you need to get the value (not in pixels) of the slider
		handleGetRealValue: function()
		{
			if(nameValue == "Goal for CO2 in the atmosphere"){
				var scaleFactor = 10;
			} else {
				var scaleFactor = (maxValue - minValue) / (pixelWidth + 0.0);
			}
			return FD_formatNumber(minValue + slider.getValue() * scaleFactor, numberFormat);  
		},

        // use this when someone types in the text field and hits enter
		handleTextFieldChange: function(e, pass)
		{
			// set the value when the 'return' key is detected 
			if (Event.getCharCode(e) === 13) 
			{ 
				var v = parseFloat(Dom.get(sliderConvertedValueId).value, 10); 
				v = (lang.isNumber(v)) ? v : 0; 

				var scaleFactor = (maxValue - minValue) / (pixelWidth + 0.0);
				// convert the real value into a pixel offset 
				slider.setValue(Math.round(v/scaleFactor)); 
			} 
		}
	}
}; 
