var jqb_vCurrent = 0;
var jqb_vTotal = 0;
var jqb_vDuration;
var jqb_intInterval = 0;
var jqb_vGo = 1;
var jqb_vIsPause = false;
var jqb_tmp = 20;
var jqb_title;
var jqb_Width;
var jqb_Height;
var jqb_TransitionType;

jQuery(document).ready(function() {
    //Get transition type
    jqb_TransitionType = document.getElementById("hfTransition").value;

    //Get pause length
    jqb_vDuration = document.getElementById("hfPauseLength").value;

    //Get width and height
    var w = document.getElementById("hfWidth").value;
    var h = document.getElementById("hfHeight").value;
    if (w.length != 0) {
        jqb_Width = w
    }
    if (h.length != 0) {
        jqb_Height = h
    }
    
    jqb_vTotal = $(".jqb_slides").children().size() - 1;
    $(".jqb_info").text($(".jqb_slide").attr("title"));
    jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);

    $("#jqb_object").find(".jqb_slide").each(function(i) {
    jqb_tmp = ((i - 1) * jqb_Width) - ((jqb_vCurrent - 1) * jqb_Width);
       $(this).animate({ "left": jqb_tmp + "px" }, 0); // change the zero to something else to get the intial images to show on load and then move to their places
    });

    $("#btn_pauseplay").click(function() {
        if (jqb_vIsPause) {
            jqb_fnChange();
            jqb_vIsPause = false;
            $("#btn_pauseplay").removeClass("jqb_btn_play");
            $("#btn_pauseplay").addClass("jqb_btn_pause");
        } else {
            clearInterval(jqb_intInterval);
            jqb_vIsPause = true;
            $("#btn_pauseplay").removeClass("jqb_btn_pause");
            $("#btn_pauseplay").addClass("jqb_btn_play");
        }
    });
    $("#btn_prev").click(function() {
        jqb_fnChange();
        jqb_vGo = -1;
    });

    $("#btn_next").click(function() {
        jqb_fnChange();
        jqb_vGo = 1;
    });
});

function jqb_fnChange() {
    clearInterval(jqb_intInterval);
    jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);
    jqb_fnLoop();
}

function jqb_fnLoop() {
    if (jqb_vGo == 1) {
        jqb_vCurrent == jqb_vTotal ? jqb_vCurrent = 0 : jqb_vCurrent++;
    } else {
        jqb_vCurrent == 0 ? jqb_vCurrent = jqb_vTotal : jqb_vCurrent--;
    }

    $("#jqb_object").find(".jqb_slide").each(function(i) {

        if (i == jqb_vCurrent) {
            jqb_title = $(this).attr("title");
            $(".jqb_info").animate({ opacity: 'hide', "left": "-50px" }, 250, function() {
                $(".jqb_info").text(jqb_title).animate({ opacity: 'show', "left": "0px" }, 500);
            });
        }



        if (jqb_TransitionType == 0) {

            //Slide
            jqb_tmp = ((i - 1) * jqb_Width) - ((jqb_vCurrent - 1) * jqb_Width);
            $(this).animate({ "left": jqb_tmp + "px" }, 500);
        } else if (jqb_TransitionType == 1) {
           
            //Fade
            jqb_tmp = ((i - 1) * jqb_Width) - ((jqb_vCurrent - 1) * jqb_Width);
            $(this).animate({ "left": jqb_tmp + "px" }, 0);

            if (i == jqb_vCurrent) {
                $(this).animate({ opacity: 'hide', height: 'hide' }, 0); //Added to fix the issue with it not fading in correctly
                $(".jqb_info").text($(this).attr("title"));
                $(this).animate({ opacity: 'show' }, 600);
            } else {
                $(this).animate({ opacity: 'hide' }, 100);
            }
        } else {

            //Fade From Top To Bottom
            jqb_tmp = ((i - 1) * jqb_Width) - ((jqb_vCurrent - 1) * jqb_Width);
            $(this).animate({ "left": jqb_tmp + "px" }, 0);

            if (i == jqb_vCurrent) {
                $(this).animate({ opacity: 'hide', height: 'hide' }, 0); //Added to fix the issue with it not fading in correctly
                $(".jqb_info").text($(this).attr("title"));

                $(this).animate({ opacity: 'show', height: 'show' }, 600);
            } else {
                $(this).animate({ opacity: 'hide', height: 'hide' }, 100);
            }
        }
    });


}





