// sorBarSupport.js - utility functions to support adjustable SOR bars

// Slides an SOR bar
//
// iDiff       - difference to add to the value object's value
// sValObjId   - element id of the value object
// sSORId      - element id of the filled in section of the SOR bar
// sEmtpySORId - element id of the empty section of the SOR bar
// iMinValue   - min bound of the value
// iMaxValue   - max bound of the value
// iMaxWidth   - maximum HTML width (in px)
// sAltFmt     - format string for alt property of SOR bar
//
// NOTE: depends on proto.js's String.format()
var SorBar = new Object();

SorBar.slideSORBar = function (iDiff,
                     sValObjId, sSORId, sEmptySORId,
                     iMinValue, iMaxValue, iMaxWidth,
                     sAltFmt)
{
    this.slideSORBar(iDiff, sValObjId, sSORId, sEmptySORId, iMinValue, iMaxValue, iMaxWidth, sAltFmt, true);
}

SorBar.slideSORBar = function (iDiff,
                     sValObjId, sSORId, sEmptySORId,
                     iMinValue, iMaxValue, iMaxWidth,
                     sAltFmt, bDisplayAlt)
{
    // get all the objects
    var oValObj      = document.getElementById(sValObjId);
    var iValue       = parseInt(oValObj.value);

    // update the iValue
    iValue += iDiff;

    this.updateSORBar(iValue, sValObjId, sSORId, sEmptySORId, iMinValue, iMaxValue, iMaxWidth, sAltFmt, bDisplayAlt);
}

SorBar.updateSORBar = function (iNewValue,
                      sValObjId, sSORId, sEmptySORId,
                      iMinValue, iMaxValue, iMaxWidth,
                      sAltFmt, bDisplayAlt)
{
    // get all the objects
    var oValObj      = document.getElementById(sValObjId);
    var oSORBar      = document.getElementById(sSORId);
    var oEmptySORBar = document.getElementById(sEmptySORId);
    var iValue       = parseInt(oValObj.value);

    // update the iValue
    iValue = iNewValue;

    // bound iValue
    if (iValue < iMinValue) iValue = iMinValue;
    if (iValue > iMaxValue) iValue = iMaxValue;

     // set the value object's value
    oValObj.value = iValue;

    // fiddle with the bars
    oSORBar.width = iValue / iMaxValue * iMaxWidth;
    oEmptySORBar.width = iMaxWidth - oSORBar.width;

    // update the alt values for the bars
    if (bDisplayAlt)
    {
        var sAlt = sAltFmt.format(new Array(new String(iValue)));
        oSORBar.alt        = sAlt;
        oEmptySORBar.alt   = sAlt;
        oSORBar.title      = sAlt;
        oEmptySORBar.title = sAlt;
    }
}


// adjustableSorBarSupport.js - utility functions to support adjustable SOR bars

// Slides an SOR bar
//
// iDiff       - difference to add to the value object's value
// sId   - id of the span containing the adjustable SOR bar elements
// iMinValue   - min bound of the value
// iMaxValue   - max bound of the value
// iBarWidth   - maximum HTML width (in px)
// sAltFmt     - format string for alt property of SOR bar
//
// NOTE: depends on proto.js
var AdjustableSorBar = new Object();

AdjustableSorBar.slideSorBar = function (iDiff, sId, iMinValue, iMaxValue, iBarWidth, sAltFmt)
{
    var oSorBarConf = this.getSorBarConf(sId);

    this.bindSorBarValue(oSorBarConf, getSorBarIntValue(oSorBarConf) + iDiff, iMinValue, iMaxValue);
    this.updateSorBarDisplay(oSorBarConf, iMaxValue, iBarWidth);
    this.updateSorBarDisplayAlt(oSorBarConf, sAltFmt);
}

// Gets all the components of the SOR bar
AdjustableSorBar.getSorBarConf = function (sId)
{
    var oConf = new Object();
    var oSpan = document.getElementById(sId);

    var oaInput = oSpan.getElementsByTagName("input");
    oConf.valObj = oaInput[0];

    var oaImg = oSpan.getElementsByTagName("img");
    oConf.filledBar = oaImg[0];
    oConf.emptyBar = oaImg[1];

    return oConf;
}

