﻿$(function() {
    home.liveboard.init();
    home.offer.init();
    home.init();
});

var home = {
    init: function() {
        $("#travelOfferArea").click(function() {
            goTo('/reizen.aspx');
        });
        
        
        $(".advisory").click(function () {
            var target = $(this).find("a").attr("href");
            goTo(target);    
        });
        
        $(".calendarItem").click(function () {
            var target= "/agenda.aspx#" + $(this).data("target");
            goTo(target);
        });
    },
    offer: {
        // Offer slider init
        init: function() {
            $('div.offerItem > div.orangeBtn').click(function() {
                goTo($('a', $(this).parent()).attr('href'));
            });

            $(".offerArrow").click(home.offer.move);
        },
        // Move the slider to left or right
        move: function() {
            var container = null;
            var itemWidth = 238;
            var right = false;
            var left = false;
            var move = 0;
            var offset = 0;
            var hiddenLeftSide = 0;
            var visibleItems = 4;
            var totalItems = 0;

            if ($(this).hasClass("offerArrowRight")) {
                container = $(this).prev();
                right = true;
                left = false;
                move = itemWidth;
            }
            else {
                container = $(this).next();
                right = false;
                left = true;
                move = -itemWidth;
            }

            offset = container.scrollLeft();
            hiddenLeftSide = Math.floor(offset / itemWidth);
            totalItems = container.find(".offerItem").length;

            // If moving, return false
            if (container.data("moving") === true) {
                return false;
            }

            // Of not offsetLeft = 0, and not last item visible yet
            if ((((visibleItems + hiddenLeftSide) < totalItems) && right)
                || ((offset + move) >= 0 && left)
            ) {
                // Move + set moving = true
                container.data("moving", true);
                container.animate({ scrollLeft: offset + move }, 500, function() {
                    container.data("moving", false);
                });
            }
        }
    },
    liveboard: {
        init: function() {
            // Init the liveboard

            // Add navigation
            $("#lbSlides li").each(function(i) {
                var li = $("<li />").click(home.liveboard.navClick);
                if (i == 0) {
                    li.addClass("selected");
                }

                $("#lbNav").append(li);
            });

            // Hide all but first
            $("#lbSlides li:eq(0)").nextAll().hide();

            // set timer
            home.liveboard.setTimer();
            
            $("#lbSlides li").unbind("click");
            $("#lbSlides li").click(function() {
                var target = $(this).find("a").attr("href");
                goTo(target);
            });
        },
        navClick: function(e) {
            e.stopPropagation();

            var index = $(this).index();
            var oldIndex = $("#lbNav li.selected").index();

            home.liveboard.select(index, oldIndex);
        },
        // Reset timer
        resetTimer: function() {
            clearTimeout($("#liveboard").data("timer"));
        },
        // Set the timer
        setTimer: function() {
            $("#liveboard").data("timer", setTimeout(home.liveboard.tick, home.liveboard.settings.time));
        },
        // Timer tick
        tick: function() {
            var index = $("#lbNav li.selected").index();
            var oldIndex = index;
            index++;

            home.liveboard.select(index, oldIndex);
        },
        select: function(index, oldIndex) {
            home.liveboard.resetTimer();
            if (index == $("#lbNav li").length)
                index = 0;

            $("#lbNav li.selected").removeClass("selected");
            $("#lbNav li").eq(index).addClass("selected");

            $("#lbSlides li").eq(oldIndex).fadeOut();
            $("#lbSlides li").eq(index).fadeIn();

            // set timer
            home.liveboard.setTimer();
        },
        // Liveboard settings
        settings: {
            time: 5000
        }
    }
};

