var sceneTimeout = null;
var scenePaused = false;
var pagerIndex = 0;
var pagerCount = 0;
var windowWith = 0;
var animateRightToLeft = true;
$(document).ready(function() {
    resize();
    $(window).resize(function() {
        resize();
    });
    $(".pager").each(function() {
        this.onmouseover = pauseSceneSlider;
        this.onmouseout = continueSceneSlider;
        pagerCount++;
    });
    progressSceneSlider();
});


function pauseSceneSlider() {
    if (sceneTimeout != null) {
        clearTimeout(sceneTimeout);
        sceneTimeout = null;
    }
    scenePaused = true;
}

function continueSceneSlider() {
    scenePaused = false;
    progressSceneSlider();
}

function progressSceneSlider() {
    if (!scenePaused && sceneTimeout == null) {
        if (pagerIndex == pagerCount - 1) {
            sceneTimeout = setTimeout(slideSceneLeft, 3000);
        } else if (pagerIndex == 0) {
            sceneTimeout = setTimeout(slideSceneRight, 3000);
        } else {
            if (animateRightToLeft) {
                sceneTimeout = setTimeout(slideSceneRight, 3000);
            } else {
                sceneTimeout = setTimeout(slideSceneLeft, 3000);
            }
        }
    }
}

function slideSceneRight() {
    animateRightToLeft = true;
    sceneTimeout = null;
    pagerIndex++;
    $(".pager").animate({ left: "-=" + windowWith }, 1000, progressSceneSlider);

}

function slideSceneLeft() {
    animateRightToLeft = false;
    sceneTimeout = null;
    pagerIndex--;
    $(".pager").animate({ left: "+=" + windowWith }, 1000, progressSceneSlider);
}

function resize() {
    windowWith = $(window).width();
    $('.pager').width(windowWith);
    $('#scene').width(windowWith * 3);
}

