/**************************************************************
*
* CLASSE JAVASCRIPT COMBO BOX
*
* CRIADO POR: JONATAN BRANDI
* DATA: 13/07/2010
* OBS:
* EDITADO POR: JONATAN BRANDI
* DATA: 13/07/2010
* OBS:
*
*************************************************************/

function clickChk(id) {


    $("#" + id).val("");


    var i = 0;

    $(".comboChk_" + id + "").each(function () {
        if ($(this).attr("checked")) {
            if ($(this).val() != "Todos") {
                
                if ($.trim($("#" + id).val()) == "") {
                    $("#" + id).val(rawurldecode($(this).val()));
                }
                else {
                    $("#" + id).val($("#" + id).val() + ", " + rawurldecode($(this).val()));
                }
            }
            i++;
        }
        
    });

    if(i>=2){

        if ($("#" + id + "_check_0").attr("checked")) {
            $("#" + id + "_check_0").attr("checked", false);
           
        }
    }

   
    if ($.trim($("#" + id).val()) == "")
    {
        $("#" + id).val("Todos");
        $("#" + id + "_check_0").attr("checked", true);
    }

}

function populaInputCheckCheck(id, descricao, controle) {
    $("#" + controle).val(rawurldecode(descricao));
    $("#hdn_" + controle).val(id);
    $("#master_cont_" + controle).hide()
}

function controlaOpcoes(controle) {

    if (controle == "master_cont_inf_bairro")
        var label_cidade = "inf_cidade"
    else if (controle == "master_cont_emp_bairro")
        var label_cidade = "emp_cidade"


    if ($.trim($("#" + label_cidade).val()) == '')
        $("#" + controle+"_cidade").html('Nenhuma cidade encontrada, refaça a pesquisa por favor.');
    else
        $("#" + controle + "_cidade").html('Bairros da cidade de ' + $.trim($("#" + label_cidade).val()));

    if ($("#" + controle).css("display") == "none") {
        $("#" + controle).hide();
        $("#" + controle).show()
        $("#" + controle).css("left",( document.body.offsetWidth - 650) / 2);
        $("#" + controle).css("top", (screen.height - 680) / 2);

        $("#" + controle).focus();

    }
    else {
        $("#" + controle).hide()
    }
}

function replaceAll(str, de, para) {
    if (typeof (str) == "undefined")
        return "";
    var pos = str.indexOf(de);
    while (pos > -1) {
        str = str.replace(de, para);
        pos = str.indexOf(de);
    }
    return (str);
}

function printChk(combo) {
    var cont_height = parseInt(combo.height) - 4;
    var z_index_1 = 100 - parseInt(combo.z_index);
    var z_index_2 = 100 - parseInt(combo.z_index) - 1;
    $("#" + combo.destino).html("<div id='bg_" + combo.id + "' onclick='controlaOpcoes(\"master_cont_" + combo.id + "\"); $(\"#fundo_" + combo.id + "\").show();' style='width: " + combo.width + "px; height: " + combo.height + "px; background: url(" + combo.background + ") no-repeat;' onmouseover=\"$(this).css(\'background\', \'url(" + combo.background + ") no-repeat 0px -" + combo.height + "px\');\" " +
                                "onmouseout=\"$(this).css(\'background\', \'url(" + combo.background + ") no-repeat 0px 0px\');\">" +
                                "<input type=text class='chaveCombo' campo='" + combo.id + "' id=" + combo.id + " readonly=readonly style='outline:0 none; cursor:default; width: " + combo.input_width + "px; margin-left: 6px; margin-top: 1px; border: 0px; background: transparent;' />" +
                                "</div>" +
                                "<div class=master_cont id='master_cont_" + combo.id + "' style='display:none;  width:650px; height:480px; top: 0px; left: 0px; border:solid 3px #B3B3B3; background: #FFF; z-index: 999; position:fixed;'>" +
                                "<div style='background:#B3B3B3 url(Images/bg_bairro.png); font-weight:bold; color:#FFF; padding:7px; height:14px;'><div style='float:left'>Selecione os bairros</div><div style='float:right;'><span style='cursor:pointer' onclick='$(\"#master_cont_" + combo.id + "\").hide(); $(\"#fundo_" + combo.id + "\").hide();'>FECHAR</span> ou pressione ESC</div></div>" +
                                "<div class='titulo' style='margin-left:15px;' ><span id='master_cont_" + combo.id + "_cidade'></span></div>" + 
                                "<div id='cont_" + combo.id + "' style='  overflow-y: auto; overflow-x:hidden;  background: #FFF; position:relative; margin-left:25px; height: 365px;  '>" +
                                "</div><div style='height:35px;  background:#F2F2F2; border-top:1px solid #B3B3B3; text-align:center; padding:10px;'><div class='btn_buscar_rapida' style='margin-left:270px;' onclick='$(\"#master_cont_" + combo.id + "\").hide(); $(\"#fundo_" + combo.id + "\").hide();'>Concluir</div></div>" +
                                "</div><div id='fundo_" + combo.id + "' style='position:fixed; width:100%; height:100%; background-color:#000000;opacity: 0.20;filter:alpha(opacity=20); left:0; top:0; display:none;z-index: 10'>&nbsp;</div>");

    $("#" + combo.id).val(combo.valueInicial);

    var browser = navigator.appName
    var ver = navigator.appVersion
    var thestart = parseFloat(ver.indexOf("MSIE")) + 1
    var brow_ver = parseFloat(ver.substring(thestart + 4, thestart + 7))
    if ((browser == "Microsoft Internet Explorer") && (brow_ver == 7)) {
        $("#master_cont_" + combo.id).css("left", getPosicaoElemento(combo.destino).left);
    }
    else {
        $("#master_cont_" + combo.id).css("left", getPosicaoElemento(combo.destino).left - 8);
    }

    $("#master_cont_" + combo.id).css("top", getPosicaoElemento(combo.destino).top + 18);


    $(document).click(function () {

 
        $("#master_cont_" + combo.id).hide();
        $("#fundo_" + combo.id + "").hide();

    });

    $(document).bind("keydown", function (event) {

        var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (keycode == 27) {

            $("#master_cont_" + combo.id + "").hide(); $("#fundo_" + combo.id + "").hide();
        }

    }); 

    $("#bg_" + combo.id).click(function () {
        if ($("#master_cont_" + combo.id).css("display") == "block") {
            $(".master_cont").hide();
            $("#master_cont_" + combo.id).show()
        }
        else {
            $("#master_cont_" + combo.id).hide()
        }
        return false;
    });

    $("#cont_" + combo.id).click(function () {
        if ($("#cont_" + combo.id).css("display") == "block") {
            $("#cont_" + combo.id).show();
        }
        else {
            $("#cont_" + combo.id).hide();
            $(".master_cont").hide();
        }

        return false;
    });

    $("#master_cont_" + combo.id).click(function () {
        if ($("#master_cont_" + combo.id).css("display") == "block") {
            $(".master_cont").hide();
            $("#master_cont_" + combo.id).show()
        }
        else {
            $("#master_cont_" + combo.id).hide()
        }
        return false;
    });

}

