﻿var images = new Array();
var moving = true;
var currentImage = 0;

function showNextBtn() {
    currentImage++;
    if (currentImage >= images.length) {
        currentImage = 0;
    }
    $(".homeRotator .buttons .button").each(function () {
        $(this).removeClass("highlight");
    });
    $(".homeRotator .buttons div[image='" + currentImage + "']").addClass("highlight");
}
function swapImage(force) {
    force = typeof (force) != 'undefined' ? force : false;
    if (moving || force) {
        $(".homeRotator img:eq(0)").insertAfter($(".homeRotator img:last"));
        showNextBtn();
    }
}

function setup() {
    $(".homeRotator").prepend("<div class='buttons' style='position:absolute; width:700px'></div>");
    $(".homeRotator img").each(function (index) {
        $(".homeRotator .buttons").prepend("<div class='button' image='" + index + "' style='cursor:pointer; height:10px; width:10px; margin:2px; float:right;'></div>");

        images[index] = $(".homeRotator img:eq(" + index + ")").attr("src");
        $(".homeRotator .buttons div[image='0']").addClass("highlight");
    });
    $(".homeRotator .buttons .button").hover(function () {
        moving = false;
    }, function () {
        moving = true;
    }).click(function () {

        setupInOrder($(this).attr("image"));
    });
    setInterval(swapImage, 5000);
}
function setupInOrder(index) {
    var found = false;
    while (!found) {
        swapImage(true);

        if ($(".homeRotator img:eq(0)").attr("src") == images[index]) {
            found = true;
        }
    }
}
$(document).ready(function () {
    setup();
});
