function setup_gallery()
{
  var site_width = 1240;
  var bar_width = site_width;

  function calc_width()
  {
    var cont_width = 0;
    $('#pics img').each(function() { cont_width += $(this).width() + 15; });

    var mode_double = $('#pics').hasClass('double');


    if (mode_double)
    {
      var cont_width_double = (cont_width / 2) + 150;

      if (cont_width_double < site_width)
      {
        cont_width_double = site_width;
        $('#scroller').fadeOut();
      }
      else
      {
        $('#scroller').fadeIn();
      }

      $('#pics.double').css({ width: cont_width_double });
    }
    else
    {
      if (cont_width < site_width)
      {
        cont_width = site_width;
        $('#scroller').fadeOut();
      }
      else
      {
        $('#scroller').fadeIn();
      }

      $('#pics').css({ width: cont_width });
    }
  }

  calc_width();

  function pan()
  {
    var w = $('#scroller').width();
    var x = $('#scroller').position().left;
    var px = $('#scrollbar').position().left;
    var nx = (x - px) / (bar_width - w);
    var scroll = ($('#pics').width() - site_width) * nx;
    $('#pics').offset({ left: px - scroll });
  }

  $('#middle, #scrollbar').css({ width: site_width });
  //$('.menubar').css({ width: site_width - $('ul#menu').width() });
  $('#pics a').lightBox({fixedNavigation:true});

  $('#scroller').bind('mousedown', function(e)
  {
    var px = $('#scrollbar').position().left;
    var ox = e.clientX - $('#scroller').position().left;

    $(document).bind('mousemove', function(e)
    {
      var x = e.clientX - ox;
      var w = $('#scroller').width();

      if (x < px) x = px;
      else if (x > px + bar_width - w) x = px + bar_width - w;

      $('#scroller').offset({ left: x });
      pan();
    });

    return false;
  });

  $(document).bind('mouseup', function()
  {
    $(document).unbind('mousemove');
  });

  $(window).resize(function()
  {
    var px = $('#scrollbar').position().left;
    var scroll = -($('#pics').offset().left - px);
    var nx = scroll / ($('#pics').width() - site_width);
    var w = $('#scroller').width();
    var x = nx * (bar_width - w) + px
    $('#scroller').offset({ left: x });
  });

  var mode = $('#pics').hasClass('double');

  $('#mode').click(function()
  {
    mode ^= 1;
    if (mode) $('#pics').addClass('double');
         else $('#pics').removeClass('double');
    calc_width();
    pan();
  });

  $('#search input').focus(function() { if ($(this).val() == $(this).attr('alt')) { $(this).val(''); $(this).addClass('active'); }  });
  $('#search input').blur(function() { if ($(this).val() == '') { $(this).val($(this).attr('alt')); $(this).removeClass('active'); }  });
}

$(document).ready(function()
{
  function megaHoverOver()
  {
    $(this).find(".sub").stop().fadeTo('fast', 1).show();

    (function($) {
      jQuery.fn.calcSubWidth = function()
      {
        rowWidth = 0;
        //Calculate row
        $(this).find("ul").each(function()
        {
          rowWidth += $(this).width(); 
        }); 
      };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 )
    {
      var biggestRow = 0; 
      $(this).find(".row").each(function()
      {
        $(this).calcSubWidth();
        if(rowWidth > biggestRow)
        {
          biggestRow = rowWidth;
        }
      });
      $(this).find(".sub").css({'width' :biggestRow});
      $(this).find(".row:last").css({'margin':'0'});
    }
    else
    {
      $(this).calcSubWidth();
      $(this).find(".sub").css({'width' : rowWidth});
    }
  }

  function megaHoverOut()
  {
    $(this).find(".sub").stop().fadeTo('fast', 0, function()
    {
      $(this).hide();
    });
  }

  var config = {
     sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 500, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
  };

  $('ul#menu li .sub').css({'opacity':'0'});
  $('ul#menu li').hoverIntent(config);

  var vid;

  vid = setInterval(function()
  {
    var loaded = 0;

    $('#pics img').each(function()
    {
      if ($(this)[0].complete) loaded++;
    });

    if (loaded >= $('#pics img').size())
    {
      // load complete
      clearInterval(vid);
      setup_gallery();
    }
  }, 250);

  $('#menu li.item').click(function()
  {
    location.href = $(this).find('a').attr('href');
  });
});

