(function() {
    var Event = YAHOO.util.Event,
        Dom   = YAHOO.util.Dom,
        lang  = YAHOO.lang,
        slider, 
        bg="slider-bg-writings", thumb="slider-thumb-writings", 
        valuearea="slider-value", textfield="slider-converted-value",
        treenav="content_wrap"
		
    // The slider can move XXX pixels up
    var topConstraint = 0;

    // The slider can move XXX pixels down
    var bottomConstraint = 537;

    // Custom scale factor for converting the pixel offset into a real value
    var scaleFactor = 1.2;

    // The amount the slider moves when the value is changed with the arrow
    // keys
    var keyIncrement = 20;

	window.onload = function() {
    
    	// calculate spill height to determine conversion factor
    	var spill_height = 0;
    	var wrap_height = document.getElementById('content_wrap').offsetHeight;
    	if (wrap_height > 550) {
    		spill_height = wrap_height - 550;
    		scaleFactor = spill_height/bottomConstraint;
    	}
    	
    	// hide scroll bar if there is no spill
    	if (spill_height > 0) {
    		document.getElementById("slider-bg-writings").style.display = "block";
    	}
    	

        slider = YAHOO.widget.Slider.getVertSlider(bg, 
                         thumb, topConstraint, bottomConstraint);

        slider.getRealValue = function() {
            return Math.round(this.getValue() * scaleFactor);
        }

        slider.subscribe("change", function(offsetFromStart) {

            //var valnode = Dom.get(valuearea);
            //var fld = Dom.get(textfield);
            var tree = Dom.get(treenav);

            // Display the pixel value of the control
            //valnode.innerHTML = offsetFromStart;

            // 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 scroll
            document.getElementById(treenav).style.top = '-'+actualValue+'px';
           
            // Update the title attribute on the background.  This helps assistive
            // technology to communicate the state change
            //Dom.get(bg).title = "slider value = " + actualValue;

        });

        slider.subscribe("slideStart", function() {
                YAHOO.log("slideStart fired", "warn");
            });

        slider.subscribe("slideEnd", function() {
                YAHOO.log("slideEnd fired", "warn");
            });

        // set an initial value
        slider.setValue(0);
  
        // Use setValue to reset the value to white:
        Event.on("putval", "click", function(e) {
            slider.setValue(100, false); //false here means to animate if possible
        });
        
        // Use the "get" method to get the current offset from the slider's start
        // position in pixels.  By applying the scale factor, we can translate this
        // into a "real value
        Event.on("getval", "click", function(e) {
            YAHOO.log("Current value: "   + slider.getValue() + "\n" + 
                      "Converted value: " + slider.getRealValue(), "info", "example"); 
        });
    };// end onlod/domready
})();
