var advertClass = new Class(
{
    initialize: function()
    {
        var $this = this;
        
        this.advert     = $$('div.advert');
        this.enabled    = false;
        this.bar        = this.advert.getElement('div.top')[0];
        this.contener   = this.advert.getElement( 'div.content' )[0];
        this.statusBtns = this.bar.getElements( 'span' );
        //this.info       = this.advert.getElement( 'div.btns' ).getElements( 'span' );
        this.btns       = this.advert.getElement( 'div.btns' )[0].getElements( 'img' );
        this.i          = 0;
        
        this.btns[0].addEvent( 'mouseover', function()
        {
            //advert.info[0].setStyle( 'display', 'block' );    
        } );
        
        this.btns[0].addEvent( 'mouseout', function()
        {
            //advert.info[0].setStyle( 'display', 'none' );    
        } );
        
        this.btns[1].addEvent( 'mouseover', function()
        {
            //advert.info[1].setStyle( 'display', 'block' );    
        } );
        
        this.btns[1].addEvent( 'mouseout', function()
        {
            //advert.info[1].setStyle( 'display', 'none' );    
        } );

        this.statusBtns[0].addEvent( 'click', function()
        {
            $this.hidden();
        } ).fade( 'hide' );
        
        this.statusBtns[1].addEvent( 'click', function()
        {
            $this.show();
        } ).fade( 'hide' );
        
        this.effect     = new Fx.Morph( this.contener, 
        {
            duration: 'long', 
            transition: Fx.Transitions.Sine.easeOut,
            onStart: function()
            {
                $this.statusBtns.fade( 'out' ).setStyle( 'display', 'none' );
            },
            onComplete: function()
            {  
                $this.statusBtns[$this.i].setStyle( 'display', 'block' ).fade( 'in' );
            }
        });
        
        var status = Cookie.read( 'advertStatus' );
               
        if( !status || status == '0' )
        {
            if( this.enabled )
            {
                this.show();
            }
            else
            {
                $this.statusBtns[0].setStyle( 'display', 'block' ).fade( 'in' );
            }
        }
        else
        {
            this.close();
        }
        
        this.enabled = true;
    },
    
    close: function()
    {
        this.contener.setStyle( 'margin-top', this.contener.getSize().y * -1 );  
        this.bar.setStyle( 'height', '21px' );
        this.statusBtns[1].setStyle( 'display', 'block' ).fade( 'in' );
    },
    
    hidden: function()
    {
        this.i = 1;
        
        this.effect.start(
        {
            'margin-top': this.contener.getSize().y * -1
        } );

        this.bar.setStyle( 'height', '21px' );
        
        Cookie.write( 'advertStatus', 1 );
    },
    
    show: function()
    {
        this.i = 0;

        this.effect.start(
        {
            'margin-top': 0 
        } );
        
        this.bar.setStyle( 'height', '20px' );
        
        Cookie.write( 'advertStatus', 0 );
    }
} );

var selectCustomize = new Class( 
{
    select: null,
    input: null,
    options: null,
    
    initialize: function( select )
    {
        switch( $type( select ) )
        {
            case 'array':
            {
                $each( select, function( element )
                {
                    new selectCustomize( element );
                } );
            }
            break;
            
            case 'element':
            {
                this.init( select );
            }
            break;
        }
    },
    
    init: function( select )
    {
        var $this = this;
        
        this.select = select;
        
        this.input = new Element( 'input',
        {
            'readonly': 'readonly',
            'styles':
            {
                'cursor': 'pointer'
            },
            'events':
            {
                'click': function( ev )
                {
                    $this.change();
                    
                    ev.stopPropagation();
                }
            }
        } ).inject( select, 'before' );

        this.options = new Element( 'ul',
        {
            'class': 'selectCustomize',
            'styles':
            {
                'position': 'absolute',
                'left': select.getPosition().x,
                'top': select.getPosition().y,
                'z-index': 10001
            }
        } ).fade( 'hide' );
        
        select.setStyles(
        {
            'opacity': '0.01',
            'width': 0,
            'height': 0,
            'margin': 0,
            'padding': 0,
            'font-size': 0,
            'line-height': 0
        } ).getParent().setStyle( 'position', 'relative' ).adopt( new Element( 'span',
        {
            'class': 'select_toogle',
            'events': 
            {
                'click': function( ev )
                {
                    $this.change();
                    
                    ev.stopPropagation();
                }
            }
        } ) );
        
        $each( select.getElements( 'option' ), function( option )
        {
            if( select.value == option.get( 'value' ) )
            {
                $this.set( option, false );
            }
            
            $this.options.adopt( new Element( 'li',
            {
                'text': option.get( 'text' ),
                'events': 
                {
                    'click': function( ev )
                    {
                        $this.set( option, true );
                        
                        ev.stopPropagation();
                    }
                }
            } ) );
        } );
        
        $$( 'body' )[0].addEvent( 'click', function() 
        {
            $this.close();
        } );
    },
    
    change: function()
    {
        if( this.options.getParent() )
        {
            this.options = this.options.fade( 'out' ).dispose();
        }
        else
        {
            $$( 'body' )[0].adopt( this.options );
            this.options.get( 'tween', 
            { 
                property: 'opacity', 
                duration: 200 
            } ).start( 1 );
        } 
    },
    
    close: function()
    {
        if( this.options.getParent() )
        {
            this.options = this.options.fade( 'out' ).dispose();
        }
    },
    
    set: function( option, close )
    {
        this.input.set( 'value', option.get( 'text' ) );
        this.select.set( 'value', option.get( 'value' ) );
        
        if( close )
        {
            this.change();
        }
    }
} );

window.addEvent( 'domready', function()
{
    new advertClass();    
    
    $each( $$( 'div.timerange' ), function( element )
    {
        $each( element.getElements( 'input.datepicker' ), function( input )
        {
            new DatePicker( input,
            {
                pickerClass: 'datepicker_dashboard',
                format: 'd.m',
                toggleElements: new Element( 'span',
                {
                    'class': 'datepicker_toogle'
                } ).inject( input.getParent( 'div.fieldBorder' ), 'after' ),
                yearPicker: false,
                minDate: { date: new Date(), format: 'd.m.Y' }
            } );
        } );
    } );
    
    new selectCustomize( $$( 'select' ) );
} );
