/**************************************************************
*
* CLASSE JAVASCRIPT COMBO BOX
*
* CRIADO POR: CRISTIANO TAVELLA
* DATA: 12/05/2010
* OBS:
* EDITADO POR: CRISTIANO TAVELLA
* DATA: 12/05/2010
* OBS:
*
*************************************************************/

function imprimirSlider() {
    //printa os valores
    var script = '<div id="' + this.id + '"></div>';
    script += '<div style="float:left; margin-top:5px; font-size:10px" >De: <span id="' + this.id + '_val1"  style="  font-weight:bold;" >qualquer valor</span ></div>';
    script += '<div style="float:right; margin-top:5px;font-size:10px" >Até: <span id="' + this.id + '_val2"  style=" font-weight:bold;">qualquer valor</span ></div>';
    $(this.destino).html(script);
    
    //chama o slider
    var j = document.createElement("script");
    j.type = "text/javascript";
    j.src = v_raiz + "Scripts/jquery-ui-1.8.13.custom.min.js";
    document.body.appendChild(j);

    var j = document.createElement("link");
    j.type = "text/css";
    j.rel = "stylesheet";
    j.href = v_raiz + "Css/slider.css";
    document.body.appendChild(j);
    
    var v_this = this;
    var v_min, v_max, v_min_sel, v_max_sel;
    v_min = parseInt(v_this.options[0].value);
    v_max = parseInt(v_this.options[3].value);
    v_min_sel = parseInt(v_this.options[1].value);
    v_max_sel = parseInt(v_this.options[2].value);

    if (v_min_sel == v_min || v_min_sel == "0")
        $('#' + this.id + '_val1').html("qualquer valor");
    else
        $('#' + this.id + '_val1').html(formataMoeda(v_min_sel));

    if (v_max_sel == v_max || v_max_sel == "0")
        $('#' + this.id + '_val2').html("qualquer valor");
    else
        $('#' + this.id + '_val2').html(formataMoeda(v_max_sel));

    if (v_min_sel != v_min && v_max_sel != v_max) {
        this.val = v_min_sel + "_" + v_max_sel;
        this.label = v_min_sel + "_" + v_max_sel;
    }

    switch (v_min_sel) {
        case 1250: v_min_sel = 1100; break;
        case 1500: v_min_sel = 1200; break;
        case 2000: v_min_sel = 1300; break;
        case 3000: v_min_sel = 1400; break;
        case 5000: v_min_sel = 1500; break;
        case 7500: v_min_sel = 1600; break;
        case 10000: v_min_sel = 1700; break;
        case 12500: v_min_sel = 1800; break;
        case 15000: v_min_sel = 1900; break;
        case 30000: v_min_sel = 2000; break;
        case 1500000: v_min_sel = 1100000; break;
        case 3000000: v_min_sel = 1200000; break;
    }

    switch (v_max_sel) {
        case 0: v_max_sel = v_max; break;
        case 1250: v_max_sel = 1100; break;
        case 1500: v_max_sel = 1200; break;
        case 2000: v_max_sel = 1300; break;
        case 3000: v_max_sel = 1400; break;
        case 5000: v_max_sel = 1500; break;
        case 7500: v_max_sel = 1600; break;
        case 10000: v_max_sel = 1700; break;
        case 12500: v_max_sel = 1800; break;
        case 15000: v_max_sel = 1900; break;
        case 30000: v_max_sel = 2000; break;
        case 1500000: v_max_sel = 1100000; break;
        case 3000000: v_max_sel = 1200000; break;
    }


    $(function () {
        $('#' + v_this.id).slider({
            range: true,
            animate: true,
            step: (v_max == 2100 ? 100 : 100000),
            min: v_min,
            max: v_max,
            values: [v_min_sel, v_max_sel],
            slide: function (event, ui) {

                if (ui.values[0] == $('#' + v_this.id).slider("option", "min")) {
                    var txtmin = "qualquer valor";
                }
                else {
                    var txtmin = ui.values[0];
                }

                if (ui.values[1] == $('#' + v_this.id).slider("option", "max")) {
                    var txtmax = "qualquer valor";
                }
                else {
                    var txtmax = ui.values[1];
                }

                switch (txtmin) {
                    case 1100: txtmin = 1250; break;
                    case 1200: txtmin = 1500; break;
                    case 1300: txtmin = 2000; break;
                    case 1400: txtmin = 3000; break;
                    case 1500: txtmin = 5000; break;
                    case 1600: txtmin = 7500; break;
                    case 1700: txtmin = 10000; break;
                    case 1800: txtmin = 12500; break;
                    case 1900: txtmin = 15000; break;
                    case 2000: txtmin = 30000; break;
                    case 1100000: txtmin = 1500000; break;
                    case 1200000: txtmin = 3000000; break;
                }

                switch (txtmax) {
                    case 1100: txtmax = 1250; break;
                    case 1200: txtmax = 1500; break;
                    case 1300: txtmax = 2000; break;
                    case 1400: txtmax = 3000; break;
                    case 1500: txtmax = 5000; break;
                    case 1600: txtmax = 7500; break;
                    case 1700: txtmax = 10000; break;
                    case 1800: txtmax = 12500; break;
                    case 1900: txtmax = 15000; break;
                    case 2000: txtmax = 30000; break;
                    case 1100000: txtmax = 1500000; break;
                    case 1200000: txtmax = 3000000; break;
                }

                $('#' + v_this.id + '_val1').html(formataMoeda(txtmin));
                $('#' + v_this.id + '_val2').html(formataMoeda(txtmax));

                if (txtmin == "qualquer valor" && txtmax != "qualquer valor") {
                    eval(v_this.elem + ".val = '0_" + txtmax + "';");
                    eval(v_this.elem + ".label = '0_" + txtmax + "';");
                }
                else if (txtmin != "qualquer valor" && txtmax == "qualquer valor") {
                    eval(v_this.elem + ".val = '" + txtmin + "_0';");
                    eval(v_this.elem + ".label = '" + txtmin + "_0';");
                }
                else if (txtmin != "qualquer valor" && txtmax != "qualquer valor") {
                    eval(v_this.elem + ".val = '" + txtmin + "_" + txtmax + "';");
                    eval(v_this.elem + ".label = '" + txtmin + "_" + txtmax + "';");
                }
                else {
                    eval(v_this.elem + ".val = '';");
                    eval(v_this.elem + ".label = '';");
                }



            },
            change: function (event, ui) { eval((typeof (v_this.atualizar) != "undefined" && v_this.atualizar != "" && !v_this.consultaOnChange ? v_this.elem.substring(0, v_this.elem.indexOf(".combo")) + ".atualizarBDCombos(\"" + v_this.atualizar + "\");" : "") + " " + (v_this.consultaOnChange ? v_this.elem.substring(0, v_this.elem.indexOf(".combo")) + ".filtrarResultado(" + v_this.elem + ");" : "")); }
        });
    });
            
}

