var Effects = {
    init: function() {
        /** promowane */
        $('#promowane a.next,#promowane a.prev').css('opacity', 0.8);
        $('#promowane a.next,#promowane a.prev').mouseover(function(){$(this).stop().animate({'opacity': 1.0})});
        $('#promowane a.next,#promowane a.prev').mouseout(function(){$(this).stop().animate({'opacity': 0.8})});
        
        /** wyszukiwarka */
        $('#search input.text').focus(function(){if(this.value=='wpisz tytuł szukanego filmu i kliknij szukaj')this.value='';});
        
        /** zamiana checkboxów **/
        jQuery.each($('#top input[type=checkbox]'), function(i, e){
            $(e).hide();
            $(e).after('<button class="checkbox"></button>');
            $(e).next().click(function(){
                this.blur();
                
                if ($(this).prev().attr('checked')) {
                    $(this).prev().attr('checked', '');
                    $(this).css('background-position', '0px 0px');
                } else {
                    $(this).prev().attr('checked', 'checked');
                    $(this).css('background-position', '0px -15px');
                }
                
                return false;
            });
            $(e).change(function(){
                if ($(this).attr('checked')) {
                    $(this).next().css('background-position', '0px -15px');
                } else {
                    $(this).next().css('background-position', '0px 0px');
                }
            });
        });
        
        /** zamiana obrazków w serialach */
        jQuery.each($('#content div.tv img.thumb'), function(i, e){
            $(e).replaceWith('<img class="thumb" src="'+baseUrl+'images/bg-tvborder.gif" style="border:0;background:url('+$(e).attr('src')+') no-repeat 0 0" />');
        });
        
        return;
        
        /** miarka */
        $('body').append('<span id="ruler" style="visibility:hidden;"></span>');
        
        jQuery.each($('#last div.film p.name'), function(i, e){
            var text = $(this).text();
            $('#ruler').text(text);
            
            if ($('#ruler').attr('offsetWidth') > 150) {
                while ($('#ruler').attr('offsetWidth') > 145) {
                    text = text.substring(0, text.length - 1);
                    $('#ruler').text(text);
                }
                
                $(this).text(text + '...');
            }
        });
        
        jQuery.each($('#polecamy_my div.film a'), function(i, e){
            var text = $(this).text();
            $('#ruler').text(text);
            
            if ($('#ruler').attr('offsetWidth') > 150) {
                while ($('#ruler').attr('offsetWidth') > 145) {
                    text = text.substring(0, text.length - 1);
                    $('#ruler').text(text);
                }
                
                $(this).text(text + '...');
            }
        });
    }
}

var Przewijacz = {
    init: function() {
        $('#last a.prev').click(function(){
            $('#last div.films').animate({'scrollLeft':'-=582'}, 800);
            this.blur();
            return false;
        });
        
        $('#last a.next').click(function(){
            $('#last div.films').animate({'scrollLeft':'+=582'}, 800);
            this.blur();
            return false;
        });
    }
}

var Promowane = {
    items: null,
    current: null,
    
    init: function() {
        Promowane.current = 1;
        Promowane.items   = $('#promowane div.film_view').length;
        $('#promowane a.next').click(Promowane.next);
        $('#promowane a.prev').click(Promowane.prev);
        $('#promowane').attr('scrollLeft', 0);
    },
    
    next: function() {
        this.blur();
        if (Promowane.current == Promowane.items) {
            Promowane.current = 1;
            $('#promowane').stop().animate({'scrollLeft':'0'}, 600);
        } else {
            Promowane.current++;
            $('#promowane').stop().animate({'scrollLeft':'+=941'}, 600);
        }
    },
    
    prev: function() {
        this.blur();
        if (Promowane.current == 1) {
            Promowane.current = Promowane.items;
            $('#promowane').stop().animate({'scrollLeft':(Promowane.items-1)*941}, 600);
        } else {
            Promowane.current--;
            $('#promowane').stop().animate({'scrollLeft':'-=941'}, 600);
        }
    }
}

var Voting = {
    init: function() {
        $('form.pole-glosowania div.glos').append('<a href="" style="width:20%;z-index:6"></a><a href="" style="width:40%;z-index:5"></a><a href="" style="width:60%;z-index:4"></a><a href="" style="width:80%;z-index:3"></a><a href="" style="width:100%;z-index:2"></a>');
        $('form.pole-glosowania div.glos a').click(function(){
            this.blur();
            $('input', $(this).parent()).val(parseInt($(this).css('width'))/20);
            $('a', $(this).parent()).removeClass('notactive');
            $(this).addClass('active');
            Voting.update();
            return false;
        });
        $('form.pole-glosowania div.glos a').mousemove(function(){
            $('a.active', $(this).parent()).addClass('notactive').removeClass('active');
        });
        $('form.pole-glosowania div.glos a').mouseout(function(){
            $('a.notactive', $(this).parent()).addClass('active').removeClass('notactive');
        });
    },
    
    update: function() {
        count = 0;
        sum   = 0;
        
        if ($('form.pole-glosowania input[name=glos-video]').val() != 0) {
            count++;
            sum += parseInt($('form.pole-glosowania input[name=glos-video]').val());
        }
        
        if ($('form.pole-glosowania input[name=glos-audio]').val() != 0) {
            count++;
            sum += parseInt($('form.pole-glosowania input[name=glos-audio]').val());
        }
        
        if ($('form.pole-glosowania input[name=glos-scenariusz]').val() != 0) {
            count++;
            sum += parseInt($('form.pole-glosowania input[name=glos-scenariusz]').val());
        }
        
        sum = parseInt(sum*10/count);
        $('form.pole-glosowania input[name=glos_oddany]').val(parseInt(sum/10) + '.' + parseInt(sum%10));
        $('form.pole-glosowania input[type=submit]').show();
    }
}

$(document).ready(function(){
    Effects.init();
    Przewijacz.init();
    Promowane.init();
    Voting.init();
    
    $('a[rel=nofollow]').attr('target', '_blank');
    $('a[rel=ext]').attr('target', '_blank');
});
