function init_boto_close(b) {
    
    jQuery(b).click(function(){
        jQuery('#mapa_div').hide();
        jQuery('#formcercagran').show();
        return false;
    });

    return b;
}

jQuery(document).ready(function(){

    document.habilitar_mapa = false;
    jQuery.get('/'+jQuery('#info_lang').html() + '/wijwij/ajax/load-map/',desa_marcadors);

    jQuery('#boto_mapa').click(function(){

        if(jQuery('#boto_mapa_close').size()) {
            jQuery('#mapa_div').show();
            jQuery('#formcercagran').hide();
        } else {

            p = jQuery('#formcercagran').parent();
            
            jQuery('#formcercagran').hide();

            p.append('<div id="mapa_div" style="visibility:hidden">' +
                '<div id="mapa_home"></div>' +
                '<div id="mapa_metadata"><div class="boto_mapa" id="boto_mapa_close">x</div>' +
                '<div style="height:100px;"> </div>' +
                '<div class="boto_mapa" id="boto_mapa_menjar"><img src="/imatges/marker-menjar.png"></div>' +
                '<div class="boto_mapa" id="boto_mapa_beure"><img src="/imatges/marker-beure.png"></div>' +
                '<div class="boto_mapa" id="boto_mapa_dormir"><img src="/imatges/marker-dormir.png"></div>' +
                '</div>' +
                '<div style="clear:both"> </div></div>');

            b = init_boto_close(jQuery('#boto_mapa_close'));
            
            jQuery('#mapa_div').css({
                opacity: 0,
                visibility: "visible"
            }).animate({
                opacity: 100
            },2000);

            document.mapa_menjar = true;
            document.mapa_beure = true;
            document.mapa_dormir = true;

            jQuery('#boto_mapa_menjar').click(manage_mapa_menjar);
            jQuery('#boto_mapa_beure').click(manage_mapa_beure);
            jQuery('#boto_mapa_dormir').click(manage_mapa_dormir);

            jQuery.cacheImages('/imatges/marker-menjar-gris.png',
                '/imatges/marker-beure-gris.png',
                '/imatges/marker-dormir-gris.png');

            init_mapa();
        }
        return false;
    });
});

function manage_mapa_menjar() {
    document.mapa_menjar = !(document.mapa_menjar);
    if(!document.mapa_menjar) {
        jQuery('#boto_mapa_menjar img').attr('src','/imatges/marker-menjar-gris.png');
        jQuery('#boto_mapa_menjar').addClass('boto_mapa_gris');
    } else {
        jQuery('#boto_mapa_menjar img').attr('src','/imatges/marker-menjar.png');
        jQuery('#boto_mapa_menjar').removeClass('boto_mapa_gris');
    }
    afegeix_marcadors(false);
}

function manage_mapa_beure() {
    document.mapa_beure = !(document.mapa_beure);
    if(!document.mapa_beure) {
        jQuery('#boto_mapa_beure img').attr('src','/imatges/marker-beure-gris.png');
        jQuery('#boto_mapa_beure').addClass('boto_mapa_gris');
    } else {
        jQuery('#boto_mapa_beure img').attr('src','/imatges/marker-beure.png');
        jQuery('#boto_mapa_beure').removeClass('boto_mapa_gris');
    }
    afegeix_marcadors(false);
}

function manage_mapa_dormir() {
    document.mapa_dormir = !(document.mapa_dormir);
    if(!document.mapa_dormir) {
        jQuery('#boto_mapa_dormir img').attr('src','/imatges/marker-dormir-gris.png');
        jQuery('#boto_mapa_dormir').addClass('boto_mapa_gris');
    } else {
        jQuery('#boto_mapa_dormir img').attr('src','/imatges/marker-dormir.png');
        jQuery('#boto_mapa_dormir').removeClass('boto_mapa_gris');
    }
    afegeix_marcadors(false);
}



function init_mapa() {

    var geopos = [38.709, -0.207];
    var tzoom = 9;

    $('#mapa_home').gmaps('init',{
        lat: geopos[0],
        lng: geopos[1],
        zoom:tzoom,
        width:400,
        height:250,
        manager: true,
        markers:[]
    });

    afegeix_marcadors();
}

function desa_marcadors(data) {
    document.marcadors = data;
    document.habilitar_mapa = true;
}

function redemana_marcadors(data) {
    desa_marcadors(data);
    afegeix_marcadors();
}

function afegeix_marcadors(ctr) {
    if(document.habilitar_mapa == false) {
        jQuery.get('/'+jQuery('info_lang').html() + '/wijwij/ajax/load-map/',redemana_marcadors);
        return;
    }

    if(typeof ctr == 'boolean') {
        first = ctr;
    } else {
        first = true;
    }

    data = document.marcadors;
    
    jQuery('#mapa_home').gmaps('clear');
    jQuery.each(data,function (index,value) {
        mks = filter_markers(value.markers);
        jQuery('#mapa_home').gmaps('add',{
            markers: mks,
            minZoom: value.minZoom,
            maxZoom: value.maxZoom,
            center: first
        });
        first = false;
    });
}

function filter_markers(mks) {
    
    ret = [];
    jQuery.each(mks,function(index,value) {
        add = true;
        if(value.wijwij == 'menjar' && !document.mapa_menjar)
            add = false;
        if(value.wijwij == 'beure' && !document.mapa_beure)
            add = false;
        if(value.wijwij == 'dormir' && !document.mapa_dormir)
            add = false;

        if(add)
            ret.push(value);
    });

    return ret;
}
