﻿var tape = $("div.js-slider > div.viewport > div.tape");
var controlBlob = $("div.js-slider > div.controls > div.blob0");
var offset = 0;
var items = 0;
var timer;
var interval = 5000;

function animationLoop() {
    offset = offset + 1;
    if (offset > items) offset = 0;
    setOffset(offset);    
    timer = setTimeout(animationLoop, interval);
}

function animateToOffset() {    
    tape.stop();
    tape.animate({
        left: '-' + offset * 990 + 'px'
    }, 600, 'swing');
}

function setOffset(value) {
    offset = value;
    animateToOffset();
    controlBlob.removeClass("active");
    controlBlob = $("div.js-slider > div.controls > div.blob" + offset);
    controlBlob.addClass("active");
}

$().ready(function() {
    offset = 0;
    tape = $("div.js-slider > div.viewport > div.tape");
    controlBlob = $("div.js-slider > div.controls > div.blob" + offset);
    controlBlob.addClass("active");
    items = $("div.js-slider > div.viewport > div.tape > div.article").length - 1;
});

/*/  /  /  /  /  /  /  /  /  /  /  /  /  /  /  /  / */
/*  iPhone touch interfaceing  /  /  /  /  /  /  / */
/* /  /  /  /  /  /  /  /  /  /  /  /  /  /  /  / */
var xValues = { mouse1: 0, mouse2: 0, left: 0 }
//var movitmovit = false;

function touchStart(event) {
    xValues.mouse1 = event.targetTouches[0].pageX;
    xValues.left = parseInt(tape.css('left').match(/-*\d+/) || 0);
    //movitmovit = true;
}

function touchMove(event) {
    event.preventDefault();
    tape.stop();
    xValues.mouse2 = event.targetTouches[0].pageX - xValues.mouse1;
    tape.css('left', xValues.left + xValues.mouse2);

    // original just quick swipe pickup
    //if (false && movitmovit) {
    //    movitmovit = false;
    //    if (event.targetTouches[0].pageX > xValues) {
    //        if (offset > 0) superwide.find('.rw').click();
    //    }
    //    else {
    //        if (offset < items - 6) superwide.find('.ff').click();
    //    }
    //}
}

function touchEnd(event) {
    event.preventDefault();
    offset = offset - Math.round(xValues.mouse2 / 990);
    if (offset < 0) offset = 0;
    if (offset > items) offset = items; 
    setOffset(offset);
}

function touchCancel(event) {
    event.preventDefault();
    animateToOffset();
    //superwide.find('.doc-header1').text('touchCancel');
}

$().ready(function() {
    tape.each(function() {
        this.ontouchstart = touchStart;
        this.ontouchmove = touchMove;
        this.ontouchend = touchEnd;
        this.ontouchcancel = touchCancel;
    });
});

