﻿//<!--

$("#image-rotator").ready(

    function() {

        // BUTTONS //
        // button selection
        var $headlinesDiv = $("#image-rotator div.headline");
        var irButtonSelect = function(doAutoStop) {
            if (doAutoStop) {
                autoStop();
            }
            $(this).removeAttr("hover");
            $("#image-rotator div.buttons img.inactive").hide();
            $("#image-rotator div.buttons img.active").show();
            $(this).show();
            $(this).prev().hide();

            if (jQuery.support.opacity) {
                $("#image-rotator div.headline[headline!=" + $(this).attr("headline") + "]").fadeOut(2000);
                $("#image-rotator div.headline[headline=" + $(this).attr("headline") + "]").fadeIn(2000);
            } else {
                $("#image-rotator div.headline[headline!=" + $(this).attr("headline") + "]").hide();
                $("#image-rotator div.headline[headline=" + $(this).attr("headline") + "]").show();
            }

            // if user clicks on any button then stop the rotate
            return false;
        }

        var $buttonImgTags = $("#image-rotator div.buttons img.inactive");
        $buttonImgTags.click(irButtonSelect, true);

        // hover over items
        var irButtonsHover = function() {
            $(this).next().attr("hover", "true");
            $(this).hide();
            $(this).next().show();
        }

        // mouse out of hovered item
        var irButtonsMouseOut = function() {
            if ($(this).attr("hover") == "true") {
                $(this).removeAttr("hover");
                $(this).hide();
                $(this).prev().show();
            }
        }

        var $irImgTags = $("#image-rotator div.buttons img.active");
        $irImgTags.hover(irButtonsHover);

        var $irImgTagsInactive = $("#image-rotator div.buttons img.inactive");
        $irImgTagsInactive.mouseout(irButtonsMouseOut);

        var ImageRotate = function(doAutoStop, move) {
            var $headlinesCount = $("#image-rotator div.buttons img.active").size() + 1;
            var $currentHeadline = $("#image-rotator div.buttons img.inactive: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 div.buttons img[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 div.buttons img[headline=" + randomnumber.toString() + "]");
            irButtonSelect.call(randomHeadline, false);
            // display headline for x amount of time
            irAutoId = setInterval(function() {
                ImageRotate.call($("#image-rotator div.buttons img.arrow-inactive[id=right-arrow]"), false, 1);
            }, 7000);
        }

        function autoStop() {
            if (irAutoId != null) {
                clearInterval(irAutoId);
                irAutoId = null;
            }
        }
    }
);
// -->
