
function SlideEffect () {
    this.element = null;
    
    this.layer = null;
    this.image = null;
    
    this.src = null;
    
    this.size = null;
    
    this.display = {
        background : SlideEffect.DEFAULT_DISPLAY_BACKGROUND
    }
    
    var self = this;
    
    this.animation = {
            increment : SlideEffect.DEFAULT_ANIMATION_INCREMENT,
            refresh : SlideEffect.DEFAULT_ANIMATION_REFRESH,
            direction : SlideEffect.DEFAULT_ANIMATION_DIRECTION,
            getNumberOfIteration : function () {
                    // bug fix : tenir compte des parametres !!
                    if (self.size.x == null) {
                        return 0;
                    }
                    
                    if (this.increment == null) {
                        return 0;
                    }
                    
                    var result = Math.abs(self.size.x / this.increment);
                    
                    return Math.ceil(result);
                }
        };
    
    this.slideEffectListeners = new Array();
    
}

SlideEffect.EVENT_EFFECT_SLIDE = 'SlideEffect';

SlideEffect.DEFAULT_DISPLAY_BACKGROUND = '#ffffff';
SlideEffect.DEFAULT_ANIMATION_INCREMENT = 20;
SlideEffect.DEFAULT_ANIMATION_REFRESH = 40;

SlideEffect.DEFAULT_ANIMATION_DIRECTION = 'to.left';

SlideEffect.ANIMATION_DIRECTION_TO_LEFT = 'to.left';
SlideEffect.ANIMATION_DIRECTION_TO_RIGHT = 'to.right';
SlideEffect.ANIMATION_DIRECTION_TO_TOP = 'to.top';
SlideEffect.ANIMATION_DIRECTION_TO_BOTTOM = 'to.bottom';


SlideEffect.prototype.setElement = function (node) {
    var createParentNode = true;
    
    switch (arguments.length) {
        case 2 : {
            createParentNode = (arguments[1] === false) ? false : true;
        } break;
        
        default : {
            // do nothing
        } break;
    }
    
    
    var tmp = null;
    
    var current = null;
    
    var insertedElement = null;
    var img = null;
    var link = null;
    
    var size = null;
    
    if (node.nodeName.toLowerCase() == 'img') {
        this.image = node;
        if (createParentNode == true) {
            size = new Point2D();
            size.x = Number.parse.integer(node.clientWidth);
            size.y = Number.parse.integer(node.clientHeight);
            
            tmp = DomTools.create('div');
            tmp.style.display = 'none';
            tmp.style.width = size.x + 'px';
            tmp.style.height = size.y + 'px';
            tmp.style.margin = 0;
            tmp.style.padding = 0;
            tmp.style.overflow = 'hidden';
            
            tmp.style.position = 'relative';
            
            node.style.position = 'absolute';
            node.style.top = 0;
            node.style.left = 0;
            node.style.zIndex = 1;
            
            node.parentNode.insertBefore(tmp, node);
            node.parentNode.removeChild(node);
            tmp.appendChild(node);
            tmp.style.display = 'block';
            
            node = tmp;
            
        } else {
            node = node.parentNode;
        }
        
    }
    
    DomTools.addHelpers(node);
    
    node.rugama.set('SlideEffect', this);
    
    this.element = node;
    
    this.size = new Point2D();
    this.size.x = Number.parse.integer(this.image.width);
    this.size.y = Number.parse.integer(this.image.height);
    
    
    this.layer = DomTools.create('div');
    
    this.layer.style.display = 'block';
    this.layer.style.width = this.size.x + 'px';
    this.layer.style.height = this.size.y + 'px';
    this.layer.style.margin = 0;
    this.layer.style.padding = 0;
    this.layer.style.overflow = 'hidden';
    
    this.layer.style.position = 'absolute';
    this.layer.style.top = 0;
    this.layer.style.left = 0;
    this.layer.style.zIndex = 3;
    
    
}


