var EGDSlide = new Class ({
    options: {
        currentImage : 0,
        idx : -1,
        onetime: false,
        timer: null,
        timming: 0,
        width: 800,
        height: 400,
        autoplay: 1,
        interval: 1000,
        duration: 1000,
        effect: 3,
		direction: 1,
        rotateaction: null,
        imglist : null
    },
    initialize: function(options) {
        this.setOptions(options);
        x = this.options;
        this.activateSlide();
    },
    activateSlide: function() {
         if (x.imglist.length) {
            //-- Show the first img --
            x.imglist[0].setStyle('display','block');
            x.currentImage  = x.imglist[0];
            x.idx           = 0;
            switch (x.effect) {
                case 1:
                    this.scrollthem();
                    break;
                case 2: 
                    this.fadethem();
                    break;
                case 3:
                default:
                    this.crossfadethem();   
            }
         }
    },
    scrollthem: function() {
        //-- Need to reset the position of image --
		if (x.direction == 1) {
			x.imglist.each(function(el,i) {
				el.setStyle("left",i*x.width+"px");            
			});
		}
		if (x.direction == 2) {
			x.imglist.each(function(el,i) {
				el.setStyle("left",-1*i*x.width+"px");            
				el.setStyle("z-index",999-i);            
			});
		}
		if (x.direction == 3) {
			x.imglist.each(function(el,i) {
				el.setStyle("top",-1*i*x.height+"px"); 
				el.setStyle("z-index",999-i); 				
			});
		}		
		if (x.direction == 4) {
			x.imglist.each(function(el,i) {
				el.setStyle("top",i*x.height+"px");            
			});
		}
        //-- Now, do scroll --
        if (x.autoplay == 1)
            x.timer = this.doscroll.periodical(x.interval);
        if (x.autoplay == 2) {
            x.onetime = true;
            x.timer = this.doscroll.periodical(x.interval);
        }
        if (x.autoplay == 3) {
           var el = $('simple_bigPic');
              switch (x.rotateaction) {
                   case 1:
                        el.addEvent('click',function() {
                            var _n = new Date();
                            this.doscroll(_n.getTime()); 
                        }.bind(this));   
                        break;
                   case 2: 
                        el.addEvent('mouseenter',function() {
                            this.doscroll();
                            x.timer = this.doscroll.periodical(x.interval);
                        }.bind(this));
                        el.addEvent('mouseleave',function() {
                            $clear(x.timer);
                        }.bind(this));
                        break;
              } 
        }
    },
    doscroll: function(starttime) {
        if (starttime) {
            if (starttime - x.timming < x.duration) return;   
        }
        x.timming = starttime;
        x.imglist.each (function(el,i) {
            var fx = new Fx.Styles(el, {duration:x.duration, wait:false});
            el.setStyle("display","block");
			if (x.direction == 1) {
				fx.start ({
					left: (x.idx + i -1) * x.width + "px"
				});            
			} 
			if (x.direction == 2) {
				fx.start ({
					left: -1 * (x.idx + i -1) * x.width + "px"
				});            
			}
			if (x.direction == 3) {
				fx.start ({
					top: -1 * (x.idx + i -1) * x.height + "px"
				});            
			}
			if (x.direction == 4) {
				fx.start ({
					top: (x.idx + i -1) * x.height + "px"
				});            
			}
        });
        x.idx = x.idx - 1;
        if (x.idx <= -1 * (x.imglist.length - 1)) {
            if (x.onetime) {
                $clear(x.timer);   
            } else {
                x.idx = 1;
            }            
        }
    },
    fadethem: function() {
        //-- Init fading --
        x.imglist.each (function(el,i) {
            if (i > 0) {
                el.setStyle("display","block");
                el.setStyle("opacity","0");
            }
        });
        if (x.autoplay == 1)
            x.timer = this.dofade.periodical(x.interval);
        if (x.autoplay == 2) {
            x.onetime = true;
            x.timer = this.dofade.periodical(x.interval);
        }
        if (x.autoplay == 3) {
           var el = $('simple_bigPic');
              switch (x.rotateaction) {
                   case 1:
                        el.addEvent('click',function() {
                            var _n = new Date();
                            this.dofade(_n.getTime()); 
                        }.bind(this));   
                        break;
                   case 2: 
                        el.addEvent('mouseenter',function() {
                            this.dofade();
                            x.timer = this.dofade.periodical(x.interval);
                        }.bind(this));
                        el.addEvent('mouseleave',function() {
                            $clear(x.timer);
                        }.bind(this));
                        break;
              } 
        }
    },
    dofade: function(starttime) {
        if (starttime) {
            if (starttime - x.timming < 2*x.duration) return;   
        }
        x.timming = starttime;
        var fx = new Fx.Styles(x.currentImage, {duration:x.duration, wait:false});
        var next = x.idx + 1;
        if (next > x.imglist.length - 1) {  
            next = 0;
            if (x.onetime) {                
                $clear(x.timer);
            } 
        }
        var fx1 = new Fx.Styles(x.imglist[next], {duration:x.duration, wait:false});
        fx.start({
            opacity: 0
        });
        setTimeout(function() {
            fx1.start({
                opacity: 1
            });
        },x.duration);
        x.currentImage = x.imglist[next];
        x.idx = next;
    },
    crossfadethem: function() {
        //-- Init fading --
        x.imglist.each (function(el,i) {
            if (i > 0) {
                el.setStyle("display","block");
                el.setStyle("opacity","0");
            }
        });
        if (x.autoplay == 1)                    
            x.timer = this.dofadecross.periodical(x.interval);
        if (x.autoplay == 2) {
            x.onetime = true;
            x.timer = this.dofadecross.periodical(x.interval);
        }
        if (x.autoplay == 3) {
           var el = $('simple_bigPic');
              switch (x.rotateaction) {
                   case 1:
                        el.addEvent('click',function() {
                            var _n = new Date();
                            this.dofadecross(_n.getTime()); 
                        }.bind(this));   
                        break;
                   case 2: 
                        el.addEvent('mouseenter',function() {
                            this.dofadecross();
                            x.timer = this.dofadecross.periodical(x.interval);
                        }.bind(this));
                        el.addEvent('mouseleave',function() {
                            $clear(x.timer);
                        }.bind(this));
                        break;
              }
        }
    },
    dofadecross: function(starttime) {
        if (starttime) {
            if (starttime - x.timming < x.duration) return;   
        }
        x.timming = starttime;
        var fx = new Fx.Styles(x.currentImage, {duration:x.duration, wait:false});
        var next = x.idx + 1;
        if (next > x.imglist.length - 1) {
            next = 0;                         
            if (x.onetime) {
                $clear(x.timer);
            } 
        }
        var fx1 = new Fx.Styles(x.imglist[next], {duration:x.duration, wait:false});
        fx.stop();
        fx.start({
            opacity: 0
        });
        fx1.stop();
        fx1.start({
            opacity: 1
        });
        x.currentImage = x.imglist[next];
        x.idx = next;
    }
});
EGDSlide.implement(new Options);