function imprimirRadio() {
    //printa os valores
    var vazio = 0;
    for (var i = 0; i < this.options.length; i++) {
        if (typeof (this.options[i].selected) != " undefined" && this.options[i].selected)
            vazio++;
    }
    for (var i = 0; i < this.options.length; i++) {
        $(this.destino).append("<label><input name='" + this.id + "' type='radio' onclick='" + this.elem + ".populaInput(); " + (typeof (this.atualizar) != "undefined" && this.atualizar != "" && !this.consultaOnChange ? this.elem.substring(0, this.elem.indexOf(".combo")) + ".atualizarBDCombos(\"" + this.atualizar + "\");" : "") + " " + (this.consultaOnChange ? this.elem.substring(0, this.elem.indexOf(".combo")) + ".filtrarResultado(" + this.elem + ");" : "") + "' value='" + this.options[i].value + "' label='" + this.options[i].label + "'  " + (vazio == 0 && i == 0 ? "checked='checked'" : (typeof (this.options[i].selected) != " undefined" && this.options[i].selected ? "checked='checked'" : "")) + "  /> " + this.options[i].label + "</label>");

    }

    this.populaInput();
}

function imprimirCombo() {
    var v_this = this;
    var script = "";
    
    script += "<div id='boxCombo_" + this.id + "' onclick='" + this.elem + ".showCombo();' >";
    script += "<input type='text' id='" + this.id + "' style=' display:block; cursor:default; background:transparent; border:none; outline:0; '  readonly='readonly' /><input type='hidden' id='" + this.id + "_hidden' />";
    script += "</div>";
    if (this.checkbox) {
        script += "<div id='boxFundo_" + this.id + "' style='position:fixed; width:100%; height:100%; background-color:#000; opacity: 0.20;filter:alpha(opacity=20); left:0; top:0; display:none;z-index: 999'></div>" +
        "<div id='boxOptions_" + this.id + "' style='display:none;  outline:0;  background: #FFF;  position:fixed; z-index:9999999999999; width:650px; height:480px; left:50%; top:50%; margin-top:-240px; margin-left:-325px; border:1px solid #999 '>" +
        "<div style='background:#B3B3B3; font-weight:bold; color:#FFF; padding:7px; height:15px;'><div style='float:left'>Selecione...</div><div style='float:right;'><span style='cursor:pointer' onclick='$(\"#boxOptions_" + this.id + "\").hide(); $(\"#boxFundo_" + this.id + "\").hide();'>FECHAR</span> ou pressione ESC</div></div>" +
        "<div id='boxChecks_" + this.id + "' style='height:350px; padding:20px; overflow-y:auto; overflow-x:hidden'></div>" +
        "<div style='height:30px; padding:15px; background:#EFEFEF; border-top:1px solid #CCC; text-align:center;'><input type='button' style='height:30px' value='Concluir' onclick='$(\"#boxOptions_" + this.id + "\").hide(); $(\"#boxFundo_" + this.id + "\").hide(); " + (typeof (this.atualizar) != "undefined" && this.atualizar != "" && !this.consultaOnChange ? this.elem.substring(0, this.elem.indexOf(".combo")) + ".atualizarBDCombos(\"" + this.atualizar + "\");" : "") + " " + (this.consultaOnChange ? this.elem.substring(0, this.elem.indexOf(".combo")) + ".filtrarResultado(" + this.elem + ");" : "") + "' />" +
        ""+
        "</div></div>"; 
    }
    else
        script += "<select class='boxOptions' id='boxOptions_" + this.id + "' multiple='multiple' onkeydown=' if(OnEnter(event)){ " + this.elem + ".populaInput();  $(\"#boxOptions_" + this.id + "\").hide(); " + (typeof (this.atualizar) != "undefined" && this.atualizar != "" && !this.consultaOnChange ? this.elem.substring(0, this.elem.indexOf(".combo")) + ".atualizarBDCombos(\"" + this.atualizar + "\");" : "") + " " + (this.consultaOnChange ? this.elem.substring(0, this.elem.indexOf(".combo")) + ".filtrarResultado(" + this.elem + ");" : "") + "}' onclick='" + this.elem + ".populaInput();  " + (typeof (this.atualizar) != "undefined" && this.atualizar != "" && !this.consultaOnChange ? this.elem.substring(0, this.elem.indexOf(".combo")) + ".atualizarBDCombos(\"" + this.atualizar + "\");" : "") + " " + (this.consultaOnChange ? this.elem.substring(0, this.elem.indexOf(".combo")) + ".filtrarResultado(" + this.elem + ");" : "") + "' style='display:none;  outline:0;  background: #FFF;  position:absolute; z-index:9999; '></select>";

    $(this.destino).html(script);

    //printa os valores
    if (this.checkbox)
        $("#boxChecks_" + this.id).html("<div style='color:#000; float:left;  cursor:default; width: 290px;  background:#fff'  onmouseover=\"$(this).css(\'background-color\', \'#3399FF\'); $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'background-color\', \'#FFF\'); $(this).css(\'color\', \'#000\'); \"><label><input class='checks_" + this.id + "' type='checkbox' onclick='if($(this).attr(\"checked\")){ $(\".checks_" + this.id + "\").attr(\"checked\",false); $(this).attr(\"checked\",true); }else{ $(\".checks_" + this.id + "\").attr(\"checked\",false); $(this).attr(\"checked\",false); } " + this.elem + ".populaInput(); ' style='width:15px; height:13px; .height:25px;  padding:0px; margin:0px; margin-left:5px' value='' label='Todos' checked='checked' /><input type='text' style='font-size:12px; background:transparent; border:none; cursor:default; width:250px;' readonly='readonly' value=\"Todos\"  onmouseover=\" $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'color\', \'#000\'); \"  /></label></div><br clear='all' />");

    var vazio = 0;
    for (var i = 0; i < this.options.length; i++) {
        if (typeof (this.options[i].selected) != " undefined" && this.options[i].selected)
            vazio++;
    }
    for (var i = 0; i < this.options.length; i++) {
        if (!this.checkbox) {
            $("#boxOptions_" + this.id).append("<option value='" + this.options[i].value + "' " + (vazio == 0 && i == 0 ? "selected='selected'" : (typeof (this.options[i].selected) != " undefined" && this.options[i].selected ? "selected='selected'" : "")) + ">" + this.options[i].label + "</option>");
        }
        else {
            $("#boxChecks_" + this.id).append("<div style='color:#000; float:left; margin:3px; cursor:default; width: 290px;  background:#fff'  onmouseover=\"$(this).css(\'background-color\', \'#3399FF\'); $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'background-color\', \'#FFF\'); $(this).css(\'color\', \'#000\'); \"><label><input class='checks_" + this.id + "' type='checkbox' onclick='" + this.elem + ".populaInput(); ' style='width:15px; height:13px; .height:25px; padding:0px; margin-left:5px' value='" + this.options[i].value + "' label='" + this.options[i].label + "' " + (typeof (this.options[i].selected) != " undefined" && this.options[i].selected ? "checked='checked'" : "") + " /><input type='text' style='font-size:12px; background:transparent; border:none; cursor:default; width:250px;' readonly='readonly' value=\"" + this.options[i].label + "\"  onmouseover=\" $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'color\', \'#000\'); \"  /></label></div>");
        }
    }
    this.populaInput();

    //Oculta optionbox com botão ESC
    $(document).bind("keydown", function (event) {
        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (keycode == 27) {
            $("#boxOptions_" + v_this.id).hide();
            if (v_this.checkbox)
                $("#boxFundo_" + v_this.id).hide();
        }
    });

    if (!this.checkbox) {
        //Ocultar quando clicar fora do combo
        $("#boxCombo_" + v_this.id).click(function () {
            return false;
        });

        $(document).click(function () {
            $("#boxOptions_" + v_this.id).hide();
        });
    }

}

