$j(document).ready(function() {
        
   $div1con = $j('div.mainM .div1 .wrapper div');
   $div2con = $j('div.mainM .div2 .wrapper div');
   $div1 = $j('div.mainM .div1');
   $div2 = $j('div.mainM .div2');
   $main = $j('#mainM');
   
   $fdiv0 = $j('#fdiv0');
   $fdiv1 = $j('#fdiv1');
   $fdiv2 = $j('#fdiv2');
   
   jQuery.fn.mountCarousel = function($obj) {$obj.infiniteCarousel();}
   jQuery.fn.exists = function() {return this.length>0;}
   
   var reset = true;
   
   resetPosition(true);
   
   function resetPosition(hard) {
   $j('#mainM').find('a').removeClass('hover');
   $j('#company').css({color: '#666666'});  
   if($fdiv0.exists()){                                                
      var fdiv0con = $fdiv0.html();
      if (fdiv0con == 'company')
         $j('#'+fdiv0con).css({color: '#C60000'});
      else
         $j('#'+fdiv0con).find('> a').addClass('hover');
      
      if($fdiv1.exists()){
         var fdiv1con = $fdiv1.html()     
         $j('#'+fdiv0con+'M').find('> span a.id'+fdiv1con).addClass('hover');
         
         if($fdiv2.exists()){
            var fdiv2con = $fdiv2.html()     
            var fdiv2m = $j('#'+fdiv0con+'M').find('> span a.id'+fdiv1con).parent().attr('id');
            $j('#'+fdiv2m+'M').find('> span a.id'+fdiv2con).addClass('hover');
         }

         var fdiv2m = $j('#'+fdiv0con+'M').find('> span a.id'+fdiv1con).parent().attr('id');
         if($j('#'+fdiv2m+'M').exists()) {
            var con2 = $j('#'+fdiv2m+'M').html();
            if(con2 != null) {
               var cls = $j('#'+fdiv0con+'M > span > a.id'+$fdiv1.html()).parent().attr('id');
               $div2con.addClass(cls);
               if(hard)
                  $div2con.html(con2).mountCarousel($div2);
               else
                  $div2con.find('> span a.id'+fdiv2con).addClass('hover');   
               $div2.css({height: '22px'});            
            }   
         }            
      }
      
      var con1 = $j('#'+fdiv0con+'M').html();
      if(con1 != null) {
         $div1con.addClass($j('#'+fdiv0con).attr('id'));
         if(hard)
            $div1con.html(con1).mountCarousel($div1);
         else
            $div1con.find('> span a.id'+fdiv1con).addClass('hover');   
         $div1.css({height: '22px'});            
      }
   }
   }
    
   $main.delegate('span', 'mouseover', function(){
      if(!$j(this).hasClass('empty')) {
      reset = true;
      var con = $main.find('#'+$j(this).attr('id')+'M').html();
      if($j(this).parent().hasClass('mainM')) {
         $div1.stop(true, true);
         $div2.stop(true, true);
         $j(this).parent().find('a').removeClass('hover');
         $j(this).parent().find('span').css({color: '#666666'});
         $j(this).find('a').addClass('hover');
         $j(this).css({color: '#C60000'});
         
         if(con != null) {
            $div1con.removeClass();
            $div1con.addClass($j(this).attr('id'));
            
            $div2.delay(300).animate({height: '0px'}, 300);
            $div1.delay(300).animate({height: '0px'}, 300, function() {$div1con.html(con).mountCarousel($div1);}).animate({height: '22px'}, 300);            
         }
         else {
            $div2.delay(300).animate({height: '0px'}, 300, function() {$div1.animate({height: '0px'}, 300);});            
         }         
      }
      else if($j(this).parent().parent().parent().hasClass('div1')) {
         $div1.stop(true, true);
         $div2.stop(true, true);
         $j(this).parent().find('a').removeClass('hover');
         $j(this).find('a').addClass('hover');
         
         if ($j(this).parent().attr('class') != 'company')
            $j('#'+$j(this).parent().attr('class')+' a').addClass('hover');
         else
            $j('#'+$j(this).parent().attr('class')).css({color: '#C60000'});
            
         if(con != null) {
            $div2con.removeClass(); 
            $div2con.addClass($j(this).attr('id'));
             
            $div2.delay(300).animate({height: '0px'}, 300, function() {$div2con.html(con).mountCarousel($div2);}).animate({height: '22px'}, 300);                      
         }
         else {
            if($fdiv0.html() == $j(this).attr('class') && $j(this).attr('class') != 'exep')
               reset = false;
            $div2.delay(300).animate({height: '0px'}, 300);
         }
      }
      else if($j(this).parent().parent().parent().hasClass('div2')) {
         $div1.stop(true, true);
         $div2.stop(true, true);
         $j(this).parent().find('a').removeClass('hover');
         $j(this).find('a').addClass('hover');
         
         $j('span.'+$j('#'+$j(this).parent().attr('class')).attr('class')+' a').removeClass('hover');
         $j('#'+$j(this).parent().attr('class')+' a').addClass('hover');
         if($fdiv0.html()+$fdiv1.html() == $j(this).attr('class'))
            reset = false;
      }
      }
      else {
         reset = false;
      }
   });
   
   $main.bind('mouseleave', function () {
      if(reset)
         $div2.delay(300).animate({height: '0px'}, 300, function(){$div1.animate({height: '0px'}, 300, function(){resetPosition(true);});});       
      else
         resetPosition(false);
  });
          
});   
