﻿var tape = $("div.js-slider > div.viewport > div.tape");
var controlBlob = $("div.sliderarticle > div.controls > div.blobs > div.blob0");
var offset = 0;
var prevOffset = 0;
var items = 0;
var timer = -1;
var interval = 5000;

function animationLoop() {

    if ((offset + 1) > items) {
        setOffset(0);
    } else {
        setOffset(offset + 1);
    }  
    timer = setTimeout(animationLoop, interval);
    //console.log(offset + ":" + items);
}

function animateToOffset() {    
//    tape.stop();
//    tape.animate({
//        left: '-' + offset * 750 + 'px'
    //    }, 600, 'swing');

    $("div.tape > #art" + prevOffset).fadeOut(1000);
    $("div.tape > #art" + offset).fadeIn(1000);
}

function setOffset(value) {
    prevOffset = offset;
    offset = value;
    animateToOffset();
    //controlBlob = $("div.sliderarticle > div.controls > div.blobs > div.blob" + prevOffset);
    controlBlob.removeClass("active");
    controlBlob = $("div.sliderarticle > div.controls > div.blobs > div.blob" + offset);
    controlBlob.addClass("active");
    
    if (timer > -1) {
        clearTimeout(timer);
    }
}

$().ready(function() {

    tape = $("div.js-slider > div.viewport > div.tape");
    controlBlob = $("div.sliderarticle > div.controls > div.blobs > div.blob0");
    controlBlob.addClass("active");
    items = $("div.js-slider > div.viewport > div.tape > div.sliderarticle").length - 1;

    for (var i = 0; i <= items; i++) {
        $("div.tape > #art" + i).fadeOut(0);
    }
    offset = 0;
    $("div.tape > #art0").fadeIn(0);
});

///*/  /  /  /  /  /  /  /  /  /  /  /  /  /  /  /  / */
///*  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 / 750);
//    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() {
//    controlBlob = $("div.sliderarticle > div.controls > div.blobs > div.blob" + offset);
//    controlBlob.addClass("active");
    
    
    
//    tape.each(function() {
//        this.ontouchstart = touchStart;
//        this.ontouchmove = touchMove;
//        this.ontouchend = touchEnd;
//        this.ontouchcancel = touchCancel;
//    });
//});

