$j( document ).ready( function() {
    $j( "#logo" ).fadeIn( 2500 );

    $j( "#toplinks" )
      .mouseenter( function() {
        $j( "#toplinkslider" )
          .stop( true )
          .animate( { right:     "205",
                      opacity:   0 },
                    270,
                    function() {
                        // IE
                        $j( "#toplinkslider" )
                         .hide();

                        $j( "#toplinkrightarrow" )
                          .stop( true )
                          .css( "right",
                                "450px" )
                          .fadeIn( 100,
                                   fadeLink );
                    } );
      } )

    var openSite = function() {
        window.open( bigSite()
                      .find( "url" )
                      .text());
    };

    $j( "#sitePic, #siteName" ).click( function() {
        openSite();
    } );

    var fadeSlices = function() {
        $j( "img.slice" )
         .not( ".hoverSlice" )
         .fadeTo( 400,
                  0.13 );
    }

    $j( ".slice" )
     .hover( function() {
                 $j( this )
                  .addClass( "hoverSlice" )
                  .not( ".litSlice" )
                  .fadeTo( 50,
                           0.8 );
             },
             function() {
                 $j( this )
                  .removeClass( "hoverSlice" )
                  .not( ".litSlice" )
                  .fadeTo( 50,
                           0.13 );
             } )
     .click( function() {
                 $j( "#siteName" )
                  .stopTime();

                 if ( $j( this )
                       .is( ".litSlice" )) {
                     openSite();
                 }

                 var label = $j( this )
                              .attr( "id" )
                              .slice( 5 );
                 rotateSites( { sitesIndex:
                                 $j( "site",
                                     sitesXML )
                                  .filter( function() {
                                             return $j( this )
                                                     .find( "label" )
                                                     .text()
                                                     == label;
                                           } )
                                  .index() } );
             } );

    sitesIndex = 0;

    var bigSite = function() {
        return $j( "site",
                   sitesXML )
                .eq( sitesIndex );
    };

    var litSlice = function() {
        return $j( "#slice"
                    + bigSite()
                       .find( "label" )
                       .text());
    };

    var showSite = function() {
        $j( "#sitePic" )
          .attr( "src",
                 "/images/"
                  + bigSite()
                     .find( "label" )
                     .text()
                  + ".png" );
        $j( "#siteName" )
          .text( bigSite()
                  .find( "name" )
                  .text())
        $j( "#site" )
          .fadeIn( 500 );
        litSlice()
         .addClass( "litSlice" )
         .fadeTo( 700,
                  1 );
    };

    var rotateSites = function( opts ) {
        opts = opts || {};

        $j( ".litSlice" )
         .removeClass( "litSlice" );
        fadeSlices();
        $j( "#site" )
          .fadeOut( 500,
                    showSite );
        if ( opts.sitesIndex !== undefined ) {
            sitesIndex = opts.sitesIndex;
        } else {
            sitesIndex = ( sitesIndex + 1 )
                          % $j( "site",
                                sitesXML )
                             .length;
        }
    };

    fadeSlices();

    var sitesXML;
    $j.post( "/ajax/sites",
             {},
             function( xml ) {
                sitesXML = xml;
                showSite();

                $j( "#siteName" )
                 .everyTime( "5s",
                             rotateSites );
             },
             "xml" );
} );

