﻿(function ($) {
    $.s80Rotator = function (el, options) {

        // avoid scoping issues- reference $(this) as base internally
        var base = this;
        // assign jQuery and DOM versions of element to the plugin
        base.$el = $(el);
        base.el = el;
        // Add a reverse reference to the DOM object
        base.$el.data("s80Rotator", base);

        base.init = function () {
            base.options = $.extend({}, $.s80Rotator.defaultOptions, options);
            base.options.maxSlides = (base.options.maxSlides ? base.options.maxSlides : $(base.options.slideSelector, base.$el).length);
            base.options.slideIndex = (base.options.slideIndex ? base.options.slideIndex : 0);
            base.isPaused = false;
            base.popPanelOpen = false;
            // set up the slides
            base.initSlides();
            // set up the sidebar 
            base.initSideBar();
            // set up the pop pannel 
            if (base.options.showPopPanel) {
                base.initPopPanel();
            }
            // kick off the timer
            base.objInterval = setInterval(function () { base.nextSlide(); }, base.options.interval);
        };

        base.initSlides = function () {
            var htmlcontrols = "";
            $(base.options.slideSelector, base.$el).hide();
            $(base.options.slideSelector + base.options.slideIndex, base.$el).show();
            // set up the slides 
            $(base.options.slideSelector, base.$el).each(function (i) {
                htmlcontrols += "<div class=\"nav\" rel=\"" + (i) + "\">" + (i + 1) + "</div>";
                // fire a redirect if the slide is linked and the user clicks the big image
                if ($(".panel h2 a", this).length > 0) {
                    $(this)
						.click(function () {
						    location.href = $(".panel h2 a", this).attr("href");
						})
						.mouseenter(function () {
						    $(this).css({ cursor: "pointer" });
						});
                }
            });

            $(".controls").html(htmlcontrols + "<div class=\"pause\"></div>");
            $(base.options.slideSelector, base.$el).removeClass("hide")
        };
        base.goTo = function (goToIndex) {
            if (base.options.slideIndex !== goToIndex) {
                clearTimeout(base.objInterval);
                base.navigateToPage(goToIndex);
                base.objInterval = null;
                base.objInterval = setInterval(function () {

                    base.nextSlide();
                }, base.options.interval);
            }
            base.isPaused = false;

        };

        base.initSideBar = function () {
            $("ol", base.$el).children("li").each(function (i) {
                $(this)
				.attr("rel", i)
				.css("cursor", "pointer")
				.click(function (e) {
				    e.preventDefault()
				    base.goTo($(this).attr("rel"));
				    //base.tweaktopSidebar($(this), false);
				})
				.hover(function () {
				    $(this).addClass("hover");
				    if ($(this).hasClass(base.options.sideBarSelector.replace(".", ""))) {
				        base.tweaktopSidebar($(this), false);
				    }
				}, function () {
				    $(this).removeClass("hover");
				    if ($(this).hasClass(base.options.sideBarSelector.replace(".", ""))) {
				        base.tweaktopSidebar($(this), true);
				    }
				});

            });

            $("ul.video", base.$el).children("li.pause").click(function () {
                if (base.isPaused) {
                    $(this).removeClass("play");
                    base.isPaused = false;
                } else {
                    $(this).addClass("play");
                    base.isPaused = true;
                }
            });
            $(".control").click(function (e) {
                e.preventDefault();
            })

        };
        base.tweaktopSidebar = function ($sidebarItem, isCallback) {
            if (parseInt($sidebarItem.attr("rel"), 10) === 0) {
                if (!isCallback) {
                    $(".sidebar .top").addClass("selected");
                }
                else {
                    $(".sidebar .top").removeClass("selected");
                }

            }
            else {
                $(".sidebar .top").removeClass("selected");
            }
        };

        base.nextSlide = function (paramaters) {
            if (!base.isPaused) {
                var nextIndex = (base.options.slideIndex >= base.options.maxSlides - 1 ? 0 : base.options.slideIndex + 1);
                base.navigateToPage(nextIndex);
            }
        };


        base.initPopPanel = function () {
            $(base.options.popPanelOptions.HandleSelector).click(function (event) {
                base.toggleSlide();
                $(".toggle-btn", this).toggleClass("down");
            });
            $(".rotator-slider-content")
				.mouseenter(function () {
				    if (!base.popPanelOpen) {
				        $(base.options.popPanelOptions.HandleSelector).parent().stop().animate({ "top": "315" }, 200);
				    }
				})
				.mouseleave(function () {
				    if (!base.popPanelOpen) {
				        $(base.options.popPanelOptions.HandleSelector).parent().stop().animate({ "top": "322" }, 500);
				    }
				});
        };

        base.navigateToPage = function (newIndex) {
            // fade the current slide and remove active class
            $(base.options.slideSelector + base.options.slideIndex).fadeOut('slow');
            $("#b" + base.options.slideIndex).removeClass("active");
            $("ol.control li:eq(" + base.options.slideIndex + ")").removeClass("active");

            // set the current slide to the slide in the argument
            base.options.slideIndex = parseInt(newIndex);

            // give the new button the active class and set it to display
            $(base.options.slideSelector + base.options.slideIndex).fadeIn('slow');
            $("#b" + base.options.slideIndex).removeClass("hover").addClass("active");
            $("ol.control li:eq(" + base.options.slideIndex + ")").addClass("active");

            //hide the slidepanel if its open and enabled
            if (base.options.showPopPanel) {
                base.popPanelOpen = true;
                base.toggleSlide($("#" + base.options.popPanelOptions.selector));
            }

            // now set the first panel btn to have a different top when selected
            base.options.slideIndex === 0 ? $(".sidebar .top").addClass("selected") : $(".sidebar .top").removeClass("selected");

        };

        base.toggleSlide = function ($popPanel) {

            if (base.popPanelOpen) {
                // set panel off
                $popPanel.parent().animate({ "top": "322" }, 1000, function () { });
                // set direction arrow
                $popPanel.addClass("icon_up");
                $popPanel.removeClass("icon_down");
                base.popPanelOpen = false;
                base.isPaused = false;
            } else {
                // set panel on
                $popPanel.parent().animate({ "top": "13" });
                // set direction arrow
                $popPanel.addClass("icon_down");
                $popPanel.removeClass("icon_up");
                base.popPanelOpen = true;
                base.isPaused = true;
            }
        };

        // Run initializer
        base.init();
    };

    $.s80Rotator.defaultOptions = {
        slideIndex: 0,
        maxSlides: null,
        interval: 5000,
        autoRotate: true,
        sideBarSelector: 'ol.control li',
        slideSelector: '.slide',
        controlSelector: '.control',
        showPopPanel: false,
        CSS3: false,
        popPanelOptions: { panelOn: false, Selector: '#pop-panel', HandleSelector: '.handle', columnSelector: '.col' }
    };

    $.fn.s80Rotator = function (options) {
        return this.each(function () {
            (new $.s80Rotator(this, options));
        });
    };

})(jQuery);
