jQuery.Slide = {
    //Default settings
    settings : {
        axis : "vertical",
        position : "bottomright",
        maxElement : 3,
        speed : "fast",
        margin : 7,
        closeButtonTrigger : ".close",
        delayBeforeStart : 0,
        delayBeforeHide : 3000,
        endCallback : null
    },
        
    staticSettings : {
        animationStep : 15,
        nextMaxMargin : null
    },
    
    //Default method that slide elements, bind in jQuery
    build : function(options) {
        //Init
        jQuery.Slide.staticSettings.nextMaxMargin = null;
        
        return this.not(".allreadySlide").each(function(i) {
            //Local copy and settings
            var el = this;
            jQuery.extend(el.settings = {},jQuery.Slide.settings,options || {});
            
            //Exclude MISE6
            if(jQuery.browser.msie && navigator.appVersion.indexOf("MSIE 6.0") != -1) {
                $(el).remove();
                return;
            }
            
            //Manage max element
            if(el.settings.maxElement != -1 && i > (el.settings.maxElement -1)) {
                $(el).remove();
                return;
            }

            //Define the max position
            el.maxPosition = jQuery.Slide.staticSettings.nextMaxMargin || el.settings.margin;

            //Manage speed
            if(el.settings.speed == "slow") {
                el.settings.speed = 90;
            }
            else if(el.settings.speed == "normal") {
                el.settings.speed = 65;
            }
            else if(el.settings.speed == "fast") {
                el.settings.speed = 40;
            }
            
            //Init CSS
            $(el)
                .css("visibility","hidden")
                .css("display","block")
                .css("position","fixed")
                .css("z-index",2000-i);
            
            //Check axis case
            if(el.settings.axis.toLowerCase() == "vertical") {
                //Define the css property used for slide animation
                if(el.settings.position.toLowerCase().indexOf("top") != -1) {
                    el.settings.cssProperty = "top";
                } 
                else {
                    el.settings.cssProperty = "bottom";
                }
                
                //Define the horizontal position
                if(el.settings.position.toLowerCase().indexOf("left") != -1) {
                    $(el).css("left",el.settings.margin);
                    $(el).css("right","");
                } 
                else {
                    $(el).css("right",el.settings.margin);
                    $(el).css("left","");
                }
                
                //Define the start offset and next max margin
                el.settings.cssPropertyStart = - el.offsetHeight - 20;
                jQuery.Slide.staticSettings.nextMaxMargin = el.offsetHeight;
            }
            else {
                //Define the css property used for slide animation
                if(el.settings.position.toLowerCase().indexOf("left") != -1) {
                    el.settings.cssProperty = "left";
                } 
                else {
                    el.settings.cssProperty = "right";
                }
                
                //Define the horizontal position
                if(el.settings.position.toLowerCase().indexOf("top") != -1) {
                    $(el).css("top",el.settings.margin);
                    $(el).css("bottom","");
                } 
                else {
                    $(el).css("bottom",el.settings.margin);
                    $(el).css("top","");
                }
                
                //Define the start offset and next max margin
                el.settings.cssPropertyStart = - el.offsetWidth - 20;
                jQuery.Slide.staticSettings.nextMaxMargin = el.offsetWidth;
            }
            
            jQuery.Slide.staticSettings.nextMaxMargin += el.maxPosition + el.settings.margin + 3;
            
            //Set the position of element and make it visible
            $(el).css(el.settings.cssProperty,el.settings.cssPropertyStart).css("visibility","visible");
            
            //Start the slide animation
            window.setTimeout(function(){
                jQuery.Slide.StartSlide(el);
            },el.settings.delayBeforeStart);
            
            //Attach event to close button
            $(el.settings.closeButtonTrigger,el).click(function(){
                jQuery.Slide.Hide(el);
                return false;
            });
            
            //Attach event
            $(el).mouseover(function(){
                jQuery.Slide.SuspendHide(el);
            }).mouseout(function(){
                jQuery.Slide.StartHide(el);
            });
        }).end();
    },
    //Method for start the slide
    StartSlide : function(el) {
        window.setTimeout(function(){
            var current = parseInt($(el).css(el.settings.cssProperty));
        
            if(current + jQuery.Slide.staticSettings.animationStep >= el.maxPosition) {
                $(el).css(el.settings.cssProperty,el.maxPosition).addClass("allreadySlide");
                jQuery.Slide.StartHide(el);
            }
            else {
                $(el).css(el.settings.cssProperty,(current+jQuery.Slide.staticSettings.animationStep));
                jQuery.Slide.StartSlide(el);
            }
        },el.settings.speed);
    },
    //Method for hide elements that were slide
    Hide : function(el) {
        //Fix
        if($(el).not(".hideInProgress").size() > 0) {
            $(el).addClass("hideInProgress");
        
        //Suspend timer hide
        jQuery.Slide.SuspendHide(el);
        
            //Unbind click
            $(el.settings.closeButtonTrigger,el).unbind("click");
            
            //Unbind mouseover and mouseout event
            $(el).unbind("mouseover").unbind("mouseout");
            
        //Hide element
        $(el).fadeOut("slow",function(){
                $(this).remove();
                
            if(jQuery.isFunction(el.settings.endCallback)) {
                el.settings.endCallback();
            }
        });
        }
    },
    //Method for start the process to hide elements that were slide
    StartHide : function(el) {
        el.hideTimer = window.setTimeout(function(){
            jQuery.Slide.Hide(el);
        },el.settings.delayBeforeHide);        
    },
    //Method for suspend the process to hide elements that were slide
    SuspendHide : function(el) {
        window.clearTimeout(el.hideTimer);
            el.hideTimer = null;
    }
};

jQuery.fn.Slide = jQuery.Slide.build;
