var carouselPR_page = 1;
var carouselPR_max = 5;
var carouselPR_total = 0;

var Website = {
    run: function () {
    
        var menuTop = $('.headerBottom > ul > li.subMenu > a');
        if (menuTop.length)
            initMenuTop(menuTop);

        var lightbox = $('.overlayBox');
        if (lightbox.length)
            lightbox.fancybox({
                'type': 'image'
                //'title': 'Para guardar a imagem clique com o botão direito e escolha "Guardar imagem como..."'
            });

        // inicializar tabs
        var elemTab = $('.tabs');
        var tabSelected = 0;

        if (elemTab.length) {

            $(elemTab).each(function(){

                tabSelected = 0;
                // se houver algum com a classe tabSelectedInit, iniciar com esta tab visivel
                if($('.tabSelectedInit',this).length) {
                    tabSelected = $('.tabSelectedInit',this).index();
                }

                $(this).tabs({
                    selected: tabSelected
                });
                
            });
        }
        
        var elemCarousel = $('.carousel');
        if (elemCarousel.length)
            elemCarousel.jcarousel({ scroll: 1 });        

        carouselPR();
        
        var elemHighlight = $(".highlights ul li div");
        if (elemHighlight.length)
            HighlightInit(elemHighlight);

        var elSlideShow = $('.slideShow');
        if (elSlideShow.length) {
            elSlideShow.after('<div id="nav">').cycle({fx: 'fade',speed: 'fast',timeout: 5000,pager: '#nav'});
            slideShowShadow(elSlideShow);
        }

        var elDatePicker = $('.popupDatepicker');
        if (elDatePicker.length) {
            var months = ['Janeiro', 'Fevereiro', 'Mar&ccedil;o', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];
            var days = ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'S&aacute;b']
            elDatePicker.datepicker({ regional: 'pt', firstDay: '1', monthNames: months, dateFormat: 'yy-mm-dd', dayNamesMin: days });
            initDatePicker(elDatePicker);
        }

        var elemBar = $('.slider-range-min');
        if (elemBar.length) {
            elemBar.slider({ range: "min", value: 1, min: 1, max: 28, change: function (event, ui) {
                    $('.categoria ul').find('li').removeClass('selected');
                    $('.categoria ul').children(':nth-child(' + ui.value + ')').addClass('selected');
                }
            });
            initSlider(elemBar);
        }

        var elemFadeImg = $(".fadeImg");
        if (elemFadeImg.length) {
            initImageOpacityHover(elemFadeImg);
        }

        var elemEpisodios = $('.episodeList li');
        if (elemEpisodios.length) {
            initEpisodios(elemEpisodios);
        }
				
        var elemScroll = $('#pane1,#pane2');
        if (elemScroll.length) {
                $('#pane1,#pane2').jScrollPane({animateInterval:1,animateStep:1});
        }                
    }
};

//Initialize
$(document).ready(function(){
    center_images();
	Website.run();
});

function center_images() {
	$('.box_foto img').load(function() {
            var img_w = $(this).width();
            var img_h = $(this).height();
            var wrapper_w = $(this).closest('.box_foto').width();
            var wrapper_h = $(this).closest('.box_foto').height();

            $(this).css({
                'margin-top' : ((wrapper_h-img_h)/2)+'px'
            });
    });
}

function carouselPR() {
    carouselPR_page = 1;
    var elemCarousel = $('.carouselPR');
    if (elemCarousel.length) {
        carouselPR_total = $('li',elemCarousel).length;
        $('li',elemCarousel).hide().slice(0,carouselPR_max).show();
        
        $('li',elemCarousel).hover(function() {
            $('.shadow', this).slideDown();
        }, function() {
            $('.shadow', this).slideUp();
        });
        
        $('.carouselPR-next').click(function(e) {
            if(carouselPR_page+carouselPR_max <= carouselPR_total) {
                carouselPR_page++;
                $('li',elemCarousel).hide().slice(carouselPR_page-1,carouselPR_page+carouselPR_max).show();
            }
            e.preventDefault();
        });
        
        $('.carouselPR-prev').click(function(e) {
            if(carouselPR_page > 1) {
                carouselPR_page--;
                $('li',elemCarousel).hide().slice(carouselPR_page-1,carouselPR_page+carouselPR_max).show();
            }
            e.preventDefault();
        });
    }  
}