$j().ready(function () {
});

/**
* Czysci pamiec (Google Maps API)
*/
$j(function(){
    $j(window).unload(function(){
        GUnload();
    });
});

/**
* Wyswietla mape Google z wszystkimi lokalizacjami;
* zmienne 'latitude', 'longitude', 'zoom_level' i 'locations' zdefiniowane w szablonie (Google Maps API)
*/
$j(function(){
    if (($j('div#gmap').length > 0) && GBrowserIsCompatible())
    {
        var map;
        var markers = [];
        var info = $j('<div>');
        var tmp;

        map = new GMap2(document.getElementById('gmap'));
        map.setCenter(new GLatLng(latitude, longitude), zoom_level);
        map.setUIToDefault();
        $j(window).scrollTop(0);

        info.load('frontend/modules/main/templates/main.info.tpl', function(){
            $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 });

                info.find('dt strong').html(location.location_name);
                tmp = location.location_local_number ? '/' + location.location_local_number : '';
                info.find('dd.f-desc').html(location.location_street + ' ' + location.location_house_number + tmp + ', ' + location.location_postcode + ' Warszawa<br />');
                info.find('dd.f-desc').append('tel.: ' + (location.location_phone ? location.location_phone : '') + '<br />' + 'e-mail: ' + (location.location_email ? location.location_email : ''));
                tmp = location.location_www ? $j('<a>').attr('href', location.location_www).text(location.location_www) : '';
                info.find('dd.f-links').html(tmp);

                markers[i].info = info.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();
        });
    }
});

/**
* Inicjuje galerie dla zdjec z karuzeli (LightBox)
*/
$j(function(){
	$j('div.f-carousell div.list-wrap a[rel=lightbox]').lightBox({
    	overlayBgColor: '#000',
    	overlayOpacity: 0.6,
    	imageLoading: 'frontend/img/lightbox/loading.gif',
    	imageBtnClose: 'frontend/img/lightbox/close.gif',
    	imageBtnPrev: 'frontend/img/lightbox/prev.gif',
    	imageBtnNext: 'frontend/img/lightbox/next.gif',
    	containerResizeSpeed: 350,
    	txtImage: 'zdjęcie',
    	txtOf: 'z'
   });
});

/**
* Inicjuje galerie dla zdjecia miesiaca (LightBox)
*/
$j(function(){
	$j('dl.featured-photo dd.f-thumb a').lightBox({
    	overlayBgColor: '#000',
    	overlayOpacity: 0.6,
    	imageLoading: 'frontend/img/lightbox/loading.gif',
    	imageBtnClose: 'frontend/img/lightbox/close.gif',
    	containerResizeSpeed: 350
   });
});

/**
* Obsluguje mechanizm zapisywania/wypisywania sie do/z subskrypcji (AJAX)
*/
$j(function(){
    $j('form#form-newsletter').submit(function(event){
        event.preventDefault();
    });

    $j('form#form-newsletter :submit').click(function(){
        var submit = $j(this);
        var email = $j('form#form-newsletter input#email').attr('value');
        var action = submit.attr('name');

        $j.ajax({
            type: 'GET',
            url: cp + '/main/x_newsletter/' + email + '/' + action,
            data: {},
            dataType: 'json',
            async: true,
            success: function(data){
                if (data.success)
                {
                    $j('div#newsletter-message').removeClass().addClass('ok').text(data.message);
                    $j('form#form-newsletter input#email').attr('value', '');
                }
                else
                {
                    $j('div#newsletter-message').removeClass().addClass('error').text(data.message);
                }
            },
            error: function(){
                alert('Błąd!');
            },
            beforeSend: function(){
                submit.before($j('<img/>').attr('id', 'ajax-loader').attr('src', 'frontend/img/ajax-loader.gif')).css('visibility', 'hidden');
            },
            complete: function(){
                $j('img#ajax-loader').before(submit.css('visibility', 'visible')).remove();
            }
        });
    });
});

/**
* Rotuje obrazki w gornym boksie
*/
$j(function(){
    setTimeout('rotate_images()', 5000);
});

var rotate_images = function()
{
    var image_list = $j('div.header-box-left ul');

    if (image_list.find('li').length > 0)
    {
        var last_image = image_list.find('li:last');

        last_image.remove();
        image_list.prepend(last_image);

        setTimeout('rotate_images()', 5000);
    }
}