var featureFadeDelay = 8000;
var featureRotateTimeout = null;

function autoResizeFeaturesDiv() {
    features = $$('.task-force-feature');
    masterDiv = $('task-force-features');
    navDiv = $('task-force-nav');
    masterDiv.style.display = 'block';
    
    var max = 0;
    
    features.each(function (feature) {
        feature.show();
        if (parseInt(masterDiv.scrollHeight) > parseInt(feature.scrollHeight)) {
            max = Math.max(max, parseInt(masterDiv.scrollHeight) - 55);
        } else {
            max = Math.max(max, parseInt(feature.scrollHeight) - 25);
        }
        feature.hide();
    });
    
    masterDiv.style.height = '' + max + 'px';
    navDiv.style.height = '' + max + 'px';
    features[0].show();
}

function rotateFeatures(nextIndex) {
    clearTimeout(featureRotateTimeout);
    var list_items = $$('#task-force-features .task-force-feature');
    var nav_items = $$('#task-force-features .task-force-nav-item');
    
    if (list_items.length != nav_items.length) return false;
    if (list_items.length < 1 || nav_items.length < 1) return false;
    
    if (list_items.length == 1) {
        list_items[0].setAttribute('style', '');
        nav_items[0].className = 'task-force-nav-item selected';
    } else {
        nextFeature(nextIndex);
        featureRotateTimeout = setTimeout(function () { rotateFeatures(); }, featureFadeDelay);
    }
}

function nextFeature(nextIndex) {
    var list_items = $$('#task-force-features .task-force-feature');
    var nav_items = $$('#task-force-features .task-force-nav-item');
    
    var currentIndex = null;
    
    // first, determine currentIndex and nextIndex
    for (var i = 0; i < list_items.length; i++) {
        if (list_items[i].className == 'task-force-feature selected') {
            currentIndex = i;
        }
    }
    if (typeof(nextIndex) == 'undefined' || nextIndex == null) {
        nextIndex = currentIndex + 1;
        if (nextIndex > (list_items.length-1)) nextIndex = 0;
    }
    
    // stop now if there's nothing to do
    if (currentIndex == nextIndex) return false;
    
    // hide all elements that aren't current, and reset classes
    for (var i = 0; i < list_items.length; i++) {
        if (i != currentIndex) {
            Element.hide(list_items[i]);
        }
        
        list_items[i].className = 'task-force-feature';
        nav_items[i].className = 'task-force-nav-item';
    }
    
    // do the transition in css first
    list_items[nextIndex].className = 'task-force-feature selected';
    nav_items[nextIndex].className = 'task-force-nav-item selected';
    
    // then with js effects
    Effect.Appear(list_items[nextIndex], { duration: 0.4 });
    setTimeout(function () { $(list_items[nextIndex]).style.opacity = 1.0; }, 450);
    
    if (currentIndex != null) Effect.Fade(list_items[currentIndex], { duration: 0.4 });
}

function prevFeature(feature_id) {
    var list_items = $$('#task-force-features .task-force-feature');
    var nav_items = $$('#task-force-features .task-force-nav-item');
    
    var currentIndex = null;
    
    if (typeof(feature_id) == 'undefined') {
        nextIndex = feature_id
    } else {
        var nextIndex = 0;
        
        // map feature ID to nextIndex
        for (var i = 0; i < list_items.length; i++) {
            temp = list_items[i].getAttribute('id').split('_');
            id = temp[temp.length - 1];
            if (id == feature_id) {
                nextIndex = i;
                break;
            }
        }
    }
    
    // hide all selected
    for (var i = 0; i < list_items.length; i++) {
        if (list_items[i].className == 'task-force-feature selected') {
            if (!currentIndex) currentIndex = i;
            if (typeof(nextIndex) == 'undefined' || i != nextIndex) {
                Effect.Fade(list_items[i], { duration: 0.3 });
            }
        }
        
        list_items[i].className = 'task-force-feature';
        nav_items[i].className = 'task-force-nav-item';
    }
    
    if (currentIndex == null) {
        currentIndex = list_items.length - 1;
    }
    if (typeof(nextIndex) == 'undefined') {
        nextIndex = currentIndex + 1;
        if (nextIndex > (list_items.length-1)) nextIndex = 0;
    }
    
    list_items[nextIndex].className = 'task-force-feature selected';
    nav_items[nextIndex].className = 'task-force-nav-item selected';
    
    Effect.Appear(list_items[nextIndex], { duration: 0.4 });
    setTimeout(function () { $(list_items[nextIndex]).style.opacity = 1.0; }, 450);
    if (currentIndex != null) Effect.Fade(list_items[currentIndex], { duration: 0.4 });
}

