// <![CDATA[

/*
 * Sorry for this crappy code, but deadline was very short ;-)
 * Thanks for looking at the source code anyway.
 * You can re-use it in any manners you want.
 */

( function( $ )
{
    
    function accordion()
    {
        var OPEN_CLASS   = 'accordion-open';
        var CLOSED_CLASS = 'accordion-closed';
        
        var $elements    = $( '.news-list-item' );
        
        $( $elements[ $elements.length - 1 ] ).addClass( 'last-item' );
        
        for( var i = 0; i < $elements.length; i++ ) {
            
            var $element = $( $elements[ i ] );
            var $header  = $element.children( 'h2' ).addClass( 'accordion-header' );
            var $content = $element.children( '.news-content-container' ).addClass( 'accordion-content' );
            
            if( i !== 0 ) {
                
                $content.hide();
                $element.addClass( CLOSED_CLASS );
            
            } else {
            
                $element.addClass( OPEN_CLASS );
            }
        }
        
        $elements.find( '.accordion-header A' ).click( function( event )
        {
            event.preventDefault();
            
            var $element = $( this ).parent().parent();
            
            if( $element.hasClass( CLOSED_CLASS ) ) {
                
                open( $element );
            
            } else {
                
                close( $element );
            }
            
        } );
        
        function close( $element )
        {
            var $content = $element.children( '.accordion-content' );
            
            $content.slideUp( 'slow', function()
            {
                $element.removeClass( OPEN_CLASS ).addClass( CLOSED_CLASS );
            } );
            
            $element.find( '.accordion-header' ).animate(
                {
                    paddingBottom: '0px'
                },
                'slow'
            );
        }
        
        function open( $element )
        {
            var $content = $element.children( '.accordion-content' );
            
            $content.slideDown( 'slow', function()
            {
                $element.removeClass( CLOSED_CLASS ).addClass( OPEN_CLASS );
            } );
            
            $element.find( '.accordion-header' ).animate(
                {
                    paddingBottom: '20px'
                },
                'slow'
            );
            
            var $siblings = $element.siblings();
            
            for( var i = 0; i < $siblings.length; i++ ) {
                
                close( $( $siblings[ i ] ) );
            }
        }
    }
    
    function subscribe()
    {
        $( '#subscribe' ).fancybox( {
            hideOnContentClick : false,
            width               : 340,
            height              : 500,
            autoScale           : true,
            transitionIn        : 'none',
            transitionOut       : 'none',
            type                : 'iframe'
        } );
        
        $( '.tx-atra-pi2-other-car-link' ).fancybox( {
            hideOnContentClick : false,
            width               : 520,
            height              : 470,
            autoScale           : false,
            transitionIn        : 'none',
            transitionOut       : 'none',
            type                : 'iframe'
        } );
    }
    
    function ready()
    {
        accordion();
        subscribe();
    }
    
    $( ready );
    
} )( jQuery );

// ]]>
