/**************************************************************
*
* 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").each(function () {
        if ($(this).attr("checked")) {
            if ($(this).val() != "Todos os Bairros") {
                if ($.trim($("#" + id).val()) == "") {
                    $("#" + id).val(rawurldecode($(this).val()));
                }
                else {
                    $("#" + id).val($("#" + id).val() + ", " + rawurldecode($(this).val()));
                }
            }
           
        }
        i++;
    });

    if(i>=2){

        if ($("#check_0").attr("checked")) {
            $("#check_0").attr("checked", false);
            $("#" + id).val($("#" + id).val().substring(18, $("#" + id).val().length));
        }
    }

    if ($.trim($("#" + id).val()) == "")
    {
        $("#" + id).val("Todos os Bairros");
        $("#check_0").attr("checked", true);
    }

}

function populaInput(id, descricao, controle) {
    $("#" + controle).val(rawurldecode(descricao));
    $("#hdn_" + controle).val(id);
    $("#master_cont_" + controle).hide()
}

function controlaOpcoes(controle) {
    if ($("#" + controle).css("display") == "none") {
        $("#" + controle).hide();
        $("#" + controle).show()
    }
    else {
        $("#" + controle).hide()
    }
}

function replaceAll(str, de, para) {
    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 + "\");' 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; margin-top:-19px; margin-left:-112px; width:250px; min-width: " + combo.cont_width + "px; top: " + cont_height + "px; max-height: 160px; border:solid 1px #CCC; background: #FFF; z-index: " + z_index_2 + "; position:relative;'>" +
                                "<div id='cont_" + combo.id + "' style='  overflow-y: auto; overflow-x:hidden;  background: #FFF; position:relative; max-height: 160px;  '>" +
                                "</div></div>");

    $("#" + combo.id).val(combo.valueInicial);

    $(document).click(function () {
        $("#master_cont_" + 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;
    });

}

function checkLabel(contador) {
    if ($("#check_" + contador).attr("checked"))
        $("#check_" + contador).attr("checked", false);
    else
        $("#check_" + contador).attr("checked", true);

    if (contador == 0) {
        $(".comboChk").attr("checked", false); $("#check_0").attr("checked", true); $("#inf_bairro").val("Todos os Bairros");
    }

}

function checkCheck(contador) {
    //verifica se o navegador é Internet Explorer
    var browser = navigator.appName


    if ((browser == "Microsoft Internet Explorer")) {
        if ($("#check_" + contador).attr("checked")) {
            $("#label_" + contador).html($("#label_" + contador).html().replace(/checked="checked"|checked/i, '').replace("\>", " CHECKED \>"));
        }
        else {
            $("#label_" + contador).html($("#label_" + contador).html().replace(/checked="checked"|checked/i, ''));
        }
    }
    else {
        if ($("#check_" + contador).attr("checked")) {
            $("#label_" + contador).html($("#label_" + contador).html().replace(/checked="checked"|checked/i, ''));
        }
        else {
            $("#label_" + contador).html($("#label_" + contador).html().replace(/checked="checked"|checked/i, '').replace("\>", " CHECKED \>"));
        }
    }

    

    if (contador == 0) {
        $(".comboChk").attr("checked", false); $("#check_0").attr("checked", true); $("#inf_bairro").val("Todos os Bairros");
    }

}

function addNewItemChk(parametros) {
    $("#cont_" + parametros.combo.id).append("<div style='height:20px; color:#000; overflow:hidden; cursor:default; width: 1000px' id='label_" + parametros.combo.contador + "' onclick='checkLabel(\"" + parametros.combo.contador + "\"); 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 + "\"); clickChk(\"" + parametros.combo.id + "\");' id='check_" + parametros.combo.contador + "' type=checkbox  class='comboChk' value=" + rawurlencode(parametros.label) + " " + (parametros.checked == true ? "CHECKED" : "") + " />" + replaceAll(parametros.label, " ", "&nbsp;") + "</div><div style=clear:both;></div>");

    parametros.combo.contador++;
}

function selectItem(bairro) {
    $(".comboChk").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;
}       

