﻿// set this function as onBeforeSeek handler for scrollable to allow fade
function jqueryTools_scrollable_fade_onBeforeSeek(event, index) {
    var s = this;
    var prevIndex = s.getIndex();
    var nextIndex = index;
    var cnf=s.getConf();
    var speed = cnf.speed;
    var easing = cnf.easing;
    var circular = cnf.circular;
    var easingHide = cnf.easingPrevious;
    var easingShow = cnf.easingShow;
    var speedHide = cnf.speedHide;
    var speedShow = cnf.speedShow;
    
    if (easingHide == null)
        easingHide = easing;

    if (easingShow == null)
        easingShow = easing;

    if (speedHide == null)
        speedHide = speed;

    if (speedShow == null)
        speedShow = speed;

    //
    if (!s.fade_initialized) {
        var items = s.getItems();
        for (var i = 0; i < items.length; i++) {
            if (i == prevIndex)
                $(items[i]).css({ position: 'absolute', display: 'block' });
            else
                $(items[i]).css({ position: 'absolute', display: 'none' });
        }

        s.fade_initialized = true;
    }
    
    //
    if (nextIndex >= s.getItems().length) {
        if (circular == true) {
            nextIndex = 0;
        } else {
            return false;
        }

    } else if (nextIndex < 0) {
        if (circular == true) {
            nextIndex = s.getItems().length-1;
        } else {
            return false;
        }
    }

    

    //
    if (s.fade_skipFade == true)
        return true;

    if (nextIndex == prevIndex)
        return false;

    if (s.fade_active) {
        if (nextIndex == s.fade_nextIndex)
            return false;

        window.clearTimeout(s.fade_timer);

        prevIndex = s.fade_nextIndex;
    }

    //                    
    var prev = $(s.getItems()[prevIndex]);
    var next = $(s.getItems()[nextIndex]);

    //
    prev.stop(true, false).css({ display: 'block' , opacity:1 });
    prev.animate({
        opacity: 0
    }, speedHide, easingHide, function() {
        prev.css({ display: 'none' });
        /*
        if (prev.get(0).style.removeAttribute) {
            prev.get(0).style.removeAttribute('filter');
            prev.get(0).style.removeAttribute('opacity');
        }
        */

    });

    //
    next.stop(true, false).css({ display: 'block' , opacity:0 });
    next.animate({
        opacity: 1
    }, speedShow, easingShow, function() {
        /*
        if (next.get(0).style.removeAttribute) {
            next.get(0).style.removeAttribute('filter');
            next.get(0).style.removeAttribute('opacity');
        }
        */
    });

    //
    s.fade_active = true;
    s.fade_nextIndex = nextIndex;

    s.fade_timer = window.setTimeout(function() {
        s.fade_skipFade = true;
        s.seekTo(nextIndex, 0);
        s.fade_skipFade = false;
        s.fade_active = false;        
    }, 0);

    return false;
}
