﻿
function loadrotator() {
    var r_image;
    for (var i = 0; i < rotator.length; i++) {
        r_image = new Image();
        r_image.src = rotator[i][0];
        r_image.onclick = function() { window.location = rotator[i][1]; }
        $('#rotator_preloader').append(r_image);
    }
}

$(document).ready(function() {

    function startrotate(speed) {
        clearInterval(play);
        if (!block) {
            curIndex++;
            if (curIndex > rotator.length - 1) curIndex = 0;
            $('#imagerotator').fadeOut(speed, function() {
                rotate(speed);
            });
        }
        else {
            play = setInterval(function() { startrotate(speed); }, 1000);
        }
    }

    function rotate(speed) {
        $('#imagerotator').src = rotator[curIndex][0];
        if (rotator[curIndex][0] != '') {
            $('#imagerotator').attr("src", rotator[curIndex][0]);
        }
        else {
        }
        $(".paging a").removeClass('active');
        $('#img_' + curIndex).attr('class', 'active');
        $('#imagerotator').fadeIn(speed, function() {
            finishrotate();
        });
    }

    function finishrotate() {
        clearInterval(play);
        $('#activelink').attr("href", rotator[curIndex][3]);
        $('#imagedecorator').attr("src", rotator[curIndex][0]);
        if (!block) {
            play = setInterval(function() { startrotate('slow'); }, interval);
        }
        else {
            play = setInterval(function() { finishrotate(); }, 1000);
        }
    }

    function init() {
        for (var i = 0; i < rotator.length; i++) {
            var anchor = document.createElement('a');
            anchor.setAttribute('class', 'link');
            $('.paging').append($('<a id="img_' + i + '">' + (i + 1) + '</a>'));
        }
        $(".paging a").click(function() {
            clearInterval(play);
            curIndex = parseInt(this.innerHTML) - 1;
            rotate('fast');
            return false;
        });
        $(".imagerotator a").hover(function() {
            block = true;
        }, function() {
            block = false;
        });
        $('#img_' + curIndex).attr('class', 'active');
        window.setTimeout(function() { startrotate('slow'); }, interval);
    }
    init();
});
