jQuery(document).ready(function () {

    var timer;
    var rotNum = 1;
    var detectSwitch = false;
    var direction = 'forward';
    var cycleNo = '';
    if (document.getElementById('buttonControls')) {
        var totalObjects = document.getElementById('buttonControls');
        totalObjects = totalObjects.getElementsByTagName('li');
    }

    // JV block refreshing 
    var lengthJV = jQuery("#slidesJV > li").length;
    //cycleNoJV = 'JV' + Math.floor(Math.random() * lengthJV + 1);
    cycleNoJV = "JV2" // added by jonathan percy for temp fix 
    jQuery("#image" + cycleNoJV).css("display", "block");
    jQuery("#image" + cycleNoJV).css("opacity", "0");
    jQuery("#image" + cycleNoJV).animate({ "opacity": 1 }, 3000, "linear", null);
    jQuery("#image" + cycleNoJV).animate({ "opacity": 1 }, 3000, "linear", null);
    jQuery("#image" + cycleNoJV).animate({ "opacity": 1 }, 3000, "linear", null);

    function buttonCycle(event) {
        if (detectSwitch) {
            if (cycleNo == 'button1') {
                if (direction == 'rewind') { rotNum = totalObjects.length; }
            } else if (cycleNo == 'button' + totalObjects.length) {
                if (direction == 'forward') { rotNum = 1; }
            } else {
                if (direction == 'forward') { rotNum = rotNum + 1; }
                if (direction == 'rewind') { rotNum = rotNum - 1; }
            }
        }

        cycleNo = 'button' + rotNum;
        if (this.id) { cycleNo = this.id; playPause(document.getElementById('pause'));}
        jQuery(".slide").css("display", "none");
        jQuery("#image" + cycleNo).css("display", "block");
        jQuery("#image" + cycleNo).css("opacity", "0");
        jQuery("#image" + cycleNo).animate({ "opacity": 1 }, 1000, "linear", null);
        jQuery("ul.buttons li").removeClass("displaying");
        jQuery("#image" + cycleNo).animate({ "opacity": 1 }, 1000, "linear", null);
        jQuery("#" + cycleNo).addClass("displaying");
        clearTimeout(timer);
        if (!this.id) { timer = setTimeout(eval("buttonCycle"), "5000"); }
        jQuery("#image" + cycleNo).animate({ "opacity": 1 }, 1000, "linear", null);

        /*if (this.id) { cycleNoJV = this.id; playPause(document.getElementById('pause')); }*/
        /*Query(".slide").css("display", "none");*/

        if (direction == 'forward') {
            if (cycleNo == 'button' + totalObjects.length) {
                rotNum = 1;
                cycleNo = 'button1';
            } else {
                if (this.id) {
                    rotNum = parseInt(this.childNodes[0].childNodes[0].nodeValue);
                } else {
                    rotNum++;
                }
            }
        }
        if (direction == 'rewind') {
            if (cycleNo == 'button1') {
                rotNum = totalObjects.length;
                cycleNo = 'button' + totalObjects.length;
            } else {
                if (this.id) {
                    rotNum = parseInt(this.childNodes[0].childNodes[0].nodeValue);
                } else {
                    rotNum--;
                }
            }
        }
        detectSwitch = false;
    }

    function OnLoad(event) {
        clearTimeout(timer);
        timer = setTimeout(eval("buttonCycle"), "10");
    }

    /* CONTROLS ------- */
    function pause() {
        clearTimeout(timer);
    }

    function skipForward(event) {
        if (direction == 'rewind') { detectSwitch = true; }
        direction = 'forward';
        clearTimeout(timer);
        timer = setTimeout(eval("buttonCycle"), "0");
        playPause(document.getElementById('play'));
    }

    function skipBack(event) {

        if (direction == 'forward') { detectSwitch = true; }
        direction = 'rewind';
        clearTimeout(timer);
        timer = setTimeout(eval("buttonCycle"), "0");
        playPause(document.getElementById('play'));
    }
    /* ---------------- */

    function bindButtonActions() {
        slides = totalObjects;
        for (i = 1; i <= slides.length; i++) {
            jQuery('#button' + i).bind('click', buttonCycle);
        }
        jQuery('#pauseButton').bind('click', pause);
        jQuery('#playButton').bind('click', OnLoad);
        jQuery('#rewindButton').bind('click', skipBack);
        jQuery('#fastforwardButton').bind('click', skipForward);
		jQuery('#stopban').bind('click', pause);
		jQuery('#DestinationTo').bind('focus', OnLoad);
		jQuery('#StartDate').bind('focus', OnLoad);
		jQuery('#EndDate').bind('focus', OnLoad);
    }

    if (document.getElementById('buttonControls')) {
        bindButtonActions();
        OnLoad();
    }

});

function playPause(object) {    
        if (object.id == 'play') {
            document.getElementById('pause').style.display = 'block';
        } else if (object.id == 'pause') {
            document.getElementById('play').style.display = 'block';
        }
        object.style.display = 'none';
    }