SlideEffect.prototype.appendSlideEffectListener = function (slideEffectEvent) {
    slideEffectEvent.owner = this;
    
    if (!slideEffectEvent.doBegin) {
        slideEffectEvent.doBegin = function (fakeEvent) {
            return true;
        }
    }
    
    if (!slideEffectEvent.over) {
        slideEffectEvent.doProcess = function (fakeEvent) {
            return true;
        }
    }
    
    if (!slideEffectEvent.doEnd) {
        slideEffectEvent.doEnd = function (fakeEvent) {
            return true;
        }
    }
    
    this.slideEffectListeners.push(slideEffectEvent);
    
}

SlideEffect.prototype.removeRolloverListener = function (slideEffectEvent) {
    var i = 0;
    var sz = this.slideEffectListeners.length;
    
    for (i = 0; i < sz; i++) {
        if (this.slideEffectListeners[i] != slideEffectEvent) {
            continue;
        }
        
        this.slideEffectListeners[i].owner = null;
        delete this.slideEffectListeners[i];
    }
    
}

SlideEffect.prototype.onBegin = function (fakeEvent) {
    var i = 0;
    var sz = this.slideEffectListeners.length;
    for (i = 0; i < sz; i++) {
        this.slideEffectListeners[i].doBegin(fakeEvent);
        
        if (fakeEvent.isDisposed() == true) {
            return fakeEvent.getReturn();
        }
    }
    
    return true;
}

SlideEffect.prototype.onProcess = function (fakeEvent) {
    var i = 0;
    var sz = this.slideEffectListeners.length;
    for (i = 0; i < sz; i++) {
        this.slideEffectListeners[i].doProcess(fakeEvent);
        
        if (fakeEvent.isDisposed() == true) {
            return fakeEvent.getReturn();
        }
    }
    
    return true;
}

SlideEffect.prototype.onEnd = function (fakeEvent) {
    var i = 0;
    var sz = this.slideEffectListeners.length;
    for (i = 0; i < sz; i++) {
        this.slideEffectListeners[i].doEnd(fakeEvent);
        
        if (fakeEvent.isDisposed() == true) {
            return fakeEvent.getReturn();
        }
    }
    
    
    var pn = fakeEvent.img.parentNode;
    fakeEvent.img.style.position = '';
    pn.removeChild(fakeEvent.img);
    pn.parentNode.insertBefore(fakeEvent.img, pn);
    pn.parentNode.removeChild(pn);
    
    
    return true;
}

