﻿(function (a) {
    //Attach this new method to jQuery
    $.fn.extend({

        //This is where you write your plugin's name
        spotlight: function () {

            /* Spotlight rotation */
            function contentRotate(feature) {
                if (doAnimate) {
                    feature.fadeOut("slow", function (feature) {
                        return function () {
                            $(".spotlight-content div").hide();

                            /* Select correct spotlight # */
                            if ($(this).attr("id") == "one") {
                                $(".spotlight-nav .selected").removeClass("selected");
                                $(".spotlight-content .selected").removeClass("selected");
                                $(".num-1").addClass("selected");
                            }
                            else if ($(this).attr("id") == "two") {
                                $(".spotlight-nav .selected").removeClass("selected");
                                $(".spotlight-content .selected").removeClass("selected");
                                $(".num-2").addClass("selected");
                            }
                            else if ($(this).attr("id") == "three") {
                                $(".spotlight-nav .selected").removeClass("selected");
                                $(".spotlight-content .selected").removeClass("selected");
                                $(".num-3").addClass("selected");
                            }
                            else if ($(this).attr("id") == "four") {
                                $(".spotlight-nav .selected").removeClass("selected");
                                $(".spotlight-content .selected").removeClass("selected");
                                $(".num-4").addClass("selected");
                            }
                            else if ($(this).attr("id") == "five") {
                                $(".spotlight-nav .selected").removeClass("selected");
                                $(".spotlight-content .selected").removeClass("selected");
                                $(".num-5").addClass("selected");
                            }
                            else if ($(this).attr("id") == "six") {
                                $(".spotlight-nav .selected").removeClass("selected");
                                $(".spotlight-content .selected").removeClass("selected");
                                $(".num-6").addClass("selected");
                            }
                            else if ($(this).attr("id") == "seven") {
                                $(".spotlight-nav .selected").removeClass("selected");
                                $(".spotlight-content .selected").removeClass("selected");
                                $(".num-7").addClass("selected");
                            }

                            /* Fade in next item or go back to the first */
                            feature.fadeIn("slow", function () {
                                if ($(this).attr("id") == "seven") {
                                    setTimeout(function () {
                                        contentRotate($(".spotlight-content div:first"));
                                    }, 6000);
                                }
                                else {
                                    setTimeout(function () {
                                        contentRotate($(feature.next()));
                                    }, 6000);
                                }
                            });
                        };
                    } (feature));
                }
            }

            var doAnimate = true;
            var nav_buttons = $(this).find('.spotlight-nav div');
            var spotlight_images = $(this).find('.spotlight');

            contentRotate($(".spotlight-content div:first"));

            nav_buttons.each(function () {
                $(this).click(function () {
                    $(".spotlight-content div").hide();
                    nav_buttons.removeClass('selected');
                    $(this).addClass('selected');
                    spotlight_images.filter($(this).attr('rel')).fadeIn();
                    doAnimate = false;
                });
            });
        }
    });
    //pass jQuery to the function,   
    //So that we will able to use any valid Javascript variable name   
    //to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )         
})(jQuery)