function showCombo() {
    if ($("#boxOptions_" + this.id).css("display") != "none") {
        $(".boxOptions").hide();
        return;
    }
    $(".boxOptions").hide();
    if (!this.checkbox) {
        $("#boxOptions_" + this.id).css("top", getPosicaoElemento(this.id).top + parseInt($("#" + this.id).css("height")) + parseInt($("#" + this.id).css("padding-top")) + parseInt($("#" + this.id).css("padding-bottom")) - 1);
        $("#boxOptions_" + this.id).css("left", getPosicaoElemento(this.id).left);
    }
    if ($("#boxOptions_" + this.id).css("display") == "none") {
        $("#boxOptions_" + this.id).show();
        if (this.checkbox)
            $("#boxFundo_" + this.id).show();
        $("#boxOptions_" + this.id).focus();
    }
    else {
        $("#boxOptions_" + this.id).hide();
        if (this.checkbox)
            $("#boxFundo_" + this.id).hide();
    }
    
}

function populaInput() {
    if (this.checkbox) {
        var v_val = "";
        var v_label = "";
        $(".checks_" + this.id).each(function () {
            if ($(this).attr("checked") && $(this).val() != "Todos") {
                v_val += (v_val == "" ? "" : ",") + $(this).val();
                v_label += (v_label == "" ? "" : ",") + $(this).attr("label");
            }
        });
        this.val = v_val;
        this.label = v_label;
        $("#" + this.id).val(this.label);
        $("#" + this.id + "_hidden").val(this.val);

        if (this.val != "") {
            var i = 0;
            $(".checks_" + this.id).each(function () {
                if (i == 0)
                    $(this).attr("checked", false);
                i++;
            });
        }
        else {
            this.val = "";
            this.label = "Todos";
            $("#" + this.id).val(this.label);
            $("#" + this.id + "_hidden").val(this.val);
        }

    }
    else if (this.radio) {
        this.val = $("input[name='" + this.id + "']:checked").val();
        this.label = $("input[name='" + this.id + "']:checked").attr("label");
    }
    else if ($.trim($("#boxOptions_" + this.id + " option:selected").text()) != "") {
        this.val = $("#boxOptions_" + this.id).val();
        this.label = $("#boxOptions_" + this.id + " option:selected").text();
        $("#" + this.id).val(this.label);
        $("#" + this.id + "_hidden").val(this.val);
    }
}

