

$(function(){

    if ($(".splash-screen").length > 0){
        $(window).scroll(function(){
            var scrollTop = $(window).scrollTop();
            var scrollLimit = 10;

            if (scrollTop >= scrollLimit) {
                $("body").addClass("header-fixed");
            } else {
                $("body").removeClass("header-fixed");
            }
        });
    }

    $("#menu-toggle").click(function(){
        $(this).toggleClass('on');
        return false;
    });
    
    $(".menu a, #mobile-menu a").click(function(){
        if ($(this).parent().hasClass("active")){
            $('html, body').animate({
                scrollTop: $(".container-raffle").offset().top - $(".header").height()
            }, 300, function(){
                $('html, body').animate({
                    scrollTop: $(".container-raffle").offset().top - $(".header").height()
                }, 100);
            });
            
            $("#menu-toggle").removeClass('on');
            
            return false;
        }
    });
    
//    var isOrderPage = (window.location.pathname === '/' || (new RegExp(/^\/s\/[a-zA-Z0-9-_]+/)).test(window.location.pathname));
//    
//    if (isOrderPage){
//        $(".lottery-order-button, .lottery-header-order-button, .lottery-menu-order-button").click(function(){
//            if ($(this).hasClass("lottery-menu-order-button")){
//                $("#menu-toggle").toggleClass('on');
//            }
//            $('html, body').animate({scrollTop: $("#order").offset().top}, 500);
//            return false;
//        });
//        
//        if (window.location.hash === "#order"){
//            $('html, body').animate({scrollTop: $("#order").offset().top}, 500);
//        }
//    } else {
//        $('html, body').scrollTop($("#order").offset().top);
//    }
    
    $(".splash-screen").css('opacity', 1.0);
    
    $(".share-link").click(function(){
        
        var href = $(this).attr('href');
        var provider = href.substr(href.lastIndexOf("/") + 1);

        if (provider === 'link'){
            
            swal({
                title: lotteryNexus.translations.shareLink.wait
            });
            
            $.ajax({
                url: $(this).attr('href'),
                success: function(sharingCode){
                    var url = lotteryNexus.socialSharing.url + sharingCode;
                    swal({
                        title: lotteryNexus.translations.shareLink.title,
                        text: lotteryNexus.translations.shareLink.text + 
                                '<div class="row m-t-xs">' + 
                                '   <div class="col-md-12">' + 
                                '       <input type="text" id="sharing-link-copy-text" class="form-control" readonly="readonly" style="display: inline; height: 40px; width: 95%; margin: 0px 10px;" value="'+ url +'">' + 
                                '   </div>' + 
                                '</div>',
                        html: true,
                        allowOutsideClick: true,
                        confirmButtonText: lotteryNexus.translations.shareLink.button,
                        showCancelButton: false,
                        showConfirmButton: true
                    }, function(res){
                        var copyTextarea = document.querySelector('#sharing-link-copy-text');
                        copyTextarea.select();

                        try {
                            var copied = document.execCommand('copy');
                            if (copied){
                                lotteryNexus.notification.show('info',lotteryNexus.translations.shareLink.success);
                            }
                        } catch (err) {
                            console.log('Oops, unable to copy');
                        }
                    });
                }
            });
            
        } else {
            var w = 500;
            var h = 400;

            var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
            var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;

            var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
            var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

            var left = ((width / 2) - (w / 2)) + dualScreenLeft;
            var top = ((height / 2) - (h / 2)) + dualScreenTop;
            var newWindow = window.open($(this).attr('href'), 'Share', 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

            if (window.focus) {
                newWindow.focus();
            }
        }
        
        return false;
    });

    lotteryNexus.slider.init();
    lotteryNexus.prizePhotoSlider.init();
});

lotteryNexus.prizePhotoSlider = {
    
    sliders: [],
    speed: 4000,
    currentSlider: null,
    
    init: function(){
        
        $(document.body).append(
            '<div class="prize-image-lightbox">\n\
                <div class="prize-image-lightbox-overlap"></div>\n\
                <img class="prize-image-lightbox-full">\n\
                <img class="prize-image-lightbox-loader" src="' + ((lotteryNexus.themes &&
                                lotteryNexus.themes.current &&
                                lotteryNexus.themes.current.loaderImage) ? 
                                lotteryNexus.themes.current.loaderImage :
                                '/images/loading-bars.svg'
                            ) +'">\n\
                <div class="prize-image-lightbox-html"></div>\n\
                <i class="fa fa-close prize-image-lightbox-close"></i>\n\
                <i class="fa fa-chevron-right prize-image-lightbox-next"></i>\n\
                <i class="fa fa-chevron-left prize-image-lightbox-prev"></i>\n\
            </div>');
        
        $(".prize-image-lightbox").click(function(){
            $(".prize-image-lightbox").hide().css("opacity", 0.0);
        });
        
        $(".prize-image-lightbox-next").click(function(){
            lotteryNexus.prizePhotoSlider.nextLightbox();
            return false;
        });
        $(".prize-image-lightbox-prev").click(function(){
            lotteryNexus.prizePhotoSlider.prevLightbox();
            return false;
        });
        
        $(".prize-image-lightbox-full").load(function(){
            $(".prize-image-lightbox").addClass("prize-image-lightbox-loaded");
        });
                
        $(".prize-image-wrapper").each(function(){
            
            if ($(this).attr('data-photos')){
                var slider = {
                    parent: $(this),
                    photos: JSON.parse($(this).attr('data-photos')),
                    currentIndex: 0,
                    lightboxCurrentIndex: 0,
                    timerID: null,
                    canShowLightbox: false,
                    started: false
                };
                lotteryNexus.prizePhotoSlider.sliders.push(slider);

                var navigationHtml = '<ul class="prize-image-slider-navigation">';
                var fullSizeCounter = 0;
                var photoCounter = 0;
                for (var j = 0; j < slider.photos.length; j++){
                    $('<img/>')[0].src = slider.photos[j].preview;
                    navigationHtml += '<li data-slide-inx="'+ j +'" class="'+ (j === 0 ? "active" : "") +'"></li>';
                    if (slider.photos[j].preview){
                        photoCounter++;
                    }
                    if (slider.photos[j].full){
                        fullSizeCounter++;
                    }
                }
                slider.canShowLightbox = fullSizeCounter === photoCounter;
                
                slider.parent.append('<div class="prize-image-slider-html"></div>');
                if (slider.photos.length > 1){
                    slider.parent.append(navigationHtml);
                }
                
                slider.parent.click(function(){
                    lotteryNexus.prizePhotoSlider.currentSlider = slider;
                    lotteryNexus.prizePhotoSlider.lightbox(slider, slider.currentIndex);
                    return false;
                });
                slider.parent.find(".prize-image-slider-navigation li").click(function(){
                    lotteryNexus.prizePhotoSlider.showSlide(slider, parseInt($(this).attr('data-slide-inx')));
                    return false;
                });
            
                if (slider.canShowLightbox){
                    slider.parent.css("cursor", "pointer");
                }
            }
        });
        
        $(window).on('scroll.prizePhotoSlider', lotteryNexus.prizePhotoSlider.windowScroll);
    },
    
    windowScroll: function(){
        var startedCounter = 0;
        lotteryNexus.prizePhotoSlider.sliders.forEach(function(slider){
            //slider = lotteryNexus.prizePhotoSlider.sliders[i];
            if (!slider.started){
                if (lotteryNexus.prizePhotoSlider.isScrolledIntoView(slider)){
                    slider.started = true;
                    startedCounter++;
                    slider.timerID = setTimeout(function(){
                        lotteryNexus.prizePhotoSlider.next(slider);
                    }, lotteryNexus.prizePhotoSlider.speed);
                }
            } else {
                startedCounter++;
            }
        });
        
        if (startedCounter === lotteryNexus.prizePhotoSlider.sliders.length){
            $(window).off('scroll.prizePhotoSlider');
        }
    },
    
    showSlide: function(slider, inx){
        clearTimeout(slider.timerID);
        
        slider.currentIndex = inx;
        if (slider.photos[inx].html){
            $(slider.parent).find(".prize-image-slider-html").html(slider.photos[inx].html);
        } else {
            $(slider.parent).css({'backgroundImage': 'url('+ slider.photos[inx].preview +')'});
            $(slider.parent).find(".prize-image-slider-html").html('');
        }
        $(slider.parent).find(".prize-image-slider-navigation li").removeClass("active");
        $($(slider.parent).find(".prize-image-slider-navigation li").get(inx)).addClass("active");
        
        if (slider.photos.length > 1){
            slider.timerID = setTimeout(function(){
                lotteryNexus.prizePhotoSlider.next(slider);
            }, lotteryNexus.prizePhotoSlider.speed);
        }
    },
    
    next: function(slider){
        if (slider.currentIndex >= slider.photos.length - 1){
            lotteryNexus.prizePhotoSlider.showSlide(slider, 0);
        } else {
            lotteryNexus.prizePhotoSlider.showSlide(slider, slider.currentIndex + 1);
        }
    },
    
    prev: function(slider){
        if (slider.currentIndex <= 0){
            lotteryNexus.prizePhotoSlider.showSlide(slider, slider.photos.length - 1);
        } else {
            lotteryNexus.prizePhotoSlider.showSlide(slider, slider.currentIndex - 1);
        }
    },
    
    lightbox: function(slider, inx){
        if (slider.canShowLightbox){
            $(".prize-image-lightbox").show().css("opacity", 1.0);
            if (slider.photos.length > 1){
                $(".prize-image-lightbox-next, .prize-image-lightbox-prev").show();
            } else {
                $(".prize-image-lightbox-next, .prize-image-lightbox-prev").hide();
            }
            
            $(".prize-image-lightbox").removeClass("prize-image-lightbox-loaded");
            
            if (slider.photos[inx].html){
                $(".prize-image-lightbox-html").show();
                $(".prize-image-lightbox-full").hide();
                $(".prize-image-lightbox-html").html(slider.photos[inx].html);
            } else {
                $(".prize-image-lightbox-full").show();
                $(".prize-image-lightbox-html").hide();
                $(".prize-image-lightbox-full").attr('src', slider.photos[inx].full);
            }
            slider.lightboxCurrentIndex = inx;
        }
    },
    
    nextLightbox: function(){
        var slider = lotteryNexus.prizePhotoSlider.currentSlider;
        if (slider.lightboxCurrentIndex >= slider.photos.length - 1){
            lotteryNexus.prizePhotoSlider.lightbox(slider, 0);
        } else {
            lotteryNexus.prizePhotoSlider.lightbox(slider, slider.lightboxCurrentIndex + 1);
        }
    },
    
    prevLightbox: function(){
        var slider = lotteryNexus.prizePhotoSlider.currentSlider;
        if (slider.lightboxCurrentIndex <= 0){
            lotteryNexus.prizePhotoSlider.lightbox(slider, slider.photos.length - 1);
        } else {
            lotteryNexus.prizePhotoSlider.lightbox(slider, slider.lightboxCurrentIndex - 1);
        }
    },
    
    isScrolledIntoView: function(slider){
        var rect = slider.parent.get(0).getBoundingClientRect()
        return (rect.top >= 0 && rect.left >= 0 && rect.top <= (window.innerHeight || document.documentElement.clientHeight));
    }
};


lotteryNexus.slider = {
    
    slides: [],
    currentIndex: 0,
    timerID: null,
    
    init: function(){
        
        $(".splash-inner-slider").append('<ul class="slider-navigation">');
        
        var navigationHtml = '';
        var sliderInterval = $(".splash-inner-slider").data('slide-time') ? 
                            parseFloat($(".splash-inner-slider").data('slide-time')) * 1000 
                            : 5000
        
        $(".splash-inner-slider .splash-inner-slide").each(function(inx){
            var slide = {
                'element': $(this),
                'title': $.trim($(this).find("h2").text())
            };
            
            navigationHtml += '<li data-slide-inx="'+ inx +'" class="'+ (inx === 0 ? "active" : "") +'"></li>';
            
            lotteryNexus.slider.slides.push(slide);
        });
        
        if (lotteryNexus.slider.slides.length > 1){
            $(".slider-navigation").html(navigationHtml);
            $(".slider-navigation li").click(function(){
                lotteryNexus.slider.showSlide(parseInt($(this).attr('data-slide-inx')));
                return false;
            });
        }
        
        $(window).resize(lotteryNexus.slider.windowResize);
        
        lotteryNexus.slider.windowResize();
        
        lotteryNexus.slider.showSlide(0);
        lotteryNexus.slider.timerID = setTimeout(lotteryNexus.slider.next, sliderInterval);
    },
    
    windowResize: function(){
        if ($(".splash-inner-details-wrapper").outerHeight() > $(".splash-inner-slider > *").height()){
            $(".splash-inner").addClass("splash-inner-margin");
        } else {
            $(".splash-inner").removeClass("splash-inner-margin");
        }
    },
    
    showSlide: function(inx){
        clearTimeout(lotteryNexus.slider.timerID);
        
        for (var i = 0; i < lotteryNexus.slider.slides.length; i++){
            if (i === inx){
                $(lotteryNexus.slider.slides[i].element).css({opacity: 1}).css({'zIndex': 10});
            } else {
                $(lotteryNexus.slider.slides[i].element).css({opacity: 0}).css({'zIndex': 1});
            }
        }
        $(".slider-navigation li").removeClass("active");
        $($(".slider-navigation li").get(inx)).addClass("active");
        lotteryNexus.slider.currentIndex = inx;
        
        lotteryNexus.slider.timerID = setTimeout(lotteryNexus.slider.next, 7500);
    },
    
    next: function(){
        if (lotteryNexus.slider.currentIndex >= lotteryNexus.slider.slides.length - 1){
            lotteryNexus.slider.showSlide(0);
        } else {
            lotteryNexus.slider.showSlide(lotteryNexus.slider.currentIndex + 1);
        }
    },
    
    prev: function(){
        if (lotteryNexus.slider.currentIndex <= 0){
            lotteryNexus.slider.showSlide(lotteryNexus.slider.slides.length - 1);
        } else {
            lotteryNexus.slider.showSlide(lotteryNexus.slider.currentIndex - 1);
        }
    }
    
};
