﻿jQuery(document).ready(function() {
    var slideWidth = 721;
    var slideTime = 12000;
    var pause = 0;
    var slideshowSelected = 1;
    var currentConstructionPosition = 0;
    var constructionSlides = jQuery('#ConstructionSlideshow .slide');
    var constructionMenus = jQuery('#ConstructionSlideshow .sliderMenuDiv');
    var numberOfConstructionSlides = constructionSlides.length;

    // Remove scrollbar in JS
    jQuery('.slidesContainer').css('overflow', 'hidden');

    constructionSlides
    .wrapAll('<div id="slideInnerConstruction"></div>')
    // Float left to display horizontally, readjust .constructionSlides width
    	.css({
    	    'float': 'left',
    	    'width': slideWidth
    	});

    // Set #slideInnerConstruction width equal to total width of all constructionSlides
    jQuery('#slideInnerConstruction').css('width', slideWidth * numberOfConstructionSlides);

    // Insert Button controls in the DOM
    jQuery('#slideshow')
    .prepend('<span class="slideButtonControl" id="leftControl">Clicking moves left</span>')
    .append('<span class="slideButtonControl" id="rightControl">Clicking moves right</span>');

    // Create event listeners for .slideshow hover
    jQuery('#slideshow').hover(
         function() {
             pause = 1;
         },
         function() {
             pause = 0;
         }
     );

    // Create event listeners for .slideButtonControl clicks
    jQuery('.slideButtonControl')
        .bind('click', function() {
            if (slideshowSelected == 1) {
                // Determine new position
                currentConstructionPosition = (jQuery(this).attr('id') == 'rightControl') ? currentConstructionPosition + 1 : currentConstructionPosition - 1;
                //Move the slideshow
                slideConstructionAlong();
            }
        });

    // Create event listeners for .sliderMenuDiv click
    jQuery('.sliderMenuDiv')
        .bind('click', function() {
            var idArr = jQuery(this).attr('id').split('_');
            var index = idArr[idArr.length - 1];
            if (slideshowSelected == 1) {
                // Determine new position
                currentConstructionPosition = parseInt(index);
                //Move the slideshow
                slideConstructionAlong();
            }
        });


    // Rotation  and Timing Event
    autoPlay = function() {
        play = setInterval(autoSlide, slideTime);
    };

    // Highlight the selected menu item
    unselectSelectConstructionMenu();

    // Run function on launch
    autoPlay();

    function autoSlide() {
        if (pause == 0) {
            if (slideshowSelected == 1) {
                currentConstructionPosition = currentConstructionPosition + 1;
                slideConstructionAlong();
            }
        }
    }

    // --*** BEGIN ConstructionSlide Specific code **--    
    // Move slideInnerConstruction using margin-left
    function slideConstructionAlong() {
        p = (currentConstructionPosition * slideWidth * -1);
        jQuery('#slideInnerConstruction').animate(
    	        { marginLeft: p },
    	        { queue: false, duration: 500, complete: adjustConstruction }
             );
    }

    function adjustConstruction() {
        if (currentConstructionPosition == numberOfConstructionSlides) currentConstructionPosition = 0;
        if (currentConstructionPosition < 0) currentConstructionPosition = numberOfConstructionSlides - 1;
        jQuery("#slideInnerConstruction").css("margin-left", (currentConstructionPosition * slideWidth * -1));
        // Hide / show controls
        unselectSelectConstructionMenu();
    };

    function unselectSelectConstructionMenu() {
        jQuery('.sliderMenuDiv').removeClass('selected').addClass('unselected');
        jQuery('#ConstructionMnu_' + currentConstructionPosition).removeClass('unselected').addClass('selected');
    }
    // --*** END ConstructionSlide Specific code **--
});

