(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.expand = function(){
			base.isPaused = true;
			$(base.options.slideSelector, base.$el).addClass("LL")
			base.options.mode = "edit";
		};
		
        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;
			

			if(typeof(LLedit) != "undefined"){base.expand();}
								
            // set up the slides
            base.initSlides();
            // set up the sidebar 
            base.initSideBar();
            // set up the pop pannel 
            if (base.options.showPopPanel) {
                base.initPopPanel();
            }
			
			if(typeof(LLedit) != "undefined"){base.expand();}
			if(base.options.mode != "edit"){
            // kick off the timer
            base.objInterval = setInterval(function () { base.nextSlide(); }, base.options.interval);
			}
        };

        base.initSlides = function () {
            var htmlcontrols = "";
            $(base.options.slideSelector +":gt(0)", base.$el).hide();
            $(base.options.slideSelector + base.options.slideIndex, base.$el).show();
            // set up the slides 
            $(base.options.slideSelector, base.$el).each(function (i) {	
				var style = i = 0?"class=\"active\"":"";
                htmlcontrols += "<li " + style +" rel=\""+ i +"\" ><h3><a href=\""+ $(".panel h2 a",this).attr("href")+"\">"+ $(".panel h2 a",this).text() + "</a></h3>&nbsp;</li>";
                // fire a redirect if the slide is linked and the user clicks the big image
                if (($(".panel h2 a",this ).length >0) && (base.options.mode !="edit")) {
                    $(this)
						.click(function () {
						    location.href = $(".panel h2 a",this ).attr("href");
						})
						.mouseenter(function () {
						    $(this).css({ cursor: "pointer" });
						});
						
                }
				$(".panel a",this).attr("tabindex","-1")


				
					
            });

             base.$el.append("<ol class=\"control\">"+ htmlcontrols + "</ol><ul class=\"control video\"><li class=\"pause\">&nbsp;<a href=\"#\" title=\"stop\"><span class=\"hide\">stop</span></a></li></ul>");
			 
			$(base.options.slideSelector, base.$el).removeClass("hide")
        };
        base.goTo = function (goToIndex) {
		//console.log(goToIndex);
            if (base.options.slideIndex !== goToIndex) {
                clearTimeout(base.objInterval);
                base.navigateToPage(goToIndex);
                base.objInterval = null;
                base.objInterval = setInterval(function () {

                    base.nextSlide();
                }, base.options.interval);
            }
			if(base.options.mode !="edit"){
            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"))
				})
				.focus(function (e){
					e.preventDefault();
					$(this).addClass("hover");
				})
				.blur(function (e){
					e.preventDefault();
				 	$(this).removeClass("hover");
				})
				.hover(function () {
				    $(this).addClass("hover");
				}, function () {
				    $(this).removeClass("hover");
				});

            });

            $("ul.video",base.$el).children("li.pause").click(function (e) {
			 	e.preventDefault()
                if (base.isPaused) {
                    $(this).removeClass("play");
					$("span",this).text(function(){return ($(this).text().replace("stop","continue"))})
					$("a",this).attr("title",(function(){return ($(this).text().replace("stop","continue"))}))
                    base.isPaused = false;
                } else {
                    $(this).addClass("play");
					$("span",this).text(function(){return ($(this).text().replace("continue","stop"))})
					$("a",this).attr("title",(function(){return ($(this).text().replace("stop","continue"))}))
                    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 +":eq(" + 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 + ":eq(" + 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 = {
		mode:"",
        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);






