﻿//<!--
$("#image-rotator").ready(

    function () {

        // BUTTONS //
        // button selection
        var $headlinesDiv = $("#image-rotator .image-rotator-headline");
        var irButtonSelect = function (doAutoStop) {
            if (doAutoStop) {
                autoStop();
            }
            $(this).removeAttr("hover");
            $("#image-rotator .image-rotator-button-active").hide();
            $("#image-rotator .image-rotator-button-inactive").show();
            $(this).show();
            $(this).next().hide();

            if (jQuery.support.opacity) {
                $("#image-rotator .image-rotator-headline[headline!=" + $(this).attr("headline") + "]").fadeOut(2000);
                $("#image-rotator .image-rotator-headline[headline=" + $(this).attr("headline") + "]").fadeIn(2000);
            } else {
                $("#image-rotator .image-rotator-headline[headline!=" + $(this).attr("headline") + "]").hide();
                $("#image-rotator .image-rotator-headline[headline=" + $(this).attr("headline") + "]").show();
            }

            // if user clicks on any button then stop the rotate
            return false;
        }

        var $irButtons = $("#image-rotator .image-rotator-button-active");
        $irButtons.click(function () { irButtonSelect.call(this, true); });

        // hover over items
        var irButtonsHover = function () {
            $(this).prev().attr("hover", "true");
            $(this).hide();
            $(this).prev().show();
        }

        // mouse out of hovered item
        var irButtonsMouseOut = function () {
            if ($(this).attr("hover") == "true") {
                $(this).removeAttr("hover");
                $(this).hide();
                $(this).next().show();
            }
        }

        var $irImgTags = $("#image-rotator .image-rotator-button-inactive");
        $irImgTags.hover(irButtonsHover);

        var $irImgTagsInactive = $("#image-rotator .image-rotator-button-active");
        $irImgTagsInactive.mouseout(irButtonsMouseOut);

        var ImageRotate = function (doAutoStop, move) {
            var $headlinesCount = $("#image-rotator .image-rotator-button-active").size() + 1;
            var $currentHeadline = $("#image-rotator .image-rotator-button-active:visible").attr("headline");
            // check to see if click was left arrow or right arrow

            var id = Number($currentHeadline);
            id = id + move;

            if (id <= 0) {
                id = id + $headlinesCount - 1;
            } else if (id >= ($headlinesCount - 1)) {
                id = 0;
            }

            var headline = $("#image-rotator .image-rotator-buttons div[headline=" + id.toString() + "]");
            irButtonSelect.call(headline, doAutoStop);
        }

        var irAutoId = null;
        if (true) { autoStart(); }

        function autoStart() {
            autoStop();
            // random headline to show
            // modified function to always display first item in list per Mike 11/10/2010
            // var randomnumber = Math.floor(Math.random() * $("#image-rotator div.buttons img.active").size());
            var randomnumber = 0;
            var randomHeadline = $("#image-rotator .image-rotator-buttons div[headline=" + randomnumber.toString() + "]");
            irButtonSelect.call(randomHeadline, false);
            // display headline for x amount of time
            irAutoId = setInterval(function () {
                ImageRotate.call($("#image-rotator .image-rotator-button-active"), false, 1);
            }, 7000);
        }

        function autoStop() {
            if (irAutoId != null) {
                clearInterval(irAutoId);
                irAutoId = null;
            }
        }
    }
);
// -->
