(function($) {
        
        $.fn.goAutoXFader = function(settings) {
            return this.each(function() {
                $(this).data('goAutoXFader', new $.goAutoXFader(this, settings));
            });
        };

        var ulcss ={
            "listStyle": "none",
            "margin": "0px",
            "padding": "0px",
            "display": "block",
            "position": "relative",
            "overflow": "hidden"
        }
        var licss = {
            "margin": "0px",
            "position": "absolute",
            "top": "0px",
            "left": "0px"
        }
        
        $.goAutoXFader=function(elem, settings) {
            var self=this;
            this.playing=true;
            this.ptr=-1;
            this.timer=null;
            this.container=$(elem);
            this.container.css("position", "relative");
            this.list=this.container.children("ul").first();
            this.slides=this.list.children("li");
            
            this.list.css(ulcss);
            this.list.css("width", this.container.css("width"));
            this.list.css("height", this.container.css("height"));
            this.slides.css(licss);
            this.slides.hide();
            
            this.nextSlide=function() {
                var n=this.ptr+1;
                if (n>=this.slides.length) n=0;
                this.showSlide(n);
            };
            this.showSlide=function(n) {
                if (this.ptr!=n) {
                    var slide1=this.slides[this.ptr];
                    this.ptr=n;
                    var slide2=this.slides[this.ptr];
                    $(slide1).fadeOut("slow");
                    $(slide2).fadeIn("slow");
                    this.button_list.find("a").removeClass("selected");
                    this.button_list.find("a[href=#"+n+"]").addClass("selected");
                   // alert("#"+n+" matches "+this.button_list.find("a[href=#"+n).length)
                    
                }
            }
            this.container.mouseenter(function() {
                self.playing=false;
            });
            this.container.mouseleave(function() {
                self.playing=true;
            });
            this.play=function() {
                if(this.timer) window.clearInterval(this.timer);
                this.timer = window.setInterval(function(){
                    if (self.playing) self.nextSlide();
                }, 9000);
            }
            this.pause=function() {
                if(this.timer) window.clearInterval(this.timer);
            }
            this.button_list = $("<ul class='xfader-buttons'></ul>");
            this.container.append(this.button_list);
            this.button_list.css("position", "absolute");
            //this.button_list.css("bottom", "10px");
            //this.button_list.css("right", "10px");
            for (var i=0; i<this.slides.length; i++) {
                this.button_list.append("<li><a href='#"+i+"'>"+(i+1)+"</a></li>");
            }
            this.button_list.find("a").click(function() {
                var n = $(this).attr("href").split("#")[1];
                self.showSlide(Number(n));
                self.play();
                return false;
            });
            this.showSlide(0);
            this.play();            
        }
        
        
    })(jQuery);
