/* #################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
################################################################## */

//This script only supports ie and standard complient browsers, it will
//gracefully display a list of radio buttons if objects/functions aren't supported
function Slider(sliderID, inputID, displayID) {
    //the id of the sliderContainer
    this.debug = false;
    this.debugID = "rateTest";
    this.sliderID = sliderID;
    this.displayID = displayID;
    this.inputID = inputID;

    this.sMin = 0;
    this.sMax = 10;
    this.sDefaultValue = 5;
    this.sWidth  = 300;
    this.sHeight = 18;
    this.dragWidth = 12;
    this.dragHeight = 18;

    //this is the number multiplied and divided by before the round function
    //ie: 24.5678 => 24.5678 * 100 = 2456.78 => round( ) = 2457 => 2457/100 = 24.57
    this.precision = 10;
    this.sValue = 0;

    this.initSlider             = Slider.initSlider;
    this.setDefaultSliderValue  = Slider.setDefaultSliderValue;
    this.setSliderValue         = Slider.setSliderValue;
    this.moveSlider             = Slider.moveSlider;
    this.initSlider( );
}

Slider.initSlider = function( ){
    //if the id isn't found in the document, it's not there.
    if(!document.getElementById(this.sliderID)) return;

    //this should make it so that it only supports newer browsers
    if(!document.getElementsByTagName){
        return;
    }

    var sContain = document.getElementById(this.sliderID);

    tmp = "";


    if(this.debug){
        tmp += '<div id="' + this.debugID + '" style="border: 1px solid #FF0000; height: 100px;">&nbsp;</div>';
    }
    tmp += '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td>' +
        'Terrible (0)</td><td>';

    tmp += '<input type="hidden" name="' + this.inputID + '" id="' + this.inputID + '" />' +
        '<div id="' + this.sliderID + '_0" class="slider" style="width: ' + this.sWidth + 'px; height: ' + this.sHeight + 'px;">' +
        '<div id="' + this.sliderID + '_1" class="drag" style="width: ' + this.dragWidth + 'px; height: ' + this.dragHeight + 'px;"></div></div>'

    tmp += '</td><td>Perfect (10)</td></tr></table>';

    sContain.innerHTML = tmp;

    this.bar = document.getElementById(this.sliderID + '_0');
    this.drag = document.getElementById(this.sliderID + '_1');

    this.setDefaultSliderValue( );
    //controls clicking on the button (drag)
    this.drag.isDown        = false;
    this.drag.sParent       = this;
    this.drag.onmousedown   = Slider.dragDown;
    this.drag.onmouseup     = Slider.dragUp;
    this.drag.onmousemove   = Slider.dragMouseMove;

    this.bar.onmousedown    = Slider.barDown;
    this.bar.sParent        = this;
}


/* #################################################################
Slider.drag functions + barClick
################################################################# */

Slider.barDown = function(e){
    var tmp = this;
    var ofs = 0;
    var t_x, t_y;
    while (tmp) {
        ofs += tmp.offsetLeft;
        tmp = tmp.offsetParent;
    }

    if(window.is_ie && navigator.userAgent.indexOf("Opera") == -1){
        ofs = event.clientX - ofs;
    }
    else{
        ofs = e.pageX - ofs;
    }

     ofs = ofs - this.sParent.dragWidth/2

    if(ofs < 0){
        ofs = 0;
    }
    else if(ofs > (this.sParent.sWidth - this.sParent.dragWidth)){
        ofs = (this.sParent.sWidth - this.sParent.dragWidth)
    }

    var dx = parseInt(this.sParent.drag.style.left);
    m2 = ofs;

    this.sParent.drag.style.left = parseInt(m2) + "px";
    this.sParent.setSliderValue( );

    if(this.sParent.debug){
        rt = document.getElementById(this.sParent.debugID);
        rt.innerHTML = m2 + " --&gt; " + ofs + " --&gt;" + this.sParent.sValue;
    }
}

Slider.dragDown = function(e){
    this.isDown = true;
    if(window.is_ie && navigator.userAgent.indexOf("Opera") == -1){
        window.event.cancelBubble = true;
    }
    else{
        e.stopPropagation();
        e.preventDefault( );
    }
    return false;
}
Slider.dragUp = function(e){
    this.isDown = false;
}

Slider.dragMouseMove = function(e){
    if(this.isDown){
        var tmp = this.parentNode;
        var ofs = 0;
        var t_x, t_y;
        while (tmp) {
            ofs += tmp.offsetLeft;
            tmp = tmp.offsetParent;
        }

        if(window.is_ie && navigator.userAgent.indexOf("Opera") == -1){
            ofs = event.clientX - ofs;
        }
        else{
            ofs = e.pageX - ofs;
        }

        if(ofs < 0){
            ofs = 0;
        }
        else if(ofs > (this.sParent.sWidth - this.sParent.dragWidth)){
            ofs = (this.sParent.sWidth - this.sParent.dragWidth)
        }


        var dx = parseInt(this.style.left);
        m2 = ofs;

        this.style.left = parseInt(m2) + "px";
        this.sParent.setSliderValue( );

        if(this.sParent.debug){
            rt = document.getElementById(this.sParent.debugID);
            rt.innerHTML = m2 + " --&gt; " + ofs + " --&gt;" + this.sParent.sValue;
        }
    }
}

/* #################################################################
Slider base functions
################################################################# */
Slider.setSliderValue = function( ){
    if(!this.drag) return;
    var tmax = parseInt(this.sWidth - this.dragWidth);
    var pos = parseInt(this.drag.style.left);
    var scale = tmax / (this.sMax - this.sMin)
    var tmp;
    if(scale <= 0){
        this.sValue = NaN;
    }else{
        this.sValue = pos / scale;
    }
    this.sValue = Math.round(this.sValue * this.precision) / this.precision;
    if(this.displayID){
        document.getElementById(this.displayID).innerHTML = this.sValue;
    }

    document.getElementById(this.inputID).value = this.sValue;
}

Slider.setDefaultSliderValue = function( ){
    if(!this.drag) return;
    var tmax = parseInt(this.sWidth - this.dragWidth);
    var scale = tmax / (this.sMax - this.sMin)
    if(scale <= 0){
        this.sValue = NaN;
    }else{
        this.drag.style.left = (this.sDefaultValue * scale) + "px";
    }
    this.setSliderValue( );

}

function windowMouseUp(e){
    for(var i = 0; i < document.sliders.length; i++){
        var slider = document.sliders[i];
        if(document.getElementById(slider.sliderID)){
            if(slider.drag.isDown){
                slider.drag.isDown = false;
            }
        }
    }
}

function windowMouseMove(e){
    for(var i = 0; i < document.sliders.length; i++){
        var slider = document.sliders[i];
        if(document.getElementById(slider.sliderID)){
            if(slider.drag.isDown){
                slider.drag.onmousemove(e);
                //this prevents the browser from selecting text while the slider is in motion.
                if(window.is_ie && navigator.userAgent.indexOf("Opera") == -1){
                    window.event.cancelBubble = true;
                }
                else{
                    e.stopPropagation();
                    e.preventDefault( );
                }
                return false;
            }
        }
    }
}

/* these will handle the sliders for the rest of the document */
function initSliders( ){
    document.sliders = new Array( );
    if(document.getElementsByTagName){
        var aslider = new Slider("aslider", "ratingValue", "ratingDisplay");
        document.sliders[0] = aslider;
        var body = document.getElementsByTagName('body');
        //document.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
        document.onmouseup = windowMouseUp;
        document.onmousemove = windowMouseMove;
     }
}