/**********
 * @file
 * Shift Slider script.js file.
 *
 * Written by David Reagan (david@davidreagan.net) as part of his
 * job as a Student Worker at Lane Community College, Eugene, Oregon
 * in July 2010.
 **********/



$('document').ready(function(){//the document is ready. Run the script.
    /**
     * User controled Variables.
     **/
    //Control image locations. Ignore this unless you are doing something
    //fancy with the buttons, or are changing their location.
    slideShifterPlayImage = 'articles/slideShifterImages/play.png';
    slideShifterPauseImage = 'articles/slideShifterImages/pause.png';
    slideShifterNextImage = 'articles/slideShifterImages/next.png';
    slideShifterPrevImage = 'articles/slideShifterImages/prev.png';

    //set how many milliseconds to wait before shifting
    //This is the only thing users need to change.
    timeInterval = $('#slideShifterTimeInterval').attr('value');

    /********
     * Deal with showing and hiding the controls
     *******/
     if($('#slideShifterShowHideControls').attr('value')=='true')
     {
        $('#slideShifterControls').hide();//Hide the controls so they aren't in the way.

        //Get the coordinates of the #slideShifterContainer.
        areaLeft = $('#slideShifterContainer').offset().left;
        areaTop = $('#slideShifterContainer').offset().top;
        areaWidth = parseInt($('#slideShifterContainer').css('width'));
        areaHeight = parseInt($('#slideShifterContainer').css('height'));
        areaRight = areaLeft + areaWidth;
        areaBottom = areaTop + areaHeight;
        //Watch where the mouse is
        $(document).bind('mousemove',function(e){
            //In in the slideShifterContainer show the controls
            if(e.pageX>areaLeft && e.pageX<areaRight && e.pageY>areaTop && e.pageY<areaBottom)
            {
                $('#slideShifterControls').fadeIn('fast');
            }
            //if not in the slideShifterContainer, hide the controls
            if(e.pageX<areaLeft || e.pageX>areaRight || e.pageY<areaTop || e.pageY>areaBottom)
            {
                $('#slideShifterControls').fadeOut('fast');
            }
        });//End of the code to control the controls.
    }

    //Set the images for the control buttons to what the user set above.
    $('#slideShifterPauseplayButton').attr('src', slideShifterPauseImage);
    $('#slideShifterNextButton').attr('src', slideShifterNextImage);
    $('#slideShifterPrevButton').attr('src', slideShifterPrevImage);

    //Get the position of the slideShifterViewport so we can place the alides.
    slideShifterViewportLeft = $('#slideShifterViewport').offset().left + 1;
    slideShifterViewportTop = $('#slideShifterViewport').offset().top + 1;
    //Initialize variables to control which slides to use.
    firstslideShifterSlide = 1; //the integer id of the first slide. Should always be 1.
    lastslideShifterSlide = $('.slideShifterSlide').length; //count the number of divs with class .slideShifterSlide,
                                  //use that number for the last slide id.
    //start slideShifter at a random slide.
    //Math.floor to get an int, Math.random times lastslide + 1 to choose between
    //1 and the number of slides.
    if($('#slideShifterIsRandom').attr('value')=='true')
        currentslideShifterSlide = Math.floor(Math.random()*lastslideShifterSlide) + 1; //initially the current slide should be the first slide.
    else
        currentslideShifterSlide = firstslideShifterSlide;

    /**
     * if the window is resized, reset the positioning of the viewport
     * so the slides don't show in the wrong place.
     * Also recalculate the coordinates of the slideShifterContainer.
     * */
    $(window).resize(function() {
        slideShifterViewportLeft = $('#slideShifterViewport').offset().left + 2;
        slideShifterViewportTop = $('#slideShifterViewport').offset().top + 2;
        slideShifter_prePos(currentslideShifterSlide);

        if($('#slideShifterShowHideControls').attr('value')=='true')
        {
            //recalculate the slideShifterContainer coordinates
            areaLeft = $('#slideShifterContainer').offset().left;
            areaTop = $('#slideShifterContainer').offset().top;
            areaWidth = parseInt($('#slideShifterContainer').css('width'));
            areaHeight = parseInt($('#slideShifterContainer').css('height'));
            areaRight = areaLeft + areaWidth;
            areaBottom = areaTop + areaHeight;
        }
        
    });
    /**
     * Start the shifter running
     **/
    slideShifter_prePos(currentslideShifterSlide); //move the current slide to the viewport.
    $('#'+currentslideShifterSlide).show(); //show the current slide.
    spin = setInterval("slideShifter_next();", timeInterval); //start slides shifting
});//end document.ready

