﻿$(document).ready(function () {

  $("#SearchContainer").hide();

  menuLoading(function () {
    if ($('#mainContent').find('img').length > 0) {
      //alert('omg');
      $('#mainContent').find("img:last").one('load', function () {
        //alert('wtf');
        scrolling();
      }).each(function () {
        if (this.complete) $(this).load();
      });
    }
    else
      scrolling();
  });
});

function scrolling() {
  // Da qui in poi partono le animazioni di scrolling del menu
  var fast = 600;

  var outer = $('#mainContent').height();
  var inner = $('#fixed_menu').height();
  //alert(inner + " <-> " + outer);
  if (outer <= inner)
    $('#mainContent').css("min-height", inner + "px");
  else {
    $('#menu').scrollFollow({ speed: fast, offset: 0, container: 'container' });
    $('.ac_results').scrollFollow({ speed: fast, offset: 0, container: 'idbody' });
  }

  $("#SearchContainer").show('fade','slow');
}

function menuLoading(callback) {
  //  Per far partire una animazione dobbiamo avere una voce di menù di primo livello attiva

  if ($('ul#main_menu').children('li').hasClass('active')) {

    //  Nel caso ci troviamo con una lista di terzo livello appena aperta dobbiamo considerare 
    //  l'altezza delle voci di terzo livello in modo che l'animazione non influisca sulle voci del secondo
    var highness = 0;
    $('li.open>ul.chips_menu').children('li').each(function (index, child) {
      highness = highness + $(child).height();
    });
    $('li.open>ul.chips_menu').height(highness);

    //  Nascondiamo tutti gli elementi
    $('#menu').find('li').hide();

    $('ul#main_menu>li.active').insertAfter('ul#main_menu>li:last');
    $('ul#main_menu>li[class!="active"]').show();

    //  La classe open del secondo livello non esiste se una voce del menu del terzo livello
    //  è selezionata, con questo fix risolviamo il problema
    if ($('ul.onions_menu').find('li').hasClass('select')) {
      if (!$('li.select').parent('ul').parent('li').hasClass('open'))
        $('li.select').parent('ul').parent('li').addClass('open');
    }

    // Passiamo al secondo livello
    if ($('ul.onions_menu').children('li').hasClass('open')) {

      $('ul#main_menu>li.active').show();
      $('li.open').insertBefore('ul.onions_menu>li:first');

      // Passiamo al terzo livello
      if ($('ul.onions_menu').find('li').hasClass('select')) {
        $('ul.onions_menu').children('li').show();
        $('li.open>ul.chips_menu>li:eq(0)').show(0, function () {
          if ($(this).is(':last-child')) callback(); else
            $(this).next("li").show(0, arguments.callee);
        });
      }
      else {
        $('li.open>ul.chips_menu>li:eq(0)').show('drop', { direction: 'up' }, 50, function () {
          if ($(this).is(':last-child')) callback(); else
          $(this).next("li").show('drop', { direction: 'up' }, 50, arguments.callee);
        });
        $("ul.onions_menu>li:eq(0)").show(0, function () {
          if ($(this).is(':last-child')) callback(); else
          $(this).next("li").show(0, arguments.callee);
        });
      }

    }
    else {
      $('ul#main_menu>li.active').show('drop', { direction: 'up' }, 500, function () {
        $("ul.onions_menu>li:eq(0)").show('drop', { direction: 'up' }, 100, function () {
          if ($(this).is(':last-child')) callback(); else
          $(this).next("li").show('drop', { direction: 'up' }, 100, arguments.callee);
        });
      });  //.stop(true, true).removeAttr('style'); //  Per compensare l'inefficienza di IE
    }
  }
  else callback();
}

