$(document).ready(function () {

    ///create images:
    for (var i = 0; i < maxImages; i++) {
        var img = new Image();
        thumb = new Image();

        img.src = absPath + paths[i];
        img.className = "flash_image";
        img.alt = "Exercisesoftware Image" + i;
        img.id = i + "_image";

        images.push(img);
        $(".flash_lenta").append(img);

        //add thumbs
//        thumb = new Image();
//        thumb.src = absPath + "_" + paths[i];
//        thumb.alt = "Exercisesoftware thumb" + i;
//        thumb.className = "flash_thumb";
//        thumb.id = i + "_thumb";
//        thumbs.push(thumb);
//        $(".flash_thumbs").append(thumb);

        ///add controls
        control = $("<div/>");
        control.addClass("flash_control");
        control.attr("id", i + "_control");
        $(".flash_controls").append(control);
        controls.push(control);

        if (i > 0) {
            $(img)
                .hide()
        } else {
            $(img).addClass("active");
            //$(thumb).addClass("active");
            $(control).addClass("active");
        }
    }

    $(".flash_control").bind("click", function () {
        rotateImage.call(this, this);
    });
//    $(".flash_thumb").bind("click", function () {
//        rotateImage.call(this, this);
//    });

    interval = setTimeout(function () { rotateImage(); }, changeTimeout);

    ///render images:
    function rotateImage(a) {

        $(".flash_image").removeClass("active");
        //$(".flash_thumb").removeClass("active");
        $(".flash_control").removeClass("active");

        $(images[activeImage])
                .fadeOut(changeTimeout);

        if (typeof (a) != "undefined" && (a.tagName.toLowerCase() == "div" || a.tagName.toLowerCase() == "img")) {
            clearTimeout(interval);
            activeImage = parseInt($(this).attr("id"));
        } else {
            activeImage++;
        }
        if (activeImage > maxImages - 1 || activeImage < 0) {
            activeImage = 0;
        }

        for (var i = 0; i < maxImages; i++) {
            if (i == activeImage) {
                $(images[i]).fadeIn(changeTimeout / 2);

                $(images[i]).addClass("active");
                //$(thumbs[i]).addClass("active");
                $(controls[i]).addClass("active");

            }
        }
        interval = setTimeout(function () { rotateImage(); }, changeTimeout);
    }

});
