﻿(function() {
    var window = this,
		document = window.document,
		$ = jQuery.noConflict(),
		view;

    view = {
        init: function() {
            var slideShowSlide = null;
            /* Handle the startpage slideShowslider. */
            $('#stepsWrap a').click(function(e) {
                clearInterval(slideShowSlide);
                this.blur();
                view.slider.slide($('.theSlideshow .container #slideImages'), $(this).parent().index());
            });

            /* Start the startpage slideShowslider-interval. */
            if ($('.theSlideshow .container #slideImages img').length > 1) {
                $('.theSlideshow .container #slideImages').width($('.theSlideshow .container #slideImages img').length * 638); //$('.theSlideshow .container #slideImages img').get(0).width);
                slideShowSlide = setInterval(function() { view.slider.slide($('.theSlideshow .container #slideImages'), (view.slider.index++), 'slideShowLeft') }, $("#JSBridge #SliderSpeedslideShow").val());
            }

            $('.UnselectedPagingItem[title~=sidan]').remove();
        },

        slider: {
            index: 0,
            /* Used to remember the last left-position in the sliders */
            slideShowLeft: 0,

            fade: function(sender, number) {
                var next = ($(sender).children(':visible').next().length > 0 ? $(sender).children(':visible').next() : $(sender).children().get(0));

                $(sender).children(':visible').fadeOut(500, function() {
                    $(next).fadeIn(500);
                });
            },

            /*
            function: view.slider.slide()
            param: sender = the element that executes the event
            param: number = the number of the element that should be selected.
            Changes the image to the next/previous in the startpage slideShowslider. 
            */
            slide: function(sender, number, counter) {
                /* Get the total count of the image (div) array */
                var imageCount = $(sender).children().length,
                /* Extra addon to the calculation, needed to get crossbrowser functionality. */
					addon = 300;
                this.index = this.index == imageCount ? 0 : this.index;
                /* The slider should move to a set position. */
                if (typeof number !== 'undefined' && number !== null) {

                    /* Calculate the nLeft variable. */
                    this[counter] = ($(sender).scrollLeft() + $(sender).children(0).outerWidth()) * (number);

                    /* Fetch the active dot. */
                    var dDot = $('#stepsWrap a.active'),
                    /* Fetch the active tape. */
						dTape = $('#slideText a.active');

                    /* Inactivate the current dot. */
                    dDot.removeClass('active');
                    /* Inactivate the current tape. */
                    dTape.removeClass('active');

                    /* The set position isn't the last one. */
                    if (number != imageCount) {
                        /* Execute the right-to-left-animation */
                        $(sender).animate({ left: -this[counter] + 'px' }, 1000);
                        $(sender).siblings().children(':eq(' + number + ')').addClass('active');
                        $(sender).parent().find('#stepsWrap a:eq(' + number + ')').addClass('active');
                    }
                    else {
                        /* Execute the left-to-right-animation */
                        $(sender).animate({ left: '0px' }, 500);
                        /* Activate the first dot. */
                        $(sender).siblings().children(':first').addClass('active');
                        /* Activate the first tape. */
                        $(sender).parent().siblings().children(':first').addClass('active');
                    }

                }
                else {

                    /* Repopulate the nLeft variable based on the coming left position */
                    this[counter] = $(sender).scrollLeft() + $(sender).children(0).outerWidth() + this[counter];

                    /* Fetch the active dot. */
                    var dDot = $(sender).siblings().children('.active'),
                    /* Fetch the active tape. */
						dTape = $(sender).parent().siblings().children('.active');

                    /* Check that the new nLeft-value is lower then the full width of the wrapper. */
                    if (this[counter] + addon < $(sender).outerWidth()) {
                        /* Execute the right-to-left-animation */
                        $(sender).animate({ left: -this[counter] + 'px' }, 1000);
                        /* Inactivate the current dot. */
                        dDot.removeClass('active');
                        /* Inactivate the current tape. */
                        dTape.removeClass('active');
                        /* Activate the next dot. */
                        dDot.next().addClass('active');
                        /* Activate the next tape. */
                        dTape.next().addClass('active');
                    }
                    else {
                        /* Execute the left-to-right-animation */
                        $(sender).animate({ left: '0px' }, 1000);
                        /* Inactivate the current dot. */
                        dDot.removeClass('active');
                        /* Inactivate the current tape. */
                        dTape.removeClass('active');
                        /* Activate the first dot. */
                        $(sender).siblings().children(':first').addClass('active');
                        /* Activate the first tape. */
                        $(sender).parent().siblings().children(':first').addClass('active');
                        /* Reset the nLeft. */
                        this[counter] = 0;
                    }
                }

                /* Return and halt. */
                return false;
            }
        }

    };

    $(document).ready(function() {
        view.init();
    });

})();
