(function ($) {
    $.fn.extend({
        sidebar: function(options) {
             var defaults = {
                    event: 'click',         // click, hover not yet implement
                    resettimer: 5000,       // 0 = disabled
                    speed: 500,
                    openeffect: '',
                    closeeffect: '',
                    mode: 0
                };

                var opts = $.extend(defaults, options);
                var $this = $(this);
                
                setup();
                
                function setup() {
                    $this.each(function() {
                        var hover_set = false;
                        
                        if ($(this).is('.top')) {
                            
                            //init click event
                            $('.slider', this).click(function() {
                                var c_content = $(this).prevAll('.content');
                                
                                if ($(c_content).is(':visible')) {
                                    resettimer(c_content);
                                } 
                                
                                $(this).toggleClass('active');
                                $(c_content).animate(
                                    { height: 'toggle' }, 
                                    opts.speed, 
                                    function() {
                                        if ($(c_content).is(':visible') && !hover_set) {
                                            if (opts.resettimer > 0) {
                                                obj_timeout = setTimeout( (function () { resettimer(this); toggleTopSidebar(this) }).binding(c_content), opts.resettimer );
                                                $(c_content).data('timeout', obj_timeout);
                                            }
                                        }
                                    }
                                )
                            });
                            
                            //init hover event
                            $('.content', this).hover(
                                function() {
                                    hover_set = true;
                                    resettimer(this);
                                },
                                function() {
                                    hover_set = false;
                                    if (opts.resettimer > 0) {
                                        obj_timeout = setTimeout( (function () { resettimer(this); closeSidebar_top(this) }).binding(this), opts.resettimer );
                                        $(this).data('timeout', obj_timeout);
                                    }
                                }
                            )
                        }
                        
                        if ($(this).is('.bottom')) {
                        
                            //init click event
                            $('.slider', this).click(function() {
                                var c_content = $(this).nextAll('.content');
                                
                                if ($(c_content).is(':visible')) {
                                    resettimer(c_content);
                                } 
                                
                                $(this).toggleClass('active');
                                $(c_content).animate(
                                    { height: 'toggle' }, 
                                    opts.speed, 
                                    function() {
                                        if ($(c_content).is(':visible') && !hover_set) {
                                            if (opts.resettimer > 0) {
                                                obj_timeout = setTimeout( (function () { resettimer(this); toggleBottomSidebar(this) }).binding(c_content), opts.resettimer );
                                                $(c_content).data('timeout', obj_timeout);
                                            }
                                        }
                                    }
                                )
                            });
                            
                            //init hover event
                            $('.content', this).hover(
                                function() {
                                    hover_set = true;
                                    resettimer(this);
                                },
                                function() {
                                    hover_set = false;
                                    if (opts.resettimer > 0) {
                                        obj_timeout = setTimeout( (function () { resettimer(this); closeSidebar_top(this) }).binding(this), opts.resettimer );
                                        $(this).data('timeout', obj_timeout);
                                    }
                                }
                            );
                        }
                        
                        if ($(this).is('.left')) {
                             //init click event
                            $('.slider', this).click(function() {
                                var c_content = $(this).nextAll('.content');
                                
                                if ($(c_content).is(':visible')) {
                                    resettimer(c_content);
                                } 
                                
                                $(this).toggleClass('active');
                                $(c_content).animate(
                                    { width: 'toggle' }, 
                                    opts.speed, 
                                    function() {
                                        if ($(c_content).is(':visible') && !hover_set) {
                                            if (opts.resettimer > 0) {
                                                obj_timeout = setTimeout( (function () { resettimer(this); toggleLeftSidebar(this) }).binding(c_content), opts.resettimer );
                                                $(c_content).data('timeout', obj_timeout);
                                            }
                                        }
                                    }
                                )
                            });
                            
                            //init hover event
                            $('.content', this).hover(
                                function() {
                                    hover_set = true;
                                    resettimer(this);
                                },
                                function() {
                                    hover_set = false;
                                    if (opts.resettimer > 0) {
                                        obj_timeout = setTimeout( (function () { resettimer(this); closeSidebar_left(this) }).binding(this), opts.resettimer );
                                        $(this).data('timeout', obj_timeout);
                                    }
                                }
                            )
                        }
                        if ($(this).is('.right')) {
                              //init click event
                            $('.slider', this).click(function() {
                                var c_content = $(this).nextAll('.content');
                                
                                if ($(c_content).is(':visible')) {
                                    resettimer(c_content);
                                } 
                                
                                $(this).toggleClass('active');
                                $(c_content).animate(
                                    { width: 'toggle' }, 
                                    opts.speed, 
                                    function() {
                                        if ($(c_content).is(':visible') && !hover_set) {
                                            if (opts.resettimer > 0) {
                                                obj_timeout = setTimeout( (function () { resettimer(this); toggleRightSidebar(this) }).binding(c_content), opts.resettimer );
                                                $(c_content).data('timeout', obj_timeout);
                                            }
                                        }
                                    }
                                )
                            });
                            
                            //init hover event
                            $('.content', this).hover(
                                function() {
                                    hover_set = true;
                                    resettimer(this);
                                },
                                function() {
                                    hover_set = false;
                                    if (opts.resettimer > 0) {
                                        obj_timeout = setTimeout( (function () { resettimer(this); closeSidebar_left(this) }).binding(this), opts.resettimer );
                                        $(this).data('timeout', obj_timeout);
                                    }
                                }
                            )
                        }
                    });
                }
                
                function resettimer(obj) {
                    if ($(obj).data('timeout')) { 
                        window.clearTimeout($(obj).data('timeout'));
                        $(obj).removeData('timeout');
                    }
                }
            
                function toggleTopSidebar(ele) {
                    $(ele).nextAll('.slider').toggleClass('active');
                    $(ele).animate(
                        { height: 'toggle' }, 
                        1500, 
                        function() {}
                    )      
                }
                
                function toggleBottomSidebar(ele) {
                    $(ele).prevAll('.slider').toggleClass('active');
                    $(ele).animate(
                        { height: 'toggle' }, 
                        1500, 
                        function() {}
                    )
                }
                
                function toggleLeftSidebar(ele) {
                   $(ele).prevAll('.slider').toggleClass('active');
                    $(ele).animate(
                        { width: 'toggle' }, 
                        1500, 
                        function() {}
                    )      
                }
                
                function toggleRightSidebar(ele) {
                    toggleLeftSidebar(ele);
                }
                
                function openTopSidebar(ele) {
                    $(ele).animate(
                        { height: 'toggle' },
                        1500,
                        function() {}
                    );
                }
                
                function closeSidebar_left (ele) {
                    $(ele).prevAll('.slider').removeClass('active');
                    $(ele).animate({
                        width: '0'
                    }, 1500, function() {
                        $(this).hide();
                    });
                }
                
                function closeSidebar_top (ele) {
                    $(ele).prevAll('.slider').removeClass('active');
                    $(ele).nextAll('.slider').removeClass('active');
                    $(ele).animate({
                        height: '0'
                    }, 1500, function() {
                        $(this).hide();
                    });
                }
        }
    })
})(jQuery);