function checkLabel(contador, id) {
    if ($("#" + id + "_check_" + contador).attr("checked"))
        $("#" + id + "_check_" + contador).attr("checked", false);
    else
        $("#" + id + "_check_" + contador).attr("checked", true);

    if (contador == 0) {
        $(".comboChk_" + id + "").attr("checked", false); $("#" + id + "_check_0").attr("checked", true); $("#inf_bairro").val("Todos"); $("#emp_bairro").val("Todos");
    }

}

function checkCheck(contador, id) {
    //verifica se o navegador é Internet Explorer
    var browser = navigator.appName


    if ((browser == "Microsoft Internet Explorer")) {
        if ($("#"+id+"_check_" + contador).attr("checked")) {
            $("#" + id + "_label_" + contador).html($("#" + id + "_label_" + contador).html().replace(/checked="checked"|checked/i, '').replace("\>", " CHECKED \>"));
        }
        else {
            $("#" + id + "_label_" + contador).html($("#" + id + "_label_" + contador).html().replace(/checked="checked"|checked/i, ''));
        }
    }
    else {
        if ($("#" + id + "_check_" + contador).attr("checked")) {
            $("#" + id + "_label_" + contador).html($("#" + id + "_label_" + contador).html().replace(/checked="checked"|checked/i, ''));
        }
        else {
            $("#" + id + "_label_" + contador).html($("#l" + id + "_abel_" + contador).html().replace(/checked="checked"|checked/i, '').replace("\>", " CHECKED \>"));
        }
    }

    

    if (contador == 0) {
        $(".comboChk_" + id + "").attr("checked", false); $("#check_0").attr("checked", true); $("#inf_bairro").val("Todos"); $("#emp_bairro").val("Todos");
    }

}

function addNewItemChk(parametros) {
    if (parametros.value == "indice")
        $("#cont_" + parametros.combo.id).append("<br clear='all'><br clear='all'><div style='text-align:center; font-weight:bold; background:#ccc; padding:2px; width:95%'>" + parametros.label + "</div><br clear='all'>");
    else
        $("#cont_" + parametros.combo.id).append("<div style='color:#000; float:left; margin:3px; cursor:default; width: 295px;  background:#fff' id='" + parametros.combo.id + "_label_" + parametros.combo.contador + "' onclick='checkLabel(\"" + parametros.combo.contador + "\",\"" + parametros.combo.id + "\"); clickChk(\"" + parametros.combo.id + "\");' onmouseover=\"$(this).css(\'background-color\', \'#3399FF\'); $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'background-color\', \'#FFF\'); $(this).css(\'color\', \'#000\'); \"><input onclick='checkCheck(\"" + parametros.combo.contador + "\",\"" + parametros.combo.id + "\"); clickChk(\"" + parametros.combo.id + "\");' id='" + parametros.combo.id + "_check_" + parametros.combo.contador + "' type=checkbox  class='comboChk_" + parametros.combo.id + "' value=" + rawurlencode(parametros.label) + " " + (parametros.checked == true ? "CHECKED" : "") + " /><input type='text' style='background:transparent; border:none; cursor:default; width:260px; height:100%;  ' readonly='readonly' value=\"" + replaceAll(parametros.label, " ", "&nbsp;") + (typeof (parametros.total) == "undefined" ? "" : " (" + parametros.total + ")") + "\"  onmouseover=\" $(this).css(\'color\', \'#FFF\');\" onmouseout=\"$(this).css(\'color\', \'#000\'); \"  /></div>");

    parametros.combo.contador++;
}

function selectItem(bairro,id) {
    $(".comboChk_" + id + "").each(function () {
        if ($(this).val() == replaceAll(bairro, " ", "+"))
            $(this).attr("checked", true);
    });
}


function ComboCheckBox(parametros) {
    // Variaveis de controle
    this.destino = parametros.destino;
    this.id = parametros.id;
    this.width = parametros.width;
    this.height = parametros.height;
    this.background = parametros.background;
    this.z_index = parametros.z_index;
    this.input_width = parametros.input_width;
    this.cont_width = parametros.cont_width;
    this.valueInicial = parametros.valueInicial;
    this.contador = 0;

    // Métodos
    this.printChk = printChk;
    this.addNewItemChk = addNewItemChk;
    this.selectItem = selectItem;
}       

