var pagesPath = "/files/global/frontpagefeaturebox/slides/"; //relative to the home page
var pages = new Array (
    //add an entry for every slide
	"bfb.html",
	"about_us.html",
	"playstreets.html",
	"rebel.html",
	"join.html"
	
    //if the slides are not showing up, make sure there is no trailing comma after the last element here
);

var pageTitles = new Array ();

//add a title here for each slide
pageTitles[0] = "Become a Bike-Friendly Business Today!";
pageTitles[1] = "About Transportation Alternatives";
pageTitles[2] = "Apply for Play Streets Today";
pageTitles[3] = "Join the Rider Rebellion";
pageTitles[4] = "Join Transportation Alternatives";





var buttons = new Array (
    //each of these needs a corresponding onclick function to check when it should be active and define how the button should behave
    "next",
    "prev",
    "playpause"
    //make sure there is no trailing comma after the last element here
);

//initial slide number (0 is first)
var position = 0;

//fade speed (slow, normal, fast)
var fadeSpeed = "fast";

//Circular carousel? (1 for yes, 0 for no)
var circ = 1;

//cycle speed (milliseconds)
var cycleSpeed = 7000;

//play on start? (1 for yes, 0 for no)
var playOnStart = 1;

//var playOnStart = 0;

///////////////////////////////////////////////////////////////////////////////


//global variables (do not change)
var play;

//IE doesn't fade text well, so turn it off if UA is IE
if ($.browser.msie) {
    fadeSpeed = 0;
}

//Function: createCarousel ();
function createCarousel() {
    //create the slides
    for (var i = 0; i < pages.length; i++) {
        $(".tacarousel-list").append("<li id=\"tacarousel-item-" + i + "\"></li>");
        $(".tacarousel-titles").append("<li id=\"tacarousel-title-" + i + "\"></li>");
        $(".tacarousel-counter").append("<li id=\"tacarousel-counter-" + i + "\"></li>");
    }

    //hide all of the slides
    for (var i = 0; i < pages.length; i++) {
        $("#tacarousel-item-" + i).hide();
        $("#tacarousel-title-" + i).hide();
        $("#tacarousel-counter-" + i).hide();
    }

    //add content to the slides
    for (var i = 0; i < pages.length; i++) {
        $("#tacarousel-item-" + i).html("<iframe class=\"tacarousel-item\" width=\"697\" height=\"402\" scrolling=\"no\" src=\"" + pagesPath + pages[i] + "\">Your browser does not support frames.</iframe>");
        //add title for the next slide while adjusting for the js modulo bug fix
        var titleToAdd = (((i + 1) % pageTitles.length) + pageTitles.length) % pageTitles.length;
        $("#tacarousel-title-" + i).html("next: " + "<span id=\"tacarousel-title-norm-" + i + "\" class=\"tacarousel-title-norm\">" + pageTitles[titleToAdd] + "</span>");
        var counterToAdd = i + 1;
        var counterTotal = pages.length;
        $("#tacarousel-counter-" + i).html("Slide " + counterToAdd + " of " + counterTotal);
    }
}

//Function: changeSlide (pos, init, cycleCall);
//pos: New slide number (integer) (they start at 0!)
//init: Is this the initialization of the carousel? (1 for yes, 0 for no)
//cycleCall: Is this call from the play feature?
function changeSlide(pos, init, cycleCall) {
    //Is this a cycleCall and play should stop? If so, terminate function execution early.
    if (cycleCall && (! play)) {
        return 0;
    }
    //validate the inputs to conditions stated in desc
    if ((Math.ceil(pos) == Math.floor(pos))) {
        var step = pos - position;
        position = ((pos % pages.length) + pages.length) % pages.length; //JavaScript Modulo Bug fix
        //disable the buttons during the animation to prevent weird effects if one double-clicks
        if (! init) {
            buttonStateChange(buttons, 0);
        }
        if (!(step == 0)) {
            //hide the old slide and use the callback to show the new slide
            $("#tacarousel-item-" + (pos - step)).fadeOut(fadeSpeed, changeSlideCallback);
            $("#tacarousel-title-" + (pos - step)).fadeOut(fadeSpeed, changeTitleCallback);
            $("#tacarousel-counter-" + (pos - step)).fadeOut(fadeSpeed, changeCounterCallback);
        }
        if (init) {
            $("#tacarousel-item-" + pos).fadeIn(fadeSpeed);
            $("#tacarousel-title-" + pos).fadeIn(fadeSpeed);
            $("#tacarousel-counter-" + pos).fadeIn(fadeSpeed);
            togglePlayPauseButton();
        }
        //turn the buttons back on now as necessary
        buttonStateChange(buttons, 1);
        //if playing the carousel, change slide if possible
        if (play) {
            if (!((position == pages.length - 1) && (! circ))) {
                setTimeout("changeSlide(position + 1, 0, 1)", cycleSpeed);
            } else {
                changePlay(false);
            }
        }
    }
}