/**************
 *Functions to control the slideShifterContainer
 **************/

/**
 * pre position the slide before showing it
 * @param slide The integer id of the slide to position
 */
function slideShifter_prePos(slide)
{
    //create select string
    slideselect = '#'+slide;
    //set css pos variables in string format
    cssLeft = slideShifterViewportLeft + 'px';
    cssTop = slideShifterViewportTop + 'px';
    //set the slide's position
    $(slideselect).css({position: 'absolute', top: cssTop, left: cssLeft});
}


/**
 * show the slide
 * @param slide The int id for the slide
 * @param dir What direction the slide effect should use.
 */
function slideShifter_show(slide,dir)
{
    //create select string
    slideselect = '#'+slide;
    //show it
    $(slideselect).show('slide',{direction: dir},'slow');
}
/**
 * hide the slide
 * @param slide The int id for the slide
 * @param dir What direction the slide effect should use.
 */
function slideShifter_hide(slide,dir)
{
    //create select string
    slideselect = '#'+slide;
    //hide it
    $(slideselect).hide('slide',{direction: dir},'slow');
}

/**
 * Show the next slide
 */
function slideShifter_next()
{
    //if the current slide is the last slide, then got to the first slide.
    if(currentslideShifterSlide==lastslideShifterSlide)
    {
        currentslideShifterSlide = firstslideShifterSlide;
        slideShifter_prePos(currentslideShifterSlide);
        slideShifter_hide(lastslideShifterSlide,'left');
        slideShifter_show(currentslideShifterSlide,'right');
    }
    //otherwise, just increment to the next slide.
    else
    {
        slideShifter_prePos(++currentslideShifterSlide);
        slideShifter_hide(currentslideShifterSlide-1,'left');
        slideShifter_show(currentslideShifterSlide,'right');
    }

}

/**
 * go to the previous slide
 */
function slideShifter_prev()
{
    //If the current slide is the first slide, make prev go to last slide.
    if(currentslideShifterSlide==firstslideShifterSlide)
    {
        currentslideShifterSlide = lastslideShifterSlide;
        slideShifter_prePos(currentslideShifterSlide);
        slideShifter_hide(firstslideShifterSlide,'right');
        slideShifter_show(currentslideShifterSlide,'left');
    }
    //otherwise, just decrement to the previous slide.
    else
    {
        slideShifter_prePos(--currentslideShifterSlide);
        slideShifter_hide(currentslideShifterSlide+1,'right');
        slideShifter_show(currentslideShifterSlide,'left');
    }
}

/**
 * Pause/Play slideShifterContainer
 */
function slideShifter_pauseplay()
{
    if(spin)//pause it
    {
        clearInterval(spin);
        spin=false;
        $('#slideShifterPauseplayButton').attr('src', slideShifterPlayImage);//switch to play image
    }
    else//play it
    {
        slideShifter_next();
        spin = setInterval("slideShifter_next();",timeInterval);
        $('#slideShifterPauseplayButton').attr('src', slideShifterPauseImage);//switch to pause image
    }
}

/**
 * For when the next button is pushed. Had to do this since
 * it screwed when clicked while spinning.
 */
function slideShifter_nextClick()
{
    if(spin)//the slideShifterContainer is spinning
    {
        slideShifter_pauseplay();//pause it
        slideShifter_next();
    }
    else
    {
        slideShifter_next();
    }
}

/**
 * For when the prev button is pushed. Had to do this since
 * it screwed when clicked while spinning.
 */
function slideShifter_prevClick()
{
    if(spin)//the slideShifterContainer is spinning
    {
        slideShifter_pauseplay();//pause it
        slideShifter_prev();
    }
    else
    {
        slideShifter_prev();
    }
}
