// Simple JavaScript Rotating Banner Using jQuery
// www.mclelun.com
var jqb_fotovCurrent = 0;
var jqb_fotovTotal = 0;
var jqb_fotovDuration = 15000;
var jqb_fotointInterval = 0;
var jqb_fotovIsPause = false;
var jqb_fototmp = 20;
var jqb_fototitle;

$(document).ready(function () {
    GetPicture();
    $(".jqb_fotoinfo").text($(".jqb_fotoslide").attr("title"));
    jqb_fotointInterval = setInterval(jqb_fotofnLoop, jqb_fotovDuration);

    $("#btn_fotopauseplay").click(function () {
        if (jqb_fotovIsPause) {
            jqb_fotofnChange();
            jqb_fotovIsPause = false;
            $("#btn_fotopauseplay").removeClass("jqb_btn_play");
            $("#btn_fotopauseplay").addClass("jqb_btn_pause");
        } else {
            clearInterval(jqb_fotointInterval);
            jqb_fotovIsPause = true;
            $("#btn_fotopauseplay").removeClass("jqb_btn_pause");
            $("#btn_fotopauseplay").addClass("jqb_btn_play");
        }
    });

    $("#btn_fotonext").click(function () {
        jqb_fotofnChange();
    });
});

function jqb_fotofnChange() {
    clearInterval(jqb_fotointInterval);
    jqb_fotointInterval = setInterval(jqb_fotofnLoop, jqb_fotovDuration);
    jqb_fotofnLoop();
}

function jqb_fotofnLoop() {
    GetPicture();
    jqb_fotovCurrent == jqb_fotovTotal ? jqb_fotovCurrent = 0 : jqb_fotovCurrent++;

    $("#jqb_fotoobject").find(".jqb_fotoslide").each(function (i) {
        if (i == 0) {
            jqb_fototitle = $(this).attr("title");
            $(".jqb_fotoinfo").animate({ opacity: 'hide', "left": "-50px" }, 250, function () {
                $(".jqb_fotoinfo").text(jqb_fototitle).animate({ opacity: 'show', "left": "0px" }, 500);
            });
        }

        /*
        //Horizontal Scrolling
        jqb_fototmp = ((i - 1) * 349) - ((jqb_fotovCurrent - 1) * 349);
        $(this).animate({ "left": jqb_fototmp + "px" }, 500);
        */
        
        //Fade In & Fade Out
        if(i == jqb_fotovCurrent){
        $(".jqb_fotoinfo").text($(this).attr("title"));
        $(this).animate({ opacity: 'show', height: 'show' }, 500);
        } else {
        $(this).animate({ opacity: 'hide', height: 'hide' }, 500);
        }

        if (i == 2) {
            $(this).remove();
        }

    });
}

function GetPicture() {
    $.ajax({
        type: "GET",
        url: "/ajax/getpicture.php",
        cache: false,
        success: function (html) {
            $(".jqb_fotoslides").prepend(html);
        }
    });
}

