var map;
$(function(){
    var base_lat = 49.38237278700955;
    var base_lon = 6.04248046875;
    var base_zoom = 6;
    try {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(base_lat, base_lon), base_zoom);
            map.addControl(new GSmallMapControl());
        }
    } catch (err){
        ;//if GMap isn't available, other scripts work fine
    }
    // search switcher 
    var loc = document.location.toString();
    var checked = 0;
    $('input:radio[name=action]').each(function(){
        $(this).click(function(){document.location = $(this).val()});
        var position = loc.search($(this).val());
        if (position >= 0){
            $(this).attr('checked','checked');
            checked++;
        } else {
            $(this).attr('checked','');
        }
    });
    if (checked == 0){
        $("input:radio[value="+button_url+"]").attr('checked','checked');
    };
    // map controls 
    function restoreMapButtons(){
        $(".map_button").each(function(){
            $(this).find('img').attr(
                'src',
                $(this).find('img').attr('src').replace('map-on.png','map-off.png')
            );  
        });
    };
    function set_marker(obj){
        var zoom = 16;
        map.clearOverlays();
        restoreMapButtons();
        if (obj.attr('name') == undefined) { /*console.log("Function 'set_marker': no such element with attribute 'name'"); */return false; };
        var lat = obj.attr('name').split(',')[0];
        var lon = obj.attr('name').split(',')[1];
        if ((lat == 'None') || (lon == 'None')){ //if latitude+longitude is empty
            lat = base_lat;
            lon = base_lon;
            zoom = base_zoom;
        }
        var point = new GLatLng(lat, lon);

        var marker_icon = new GIcon();
		marker_icon.image = '/media/images/marker.png';
		marker_icon.iconSize = new GSize(22, 25);
		marker_icon.iconAnchor = new GPoint(0, 25);
		marker_icon.infoWindowAnchor = new GPoint(5, 1);

        var marker = new GMarker(point, { icon: marker_icon });
        map.setCenter(point, zoom);
        if (zoom == base_zoom) return false; //if no latitude+longitude then show only map
        map.addOverlay(marker);
        map.panTo(point);
        obj.find('img').attr(
            'src',
            obj.find('img').attr('src').replace('map-off.png','map-on.png')
        );
        return false;
    }
    $('.map_button').click(function(){
        return set_marker($(this));
    });
    // click and unclick object 
    $('a[id^="click_link"]').click(function(){
  
        if (this.href.search(del_click_url ) >= 0 ){
            $.getJSON(this.href,{},function(){});
            this.href = this.href.replace(del_click_url, add_click_url);
            $(this).find('img').attr('src', media_url + 'images/favorite.png');
            // try to detect text near "click" control and change it to opposite
            $(this).parent().find('span.click-text:first').text(gettext("Click here if it clicks!"));
            
            $(this).trigger('ajax_click_sent', ['deleted']); // custom js event for integration
        } else {
            $.getJSON(this.href,{},function(){});
            this.href = this.href.replace(add_click_url, del_click_url);
            $(this).find('img').attr('src',media_url + 'images/unfavorite.png');
            $(this).parent().find('span.click-text:first').text(gettext("Remove it out of my clicks!"));
            $(this).trigger('ajax_click_sent', ['added']); // custom js event for integration
        }
        return false;
    });
    // you need to be sure that $('.map_button:first') is not underfined!
    if ($('.map_button:first').size())
        set_marker($('.map_button:first'));
    $("#id_cuisine, #id_budget").change(function(){
       $(this).parent().parent().submit(); 
    });
});
