(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 ($(this).attr("rel").length >= 1) {
                    $(this)
						.click(function () {
						    location.href = $(this).attr("rel");
						})
						.mouseenter(function () {
						    $(this).css({ cursor: "pointer" });
						});
                }
            });

            $(".controls").html(htmlcontrols + "<div class=\"pause\"></div>");

        };
        base.goTo = function (goToIndex) {
            if ((base.options.slideIndex !== goToIndex) && (goToIndex> -1) && (goToIndex <= base.options.maxSlides-1)){ 
                clearTimeout(base.objInterval);
                base.navigateToPage(goToIndex);
                base.objInterval = null;
                base.objInterval = setInterval(function () { 
					 
					 base.nextSlide(); }, base.options.interval);
            }
            base.isPaused = false;

        };

        base.initSideBar = function () {
            $(base.options.controlSelector + "," + base.options.sideBarSelector, base.$el).each(function () {
                $(this)
				.css("cursor", "pointer")
				.click(function () {
				    base.goTo($(this).attr("rel"));
				    base.tweaktopSidebar($(this), false);
				})
				.hover(function () {
				    //$(this).addClass("hover");
					var posY =  $(this).css('backgroundPosition').split(" ")[1].replace("px","")
						console.log($(this).css('backgroundPosition'))
					posY = parseInt(posY)
				
					posY +=10
					 $(this).css("backgroundPosition", "0px " + posY + "px");

				    if ($(this).hasClass(base.options.sideBarSelector.replace(".", ""))) {
				        base.isPaused = true;
				    }
				}, function () {
				    //$(this).removeClass("hover");
					$(this).css("backgroundPosition","");
				    if ($(this).hasClass(base.options.sideBarSelector.replace(".", ""))) {
				        base.isPaused = false;
				    }
				});

            });

            $(".pause").click(function () {
                if (base.isPaused) {
                    $(this).removeClass("play");
                    base.isPaused = false;
                } else {
                    $(this).addClass("play");
                    base.isPaused = true;
                }
            });
			
			//$(".btn:eq(1)",base.$el).append("<b class=\"separator\"/>")
			$(".up").click(function (e) {
				e.preventDefault()
				base.goTo(base.options.slideIndex-1);
			})
			$(".down").click(function (e) {
				e.preventDefault()
				base.goTo(base.options.slideIndex+1);
			})
			};
        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");
				//$(".nav:eq(" + base.options.slideIndex + ")", base.$el).removeClass("active");
				//var imgheight =  $("#b" + base.options.slideIndex + " img", base.$el).height()


				if (newIndex> 0){
					$(".sidebar .btnwrap").stop().animate({top: "-" + ((newIndex *96)-20) })
				}
				else{
					$(".sidebar .btnwrap").stop().animate({top:"20"  })

				}

				// 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");
				
				$(".nav:eq(" + base.options.slideIndex + ")", base.$el).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: '.btn',
        slideSelector: '.slide',
        controlSelector: '.nav',
        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);