SlideEffect.prototype.slide = function () {
    if (this.element == null) {
        return true;
    }
    
    var self = this;
    
    
    var fe = DomTools.createFakeEvent(SlideEffect.EVENT_EFFECT_SLIDE, this);
    
    fe.src = this.src;
    fe.full = this.size.clone();
    fe.actual = this.size.clone();
    fe.img = this.image;
    fe.topLayer = null;
    fe.animation = this.animation;
    
    switch (fe.animation.direction) {
        
        
        case SlideEffect.ANIMATION_DIRECTION_TO_BOTTOM : {
            fe.actual = new Point2D();
            fe.actual.x = 0;
            fe.actual.y = 0;
        } break;
        
        case SlideEffect.ANIMATION_DIRECTION_TO_TOP : {
            fe.actual = new Point2D();
            fe.actual.x = 0;
            fe.actual.y = 0;
        } break;
        
        
        case SlideEffect.ANIMATION_DIRECTION_TO_RIGHT : {
            fe.actual = new Point2D();
            fe.actual.x = 0;
            fe.actual.y = 0;
        } break;
        
        case SlideEffect.ANIMATION_DIRECTION_TO_LEFT :
        default : {
            fe.actual = this.size.clone();
        } break;
    }
    
    
    var tg = fe.getTarget();
    fe.img.style.visibility = 'visible';
    fe.img.style.display = '';
    
    fe.topLayer = this.layer;
    
    fe.count = 0;
    
    fe.topLayer.style.backgroundColor = '#ffffff';
    
    tg.element.appendChild(fe.topLayer);
    
    tg.onBegin(fe);
    
    fe.timerId = setInterval(function () {
            var tmp = null;
            fe.count++;
            switch (fe.animation.direction) {
                
                case SlideEffect.ANIMATION_DIRECTION_TO_BOTTOM : {
                    if (fe.actual.y < fe.full.y) {
                        fe.actual.y = fe.actual.y + fe.animation.increment;
                        
                    } else {
                        fe.actual.y = fe.full.y;
                        
                        tg.onEnd(fe);
                        fe.topLayer.parentNode.removeChild(fe.topLayer);
                        clearTimeout(fe.timerId);
                        return;
                    }
                    
                    if (fe.full.y < fe.actual.y) {
                        fe.actual.y = fe.full.y;
                    }
                    
                    
                } break;
                    
                
                case SlideEffect.ANIMATION_DIRECTION_TO_TOP : {
                    if (fe.actual.y < fe.full.y) {
                        fe.actual.y = fe.actual.y + fe.animation.increment;
                        
                    } else {
                        fe.actual.y = fe.full.y;
                        
                        tg.onEnd(fe);
                        fe.topLayer.parentNode.removeChild(fe.topLayer);
                        clearTimeout(fe.timerId);
                        return;
                    }
                    
                    if (fe.full.y < fe.actual.y) {
                        fe.actual.y = fe.full.y;
                    }
                    
                    
                } break;
                
                
                case SlideEffect.ANIMATION_DIRECTION_TO_RIGHT : {
                    if (fe.actual.x < fe.full.x) {
                        fe.actual.x = fe.actual.x + fe.animation.increment;
                        
                    } else {
                        fe.actual.x = fe.full.x;
                        
                        tg.onEnd(fe);
                        fe.topLayer.parentNode.removeChild(fe.topLayer);
                        clearTimeout(fe.timerId);
                        return;
                    }
                    
                    if (fe.full.x < fe.actual.x) {
                        fe.actual.x = fe.full.x;
                    }
                
                
                } break;
                
                case SlideEffect.ANIMATION_DIRECTION_TO_LEFT :
                default : {
                    if (0 < fe.actual.x) {
                        fe.actual.x = fe.actual.x - fe.animation.increment;
                        
                    } else {
                        tg.onEnd(fe);
                        fe.topLayer.parentNode.removeChild(fe.topLayer);
                        clearTimeout(fe.timerId);
                        return;
                    }
                    
                    if (fe.actual.x < 0) {
                        fe.actual.x = 0;
                    }
                    /*
                    if (5 < fe.count) {
                        fe.topLayer.parentNode.removeChild(fe.topLayer);
                        clearTimeout(fe.timerId);
                    }
                    */
                } break;
            }
            
            tg.onProcess(fe);
            
            switch (fe.animation.direction) {
                
                case SlideEffect.ANIMATION_DIRECTION_TO_BOTTOM : {
                    fe.topLayer.style.top = fe.actual.y  + 'px';
                    fe.topLayer.style.height = (fe.full.y - fe.actual.y)  + 'px';
                    
                } break;
                
                case SlideEffect.ANIMATION_DIRECTION_TO_TOP : {
                    fe.topLayer.style.height = (fe.full.y - fe.actual.y) + 'px';
                    
                } break;
                
                case SlideEffect.ANIMATION_DIRECTION_TO_RIGHT : {
                    fe.topLayer.style.left = fe.actual.x + 'px';
                    fe.topLayer.style.width = (fe.full.x - fe.actual.x) + 'px';
                } break;
                
                case SlideEffect.ANIMATION_DIRECTION_TO_LEFT :
                default : {
                    fe.topLayer.style.width = fe.actual.x + 'px';
                } break;
            }
            
            
        }, fe.animation.refresh);
        
    return true;
}

