var portModalOptions = {
    onOpen: function ( dialog )
    {
        dialog.overlay.fadeIn( 'slow', function ()
        {
            dialog.data.show();
            dialog.container.slideDown( 'slow' );
        } );
    },
    onClose: function ( dialog )
    {
        dialog.container.slideUp( 'normal', function ()
        {
            dialog.overlay.fadeOut( 'normal', function ()
            {
                $.modal.close();
            } );
        } );
    },
    containerId: 'portmodal-container',
    overlayId: 'portmodal-overlay',
    overlayClose: true,
    position: Array( '0' ),
    opacity: 80,
    closeClass: 'portlink'
};

$( document ).ready( function()
{

    // modal port menu using simplemodal
    $( '#bottomads .promo .promocontent a' ).click( function ( e )
    {
        e.preventDefault();

        var listId = "#list" + $( this ).closest( '.promo' ).attr( "id" );

        if ( showPortList( listId ) )
        {
            $( "#portmodal" ).modal( portModalOptions )
        }
    } );

    // add class to make middle (2nd) bottom ad narrower
    $( '#bottomads .promo:nth-child(2)' ).addClass( 'middle' );

    // bottom ad mouseovers
    var promotimers = new Array();
    $( '#bottomads .promo h4' ).click( function()
    {
        var panel = $( 'img', this );
        var promoId = $( this ).closest( '.promo' ).attr( 'id' );
        panel.fadeOut( 'fast' );
        promotimers[promoId] = setTimeout( function()
        {
            panel.fadeIn( 'slow' );
        }, 6000 );
    } );
    $( '#bottomads .promo' ).mouseenter( function()
    {
        var promoId = $( this ).attr( 'id' );
        var panel = $( 'h4 img', this );
        panel.fadeOut( 'fast' );
        if ( promotimers[promoId] )
        {
            clearTimeout( promotimers[promoId] );
            promotimers[promoId] = null;
        }
    } );
    $( '#bottomads .promo' ).mouseleave( function()
    {
        var promoId = $( this ).attr( 'id' );
        var panel = $( 'h4 img', this );
        promotimers[promoId] = setTimeout( function()
        {
            panel.fadeIn( 'fast' );
        }, 50 );
    } );
    $( '#bottomads .promo .promocontent a' ).mouseover( function()
    {
        var promoId = $( this ).closest( 'promo' ).attr( 'id' );
        clearTimeout( promotimers[promoId] );
    } );

} );

// Shows the Ports in the given ul.
function showPortList( ulId )
{
    // Hide all other port lists.
    $( "ul", "#portmodal" ).css( "display", "none" );

    // Show the selected list.
    var list = $( ulId );
    list.css( "display", "block" );

    // If the list only has one link then navigate to it.
    var links = $( "a", list );
    if ( links.length == 0 )
    {
        return false;
    }
    if ( links.length == 1 )
    {
        Global.nav( links[0].href );
        return false;
    }
    return true;
}

// Shows the Ports for the given ProductCategory.
function showCategoryPorts( productCategoryId )
{
    Global.Ajax.doGetData( categoryPortsUrl, productCategoryId, function( data )
    {
        $( "#categoryPorts" ).html( data );
        showPortList( "#categoryPorts" );
        $.modal( $( '#portmodal' ), portModalOptions );
    } );
}
