/*
 * Project: P. Fitzpatrick Quarries
 * Author: NFA <hi@notforgottenstudio.com>
 * Version: 1.0
 */

$(document).ready(function() {
    $('.productpage .right ul li:nth-child(odd)').addClass('odd');
    $('#slideshow').cycle({
        speed:  'fast',
        timeout: 10000,
        pager:  '#nav',
        slideExpr: 'div'
    });
});

/*!
    Simple timeline style animation.
    Hardcoded for the home (index) page.
*/

var animateID;
var animationSpeed = 31; // ms
var scrubberXpos = 9; // start position
var markerID = 1; // first marker
var targetTitle, targetMarker;
var scrubber = $('.scrubber');

function startTimeline() {
    var startDelay = setInterval(function() {
        scrubber.delay(50).fadeIn(400, function(){
            animateID = setInterval('animate()', animationSpeed);
            $('.11').fadeTo(150, .9);
        });
        clearInterval(startDelay);
    }, 800);
};
function animate() {
    // check position, highlight section
    switch(scrubberXpos) {
        case 230:
            // prev
            $('.11').fadeTo(150, 0);
            $('.t11').fadeTo(50, 0, function(){
                $(this).hide();
                $('.t22').fadeTo(250, 1);
            });
            // next
            $('.22').fadeTo(150, .9);
        break;
        case 471:
            // prev
            $('.22').fadeTo(150, 0);
            $('.t22').fadeTo(50, 0, function(){
                $(this).hide();
                $('.t33').fadeTo(250, 1);
            });
            // next
            $('.33').fadeTo(150, .9);
        break;
        case 712:
            // prev
            $('.33').fadeTo(150, 0);
            $('.t33').fadeTo(50, 0, function(){
                $(this).hide();
                $('.t44').fadeTo(250, 1);
            });
            // next
            $('.44').fadeTo(150, .9);
        break;
        case 922:
            // next
            restartTimeline();
        break;
    };
    ++scrubberXpos;
    scrubber.css('left', scrubberXpos);
    return false;
};

function restartTimeline() {
    clearInterval(animateID);
    scrubber.delay(75).fadeTo(200, 0, function() {
        scrubberXpos = 9;
        scrubber.css('left', scrubberXpos);
        scrubber.delay(25).fadeTo(200, 1, function() {
            startTimeline();
            // prev
            $('.44').fadeTo(150, 0);
            $('.t44').fadeTo(50, 0, function(){
                $(this).hide();
                $('.t11').fadeTo(250, 1);
            });
            // next
            $('.11').fadeTo(150, .9);
        });
    });
};

// history timeline

var timelinePos = 0, totalWidth = 0, offset = 38, inset = 5, speed = 200000,
containerWidth = 960, p = 1, pn = 1, dx = 0, ffd = 0, rwd = 0;
var historyTimeline = $('.timeline ul');

function startHistoryTimeline() {
    // timeline total width
    $('.timeline ul li').each(function() {totalWidth += $(this).outerWidth();});
    historyTimeline.width(totalWidth);
    // scrubber with easing
    scrubber.css('left', inset);
    scrubber.mouseenter(function(){scrubber.css('cursor', 'pointer');});
    scrubber.draggable({
        axis:'x',
        containment:'parent',
        drag: function(event, ui) {
            p = $(this).position().left;
            pn = (p-(p*2)) * ((totalWidth-containerWidth/2.3)/1000) + offset;
        },
        start: function(event, ui) {
            historyTimeline.stop();
            scrubber.stop();
        },
        stop: function(event, ui) {
            historyTimeline.delay(100).animate({left: pn}, 360, 'easeInOutExpo', function() {
                animateHTimeline();
            });
        }
    });
    var startDelay = setInterval(function() {
        scrubber.fadeIn(800, function() {
            animateHTimeline();
        });
        clearInterval(startDelay);
    }, 800);
};

function animateHTimeline() {
    historyTimeline.startAnimation({left: -(totalWidth-containerWidth)}, speed, 'linear');
    scrubber.startAnimation({left: (containerWidth-offset)+inset}, speed, 'linear');
};
