/*!
 * jQuery JavaScript lightboxOverlay Plugin v1.0
 *
 * Copyright 2011, Stephan Schröter
 *
 */

(function( $ ){

    var methods = {
        
        init : function( options ) {
            
            var settings = {
                open: false,
                close: true,
                speed: 600,
                slide: true,
                minOffset: 30
            }
            
            return this.each(function(){
                
                if ( options ) {
                    $.extend( settings, options );
                }
                
                var obj = $(this);
                
                obj
                    .data('action',false)
                ;
                
                obj
                    .css({
                        'height':'100%'
                    })
                ;
                
                obj.lightboxOverlay('showLightbox', settings);
                obj.lightboxOverlay('hideLightbox', settings);
                obj.lightboxOverlay('slideLightbox', settings);
                
            });
            
        },
        
        
        // slideLightbox
        slideLightbox : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                if (settings.slide) {
                    
                    $(window)
                        .scroll(function(){
                            
                            if (!obj.data('action') && obj.is(':visible')) {
                                
                                obj.lightboxOverlay('positionLightbox', settings);
                                
                            }
                                                      
                        })
                    ;
                    
                }
                
                
            });
            
        },
        
        
        positionLightbox : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                if ( $(window).scrollTop()+(settings.minOffset*2)+obj.children().outerHeight() > $(document).height() ) {
                    var top = $(document).height()-(obj.children().outerHeight()+settings.minOffset);
                } else {
                    var top = $(window).scrollTop()+settings.minOffset;
                }
                
                obj
                    .children()
                    .stop(true,false)
                    .animate({
                        "top":""+ ( top ) +"px"
                    },300)
                ;
                
            });
            
        },
        
        
        // showLightbox
        showLightbox : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                if (settings.open) {
                    
                    settings.open
                        .click(function(){
                            
                            obj
                                .data('action','open')
                            ;
                            
                            obj
                                .show()
                                .children()
                                .show()
                            ;
                            
                            obj.lightboxOverlay('positionLightbox', settings);
                            
                            obj
                                .children()
                                .hide()
                                .fadeIn(settings.speed,function(){
                                    
                                    
                                    
                                    obj
                                        .data('action',false)
                                    ;
                                    
                                })
                            ;
                            
                            return false;
                            
                        })
                    ;
                    
                }
                
                
            });
            
        },
        
        // hideLightbox
        hideLightbox : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                if (settings.close) {
                    
                    settings.close
                        .click(function(){
                            
                            obj
                                .data('action','close')
                            ;
                            
                            obj
                                .children()
                                .show()
                                .fadeOut(settings.speed, function(){
                                    obj
                                        .hide()
                                    ;
                                    obj
                                        .data('action',false)
                                    ;
                                })
                            ;
                            
                            return false;
                            
                        })
                    ;
                    
                }
                
                
            });
            
        }
        
    };

    $.fn.lightboxOverlay = 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 on jQuery.lightboxOverlay' );
        }
    
    };

})( jQuery );