function setOptions(dados) {
    this.options = dados.slice(0);
    //printa os valores
    if (this.slider) {

         $('#' + this.id).slider( "option", "step", (this.options[3].value == 2100 ? 100 : 100000) );
         $('#' + this.id).slider( "option", "min", this.options[0].value);
         $('#' + this.id).slider( "option", "max", this.options[3].value);
         $('#' + this.id).slider("option", "values", [this.options[1].value, this.options[2].value]);

         $('#' + this.id + '_val1').html("qualquer valor");
         $('#' + this.id + '_val2').html("qualquer valor");
         this.val = "";
         this.label = "";

        return;
    }
    
    if (!this.checkbox)
        $("#boxOptions_" + this.id).html('');
    else
        $("#boxChecks_" + this.id).html("<div style='color:#000; float:left; margin:3px; cursor:default;  width: 290px; background:#fff'  onmouseover=\"$(this).css(\'background-color\', \'#3399FF\'); $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'background-color\', \'#FFF\'); $(this).css(\'color\', \'#000\'); \"><label><input class='checks_" + this.id + "' type='checkbox' onclick='if($(this).attr(\"checked\")){ $(\".checks_" + this.id + "\").attr(\"checked\",false); $(this).attr(\"checked\",true); }else{  $(this).attr(\"checked\",true); } " + this.elem + ".populaInput(); ' style='width:15px;height:13px; .height:25px;  padding:0px; margin-left:5px' value='Todos' label='Todos' checked='checked' /><input type='text' style='font-size:12px; background:transparent; border:none; cursor:default; width:250px;' readonly='readonly' value=\"Todos\"  onmouseover=\" $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'color\', \'#000\'); \"  /></label></div><br clear='all' />");

    var vazio = 0;
    for (var i = 0; i < this.options.length; i++) {
        if (typeof (this.options[i].selected) != " undefined" && this.options[i].selected)
            vazio++;
    }
    for (var i = 0; i < this.options.length; i++) {
        if (!this.checkbox) {
            $("#boxOptions_" + this.id).append("<option value='" + this.options[i].value + "' " + (vazio == 0 && i == 0 ? "selected='selected'" : (typeof (this.options[i].selected) != " undefined" && this.options[i].selected ? "selected='selected'" : "")) + ">" + this.options[i].label + "</option>");
        }
        else {
            $("#boxChecks_" + this.id).append("<div style='color:#000; float:left; margin:3px; cursor:default; width: 290px;  background:#fff'  onmouseover=\"$(this).css(\'background-color\', \'#3399FF\'); $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'background-color\', \'#FFF\'); $(this).css(\'color\', \'#000\'); \"><label><input class='checks_" + this.id + "' type='checkbox' onclick='" + this.elem + ".populaInput(); ' style='width:15px; height:13px; .height:25px; padding:0px; margin-left:5px' value='" + this.options[i].value + "' label='" + this.options[i].label + "' " + (typeof (this.options[i].selected) != " undefined" && this.options[i].selected ? "checked='checked'" : "") + " /><input type='text' style='font-size:12px; background:transparent; border:none; cursor:default; width:250px;' readonly='readonly' value=\"" + this.options[i].label + "\"  onmouseover=\" $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'color\', \'#000\'); \"  /></label></div>");
        }
    }
    this.populaInput();
}

