var i = 0;
$(document).ready(function() {
    //mostra a cidade na área específica, se exister
    initCity();
    //mostrar corretamente os campos a serem preenchidos conforme área
    showAreas($("#area").val());

    addAttachFile();
    $('#addField').click(function(){
        addAttachFile();
    });
    //carrega a cidade
    $("#state").change(function(){
        $.blockUI();
        $('#system').val('state');
        $('#formPost').submit();
    });
    //carrega a sub-area
    $("#area").change(function(){
		$.blockUI();
        $.ajax({
            type: "GET",
            url: "index.php?action=getSubArea&area=" + $(this).val() + "&" + (new Date().getTime()),
            beforeSend: function() {
                $("#sub_area").html("<option value=''>Loading...</option>");
            },
            success: function(resposta) {
                $("#sub_area").addOption(eval("(" + resposta + ")"), false);
				$.unblockUI();
            }
        });
        showAreas($(this).val());
    });
    //pergunta se tem certeza que deseja excluir a imagem
    $('.deleteImgJs').click(function(){
        return confirm('Are you sure you want to delete this file?');
    });

    //pop-up cidade
    $('#openCity, #openCityImg').click(function(){
        window.open('?action=add_city_post', 'citySelect', 'width=518, height=400, scrollbars=yes, top=' + (screen.height-400)/2 + ', left=' + (screen.width-518)/2 + '');
    });

    //mascara moeda
    $('.jsMoeda').keypress(function(e){
        reais(this, e);
    });
    $('.jsMoeda').keydown(function(e){
        backspace(this, e);
    });
});

function addAttachFile(){
    $('#areaFiles').append('<div id="af' + i + '" class="nfile"><input type="file" name="attached[]" class="inputFile" size="35" /> [<a href="#1" class="linkRemove" onclick="removeAttachFile(\'' + i + '\')">remove</a>]</div>');
}

function removeAttachFile(i){
    $("#af" + i).remove();
}

function showAreas(area){
    hideAll();
    switch(area){
        case 'jobs':
            $(".jobs").show();
            break;
        case 'resumes':
            $(".resumes").show();
            $(".msgObs").hide();
            break;
        case 'housing':
            $(".housing").show();
            break;
        case 'for_sale':
            $(".for_sale").show();
            break;
        case 'personal':
            $(".personal").show();
            break;
    }
    if ($('#state').val() != ''){
        $(".city").show();
    }
    if (area){
        $(".subArea").show();
    } else {
        $(".subArea").hide();
    }
}

function hideAll(){
    $(".msgObs").show();
    $(".jobs, .resumes, .housing, .for_sale, .personal, .subArea, .city").hide();
    if ($('#codigo').val() == '' && $('#erro').val() == ''){
        $(".jobs input, .resumes input, .housing input, .for_sale input, .personal input").val('');
    }
}

function addCity(vl){
    $('#city').val(vl[2]);
    $('#openCity').empty().append(vl[0] + ', ' + vl[1] + '/'+vl[3]);
}

function initCity() {
    vl = $('#jsCity').val().split('_');
    if (vl.length > 1) {
        addCity(vl);
    }
}
