var MAX_STORY = 5;
var leTemps = 1000;
var current_story = 1;
var now_playing;
var story_timer;

function startTimer(varTemps,varMax_story)
{
    MAX_STORY=varMax_story;
    leTemps=varTemps;
    if (!now_playing) {
        nextStory();
        var startstop = $('startstop'); 
        if (startstop != null)
        {
            $('startstop').innerHTML = '<a href="#" class="pause" onclick="stopTimer(); return false;"></a>';
        }
    }
}

function stopTimer()
{
    if (now_playing) {
        now_playing = false;
        clearTimeout(story_timer);
        story_timer = false;               
            $('startstop').innerHTML = '<a href="#" class="play" onclick="startTimer(' + leTemps + ',' + MAX_STORY + '); return false;"></a>';        
    }
}

function swapStory(new_story, chain_flag)
{
    // can we just do nothing?
    if (new_story == current_story) {
        return true;
    }

    // does requested story even exist?
    if (!$('homeStory'+new_story)) {
        return false;
    }

    // sanity check in case of unstable condition
    if (!$('homeStory'+current_story)) {
        return false;
    }

    // stop timer
    if (now_playing && !chain_flag) {
        stopTimer();
    }

    // fade out, fade in
    /*
    $('homeStory'+current_story).style.zIndex = '101';
    $('homeStory'+new_story).style.zIndex = '100';

    Effect.Appear('homeStory'+new_story, { duration: 0.50 } );
    Effect.Fade('homeStory'+current_story, { duration: 0.75 } );
    
    setTimeout("$('homeStory"+current_story+"').style.zIndex = '0'", 750);
    */

    $('homeStory'+current_story).style.zIndex = '99';
    $('homeStoryWhite').style.zIndex = '101';
    new Effect.Appear('homeStoryWhite', { duration: 0.25 });

    setTimeout("$('homeStory"+current_story+"').style.zIndex = '-1'", 250);
    setTimeout("$('homeStory"+new_story+"').style.zIndex = '100'", 250);
    setTimeout("Effect.Fade('homeStoryWhite', { duration: 0.25 })", 255);

    for (i=1; i<=MAX_STORY; i++) {
        if ($('thumbnail'+i)) {
            $('thumbnail'+i).className = (i==new_story ? 'active':'');
        }
        if ($('btnslide'+i)) {
            $('btnslide'+i).className = (i==new_story ? 'active':'');
        }
        
    }
    
    
    current_story = new_story;

    return true;
}

function nextStory()
{
    now_playing = true;

    var new_story = current_story + 1;
    if (new_story > MAX_STORY) {
        new_story = 1;
    }

    swapStory(new_story, true);

    /*story_timer = setTimeout("nextStory()", 9000);*/
    story_timer = setTimeout("nextStory()", leTemps);
}
