// Feature Rotator

function activestate(){
    if (isactive == 0) { isactive = 1; } else { isactive = 0; }
    return;
}

function checkx(){ // Get the current left setting of the div and, if it's "at rest", allow movement
    var obj = document.getElementById('slides');
    var x = parseFloat($(obj).getStyle('left') || '0');
    
    if (x == -533 || x == -866) { return 1; } else { return 0; } // 1 is true and 0 is false, in typical archaic boolean style
}

function moveleft(){
    var master = document.getElementById('slides'); // Specify the master slides div

    activeindex--; // Decrement the current index
    if (activeindex == -1) { activeindex = listcount - 1; } // If current index is 1 less than beginning of array, set it to last array item  
    
    var newindex = activeindex - 2; // Subtract two from the current index to mark the second slide to the left
    if (newindex == -1) { newindex = listcount - 1; } // If index is 1 less than beginning of items array, have it loop back to the last array item
    else if (newindex == -2) { newindex = listcount - 2; } // If index is 2 less than beginning of items array, have it loop back to the next-to-last array item  
        
    new Effect.Move('slides', {
    x: 333, y: 0, mode: 'relative',
    transition: Effect.Transitions.linear, 
    beforeStart: function(){ 
        createSlide(newindex,'first'); // Add the new slide on the left
        master.style.cssText = 'left:-866px;height:218px;width:2500px'; // Reset the left positioning of the master slides div to center the scrolled-in slide
    }, 
    afterFinish: function() { 
        removeSlide('last'); // Remove the last slide to make room for the new slide
        if (isactive == 1 && activebutton == 'left') { moveleft(); } // If mouse is still over arrow, slide in the same direction again
        else if (isactive == 1 && activebutton == 'right') { moveright(); } // If mouse is on the other arrow, move the other direction
    } });
    
    return;
}

function moveright(){
    var master = document.getElementById('slides'); // Specify the master slides div

    activeindex++; // Increment the current index
    if (activeindex == listcount) { activeindex = 0; } // If current index is 1 more than last array item, set it to the first array item
        
    var newindex = activeindex + 2; // Add two to the current index to mark the 2nd slide to the right
    if (newindex == listcount) { newindex = 0; } // If index is 1 more than last array item, have it loop back to the first array item
    else if (newindex == listcount + 1) { newindex = 1; } // If index is 2 more than last array item, have it loop back to the second array item

    new Effect.Move('slides', {
    x: -335, y: 0, mode: 'relative',
    transition: Effect.Transitions.linear, 
    beforeStart: function(){ 
        createSlide(newindex,'last'); // Create a new slide on the right side
    },
    afterFinish: function(){
        removeSlide('first'); // Remove the first slide to make room for the new slide
        master.style.cssText = 'left:-533px;height:218px;width:2500px'; // Reset the left positioning of the master slides div to realign the slides
        if (isactive == 1 && activebutton == 'right') { moveright(); } // If mouse is still over arrow, slide in the same direction again
        else if (isactive == 1 && activebutton == 'left') { moveleft(); } // If mouse is on the other arrow, move the other direction
    } });
    
    return;
}

function createSlide(index, order){
    var master = document.getElementById('slides'); // Specify the master slides div
    
    var newSlide = document.createElement('div'); // Create a new slide
    newSlide.className = 'slide';
    
    var newImage = document.createElement('img'); // Create a new image for the slide
    newImage.src = 'images/'+imagelist[index]; // Pull the image filename from the array for the index specified
    
    var newOverlay = document.createElement('div'); // Create a new overlay div for the slide
    newOverlay.className = 'slideOverlay';
    newOverlay.innerHTML = '<!-- x -->';
    
    var newContent = document.createElement('div'); // Create a new content div for the slide
    newContent.className = 'slideContent';
    
    var newHeading = document.createElement('h2'); // Create a new heading for the slide
    newHeading.innerHTML = headinglist[index]; // Pull the heading text from the array for the index specified
    
    var newText = document.createElement('p'); // Create a new blurb of text for the slide
    newText.innerHTML = textlist[index];
    
    var newLink = document.createElement('a'); // Create new "Click Here" link to be appended to text blurb
    newLink.setAttribute('href',linklist[index]);
    newLink.innerHTML = 'Click here';
    
    newContent.appendChild(newHeading); // Add the heading to the content div
    newContent.appendChild(newText); // Add the text, below the heading, to the content div
    newContent.appendChild(newLink); // Add the link, after the text, to the content div
    
    newSlide.appendChild(newImage); // Add the image to the top of the slide
    newSlide.appendChild(newOverlay); // Add the overlay div, right below the image, to the slide
    newSlide.appendChild(newContent); // Add the content div, right below the overlay div, to the slide
    
    if (order == 'first'){ // If moving left, we need to add a slide at the beginning of the slides div (on the left)
        master.insertBefore(newSlide,master.firstChild);
    } else if (order == 'last'){ // Else, we're moving right, so we need one at the end of the slides div (on the right)
        master.appendChild(newSlide);
    }
    
    return;
}

function removeSlide(order){
    var master = document.getElementById('slides'); // Specify the master slides div
    var childDivs = master.getElementsByTagName('div'); // Get all of the divs in the slides div
    
    if (order == 'first') { // If moving right, we're dropping the leftmost slide so that our slides div doesn't fill up
        master.removeChild(childDivs[0]); // remove the first slide-classed div
    } else if (order == 'last') { // Else, we're moving left, so we're dropping the rightmost slide in the slides div
        master.removeChild(childDivs[childDivs.length - 3]); // remove the last slide-classed div (it encloses 2 divs, so we subtract 3, not just 1)
    }
    
    return;
}