/*

    this.image.onload = 
        function () {
            var tg = fe.getTarget();
            fe.img.style.display = 'none';
            
            tg.element.style.backgroundRepeat = 'no-repeat';
            tg.element.style.backgroundPosition = 'center center';
            tg.element.style.backgroundImage = 'url(' + fe.src + ')';
            
            fe.topLayer = DomTools.create('div');
            
            fe.topLayer.style.display = 'block';
            fe.topLayer.style.width = fe.full.x + 'px';
            fe.topLayer.style.height = fe.full.y + 'px';
            fe.topLayer.style.margin = 0;
            fe.topLayer.style.padding = 0;
            fe.topLayer.style.overflow = 'hidden';
            
            fe.topLayer.style.position = 'absolute';
            fe.topLayer.style.top = 0;
            fe.topLayer.style.left = 0;
            fe.topLayer.style.zIndex = 3;
            
            fe.count = 0;
            
            fe.topLayer.style.backgroundColor = '#ffffff';
            
            tg.element.appendChild(fe.topLayer);
            
            tg.onBegin(fe);
            
            fe.timerId = setInterval(function () {
                    var tmp = null;
                    fe.count++;
                    switch (fe.animation.direction) {
                        
                        case SlideEffect.ANIMATION_DIRECTION_TO_BOTTOM : {
                            if (fe.actual.y < fe.full.y) {
                                fe.actual.y = fe.actual.y + fe.animation.increment;
                                
                            } else {
                                fe.actual.y = fe.full.y;
                                
                                tg.onEnd(fe);
                                fe.topLayer.parentNode.removeChild(fe.topLayer);
                                clearTimeout(fe.timerId);
                                return;
                            }
                            
                            if (fe.full.y < fe.actual.y) {
                                fe.actual.y = fe.full.y;
                            }
                            
                            
                            if (10 < fe.count) {
                                
                                clearTimeout(fe.timerId);
                                return;
                                
                            }
                            
                        } break;
                            
                        
                        case SlideEffect.ANIMATION_DIRECTION_TO_TOP : {
                            if (fe.actual.y < fe.full.y) {
                                fe.actual.y = fe.actual.y + fe.animation.increment;
                                
                            } else {
                                fe.actual.y = fe.full.y;
                                
                                tg.onEnd(fe);
                                fe.topLayer.parentNode.removeChild(fe.topLayer);
                                clearTimeout(fe.timerId);
                                return;
                            }
                            
                            if (fe.full.y < fe.actual.y) {
                                fe.actual.y = fe.full.y;
                            }
                            
                            
                        } break;
                        
                        
                        case SlideEffect.ANIMATION_DIRECTION_TO_RIGHT : {
                            if (fe.actual.x < fe.full.x) {
                                fe.actual.x = fe.actual.x + fe.animation.increment;
                                
                            } else {
                                fe.actual.x = fe.full.x;
                                
                                tg.onEnd(fe);
                                fe.topLayer.parentNode.removeChild(fe.topLayer);
                                clearTimeout(fe.timerId);
                                return;
                            }
                            
                            if (fe.full.x < fe.actual.x) {
                                fe.actual.x = fe.full.x;
                            }
                        
                        
                        } break;
                        
                        case SlideEffect.ANIMATION_DIRECTION_TO_LEFT :
                        default : {
                            if (0 < fe.actual.x) {
                                fe.actual.x = fe.actual.x - fe.animation.increment;
                                
                            } else {
                                tg.onEnd(fe);
                                fe.topLayer.parentNode.removeChild(fe.topLayer);
                                clearTimeout(fe.timerId);
                                return;
                            }
                            
                            if (fe.actual.x < 0) {
                                fe.actual.x = 0;
                            }
                        } break;
                    }
                    
                    tg.onProcess(fe);
                    
                    switch (fe.animation.direction) {
                        
                        case SlideEffect.ANIMATION_DIRECTION_TO_BOTTOM : {
                            //fe.topLayer.style.height = (fe.full.y - fe.actual.y) + 'px';
                            
                            fe.topLayer.style.margin = '0';
                            fe.topLayer.style.marginTop = fe.actual.y  + 'px';
                            fe.topLayer.style.height = (fe.full.y - fe.actual.y)  + 'px';
                            
                            //fe.topLayer.style.marginTop = fe.actual.y + 'px';
                            
                        } break;
                        
                        case SlideEffect.ANIMATION_DIRECTION_TO_TOP : {
                            fe.topLayer.style.height = (fe.full.y - fe.actual.y) + 'px';
                            
                        } break;
                        
                        case SlideEffect.ANIMATION_DIRECTION_TO_RIGHT : {
                            fe.topLayer.style.marginLeft = fe.actual.x + 'px';
                            fe.topLayer.style.width = (fe.full.x - fe.actual.x) + 'px';
                        } break;
                        
                        case SlideEffect.ANIMATION_DIRECTION_TO_LEFT :
                        default : {
                            fe.topLayer.style.width = fe.actual.x + 'px';
                        } break;
                    }
                    
                    
                }, fe.animation.refresh);
            
            
            
            return true;
        };
*/

