﻿$(document).ready(function() {
    initializeHeaderRotation();
});

var rotationCount = 1;
var intervalID = 0;

function initializeHeaderRotation() {

    if ($('#rotationMainImages img').length > 1) {
        rotateImageSetup(0);

        intervalID = setInterval("rotateHeaderImage()", 4000);
    }
}

function rotateHeaderImage() {

    if (rotationCount > $('#rotationMainImages img').length - 1)
        rotationCount = 0

    rotateImageSetup(rotationCount);

    rotationCount += 1;
}

function rotationImageClick(itemPosition) {
    rotateImageSetup(itemPosition);

    rotationCount = itemPosition + 1;

    clearInterval(intervalID);

    intervalID = setInterval("rotateHeaderImage()", 4000);

}

function rotateImageSetup(index) {

    //set main banner to index image src
    $('h1.top-img-large span').css("background-image", "url(" + $('#rotationMainImages img:eq(' + index + ')').attr('src') + ")");
    
    //hide small banner for index image src
    $('#rotationSmallImages img:eq(' + index + ')').attr('class', '').attr('class', 'rotationImagePosition0');
    $('#rotationSmallImages img:eq(' + index + ')').unbind('click');

    var nextItem = index;

    if ($('#rotationMainImages img').length > 1) {
        nextItem += 1;
        if (nextItem > $('#rotationMainImages img').length - 1)
            nextItem = 0;

        var newclick1 = new Function('rotationImageClick(' + nextItem + ');');
        $('#rotationSmallImages img:eq(' + nextItem + ')').attr('class', '').attr('class', 'rotationImagePosition1');
        $('#rotationSmallImages img:eq(' + nextItem + ')').unbind('click').click(newclick1);
        
    }
    if ($('#rotationMainImages img').length > 2) {
        nextItem += 1;
        if (nextItem > $('#rotationMainImages img').length - 1)
            nextItem = 0;

        var newclick2 = new Function('rotationImageClick(' + nextItem + ');');
        $('#rotationSmallImages img:eq(' + nextItem + ')').attr('class', '').attr('class', 'rotationImagePosition2');
        $('#rotationSmallImages img:eq(' + nextItem + ')').unbind('click').click(newclick2);
    }

    if ($('#rotationMainImages img').length > 3) {
        nextItem += 1;
        if (nextItem > $('#rotationMainImages img').length - 1)
            nextItem = 0;

        var newclick3 = new Function('rotationImageClick(' + nextItem + ');');
        $('#rotationSmallImages img:eq(' + nextItem + ')').attr('class', '').attr('class', 'rotationImagePosition3');
        $('#rotationSmallImages img:eq(' + nextItem + ')').unbind('click').click(newclick3);
    }
}
