/**
 * Description: general javascript actions and functions for navigation
 * User: pedro alfaiate
 * Date: Dez/2011
 **/

 var obj_imageArray = [],
        str_menuPhotosContainerWidth;

 $(function() {
    $('.MyImageGallery a').lightBox();
 });

  function onLoadMainMenu(){

    if ( $.browser.msie ) {
      $(".pageLevel1 > li > a").css({height:'20px'});
    }

    createMenuPhotos();

    if($("#mainMenu > li.inpath").index() > 0){
      $("#mainMenu > li.inpath").insertAfter($("#mainMenu > li:first"));
    }

    $("#mainMenu > li").hover(function(event) {
      event.stopPropagation();
      if($(this).hasClass("hasChildren")){
        if(!$(this).hasClass("inpath")){
          closeMenuTabs($(this).index());

          animateMenuTab(this,'217');
          animateMenuTab('.menuPhotosContainer',str_menuPhotosContainerWidth-192);

          $(this).find('ul.pageLevel2').fadeIn(600);
          //$(this).find('ul.pageLevel2').show();
        }
      }else{
        closeMenuTabs();
        animateMenuTab('.menuPhotosContainer',str_menuPhotosContainerWidth);
      }
      if(!$(this).hasClass("menuPhotosContainer")){
        showPic($(this).attr('id').replace('menu',''));
      }else{
        showPic($("#mainMenu > li.inpath").attr('id').replace('menu',''));
      }
    });
    
    $('table').mouseenter(function(event) {
      event.stopPropagation();
      closeMenuTabs();
      animateMenuTab('.menuPhotosContainer',str_menuPhotosContainerWidth);
      showPic($("#mainMenu > li.inpath").attr('id').replace('menu',''));
    });
    $('.Main').mouseleave(function(event) {
      event.stopPropagation();
      closeMenuTabs();
      animateMenuTab('.menuPhotosContainer',str_menuPhotosContainerWidth);
      showPic($("#mainMenu > li.inpath").attr('id').replace('menu',''));
    });

  };

  function createMenuPhotos(){
    $("#mainMenu > li:first").after('<li class="menuPhotosContainer"><ul id="menuPhotos"></ul></li>');
    $("#mainMenu > li > a").each(function(i){
      obj_imageArray.push($(this).attr('rev'));
      $('#menuPhotos').append('<li><img src="'+obj_imageArray[i]+'"/></li>')
    });

    str_menuPhotosContainerWidth = 910-($("#mainMenu").width()-24);
    $("#mainMenu").width(1300);

    $('.menuPhotosContainer').width(str_menuPhotosContainerWidth);
    showPic($("#mainMenu > li.inpath").attr('id').replace('menu',''));
  }

  function showPic(num_index){
    $("#menuPhotos li").each(function(i){
      if(num_index != $(this).index()){
        if($(this).css('zIndex') != 900){
          $(this).css({'zIndex':700}).fadeOut(0);
        }else{
          $(this).css({'zIndex':800}).fadeIn(0);
        }
      }
    });
    $('#menuPhotos li').eq(num_index).css({'zIndex':900}).fadeIn(600);
  }

  function closeMenuTabs(num_thisIndex){
    $("#mainMenu > li.hasChildren").each(function(i){
      if(num_thisIndex != i){
        $(this).animate({
           width: '24px'
        }, { duration: 600, queue: false });
        //$(this).find('ul.pageLevel2').fadeOut(0);
        $(this).find('ul.pageLevel2').hide();
      }
    });
  }

  function animateMenuTab(str_element,num_animateWidth){
    $(str_element).animate({
      width: num_animateWidth+'px'
    }, { duration: 600, queue: false });
  }
