﻿// set scrolling indexs
homeminindex = 1;
homemaxindex = 4; //loaded from code
homeimageindex = 1;

$().ready(function () {
    rotateTimer = setTimeout('rotateTab()', 5000);

    $(".homespotlight .pages li a").click(function () {

        // Resetting selected class
        $(".homespotlight .pages li a").get(homeimageindex).setAttribute("class", "");
        $(".homespotlight .pages li a").get(homeimageindex).setAttribute("className", "");

        // getting index of clicked href
        if ($(this).attr("class") == "prev") {
            if (homeimageindex > homeminindex) {
                homeimageindex -= 1;
            }
        }
        else if ($(this).attr("class") == "next") {
            if (homeimageindex < homemaxindex) {
                homeimageindex += 1;
            }
        }
        else {
            homeimageindex = $(".homespotlight .pages li a").index(this);
        }

        // get path and title of clicked href
        var largePath = $(".homespotlight .pages li a").get(homeimageindex).rev;
        var largeRel = $(".homespotlight .pages li a").get(homeimageindex).rel;
        var largeAlt = $(".homespotlight .pages li a").get(homeimageindex).title;


        $('#mainimage').fadeTo('fast', 0.1, function () {
            $('#homeimage').attr({ src: largeRel, alt: largeAlt });
            $('#homeimagelnk').attr({ href: largePath });

            $(".homespotlight .pages li a").get(homeimageindex).setAttribute("class", "on");
            $(".homespotlight .pages li a").get(homeimageindex).setAttribute("className", "on");

            $('#mainimage').fadeTo('fast', 1);

        });

        //OR NO FADE for CLICKs
        // set selected class
        //$(".homespotlight .pages li a").get(homeimageindex).setAttribute("class", "on");
        //$(".homespotlight .pages li a").get(homeimageindex).setAttribute("className", "on");

        //// change image src and title
        //$("#homeimage").attr({ src: largeRel, alt: largeAlt });
        //$('#homeimagelnk').attr({ href: largePath });

        clearTimeout(rotateTimer);

        return false;
    });

});

function rotateTab() {
    if ($(".homespotlight").length > 0) {

        $(".homespotlight .pages li a").get(homeimageindex).setAttribute("class", "");
        $(".homespotlight .pages li a").get(homeimageindex).setAttribute("className", "");

        if (homeimageindex < homemaxindex) {
            homeimageindex += 1;
        }
        else {
            homeimageindex = 1;
        }

        //update image
        var largePath = $(".homespotlight .pages li a").get(homeimageindex).rev;
        var largeRel = $(".homespotlight .pages li a").get(homeimageindex).rel;
        var largeAlt = $(".homespotlight .pages li a").get(homeimageindex).title;


        $('#mainimage').fadeTo('fast', 0.1, function () {
            $('#homeimage').attr({ src: largeRel, alt: largeAlt });
            $('#homeimagelnk').attr({ href: largePath });

            $(".homespotlight .pages li a").get(homeimageindex).setAttribute("class", "on");
            $(".homespotlight .pages li a").get(homeimageindex).setAttribute("className", "on");

            $('#mainimage').fadeTo('fast', 1);

        });

        //reset timer//
        rotateTimer = setTimeout('rotateTab()', 5000);
    }
}

