//http://www.sohtanaka.com/web-design/automatic-image-slider-w-css-jquery//


jQuery(document).ready(function() {

//	jQuery('#cycle').cycle({
//		fx: 'fade'
//	});

//	jQuery('figure.gallery-item a').attr('rel','gallery');
//	jQuery('figure.gallery-item a[rel="gallery"]').fancybox();

});

//http://www.sohtanaka.com/web-design/automatic-image-slider-w-css-jquery//


jQuery(document).ready(function() {

//	jQuery('#cycle').cycle({
//		fx: 'fade'
//	});

//	jQuery('figure.gallery-item a').attr('rel','gallery');
//	jQuery('figure.gallery-item a[rel="gallery"]').fancybox();

});

$(document).ready(function() {
    // JavaScript Document
    init();
    
    //instance variables
    var leftArrow;
    var rightArrow;
    var slideList;
    var dots;
    var isRunning;
    var isHovering;
    var slideTimer;
    var length;

    var ITEM_WIDTH;
    var TOTAL_LIST_WIDTH;
    var CURRENT_ITEM;
    var INTERVAL_TIME;
    var SLIDE_SPEED;

    //init function
    function init() {
        setVars();
        setTimer();
        setWidth();
    }

    //action items
    leftArrow.click(function() {
        isHovering = false;
        slideAnimate(setTarget('prev'));
    });

    rightArrow.click(function() {
        isHovering = false;
        slideAnimate(setTarget('next'));
    });

    $('#slider').hover(function() {
        isHovering = true;
        stopTimer();
    }, function() {
        isHovering = false;
        setTimer();
    });

    $('#dots > li').click(function() {
        isHovering = false;
        target = $(this).index();
        slideAnimate(target);
    });
    
    //helper functions
    function setVars() {
        leftArrow = $('#previous');
        rightArrow = $('#next');
        slideList = $('#contentwrap > li');
        slideWrap = $('#contentwrap');
        dotNav = $('#dots > li');
        isRunning = false;
        isHovering = false;
        length = slideList.length;
        CURRENT_ITEM_INDEX = 0;
        INTERVAL_TIME = 4000;
        SLIDE_SPEED = 1000;
        ITEM_WIDTH = $(".slide:first").outerWidth(true);
        TOTAL_LIST_WIDTH = length * ITEM_WIDTH;
    }

    function setTarget(direction) {
        if (direction == 'prev')
          return (CURRENT_ITEM_INDEX == 0) ? length - 1 : CURRENT_ITEM_INDEX - 1;
        else
          return (CURRENT_ITEM_INDEX == length - 1) ? 0 : CURRENT_ITEM_INDEX + 1;
    }

    // timer functions
    function setTimer() {
        stopTimer(slideTimer);
        slideTimer = setTimeout(function() {
            slideAnimate(setTarget('next'));
            setTimer();
        }, INTERVAL_TIME);
    }

    function stopTimer(slideTimer) {
        clearTimeout(slideTimer);
    }

    function setWidth() {
        slideList.parent().css('width', TOTAL_LIST_WIDTH + 'px');
    }

    //slider functions
    function slideAnimate(target) {
        if (isRunning === false && isHovering === false) {
            stopTimer();
            isRunning = true;
            
            slideWrap.animate({
                left: (-1 * target * ITEM_WIDTH) + 'px'
            }, SLIDE_SPEED, function() {
                isRunning = false;
                setTimer();
            });
            
            CURRENT_ITEM_INDEX = target;
            dotNavUpdate(target);
        }
    }

    //dotNav functions
    function dotNavUpdate(target) {
        dotNav.removeClass('active');
        dotNav.eq(target).addClass('active');
    }
});
