var menu_items_up = new Array();
var menu_items_down = new Array();

function mainmenu_in ( element ) {
  for ( cont = 0, index = 0; element !== element.parentNode.childNodes[ cont ]; ++cont ) index = cont + 1;

  if ( dark_menu ( element ).childNodes[ index ].className != 'mark' ) {
    dark_menu ( element ).childNodes[ index ].style.visibility = 'hidden';
    white_menu ( element ).childNodes[ index ].style.visibility = 'visible';
    menu_up ( dark_menu ( element ).childNodes[ index ] );
    menu_up ( white_menu ( element ).childNodes[ index ] );
  }
}

function mainmenu_out ( element ) {
  for ( cont = 0, index = 0; element !== element.parentNode.childNodes[ cont ]; ++cont ) index = cont + 1;

  if ( dark_menu ( element ).childNodes[ index ].className != 'mark' ) {
    dark_menu ( element ).childNodes[ index ].style.visibility = 'visible';
    white_menu ( element ).childNodes[ index ].style.visibility = 'hidden';
    menu_down ( dark_menu ( element ).childNodes[ index ] );
    menu_down ( white_menu ( element ).childNodes[ index ] );
  }
}

function menu_in ( element ) {
  for ( cont = 0, index = 0; element !== element.parentNode.childNodes[ cont ]; ++cont ) index = cont + 1;

  if ( dark_menu ( element ).childNodes[ index ].className != 'mark' ) {
    menu_up ( white_menu ( element ).childNodes[ index ] );
    menu_down ( dark_menu ( element ).childNodes[ index ] );
  }
}

function menu_out ( element ) {
  for ( cont = 0, index = 0; element !== element.parentNode.childNodes[ cont ]; ++cont ) index = cont + 1;

  if ( dark_menu ( element ).childNodes[ index ].className != 'mark' ) {
    menu_down ( white_menu ( element ).childNodes[ index ] );
    menu_up ( dark_menu ( element ).childNodes[ index ] );
  }
}

function menu_click ( element ) {
  for ( cont = 0, index = 0; element !== element.parentNode.childNodes[ cont ]; ++cont ) index = cont + 1;

  if ( dark_menu ( element ).childNodes[ index ].className != 'mark' ) {

    dark_menu ( element ).childNodes[ index ].className = 'mark';
    instant_up ( dark_menu ( element ).childNodes[ index ], white_menu ( element ).childNodes[ index ] );
    if ( element.parentNode.parentNode.parentNode.id == 'menu' ) {
      for ( object = dark_menu ( element ).firstChild; object; object = object.nextSibling ) if ( object.nodeType == 1 ) if ( object !== dark_menu ( element ).childNodes[ index ] ) object.className = '';
      for ( object = document.getElementById( 'submenu' ).firstChild; object; object = object.nextSibling ) if ( object.nodeType == 1 ) menu_down ( object );
      menu_up ( document.getElementById( 'submenu' ).childNodes[ index ] );
      if ( document.getElementById( 'submenu' ).childNodes[ index ].childNodes.length > 1 ) submenu_click ( searchChild ( searchChild ( document.getElementById( 'submenu' ).childNodes[ index ], 1 ), 1 ) ) ;
    } else if ( element.id == 'gallery' ) {
      if ( document.getElementById( 'menu' ).childNodes[ 0 ].nodeType == 1 ) menu_up ( document.getElementById( 'menu' ).childNodes[ 0 ] );
      else menu_up ( document.getElementById( 'menu' ).childNodes[ 1 ] );
    } else {
      for ( object = document.getElementById( 'submenu' ).firstChild; object; object = object.nextSibling ) if ( object.nodeType == 1 ) menu_down ( object );
      if ( document.getElementById( 'menu' ).childNodes[ 0 ].nodeType == 1 ) menu_down ( document.getElementById( 'menu' ).childNodes[ 0 ] );
      else menu_down ( document.getElementById( 'menu' ).childNodes[ 1 ] );
      for ( object = document.getElementById( 'menuitem' ); object; object = object.nextSibling ) if ( object.nodeType == 1 ) object.className = '';
    }
  }

  return false;
}

function submenu_click ( active_element ) {
  for ( element = searchChild ( active_element.parentNode, 1 ); element; element = nextElement ( element ) ) element.className = '';
  active_element.className = 'mark';
}

function white_menu ( element ) {
  if ( element.parentNode.previousSibling.nodeType == 1 ) return element.parentNode.previousSibling.previousSibling;
  else return element.parentNode.previousSibling.previousSibling.previousSibling.previousSibling;
}

function dark_menu ( element ) {
  if ( element.parentNode.previousSibling.nodeType == 1 ) return element.parentNode.previousSibling;
  else return element.parentNode.previousSibling.previousSibling;
}

function move_menu () {
  for ( cont = 0; cont < menu_items_down.length; cont++ ) move_down ( menu_items_down [ cont ] );
  for ( cont = 0; cont < menu_items_up.length; cont++ ) move_up ( menu_items_up [ cont ] );
  if ( document.getElementById( 'next_content' ) && ( document.getElementById( 'next_content' ).innerHTML != '' ) ) rotate_sections ();
  setTimeout ( move_menu, 1 );
}

function menu_up ( element ) {
  array_delete ( menu_items_down, element );
  array_delete ( menu_items_up, element );
  menu_items_up.push( element );
}

