$(function(){
  // Fix transparent png's in IE6
    $(document).pngFix(); 
    
    // External links in a new window.
  $("a[href^='http']:not(a[href^='http://localhost'], a[href^='http://www.vfestival.com'], a[href^='http://staging.vfestival.com'])").attr('target','_blank');
    
    // Hide all the navigation panels at the foot of the page.
    $('div.navigation-panel').children().hide();
    
    // Hide the overlay panel as it shows in IE6 otherwise.
    $('div.navigation-overlay').hide();
    
    // Catch clicks outside the overlay and close it.
    $('div.background').click(function(e) {
      if (!$(e.target).hasClass('navigation-overlay') && $(e.target).parents('.navigation-overlay').length == 0)
      { 
        if ($.browser.msie && $.browser.version.substring(0,1) === '6')
        {
          $('div.navigation-overlay').hide()
        }
        else
        {
          $('div.navigation-overlay').slideUp('normal', function(){
            resetNavigationBorders();
          });
        }
      }
    });
    
    // Copy the content of the footer navigation panels into the 
    // overlay and show it.
    var nav = '';
    
    $('div.navigation a').click( function(){
      if (nav != $(this).attr('href').substring(1))
      { 
        nav = $(this).attr('href').substring(1);
        
        $('div.navigation-overlay').children().remove();
      
        $('div.' + nav)
          .clone()
          .prependTo('div.navigation-overlay')
          .show();
        
        if ($.browser.msie && $.browser.version.substring(0,1) === '6')
        {
          $('div.navigation-overlay').show()
        }
        else
        {
          $('div.navigation-overlay').slideDown('normal');
        }
      }
      else
      {
        if ($.browser.msie && $.browser.version.substring(0,1) === '6')
        {
          $('div.navigation-overlay').toggle()
        }
        else
        {   
          $('div.navigation-overlay').slideToggle('normal', function(){
            if ($(this).is(':hidden')) {
            resetNavigationBorders();
          }
          });
        }
      }
      
    resetNavigationBorders();
      
      if ($('div.navigation-overlay').is(':visible'))
      {
        $(this).css('border-bottom-color', $(this).css('background-color'));
      }
            
      return false;
    });
    
    // Reset all the bottom borders whenever the user selects a 
    // new nav element, clicks out, or closes the nav.
    function resetNavigationBorders()
    {
      $('div.navigation a').each( function(){
        $(this).css('border-bottom-color', $(this).css('border-right-color'));
      });
    }
    
    // Turn the complete block into a clickable link
    $('.link-block').css('cursor', 'pointer').click(function(){
      if ($(this).find('a:first').attr('href').substr(0, 4) != 'http')
      {
        window.location = $(this).find('a:first').attr('href');
      }
      else
      {
        window.open($(this).find('a:first').attr('href'));
      }
      
      return false;
      
    }); 

});

