/*
jQuery-Plugin Slideout

Ersetzt den Inhalt eines Block-Elements durch einen Link mit der Klasse "slideout-replacement",
der auf Klick das Element wieder aufschiebt und den Inhalt anzeigt.
*/
(function ($) {
    var settings = {
            replacementHtml: '<div class="slideout-replacement"><a href="">mehr</a></div>',
            bindOnInit: true // false to prevent immediate event binding (call 'bind' method manually)
        },
        methods = {
        init: function (options) {
            return this.each(function () {
                var $slideoutContainer,
                    $slideoutContent,
                    contentWidth,
                    $replacement,
                    replacementWidth;
                
                if (options) {
                    $.extend(settings, options);
                }
                
                $slideoutContainer = $(this);
                $slideoutContent = $slideoutContainer
                    .wrapInner('<div class="slideout-content" />').children();
                contentWidth = $slideoutContent.width();
                $slideoutContent.hide();
                // Replacement-Parameter nur Default, per Options überschreibbar
                $replacement = $(settings.replacementHtml)
                    .insertAfter($slideoutContent);
                replacementWidth = $replacement.width();
                
                // Save data for later plugin method calls
                $slideoutContainer.data('slideout', {
                    target: $slideoutContainer,
                    contentWidth: contentWidth,
                    content: $slideoutContent,
                    replacementWidth: replacementWidth,
                    replacement: $replacement
                });
                
                // Bind click events
                // Separate function to allow for later re-binding
                if (settings.bindOnInit) {
                    methods.bind.apply($slideoutContainer);
                }
            });
        },
        bind: function () {
            return this.each(function () {
                var data = $(this).data('slideout');
                if (!data) {
                    $.error('Call init before bind at jQuery.slideout');
                    return;
                }
                
                if (data.bound) {
                    // already bound
                    //alert('already bound');
                    return;
                }
                /*
                data.replacement.bind('click.slideout', function (e) {
                    e.preventDefault();
                    data.target.animate({width: data.contentWidth}, 'slow',
                        function () {
                            data.content.fadeIn('slow');
                        });
                    $(this).hide();
                });
                data.content.bind('click.slideout', function (e) {
                    e.preventDefault();
                    $(this).hide();
                    data.target.animate({width: data.replacementWidth}, 'slow',
                        function () {
                            data.replacement.show();
                        });
                });
                */
                data.target.hoverIntent(function () {
                    data.target.animate({width: data.contentWidth}, 'slow',
                        function () {
                            data.content.fadeIn('slow');
                        });
                }, function () {
                    data.content.hide();
                    data.target.animate({width: data.replacementWidth}, 'slow',
                        function () {
                            //data.replacement.show();
                            // make double sure
                            data.content.hide();
                        });
                });
                // Set binding state data
                data.bound = true;
                $(this).data('slideout', data);
            });
        },
        unbind: function () {
            return this.each(function () {
                var data = $(this).data('slideout');
                if (!data) {
                    $.error('Call init before unbind at jQuery.slideout');
                    return;
                }
                
                data.replacement.unbind('.slideout');
                data.content.unbind('.slideout');
                
                // Set binding state data
                data.bound = false;
                $(this).data('slideout', data);
            });
        },
        /*
         Immediately close slideout
        */
        reset: function () {
            return this.each(function () {
                var data = $(this).data('slideout');
                if (!data) {
                    $.error('Call init before reset at jQuery.slideout');
                    return;
                }
                
                // @todo check if actually opened
                data.content.hide();
                data.target.width(data.replacementWidth);
                data.replacement.show();
            });
        }
    };
    
    $.fn.slideout = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || ! method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist in jQuery.slideout');
        }
    };
}(jQuery));
