var Effects = {
    init: function() {
        /** promowane */
        $('#promo a.next,#promo a.prev').css('opacity', 0.8);
        $('#promo a.next,#promo a.prev').mouseover(function(){$(this).stop().animate({'opacity': 1.0})});
        $('#promo a.next,#promo 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');
                }
            });
        });
        
        /** wysokość tytulow */
        jQuery.each($('#content div.movie h3'), function(i, e){
            if ($(e).height() > 36) {
                $(e).css({'height': '36px', 'overflow': 'hidden'});
            }
        });
        
        /** 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" />');
        });
        
        jQuery.each($('#submenu a'), function(i, e){
            $(e).html('<img class="bg" src="'+baseUrl+'images/bg-footer-shadow.png" /><span>'+$(e).html()+'</span>');
        });
        
        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($('#recommended 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 Scroller = {
    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 Promo = {
    items: null,
    current: null,
    
    init: function() {
        Promo.current = 1;
        Promo.items   = $('#promo div.movie').length;
        $('#promo a.next').click(Promo.next);
        $('#promo a.prev').click(Promo.prev);
        $('#promo').attr('scrollLeft', 0);
        
        $('#promo div.scroller').css('width', (Promo.items * 996) + 'px');
    },
    
    next: function() {
        this.blur();
        if (Promo.current == Promo.items) {
            Promo.current = 1;
            $('#promo').stop().animate({'scrollLeft':'0'}, 600);
        } else {
            Promo.current++;
            $('#promo').stop().animate({'scrollLeft':'+=996'}, 600);
        }
    },
    
    prev: function() {
        this.blur();
        if (Promo.current == 1) {
            Promo.current = Promo.items;
            $('#promo').stop().animate({'scrollLeft':(Promo.items-1)*996}, 600);
        } else {
            Promo.current--;
            $('#promo').stop().animate({'scrollLeft':'-=996'}, 600);
        }
    }
}

var Voting = {
    init: function() {
        $('form.user-vote div.stars').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.user-vote div.stars 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.user-vote div.stars a').mousemove(function(){
            $('a.active', $(this).parent()).addClass('notactive').removeClass('active');
        });
        $('form.user-vote div.stars a').mouseout(function(){
            $('a.notactive', $(this).parent()).addClass('active').removeClass('notactive');
        });
    },
    
    update: function() {
        count = 0;
        sum   = 0;
        
        if ($('form.user-vote input[name=vote-video]').val() != 0) {
            count++;
            sum += parseInt($('form.user-vote input[name=vote-video]').val());
        }
        
        if ($('form.user-vote input[name=vote-sound]').val() != 0) {
            count++;
            sum += parseInt($('form.user-vote input[name=vote-sound]').val());
        }
        
        if ($('form.user-vote input[name=vote-script]').val() != 0) {
            count++;
            sum += parseInt($('form.user-vote input[name=vote-script]').val());
        }
        
        sum = parseInt(sum*10/count);
        $('form.user-vote input[name=vote]').val(parseInt(sum/10) + '.' + parseInt(sum%10));
        $('form.user-vote input[type=submit]').show();
    }
}

$(document).ready(function(){
    Effects.init();
    Scroller.init();
    Promo.init();
    Voting.init();
    
    $('a[rel=nofollow]').attr('target', '_blank');
    $('a[rel=ext]').attr('target', '_blank');
});
