$j().ready(function () {
});

/**
* Inicjuje galerie (LightBox)
*/
$j(function(){
	$j('dd.f-thumb 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'
   });
});

/**
* Zatwierdza formularz zmiany sposobu sortowania zdjec galerii
*/
$j(function(){
    $j('select#gallery-sort').change(function(){
        $j(this).parents('form').trigger('submit');
    });
});

/**
* Obsluguje mechanizm oddawania glosow na zdjecia (AJAX)
*/
$j(function(){
    var star_width = 16;

    $j('dd.f-rating span').mouseover(function(event){
        var tmp_rate = Math.ceil((event.pageX - $j(this).offset().left) / star_width);
        $j(this).removeClass().addClass('rate-' + tmp_rate);
    });

    $j('dd.f-rating span').mousemove(function(event){
        var tmp_rate = Math.ceil((event.pageX - $j(this).offset().left) / star_width);
        $j(this).removeClass().addClass('rate-' + tmp_rate);
    });

    $j('dd.f-rating span').mouseout(function(){
        $j(this).removeClass().addClass('rate-' + $j(this).text());
    });

    $j('dd.f-rating span').click(function(event){
        var span = $j(this);
        var rate = Math.ceil((event.pageX - $j(this).offset().left) / star_width);
        var picture_id = $j(this).parents('li').attr('id').substr(8);

        $j.ajax({
            type: 'GET',
            url: cp + '/gallery/x_rate_picture/' + picture_id + '/' + rate,
            data: {},
            dataType: 'json',
            async: true,
            success: function(data){
                if (data.success)
                {
                    var current_rate = Math.round(data.average_rate);
                    span.parents('dd.f-rating').fadeOut(function(){
                        span.removeClass().addClass('rate-' + current_rate).text(current_rate);
                        span.next('em').text(data.average_rate).next('em').text(data.total_votes);
                    }).fadeIn();
                }
                else
                {
                    alert(data.message);
                }
            },
            error: function() {
                alert('Błąd!');
            }
        });
    });
});

/**
* Usuwa zdjecie (AJAX)
*/
$j(function(){
    $j('a.picture-remove').click(function(event){
        event.preventDefault();
        var li = $j(this).parents('li');
        var picture_id = li.attr('id').substr(8);

        if (confirm('Czy na pewno usunąć zdjęcie?'))
        {
            $j.ajax({
                type: 'GET',
                url: cp + '/gallery/x_remove_picture/' + picture_id,
                data: {},
                dataType: 'json',
                async: true,
                success: function(data){
                    if (data.success)
                    {
                        li.fadeOut(function(){
                            li.remove();
                        });
                    }
                    else
                    {
                        alert(data.message);
                    }
                },
                error: function() {
                    alert('Błąd!');
                }
            });
        }
    });
});
