﻿var curSlide = 0; //This can be changed to switch which slide is 1st
var slides = [];
var slideSpeed = 1000;
var hideShowOptions = { duration: slideSpeed, easing: 'swing' };
var playing = true;
var timer = null;
var timesAdjusted = 0;

function getNewBgPos() {
    var newLeft = getLeftForCenter($('#slideContainer'));
    //lpOffset is to handle the fact that the light-painting-container div  
    //is intentionally larger than the screen width.
    var lpOffset = Math.abs(($('#light-painting-container').offset().left));
    return parseInt(slides[curSlide].pos + lpOffset + newLeft + (timesAdjusted * 1500));
}

function setupSlides() {
    //For the second parameter, there should be no reason to exceed 1500 (the bg image's width.)
    //When determining the best positions for the light-painting, you should set no-repeat on the light-painting
    //and work from the numbers you find without repeat on.
    slides.push(new slide($('#slide01').clone(true), 200, 75, 10, 75));
    slides.push(new slide($('#slide02').clone(true), 400, -75, -10, 300));
    slides.push(new slide($('#slide03').clone(true), 600, 200, -5, 525));
    slides.push(new slide($('#slide04').clone(true), 800, -80, 5, 750));
    $('#carousel_indicator').css({ 'left': slides[curSlide].cPos });
    displaySlide(slides[curSlide]);
}


function slide(el, pos, margin, rotation, cPos) {
    this.el = el;
    this.pos = pos;
    this.margin = margin;
    this.rotation = rotation;
    this.cPos = cPos;
}

//Moves us to the next slide.
function rotateSlide() {
    if (playing) {
        hideSlide(slides[curSlide]);
        //Increment curSlide. Reset if increment exceeds slides array length.
        curSlide = (curSlide + 1) % slides.length;
        displaySlide(slides[curSlide]);
    }
}

//Moves us to the specified slide.
function jumpToSlide(index) {
    if (curSlide != index) {
        hideSlide(slides[curSlide]);
        curSlide = index;
        displaySlide(slides[curSlide]);
    }
}

//Logic for showing a slide.
function displaySlide(obj) {
    //timesAdjusted++;
    //var newPos = getNewBgPos(); //We use getNewBgPos so the light painting always travels right-to-left.
    //
    ////Check if we'll hit Firefox' positive bg-x limit of 32885.
    ////Check for older version of IE which doesn't track BGPos properly.
    ////Reset timesAdjusted if so, and get new Bg Pos.
    //if ((newPos > 32885) || ($.browser.msie && (parseInt($.browser.version) < 9))) {
    //    $("#light-painting").css({ 'background-position': '0px' });
    //    timesAdjusted = 0;
    //    newPos = getNewBgPos();
    //}


    //Animate light painting to the new position.
    //$("#light-painting")
    //    .stop()
    //    .animate({
    //        'background-position': newPos,
    //        'margin-top': obj.margin + 'px',
    //        rotate: obj.rotation
    //    }, slideSpeed + 1500, function () {
    //        if (!$.browser.msie || (parseInt($.browser.version) > 8)) {
    //            $(this).animate({
    //                'background-position': newPos + 75
    //            }, 3000, 'linear')
    //        }
    //    });

    //Slide in the current slide from the left.
    slides[curSlide].el
        .appendTo($('#header'))
        .stop()
        .css({ 'opacity': '0.0', 'display': 'block', 'left': 0 })
        .animate({ left: getLeftForCenter($('#slideContainer')), opacity: 1.0 }, hideShowOptions);
    
    //Adjust the carousel indicator to the cPos for the current slide.
    $('#carousel_indicator')
        .stop()
        .animate({ 'left': obj.cPos }, slideSpeed);
}

//Logic for hiding a slide.
function hideSlide(obj) {
    var curLeft = getLeftForCenter(slides[curSlide].el);
    //If browser is IE < 9, hide the slide. Otherwise, fade slide out.
    if ($.browser.msie && (parseInt($.browser.version) < 9)) {
        slides[curSlide].el
            .appendTo($('#header'))
            .stop()
            .hide();
    } else {
        slides[curSlide].el
            .appendTo($('#header'))
            .stop()
            .animate({ left: curLeft + 500, opacity: 0.0 }, hideShowOptions,
                function () { $(this).css({ 'display': 'none' }); });
    }
}

//Finds and returns the appropriate position with which to set the left edge 
//of an element so that it will be centered. Takes window size into account.
function getLeftForCenter(obj) {
    return (($(window).width() - obj.outerWidth()) / 2) + $(window).scrollLeft();
}

function clearTimer() {
    // Clear the timer only if it is set
    if (timer) {
        window.clearInterval(timer)
    }
};

// Pass startStop(false) to stop and startStop(true) to play
function startStop(playing) {
    if (playing) {
        clearTimer(); // Just in case this was triggered twice in a row
        timer = window.setInterval(function () { rotateSlide(); }, 5000);
    } else {
        clearTimer();
    }
}
