jQuery( function( $ ){
    
    // preload the button rollover image for the search button
    $("<img>").attr("src", "/site/images/SearchFlightsOver.jpg" );
    
    // apply the rollover
    $( "#search_button" ).hover(
        function(){ $(this).attr( 'src', "/site/images/SearchFlightsOver.jpg" );},
        function(){ $(this).attr( 'src', "/site/images/SearchFlights.jpg" ); }
    );
    
    // rotate destination names in the example text underneath the destination text input
    if ( $( '#CityToExample' ).html() )
    {
        var examples = new Array('Sydney' , 'London' , 'Melbourne', 'Bangkok', 'Los Angeles', 'Brisbane', 'Auckland', 'Perth', 'Bali', 'Adelaide', 'Singapore', 'Gold Coast', 'Hong Kong', 'Canberra', 'Fiji' );
        var index = 1;
        setInterval( function(){
            if( index >= examples.length ) index = 0;
            $( '#CityToExample' ).html( 'e.g. <strong>' + examples[index].toString() + '</strong>'  );
            index++;
        }, 3000 );
    };
    
    // handle trip type selection (trip type radion button click handling)
    $( 'input[name=TripType]' ).click( function()
    {
        // one way is clicked
        if ($(this).val() == 'OneWay' )
            $( '#cal_dateback' ).css( 'visibility', 'hidden' );
        else
            $( '#cal_dateback' ).css( 'visibility', 'visible' );
        
        // multicity or round australis is clicked - send into TSA straight away
        if ($(this).val() == 'Multi City/Stopover' || $(this).val() == 'Round Australia' )
        {
            document.location.href = 'http://tsa.webjet.com.au/WebjetTSA/home.aspx?EntryPoint=Flight&RequestFrom=Outside&TripType=Multi';
        }
    });
    
    // handle the expanding and contracting side menus
    $( '#helpful-links' )
        .find( 'ul' ).hide()
        .end()
        .find( 'h3' )
        .css( 'cursor', 'pointer' )
        .hover(
            function(){
                $( this ).addClass( 'hover' )
            },
            function(){
                $( this ).removeClass( 'hover' )
            }
        )
        .prepend( '<img src="/site/images/linkbuttons/plus.gif" style="position: relative; left: -7px;" />')
        .end()
        .find( 'h3' )
        .toggle( 
            function(){
                $( this ).find( 'img').attr( 'src', '/site/images/linkbuttons/minus.gif' )
                $( 'ul', $(this).parent() ).slideDown();
            },
            function(){
                $( this ).find( 'img').attr( 'src', '/site/images/linkbuttons/plus.gif' )
                $( 'ul', $(this).parent()  ).slideUp();
            }
        );
        
        // handle the expanding and contracting side menus
    $( '#my-account' )
        .find( 'ul' ).hide()
        .end()
        .find( 'h3' )
        .css( 'cursor', 'pointer' )
        .hover(
            function(){
                $( this ).addClass( 'hover' )
            },
            function(){
                $( this ).removeClass( 'hover' )
            }
        )
        .prepend( '<img src="/site/images/linkbuttons/plus.gif" style="position: relative; left: -7px;" />')
        .end()
        .find( 'h3' )
        .toggle( 
            function(){
                $( this ).find( 'img').attr( 'src', '/site/images/linkbuttons/minus.gif' )
                $( 'ul', $(this).parent() ).slideDown();
            },
            function(){
                $( this ).find( 'img').attr( 'src', '/site/images/linkbuttons/plus.gif' )
                $( 'ul', $(this).parent()  ).slideUp();
            }
        );    
        
    $( 'div.online-answers-box' )
        .find( 'h3' )
        .css({
            cursor: 'pointer'
        })
        .hover(
            function(){
                $( this ).addClass( 'hover' )
            },
            function(){
                $( this ).removeClass( 'hover' )
            }
        )
        .prepend( '<img src="/site/images/linkbuttons/minus.gif" style="position: relative; left: -7px;" />')
        .end()
        .find( 'h3' )
        .toggle( 
            function(){
                $( this ).find( 'img').attr( 'src', '/site/images/linkbuttons/plus.gif' )
                $( 'form', $(this).parent()  ).slideUp();
            },
            function(){
                $( this ).find( 'img').attr( 'src', '/site/images/linkbuttons/minus.gif' )
                $( 'form', $(this).parent()  ).slideDown();
            }
        );
    
});
