function findLeft(obj) {
  var curLeft = 0;
  if (obj.offsetParent) {
    curLeft = obj.offsetLeft;
    while (obj = obj.offsetParent) {
      curLeft += obj.offsetLeft;
    }
  }
  return curLeft;
}

function Start(){

  var obj = $('ul#Menu');

  var fadeInTime = 200; // in milliseconds
  var fadeOutTime = 300; // in milliseconds

  $(obj).find('li ul li:has(ul)').prepend('<span class="menu-sub-indicator"> &nbsp;</span>');
  /*
  var pos;
  $(obj).find('li ul li ul').each(function() {
    pos = findLeft($(this).parent().parent().parent().get(0));
    //alert(pos);
    if ($("#themeContainer").width() - pos < 400) {
      $(this).css('left', '-200px');
    }
  });
  */
  $(obj).find('li ul').attr('fadeState','none');
  $(obj).find('li').hover(
    function() {
      if($(this.getElementsByTagName('ul')[0]).attr('fadeState') =='fadeOut') {
        $(this.getElementsByTagName('ul')[0]).stop(true,true);
        $(this.getElementsByTagName('ul')[0]).attr('fadeState','none')
      } 
      
      if ($(this.getElementsByTagName('ul')[0]).attr('fadeState') =='none') {
        $(this.getElementsByTagName('ul')[0]).attr('fadeState','fadeIn');
        $(this.getElementsByTagName('ul')[0]).fadeIn(fadeInTime, function(){
          $(this).attr('fadeState','none')
        });
      }
      $(this).addClass('over');
    },
    function() {
      if($(this.getElementsByTagName('ul')[0]).attr('fadeState') =='fadeIn') {
        $(this.getElementsByTagName('ul')[0]).stop(true,true)
        $(this.getElementsByTagName('ul')[0]).attr('fadeState','none');
      }
      
      if ($(this.getElementsByTagName('ul')[0]).attr('fadeState') =='none') {
        $(this.getElementsByTagName('ul')[0]).attr('fadeState','fadeOut');
        $(this.getElementsByTagName('ul')[0]).fadeOut(fadeOutTime, function(){
          $(this).attr('fadeState','none')
        });
      }
      $(this).removeClass('over');
    }
  );
  
  $(obj).find('li ul:empty').css('visibility','hidden');
    // Remove the bottom border on all last items in ULs:
    $(obj).find('ul').each(function() {
        $(this).find('li:last a').css('border-bottom','0px');
    });
  
  $(obj).find('li:has(ul)').hover(function() {
    $(this).addClass('hasChild');
  },
  function() {
    $(this).removeClass('hasChild');
  });
  //$(obj).find('ul').css({'opacity':'0.88'});
  injectHeadFoot();
}

function injectHeadFoot() {
  $('#Menu li.item ul').each(function() {
    $(this).find('li:first').before('<li class="first"><div>&nbsp;</div></li>');
    $(this).find('li:last').after('<li class="last"><div>&nbsp;</div></li>');
  });
}

$(document).ready(function() {
  pixelsilk2.renderSkin({skin: '[' + '[Menu1]' + ']', path: ''}, function(html) {
    $('#subNavigation').html(html);
    var sections = $('#subNavigation div');
    var topElements = $('#Menu li.item');
    for (z = 0; z < sections.length; z++) {
      var li = topElements[z];
      var lihtml = li.innerHTML;
      li.innerHTML = lihtml + sections[z].innerHTML;
    }
    Start();
  });
});
