//Object.prototype.addEvent = function(evtType, func) {
//   if (this.addEventListener) {
//      this.addEventListener(evtType, func, true);
//   } else if (this.attachEvent) {
//      this.attachEvent('on' + evtType, func);
//   } else {
//      this['on' + evtType] = func;
//   }
//}

var SLIDE_FADE= 1;
var SLIDE_SLIDE= 2;

function SlideShow(slideel, fadingSpeed, stopTime, stopOnMouseOver, method) { 
        var mouseIsOver = false;
        var nexttime= null;
        
        if(method==null)
                method= SLIDE_FADE;
     
        if (stopOnMouseOver) {
//                slideel.addEvent('mouseover', function() {
                addEvent(slideel, 'mouseover', function() {
                        mouseIsOver = true;
                });

//                slideel.addEvent('mouseout', function() {
                addEvent(slideel, 'mouseout', function() {
                        mouseIsOver = false;
                        if(nexttime!=null) {
                                window.clearTimeout(nexttime);
                                self.next();
                        }
                });
        }
               
        this.next = function() {
                nexttime= null;
                if (mouseIsOver) {
                        window.setTimeout(function() {self.next();}, 500);
                        return;
                }
                this.current.stop();
                this.current = this.current.nextSlide;
                this.current.start(false);
        }
       
        function createSlides() {
                var imgs = slideel.getElementsByTagName('img');
                var slides = [];
               
                for (var i = 0; i < imgs.length; i++) {  
                        slides[i] = new Slide(imgs[i], self);
                }
               
                for (var i = 0; i < slides.length; i++) {
                        if (i == slides.length - 1)
                                slides[i].nextSlide = slides[0];
                        else
                                slides[i].nextSlide = slides[i + 1];
                }
               
                self.current =  slides[i-1];
                self.current.start(true);
               
                function Slide(img, slideShow) {
                        Opacity(img, 0);

                        this.start = function(now) {
                                steps= now? 0: 40;
                                window.setTimeout(function() {
                                        fadeIn(steps);
                                }, fadingSpeed);
                        }
       
                        this.stop = function() {
                                window.setTimeout(function() {
                                        fadeOut(40);
                                }, fadingSpeed);
                        }
                        function fadeIn(i) {
                                if(method==SLIDE_FADE) 
                                        Opacity(img, 1. - i/40.);
                                else {        
                                        Opacity(img, 1);
                                        img.parentNode.style.left= (img.scrollWidth * i/40.) + "px";
                                }
                                if(--i>=0) {
                                        window.setTimeout(function() {
                                                fadeIn(i);
                                        }, fadingSpeed);
                                } else {
                                        nexttime= window.setTimeout(function() {
                                                self.next();
                                        }, 40 * fadingSpeed + stopTime);
                                }
                        }       
                        function fadeOut(i) {
                                outtime= null;
                                if(method==SLIDE_FADE) 
                                        Opacity(img, i/40.);
                                else        
                                        img.parentNode.style.left= img.scrollWidth * (i/40. - 1.) + "px";
                                if(--i>=0) {
                                        window.setTimeout(function() {
                                                fadeOut(i);
                                        }, fadingSpeed);
                                } else
                                        Opacity(img, 0);                                
                        }       
                        function Opacity(obj, value) {
                              	obj.style.opacity = value;
                              	obj.style.filter = 'alpha(opacity=' + value*100 + ')';
                        }
                }
        }
      
        var self = this;
        createSlides(slideel);
}