//Function: changeSlideCallback ();
function changeSlideCallback() {
    //Fade in the new slide
    $("#tacarousel-item-" + position).fadeIn(fadeSpeed);
}

//Function: changeTitleCallback ();
function changeTitleCallback() {
    //Fade in the new title
    if (!((position == pages.length - 1) && (! circ))) {
        $("#tacarousel-title-" + position).fadeIn(fadeSpeed);
    }
}

//Function: changeCounterCallback ();
function changeCounterCallback() {
    //Fade in the new counter number
    $("#tacarousel-counter-" + position).fadeIn(fadeSpeed);
}

//Function: buttonStateChange(button, state);
//button: Which button to change? can be an array of buttons too.
//state: which state should the buttons be switched to? (1 for active, 0 for inactive)
function buttonStateChange(button, state) {
    if (typeof button == "object") {
        for (var i = 0; i < button.length; i++) {
            buttonStateChange(button[i], state);
        }
    } else {
        if (state == 1) {
            //run the behavior functions
            var onClickFunction = "tacarousel_button_" + button + "_onclick";
            eval(onClickFunction + "()");
        } else if (state == 0) {
            $(".tacarousel-" + button)[0].onclick = "";
        }
    }
}

//Function: tacarousel_button_next_onclick();
function tacarousel_button_next_onclick() {
    if (! circ) {
        if (position < pages.length - 1) {
            $(".tacarousel-next")[0].onclick = function(event) {
                changePlay(false);
                changeSlide(position + 1);
            };
        } else {
            $(".tacarousel-next")[0].onclick = "";
        }
    } else {
        $(".tacarousel-next")[0].onclick = function(event) {
                changePlay(false);
                changeSlide(position + 1);
        };
    }
}

//Function: tacarousel_button_prev_onclick();
function tacarousel_button_prev_onclick() {
    if (! circ) {
        if (position > 0) {
            $(".tacarousel-prev")[0].onclick = function(event) {
                changePlay(false);
                changeSlide(position - 1);
            };
        } else {
            $(".tacarousel-prev")[0].onclick = "";
        }
    } else {
        $(".tacarousel-prev")[0].onclick = function(event) {
            changePlay(false);
            changeSlide(position - 1);
        };
    }
}

//Function: tacarousel_button_playpause_onclick();
function tacarousel_button_playpause_onclick() {
    $(".tacarousel-playpause")[0].onclick = function(event) {
        changePlay();
        if (play) {
            changeSlide(position + 1);
        } else {
            //you have to "change" to the current slide when play stops
            changeSlide(position);
        }
    };
}

//Function: togglePlayPauseButton ();
function togglePlayPauseButton() {
    if (play) {
        $(".tacarousel-playpause").removeClass("tacarousel-play");
        $(".tacarousel-playpause").addClass("tacarousel-pause");
    } else {
        $(".tacarousel-playpause").removeClass("tacarousel-pause");
        $(".tacarousel-playpause").addClass("tacarousel-play");
    }
}

//Function: changePlay (value);
//value: new value for play
function changePlay(value) {
    if (value != undefined) {
        play = value;
    } else {
        play = !play;
    }
    togglePlayPauseButton();
}

