$j().ready(function () {
});

var map;
var markers = [];

/**
* Czysci pamiec (Google Maps API)
*/
$j(function(){
    $j(window).unload(function(){
        GUnload();
    });
});

/**
* Wyswietla mape Google z wszystkimi lokalizacjami w danej kategorii;
* zmienne 'latitude', 'longitude', 'zoom_level' i 'locations' zdefiniowane w szablonie;
* zmienne 'map' i 'markers' zadeklarowane globalnie (Google Maps API)
*/
$j(function(){
    if (($j('div#gmap').length > 0) && GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById('gmap'));
        map.setCenter(new GLatLng(latitude, longitude), zoom_level);
        map.setUIToDefault();
        $j(window).scrollTop(0);

        $j.each(locations, function(i, location){
            var icon = new GIcon();
            icon.image = 'upload/icons/' + location.icon_file_name;
            icon.iconSize = new GSize(location.icon_width, location.icon_height);
            icon.iconAnchor = new GPoint(location.icon_width / 2, location.icon_height / 2);
            icon.infoWindowAnchor = new GPoint(location.icon_width / 2, location.icon_height / 2);

            markers[i] = new GMarker(new GLatLng(location.location_latitude, location.location_longitude), { icon: icon });

            markers[i].info = $j('div#location-' + location.location_id + ' dl.p-meta').html();

            GEvent.addListener(markers[i], 'click', function() {
                this.openInfoWindowHtml(this.info);
            });
        });

        var marker_manager = new MarkerManager(map);
        marker_manager.addMarkers(markers, 1);
        marker_manager.refresh();
    }
});

/**
* Wyswietla na mapie chmurke z opisem dla lokalizacji po kliknieciu jej nazwy na liscie likalizacji (Google Maps API)
*/
$j(function(){
    if (typeof(map) != 'undefined')
    {
        $j('div.pos dt a').click(function(event){
            event.preventDefault();
            GEvent.trigger(markers[$j('div.pos dt a').index($j(this))], 'click');
            $j(window).scrollTop(0);
        });
    }
});

/**
* Wysuwa i ukrywa pelny opis lokalizacji
*/
$j(function(){
    $j('img.icon-slide-down').click(function(){
        $j(this).nextAll('div.desc-full').show();
        $j(this).hide().next('img.icon-slide-up').show();
    });

    $j('img.icon-slide-up').click(function(){
        $j(this).next('div.desc-full').hide();
        $j(this).hide().prev('img.icon-slide-down').show();
    });
});
