$(document).ready(function(){
    var pages_slevy = false,
    pages_akce = false,
    page_slevy=0,
    page_akce=0;

    //  index slide
    $('#page_index .banner ul').cycle({
        fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        delay: 0,
        timeout: 10000,
        speed: 2000
    });

    // zjisti max pages
    $.getJSON("/ajax_pages.php", function(data){
        pages_slevy = data['slevy'];
        pages_akce = data['akce'];
        // pridej buttony
        if (pages_slevy>1) {
            $(".slevy .top h2").after("<div class='nav'><a href='' class='left off'></a><a href='' class='right on'></a></div>");
        }
        if (pages_akce>1) {
            $(".akce .top h2").after("<div class='nav'><a href='' class='left off'></a><a href='' class='right on'></a></div>");
        }
        

        // slevy
        $(".slevy .top .nav a.left").click(function () {
            $.get("/slevy_ajax_content.php?page="+(page_slevy-1), function(data){
                page_slevy--;
                $(".slevy .text>div").fadeOut("fast", function(){
                    $(".slevy .text>div").html(data);
                    prebarvi_button ("slevy", page_slevy, pages_slevy);
                    $(".slevy .text>div").fadeIn("fast");
                });
            });
            return false;
        });

        $(".slevy .top .nav a.right").click(function () {
            $.get("/slevy_ajax_content.php?page="+(page_slevy+1), function(data){
                page_slevy++;
                $(".slevy .text>div").fadeOut("fast", function(){
                    $(".slevy .text>div").html(data);
                    prebarvi_button ("slevy", page_slevy, pages_slevy);
                    $(".slevy .text>div").fadeIn("fast");
                });
            });
            return false;
        });

        // akce
        $(".akce .top .nav a.left").click(function () {
            $.get("/akce_ajax_content.php?page="+(page_akce-1), function(data){
                page_akce--;
                $(".akce .text>div").fadeOut("fast", function(){
                    $(".akce .text>div").html(data);
                    prebarvi_button ("akce", page_akce, pages_akce);
                    $(".akce .text>div").fadeIn("fast");
                });
            });
            return false;
        });

        $(".akce .top .nav a.right").click(function () {
            $.get("/akce_ajax_content.php?page="+(page_akce+1), function(data){
                page_akce++;
                $(".akce .text>div").fadeOut("fast", function(){
                    $(".akce .text>div").html(data);
                    prebarvi_button ("akce", page_akce, pages_akce);
                    $(".akce .text>div").fadeIn("fast");
                });
            });
            return false;
        });

    

    });

    // prebarvi button

    function prebarvi_button(index, page, pages_max) {
        var button = $("."+index+" .top .nav a.left");
        if (page>0) {
            $(button).attr("class","left on");
        } else {
            $(button).attr("class","left off");
        }
        var button = $("."+index+" .top .nav a.right");
        if (page<pages_max-1) {
            $(button).attr("class","right on");
        } else {
            $(button).attr("class","right off");
        }
    }

        $("#logoParade").smoothDivScroll({autoScroll: "always", autoScrollDirection: "endlessloop", pauseAutoScroll: "mouseover" })
});