function Combo(parametros) {
    // Inicia Variaveis
    this.destino = parametros.destino
    this.id = parametros.id
    this.atualizar = parametros.atualizar
    this.options = parametros.options;
    this.elem = parametros.elem;
    this.tipo = parametros.tipo;
    this.checkbox = (typeof (parametros.checkbox) == "undefined" ? false : parametros.checkbox);
    this.radio = (typeof (parametros.radio) == "undefined" ? false : parametros.radio);
    this.slider = (typeof (parametros.slider) == "undefined" ? false : parametros.slider);
    this.val = null;
    this.label = null;
    this.consultaOnChange = parametros.consultaOnChange


    //Métodos
    this.imprimirCombo = imprimirCombo;
    this.imprimirRadio = imprimirRadio;
    this.imprimirSlider = imprimirSlider;
    this.showCombo = showCombo;
    this.populaInput = populaInput;
    this.setOptions = setOptions;
    if (this.radio)
        this.imprimirRadio();
    else if (this.slider)
        this.imprimirSlider();
    else
        this.imprimirCombo();
}

function getPosicaoElemento(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
			typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return { left: offsetLeft, top: offsetTop };
}

function OnEnter(evt) {
    var key_code = evt.keyCode ? evt.keyCode :
						   evt.charCode ? evt.charCode :
						   evt.which ? evt.which : void 0;


    if (key_code == 13) {
        return true;
    }
}

function formataMoeda(valor) {
    if (valor == "qualquer valor")
        return valor;

    var preco = valor + "";
    var tam = preco.length;

    if (tam == 4)
        preco = preco.substr(0, 1) + '.' + preco.substr(1, 3);
    else if (tam == 5)
        preco = preco.substr(0, 2) + '.' + preco.substr(2, 3);
    else if (tam == 6)
        preco = preco.substr(0, 3) + '.' + preco.substr(3, 3);
    else if (tam == 7)
        preco = preco.substr(0, 1) + '.' + preco.substr(1, 3) + '.' + preco.substr(4, 3);
    else if (tam == 8)
        preco = preco.substr(0, 2) + '.' + preco.substr(2, 3) + '.' + preco.substr(5, 3);
    else if (tam == 9)
        preco = preco.substr(0, 3) + '.' + preco.substr(3, 3) + '.' + preco.substr(6, 3);

    return preco+",00";
}
