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

(function( $ ){

    var methods = {
        
        init : function( options ) {
            
            var settings = {}
            
            return this.each(function(){
                
                if ( options ) {
                    $.extend( settings, options );
                }
                
                var obj = $(this);
                
                obj
                    .data('c',0)
                ;
                
                if (!$.cookie.get('intro')) {
                    
                    $.cookie.set('intro', 'true', 1);
                    
                    obj
                        .data('textAssetsBegin', false)
                        .data('textAssetsEnd', false)
                    ;
                    
                    obj.intro('setTextAssets', settings);
                    
                    obj
                        .click(function(){
                            
                            obj
                                .data('c',obj.data('c')+1)
                            ;
                            
                            obj.intro('slideUp', settings);
                            
                            return false;
                            
                        })
                    ;
                    
                    $('div#wrapper-inner')
                        .children()
                        .not(obj)
                        .hide()
                    ;
                    
                    obj
                        .show()
                        .css({
                            'height':''+ $(window).height() +'px'
                        })
                    ;
                    
                    $(window)
                        .resize(function(){
                    
                            obj
                                .css({
                                    'height':''+ $(window).height() +'px'
                                })
                            ;
                    
                        })
                    ;
                    
                    obj
                        .find('span.special-intro-text')
                        .text(obj.data('textAssetsBegin'))
                        .delay(settings.delay)
                        .fadeOut(settings.animationSpeed, function(){
                            
                            obj
                                .find('span.special-intro-text')
                                .text(obj.data('textAssetsEnd'))
                                .fadeIn(settings.animationSpeed)
                                .delay(settings.delay)
                                .show(0,function(){
                                    
                                    obj
                                        .data('c',obj.data('c')+1)
                                    ;
                                    
                                    obj.intro('slideUp', settings);
                                    
                                })
                            ;
                            
                        })
                    ;
                
                } else {
                    
                    $('div.slideshow')
                        .slideshow({
                            width: 1280,
                            height: 540,
                            animationSpeed: 600,
                            autoPlay: true,
                            autoPlayInterval: 5000,
                            caption: true,
                            nextPrev: true,
                            counter: false,
                            pagination: true
                        })
                    ;
                    
                    obj
                        .remove()
                    ;
                    
                }
                
            });
            
        },
        
        // slideUp
        slideUp : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                if ( obj.data('c') == 1 ) {
                    
                    $('div#wrapper-inner')
                        .css({
                            'height':''+ $(window).height() +'px',
                            'overflow':'hidden'
                        })
                        .children()
                        .not('div#lightbox-overlay')
                        .show()
                    ;
                    
                    obj
                        .animate({
                            'top':'-'+ $(window).height() +'px'
                        },settings.animationSpeed,function(){
                            
                            $('div.slideshow')
                                .slideshow({
                                    width: 1280,
                                    height: 540,
                                    animationSpeed: 600,
                                    autoPlay: true,
                                    autoPlayInterval: 5000,
                                    caption: true,
                                    nextPrev: true,
                                    counter: false,
                                    pagination: true
                                })
                            ;
                            
                            $('div#wrapper-inner')
                                .css({
                                    'height':'auto',
                                    'overflow':'visible'
                                })
                            ;
                            
                            $(window)
                                .unbind('resize')
                            ;
                            
                            obj
                                .remove()
                            ;
                            
                        })
                    ;
                
                }
                
                
            });
            
        },
        
        setTextAssets : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                var textAssetRandom = Math.floor(Math.random()*settings.textAssets.length);
                var textAssetBeginRandom = Math.floor(Math.random()*settings.textAssets[textAssetRandom]['begin'].length)
                var textAssetEndRandom = Math.floor(Math.random()*settings.textAssets[textAssetRandom]['end'].length)
                
                obj
                    .data('textAssetsBegin', settings.textAssets[textAssetRandom]['begin'][textAssetBeginRandom])
                    .data('textAssetsEnd', settings.textAssets[textAssetRandom]['end'][textAssetEndRandom])
                ;
                
                
            });
            
        }
        
    };

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

})( jQuery );
