// globalize vars
var img_index = 0;
var elements;
var elementCount;
var click_ready = true;
var acc_index = -1;

var interval = -1;
var intervalTime = 3000;

 ddaccordion.init({
    headerclass: "submenuheader", //Shared CSS class name of headers group
    contentclass: "submenu", //Shared CSS class name of contents group
    revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
    mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
    collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
    defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
    onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
    animatedefault: false, //Should contents open by default be animated into view?
    persiststate: false, //persist state of opened contents within browser session?
    toggleclass: ["acc_closed", "acc_open"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
    togglehtml: ["suffix", "<img src='images/graphics/orange_arrow_up.png' class='statusicon' />", "<img src='images/graphics/orange_arrow_down.png' class='statusicon' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
    animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
    oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
        //do nothing
        //alert(headers);
    },
    onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
        //do nothing
        //alert(index);
        //alert(state);
        //alert($(header));
        //acc_index = (state == "block") ? index : -1;
        var targetObj = $(header);
        if(targetObj.hasClass("pause")){
            if(state == "block")
            {
                stopTimer();
            }else if(state == "none")
            {
                startTimer();
            }
        }
        
    }
});

$(document).ready(function() {

   

    elements = $(".image-rotation-slide");
    elementCount = elements.size();

    $(".image-rotation-slide").hide();

    var elementsToShow = 1;
    var alreadyChoosen = ",";
    var i = 0;
    while (i < elementsToShow) {
        var rand = Math.floor(Math.random() * elementCount);
        if (alreadyChoosen.indexOf("," + rand + ",") < 0) {
            alreadyChoosen += rand + ",";
            elements.eq(rand).show();
            img_index = rand;
            // elements.eq(0).css("z-index", 150);
            // elements.eq(1).css("z-index", 100);
            // elements.eq(2).css("z-index", 50);
            ++i;
        }
    }

    startTimer();

});

function stopTimer()
{
    clearInterval(interval);
    interval = -1;   
}
function startTimer()
{
    if(interval < 0){
        interval = setInterval("swapImages()", intervalTime);
    }
    
}

function swapImages()
{
    if(img_index <= 1){
        $(".image-rotation-slide").eq(img_index + 1).fadeIn(500, hideSlide);
    }else{
        $(".image-rotation-slide").eq(0).show();
        $(".image-rotation-slide").eq(img_index).fadeOut(500, hideSlide);
    }
}

function hideSlide()
{
    $(".image-rotation-slide").eq(img_index).hide();

    if(img_index == (elementCount - 1))
    {
        img_index = 0;
    }else{
        img_index++;
    }  
    click_ready = true;
}
