﻿/// <reference path="../Javascript/jQueryIntellisense.js"/>
/*
* jQuery RotatingFeature Plugin
* Caudill Web
*/

(function ($) {

    // IMPORTANT: the plugin name below must be manually edited so that it starts with a lower-case letter.
    // For example, if the control is named PizzaMaker.ascx, this needs to be changed from $.fn.PizzaMaker to $.fn.pizzaMaker = ....
    $.fn.rotatingFeature = function (options) {

        var defaults = {}; // set defaults in RotatingFeature.ascx.vb
        var config = $.extend(defaults, options);
        this.each(function () {

            // ELEMENTS

            var self = this, $self = $(this);
            var $rotatingFeatureController = $("div.RotatingFeatureController", self);


            var rotatingFeatureCount = 0;
            var rotatingFeatureDelay = (config.delay || 8) + 's'; // Default value - overriden by server-side value based on the Delay property in RotatingFeature.ascx
            var rotatingFeatureMaxRotations = config.maxRotations || 20;  // Default value - overriden by server-side value based on the MaxRotations property in RotatingFeature.ascx

            // PUBLIC 

            // PRIVATE 

            function initialize() {
                rotatingFeatureCount = $self.find('div.Feature').length;
                if (rotatingFeatureCount > 1) {
                    // Wire up automatic rotation
                    $self.everyTime(
                        rotatingFeatureDelay,
                        "RotatingFeatureTimer",
                        function () {
                            currentItem = parseInt($rotatingFeatureController.find('a.On:first').text());
                            // Hide the current item
                            featureHide($rotatingFeatureController.find('a')[currentItem - 1]);
                            // Show the next item
                            currentItem += 1;
                            if (currentItem > rotatingFeatureCount) currentItem = 1;
                            featureShow($rotatingFeatureController.find('a')[currentItem - 1]);
                        },
                        rotatingFeatureMaxRotations
                    );
                    // Add navigation links
                    for (var i = 1; i < rotatingFeatureCount + 1; i++) {
                        $rotatingFeatureController.append(String.format('<a href="#{0}">{0}</a>', i));
                    }
                    // Highlight the first item
                    featureShow($rotatingFeatureController.find('a')[0]);
                    $rotatingFeatureController.fadeIn('slow');
                    // Wire up hover on navigation links
                    $rotatingFeatureController.find('a').hover(function () {
                        if ($(this).hasClass('On') == false) {
                            $rotatingFeatureController.stopTime("RotatingFeatureTimer"); // stop rotation
                            featureHide($rotatingFeatureController.find('a.On')[0]);
                            featureShow(this);
                        }
                    });
                    // Stop automatic rotation on hover 
                    $self.hover(function () {
                        $self.stopTime("RotatingFeatureTimer");
                    });

                }
            } // initialize

            function featureShow(linkElement) {
                $rotatingFeatureController.find('a').removeClass('On');
                featureId = parseInt($(linkElement).text());
                $(linkElement).addClass('On');
                var $selectedFeature = $($self.find('div.Feature')[featureId - 1]);
                $selectedFeature.fadeIn('slow');
                var spotlightColor = $("div.RotatingFeatureCaption", $selectedFeature).attr("color");
                $("div#RotatingFeatureControllerWrapper").attr("class", spotlightColor);
            };

            function featureHide(linkElement) {
                featureId = parseInt($(linkElement).text());
                $($self.find('div.Feature')[featureId - 1]).fadeOut('slow');
            };


            // INITIALIZATION

            initialize();

        }); // each

        return this; // don't break the chain

    }; // $.fn.RotatingFeature 

})(jQuery);



