//var firstMenuTransitionSpeed = 10; //1000 for one second

$(document).ready(function() {  

    $('.current').each(function(){
      $(this).children('ul:first').addClass('visible');
    })
    
    // animation speed
    var menuAnimationSpeed = 1000;
    
    // submenu width
    var menuWidth = {
      level2: 154,
      level3: 350
    }

    // menuitem ID that has a submenu    
    var menuItemID = {
      levelOne: ["navCompany","navNews","navOperations"],
      levelTwo: ["navManagementTeam","navBoardOfDirectors"]
    }   
    
    var menuIDOperations = ["navOperations","navOverview",
														"navCambodia","navIndonesia",
														"navThailand","navVietnam"];
		

    
    $("#navPrimary li").hover(
      function(e){          
          // prevent submenu from flickering causes by event bubbling
          if($(this).parent("ul").hasClass("visible")) {
            e.stopPropagation();
          }
          
          var itemID = $(this).attr("id");


          if ($.inArray(itemID, menuIDOperations) < 0) {
            $("#topRightNew").hide();
						$("#operations-intro").css("visibility","hidden");
          }          
					
					if(itemID != "navFirstReserve") {
						$("#top_right_asset").hide();
					}

          if ($.inArray(itemID, menuItemID.levelOne) >= 0) {
            var animateWidth = menuWidth.level2;
          } 
          else if ($.inArray($(this).attr("id"), menuItemID.levelTwo) >= 0) {
            var animateWidth = menuWidth.level3;
          }              
          
          //hide the active submenu
          $(this)
            .parent()
            .find("ul.visible")
            .removeClass("visible")
            .addClass("invisible");               
          
          // slide submenu
          $(this)
            .find("> ul")
            .show()
            .animate({ width: animateWidth }, menuAnimationSpeed);

      },         
      function(e){       
        // hide submenu
        $(this)
          .find("> ul")
          .stop(false,true)
          .hide()
          .css({ width:0 });
        
        //bring back active submenu
        $(this)
          .parent()
          .find("ul.invisible")
          .removeClass("invisible")
          .addClass("visible");  
          
        $("#topRightNew").show();  
				$("#top_right_asset").show();			
				$("#operations-intro").css("visibility","visible");
      } 
    );        
    
  
    /*first menu*
    
    $('.main-menu').children('li').hover(function(){
    
      $(this).parent('ul').children('li').children('ul').css('display', 'none');
    
      $(this).children('ul').css('overflow', 'hidden');
      $(this).children('ul').css('display', 'block');
      var storedWidth = parseInt($(this).children('ul').css('width'));
      $(this).children('ul').css('width', '0');
      $(this).children('ul').css('opacity', '0');
      $(this).children('ul').animate({'width': storedWidth, 'opacity':1}, firstMenuTransitionSpeed, function(){
        $(this).css('overflow', 'visible');    
      });
      
    }, function (){
      $(this).children('ul').css('display', 'none');  
    });
    
    second menu
    $('.main-menu').children('li').children('ul').children('li').hover(function(){
    
      document.getElementById('top_right_asset').style.visibility=false;
    
      $(this).parent('ul').children('li').children('ul').css('display', 'none');
      
      $(this).children('ul').css('overflow', 'hidden');
      $(this).children('ul').css('display', 'block');
      var storedWidth = parseInt($(this).children('ul').css('width'));
      $(this).children('ul').css('width', '0');
      $(this).children('ul').css('opacity', '0');
      $(this).children('ul').animate({'width': storedWidth, 'opacity':1}, firstMenuTransitionSpeed, function(){
        $(this).css('overflow', 'visible');    
      });
    
    }, function (){
      $(this).children('ul').css('display', 'none');
      document.getElementById('top_right_asset').style.visibility=true;      
    });
  
  
  $('#navPrimary').hover(function(){    
    $(this).find('ul.visible').addClass('visible-inMove');
    $(this).find('ul.visible-inMove').removeClass('visible');
    $(this).find('.highlight').addClass('highlight-inMove');
    $(this).find('.highlight-inMove').removeClass('highlight');
    
  },function(){
    $('#top_right_asset').show();
  
    $(this).find('ul.visible-inMove').addClass('visible');
    $(this).find('ul.visible').removeClass('visible-inMove');
    
    $(this).find('.highlight-inMove').addClass('highlight');
    $(this).find('.highlight').removeClass('highlight-inMove');
  })
  
  $('#navPrimary > li, #navPrimary li > li').hover(function(){
    $('#top_right_asset').hide();                                                            
  })
*/  
  
  // show hide itemlist
  $("#itemlist-operations li h4 a").live("click", function(e){
    e.preventDefault();
    $("#itemlist-operations li p").hide();
    $(this).parents("li").find("p").show();  
  });
  
  // grab contents via AJAX
  $("#operations_content1").load('/default/index.cfm/operations1/overview/ #content');
  $("#operations_content2").load('/default/index.cfm/operations1/cambodia/ #content');
  $("#operations_content3").load('/default/index.cfm/operations1/indonesia/ #content');
  $("#operations_content4").load('/default/index.cfm/operations1/thailand/ #content');
  $("#operations_content5").load('/default/index.cfm/operations1/vietnam/ #content');

  // fancy box
  $("a.showcontract, area.showcontract, a.showcontract2").fancybox({
		padding: 0,
		titleShow: false
	});
  
  // workaround to make fancybox work on links that are loaded via AJAX
  $("a.showcontract-remote").live("click", function(e){
	   e.preventDefault();                                                    
    $("a.showcontract2")
			.attr("href", $(this).attr("href"))
			.trigger("click");
  });
  
  
})