AdjustableSorBar.bindSorBarValue = function (oSorBarConf, iValue, iMinValue, iMaxValue)
{
    var foo = Math.max(iMinValue, Math.min(iMaxValue, iValue));
    oSorBarConf.valObj.setAttribute("value", foo);
}

AdjustableSorBar.getSorBarIntValue = function (oSorBarConf)
{
    return parseInt(oSorBarConf.valObj.getAttribute("value"));
}

AdjustableSorBar.updateSorBarDisplay = function (oSorBarConf, iMaxValue, iBarWidth)
{
    var iFilledBarWidth = getSorBarIntValue(oSorBarConf) / iMaxValue * iBarWidth;
    var iEmptyBarWidth = iBarWidth - iFilledBarWidth;
    oSorBarConf.filledBar.setAttribute("width", iFilledBarWidth);
    oSorBarConf.emptyBar.setAttribute("width", iEmptyBarWidth);
}

AdjustableSorBar.updateSorBarDisplayAlt = function (oSorBarConf, sAltFmt)
{
    var sAlt = sAltFmt.format(new Array(new String(getSorBarIntValue(oSorBarConf))));
    oSorBarConf.filledBar.setAttribute("alt",   sAlt);
    oSorBarConf.filledBar.setAttribute("title", sAlt);
    oSorBarConf.emptyBar.setAttribute("alt",    sAlt);
    oSorBarConf.emptyBar.setAttribute("title",  sAlt);
}

// sorBarSupport.js - utility functions to support adjustable SOR bars

// Slides an Privacy bar
//
// iDiff       - difference to add to the value object's value
// sValObjId   - element id of the value object
// sSORId      - element id of the filled in section of the SOR bar
// sEmtpySORId - element id of the empty section of the SOR bar
// iMinValue   - min bound of the value
// iMaxValue   - max bound of the value
// iMaxHeight   - maximum HTML height (in px)
// sAltFmt     - format string for alt property of SOR bar
//
// NOTE: depends on proto.js's String.format()
var PrivacySorBar = new Object();
PrivacySorBar.slidePrivacyBar = function (iDiff,
                     sValObjId, sSORId, sEmptySORId,
                     iMinValue, iMaxValue, iMaxHeight,
                     sAltFmt)
{
    // get all the objects
    var oValObj      = document.getElementById(sValObjId);
    var oSORBar      = document.getElementById(sSORId);
    var oEmptySORBar = document.getElementById(sEmptySORId);
    var iValue       = parseInt(oValObj.value);

    // update the iValue
    iValue += iDiff;

    // bound iValue
    if (iValue < iMinValue) iValue = iMinValue;
    if (iValue > iMaxValue) iValue = iMaxValue;

     // set the value object's value
    oValObj.value = iValue;

    // fiddle with the bars
    oSORBar.height = iValue / iMaxValue * iMaxHeight;
    oEmptySORBar.height = iMaxHeight - oSORBar.height;

    // update the alt values for the bars
    var sAlt = sAltFmt.format(new Array(new String(iValue)));
    oSORBar.alt      = sAlt;
    oEmptySORBar.alt = sAlt;
}


PrivacySorBar.displayPrivacyBar = function(sValObjId, sSORId, sEmptySORId,
                     iMinValue, iMaxValue, iMaxHeight)
{
    // get all the objects
    var oValObj      = document.getElementById(sValObjId);
    var oSORBar      = document.getElementById(sSORId);
    var oEmptySORBar = document.getElementById(sEmptySORId);
    var iValue       = parseInt(oValObj.value);


    // bound iValue
    if (iValue < iMinValue) iValue = iMinValue;
    if (iValue > iMaxValue) iValue = iMaxValue;

     // set the value object's value
    oValObj.value = iValue;

    // fiddle with the bars
    oSORBar.height = iValue / iMaxValue * iMaxHeight;
    oEmptySORBar.height = iMaxHeight - oSORBar.height;
}

