﻿// JScript File

/* Funciones para el / los Sliders */
var Dom = YAHOO.util.Dom;
function sliderJ() {
    this.divSlider = "";
    this.thumbMin = "";
    this.thumbMax = "";
    this.anchoThumb = 20;
    this.txtMin = "";
    this.txtMax = "";
    this.ancho = 200;
    this.recorrido = 0;
    this.distanciaMinima = 5;
    this.funcionOnChange = null;
    this.tipo = "numerico"; /*numerico, hora*/
    this.min = 0;
    this.max = 100;
    this.minSel = 0;
    this.maxSel = 100;

    /*variables del objeto internas*/
    this.yahooSlider = null;
}

sliderJ.prototype.init = function(min, max) {
    Dom.get(this.divSlider);
    document.getElementById(this.txtMin).innerHTML = min;
    document.getElementById(this.txtMax).innerHTML = max;
    this.minSel = min;
    this.maxSel = max;
    if (this.tipo == "hora") {
        this.min = stringHoraToMinutos(min);
        this.max = stringHoraToMinutos(max);
    } else {
        this.min = min;
        this.max = max;
    }

    /* Conversion factor from 0-200 pixels to 100-1000
    Note 20 pixels are subtracted from the range to account for the
    thumb values calculated from their center point (10 pixels from
    the center of the left thumb + 10 pixels from the center of the
    right thumb)*/
    var cf = Math.round((this.max - this.min) / (this.ancho - (this.anchoThumb / 2)));
    /* Set up a function to convert the min and max values into something useful */
    var convert = function(val) {
        return Math.round(val * cf);
    };
    var sld = this;
    var updateUI = function() {
        var minim = convert(sld.yahooSlider.minVal - (sld.anchoThumb / 2));
        if (minim <= 0) minim = sld.min;
        var maxim = convert(sld.yahooSlider.maxVal - (sld.anchoThumb / 2)) + sld.min;
        if ((maxim > sld.max) || (sld.yahooSlider.maxVal == sld.ancho)) maxim = sld.max;
        if (sld.tipo == "hora") {
            sld.minSel = minutosToStringHora(minim);
            sld.maxSel = minutosToStringHora(maxim)
            document.getElementById(sld.txtMin).innerHTML = sld.minSel;
            document.getElementById(sld.txtMax).innerHTML = sld.maxSel;

        } else {
            sld.minSel = minim;
            sld.maxSel = maxim;
            document.getElementById(sld.txtMin).innerHTML = sld.minSel;
            document.getElementById(sld.txtMax).innerHTML = sld.maxSel;
        }
        eval(sld.funcionOnChange);
    };

    this.yahooSlider = YAHOO.widget.Slider.getHorizDualSlider(this.divSlider, this.thumbMin, this.thumbMax, this.ancho, this.recorrido);
    this.yahooSlider.minRange = this.distanciaMinima;
    this.yahooSlider.subscribe('slideEnd', updateUI);
}


function minutosToStringHora(minutos) {
    var hora;
    var minuto;
    hora = parseInt(minutos / 60);
    if (hora <= 9) hora = "0" + hora;
    minuto = minutos % 60;
    if (minuto <= 9) minuto = "0" + minuto;
    return hora + ":" + minuto;
}

function stringHoraToMinutos(strHora) {
    var arr = strHora.split(":");
    var minutos = parseInt(arr[0]) * 60 + parseInt(arr[1]);
    return minutos;
}

