$(function(){
    var $arrow = null;
    var $currentPetImage = null;
    var $mainMenuBlock = null;
    var petRegexp = /mmp_(.*)/
    var mm_update_time = 5000;
    var mm_fade_delay_before = 100;
    var mm_fade_in = 150;
    var MM_HoverDelay_Timer = null;
    var MM_AutoUpdate_Timer = null;
    
    /** получаем массив всех типов питомцев **/
    var pets = [];
    $('#main_menu .pets li').each(function(){pets.push(petRegexp.exec($(this).attr('id'))[1])});
    var pets_current = 0; //текущий индекс типа пета
    
    //if(for_us)
    //{
        /*$('#pet_bg>div').css({'position':'absolute','top':0,'left':0});
        $('#main_menu_arrow').css({
            '-moz-transition': 'left 0.2s',
            '-webkit-transition': 'left 0.2s',
            '-o-transition': 'left 0.2s',
            '-ms-transition': 'left 0.2s',
            'transition': 'left 0.2s'
        });*/
        MM_AutoUpdate_Timer = setInterval(MM_AutoUpdate,mm_update_time);
    //}
    
    function MM_AutoUpdate() {
        pets_current = (pets_current+1 >= pets.length) ? 0 : pets_current+1;
        MM_UpdateCurrentsPet();
    }
    
    function MM_UpdateCurrentsPet() {
        var pet = pets[pets_current];
        var $img = $('#main_menu .pets li#mmp_'+pet);
        
    	$('#pet_info .pet_info').hide();
    	$('#'+pet+'_info').show();
        
        /*if(!for_us)
        {
    	   $('#pet_bg>div').hide();
    	   $('#'+pet+'_bg').show();
        }
        else
        {*/      
            //пропадающие
            $('#pet_bg>div').css('z-index','-1').stop(1).delay(mm_fade_in+5).fadeOut();
            
            //появляющаяся
            $('#'+pet+'_bg').css('z-index','0').stop(1).fadeTo(mm_fade_in,1);
        //}
        
        if ($img.length > 0)
        $arrow.offset( { left:($img.offset().left + ($img.width()-43)/2) });
        $currentPetImage = $img; 
    }
    
    function MM_UpdateArrowPosition($img) {
    	if ($img.length > 0)
    	$arrow.offset( { left:($img.offset().left + ($img.width()-43)/2) });
    	$currentPetImage = $img;
    }
    
    function MM_UpdateBackground(pet) {
    	$('#pet_info .pet_info').hide();
    	$('#'+pet+'_info').show();
    	$('#pet_bg>div').hide();
    	$('#'+pet+'_bg').show();
    }

//$(window).load(function() {
	/*$('#main_menu .pets li a').click(function(evnt) {
		evnt.preventDefault();
	});*/

	$arrow = $('#main_menu_arrow');
	$mainMenuBlock = $('#main_menu');
	$arrow.show();
	$currentPetImage = $('#main_menu .pets li#mmp_dog img')
	MM_UpdateArrowPosition($currentPetImage);

	//$('#main_menu .pets li').click(function() {
    $('#main_menu .pets li').mouseover(function() {
        clearInterval(MM_AutoUpdate_Timer);
       
        pets_current = $('#main_menu .pets li').index(this);
        
        clearTimeout(MM_HoverDelay_Timer);
        MM_HoverDelay_Timer = setTimeout(MM_UpdateCurrentsPet,150);
        
        MM_AutoUpdate_Timer = setInterval(MM_AutoUpdate,mm_update_time);
         
		//MM_UpdateArrowPosition($(this).find('img'));
		//MM_UpdateBackground(petRegexp.exec($(this).attr('id'))[1]);
	});
	
	MM_UpdateBackground('dog');
	
	$(window).resize(function() {
		MM_UpdateArrowPosition($currentPetImage);
	});
    //});
});    