function menu_down ( element) {
  array_delete ( menu_items_up, element );
  array_delete ( menu_items_down, element );
  menu_items_down.push( element );
}

function instant_up ( main, back ) {
  array_delete ( menu_items_down, back );
  array_delete ( menu_items_up, back );
  array_delete ( menu_items_down, main );
  array_delete ( menu_items_up, main );
  if ( back.offsetHeight < 65 ) {
    back.style.bottom = ( -back.offsetHeight - 5 ) + 'px';
    main.style.bottom = '0px';
  } else {
    for ( object = main.parentNode.firstChild; object; object = object.nextSibling ) if ( object.nodeType == 1 ) if ( object !== main ) {
      if ( object.className == 'mark' ) object.style.top = '0px';
      object.style.visibility = '';
      object.className = '';
      menu_down ( object );
    }
    for ( object = back.parentNode.firstChild; object; object = object.nextSibling ) if ( object.nodeType == 1 ) if ( object !== back ) {
      if ( object.className == 'mark' ) object.style.top = '0px';
      object.style.visibility = '';
      object.className = '';
      menu_down ( object );
    }
    main.className = 'mark';
    main.style.top = '';
    back.className = 'mark';
    back.style.top = '';
  }
}

function move_up ( element ) {
  if ( element.parentNode.id == 'submenu' || element.offsetHeight < 25  ) {
    if ( !element.style.top ) position = element.offsetTop;
    else position = element.style.top.replace( 'px', '' ) - 0 + 1;
    if ( position > 0 ) array_delete ( menu_items_up, element );
    else element.style.top = position + 'px';
  } else if ( element.offsetHeight < 65 ) {
    if ( !element.style.bottom ) position = -1 * element.offsetHeight;
    else position = element.style.bottom.replace( 'px', '' ) - 0 + 1;
    if ( position > 0 ) array_delete ( menu_items_up, element );
    else element.style.bottom = position + 'px';
  } else {
    if ( element.style.top == '' ) position = element.offsetTop + 1;
    else position = element.style.top.replace( 'px', '' ) - 0 + 1;
    if ( position > 0 ) array_delete ( menu_items_up, element );
    else element.style.top = position + 'px';
  }
}

function move_down ( element ) {
  if ( element.parentNode.id == 'submenu' || element.offsetHeight < 25  ) {
    if ( element.style.top == '' ) position = element.offsetTop - 1;
    else position = element.style.top.replace( 'px', '' ) - 1;
    if ( Math.abs ( position ) > element.offsetHeight + 5 ) array_delete ( menu_items_down, element );
    else element.style.top = position + 'px';
  } else if ( element.offsetHeight < 65 ) {
    if ( element.style.bottom == '' ) position = - 1;
    else position = element.style.bottom.replace( 'px', '' ) - 1;
    if ( Math.abs ( position ) > element.offsetHeight + 5 ) array_delete ( menu_items_down, element );
    else element.style.bottom = position + 'px';
  } else {
    if ( !element.style.top ) position = element.offsetTop;
    else position = element.style.top.replace( 'px', '' ) - 1;
    if ( Math.abs ( position ) > element.offsetHeight - 35 ) array_delete ( menu_items_down, element );
    else element.style.top = position + 'px';
  }
}

function rotate_sections () {
  actual = document.getElementById( 'content' );
  next = document.getElementById( 'next_content' );

  if ( next.style.display == 'none' ) {
    next.style.left = actual.offsetWidth + 'px';
    next.style.display = 'block';
  }

  offset = next.style.left.replace( 'px', '' ) - 40;
  if ( offset < 0 ) offset = 0;

  next.style.left = offset + 'px';
  actual.style.left = ( offset - actual.offsetWidth ) + 'px';
  if ( offset == 0 ) change_content ();
}

function array_delete ( array, element ) {
  for ( cont = 0; cont < array.length; cont++ ) if ( array [ cont ] === element ) break;
  if ( cont < array.length ) array.splice( cont, 1 );
  else return 0;
  return 1;
}

function load ( new_section, new_gallery, new_category ) {

  if ( ( section == new_section ) && ( gallery == new_gallery ) && ( category == new_category )  ) return 0;

  if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest();
  else xmlHttp=new ActiveXObject( 'Microsoft.XMLHTTP' );

  url = '/content.php?lan=' + lan + '&section=' + new_section + '&gallery=' + new_gallery + '&category=' + new_category;

  xmlHttp.open ( 'GET', url, false );
  xmlHttp.send ( null );

  target = document.getElementById( 'next_content' );
  target.innerHTML = xmlHttp.responseText;

  section = new_section;
  gallery = new_gallery;
  category = new_category;

  if ( lan ) url = '/' + section + '/' + gallery + '/' + category + '/';
  else url = '/en/' + section + '/' + gallery + '/' + category + '/';

  if ( document.getElementById( 'lan' ) ) document.getElementById( 'lan' ).href = url.replace( '//', '/' ).replace( '//', '/' ).replace( '//', '/' );
}

function change_content () {
  document.getElementById( 'content' ).innerHTML = document.getElementById( 'next_content' ).innerHTML;
  document.getElementById( 'content' ).style.left = 0;
  document.getElementById( 'next_content' ).innerHTML = '';
  document.getElementById( 'next_content' ).style.display = 'none';

  if ( ielt8 () ) setTimeout ( initie, 100 );
}

move_menu ();
