var scroll_busy = false;

Shadowbox.loadSkin('classic', 'shadowbox/skin');
Shadowbox.loadLanguage('fr', 'shadowbox/');
Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img', 'qt', 'swf', 'wmp'], 'shadowbox/player');

$(window).load(function() {
  hideBrowserScrollbar();
  
  setMenuItemTop();
  setMenuItemsHeight();
  
  $(document).ready(function () {
    setMenuHeight();
    $(window).resize(function () {
      setMenuItemTop();
      setMenuHeight();
    });
    
    var options = {
      handleOversize:     'drag',
      displayNav:         true,
      handleUnsupported:  'remove',
      autoplayMovies:     false
    };
    Shadowbox.init(options);
    
    initializeSubmenus();
  });

  $(document).ready(function () {
    
  });
});

function initializeSubmenus()
{
  $('#items').children().each(function (i) {
    $('#submenu_'+i).css('top', '103px');
    $('#menu'+i).children('#submenu_'+i).children('.submenu').each(function (j) {
      $('#submenu_'+i+'_'+j).css('top', j * 25 + 'px');
      switch(j){
        case 0:
          $('#submenu_'+i+'_'+j).css('font-variant', 'small-caps');
          $('#submenu_'+i+'_'+j).css('font-weight', 'bold');
          $('#submenu_'+i+'_'+j).css('color', '#FFFFFF');
          break;
        case 1:
          $('#submenu_'+i+'_'+j).css('color', '#A0B4B4');
          break;
        case 2:
          $('#submenu_'+i+'_'+j).css('color', '#A0B4B4');
          break;
      }
    });
  });
  
  $('#items').children().each(function (i) {
    $('#menu'+i).children('#submenu_'+i).children('.submenu').each(function (j) {
      $("#menu"+i).hover(function () {
        setTimeout(function () { 
          $('#submenu_'+i).show();
          if($('#submenu_'+i+'_'+j).is(':hidden'))
          {
            
            $('#submenu_'+i+'_'+j).animate({'left': 4, opacity: 'show'}, 'slow');
          }
        }, j * 250);
      },function(){
        setTimeout(function () { 
          if(!$('#submenu_'+i+'_'+j).is(':hidden'))
          {
            $('#submenu_'+i+'_'+j).animate({'left': -204, opacity: 'hide'}, 'slow');
            if(j == '2')
            {
              $('#submenu_'+i).hide('slow');
            }
          }
        }, j * 250);
      });
    });
  });
}

function loadPage(page)
{
  window.open(page);
}

function setMenuHeight()
{
  var windowHeight = parseInt($(window).height() - $('#bottommenu').height());
  $('#bottommenu').css('top', windowHeight + 'px');
  var menuHeight = parseInt($(window).height());
  $('#menu').css('height', menuHeight + 'px');
}

$(document).mousewheel(function(objEvent, intDelta) {
  if(intDelta > 0)
  {
    scrollUp();
  }
  else if(intDelta < 0)
  {
    scrollDown();
  }
});

function scrollUp()
{
  var top = parseInt($("#items").css("top"));
  if(top < 0 && !scroll_busy)
  {
    scroll_busy = true;
    var s = setTimeout("scroll_busy = false", 500);	
    if(top + 188 > 0)
    {
      $("#items").animate({"top": "0px"});						
    }
    else
    {
      $("#items").animate({"top": top + 188 + "px"});	
    }
  }
}

function scrollDown()
{
  var menuItemsHeight = $("#items").height();
  var bottomMenu = parseInt($("#bottommenu").css("top"));
  var top = parseInt($("#items").css("top"));
  var bottomMenuItems = top + menuItemsHeight;
  if(bottomMenuItems > bottomMenu && !scroll_busy)
  {
    scroll_busy = true;
    var s = setTimeout("scroll_busy = false", 500);
    if(bottomMenuItems < bottomMenu + 188 )
    {
      var j;
      $('#items').children().each(function (i) {
        j = i + 1;
      });
      var new_top = $(window).height() - j * 188 - ($('#bottommenu').height() + $('#topmenu').height()) - 6;
      $("#items").animate({'top': new_top + 'px'});
    }
    else
    {
      $("#items").animate({"top": top - 188 + "px"});
    }
  }
}			

function setMenuItemTop()
{
  var menuTopHeight = 20;
  var menuItemHeight = 174;
  var marginAndBorder = 7;
  var top = menuTopHeight;
  $("#items").animate({'top': '0px'});
  $('#items').children().each(function (i) {
    top += marginAndBorder;
    $('#menu'+i).animate({'top': top + 'px'});
    top += (menuItemHeight + marginAndBorder);
  });
}

function setMenuItemsHeight()
{
  var menuItemHeight = 174;
  var marginAndBorder = 7;
  var menuHeight = 0;
  $('#items').children().each(function (i) {
    menuHeight += (menuItemHeight + marginAndBorder);
  });
  $('#items').css('height', menuHeight + 'px');
  
}

function hideBrowserScrollbar() 
{
  $('#menu').css('overflow','hidden');
  $('html').css('overflow','hidden');
  $('body').css('overflow','hidden');
}