var inPinkMenuTimerOpen = 0;
var inPinkMenuTimerClose = 0;
jQuery(document).ready(function(){
     
    eraseCookie('openMenu');
    
    if(readCookie('openMenu')){
        jQuery('#'+readCookie('openMenu')).next().fadeIn();
    }
});
(function( $ ){

  $.fn.pinkmenu = function() { 
	var menuId = $(this).attr('id');
	
	//click

	$(this).find('li').each(function(i, item){
	    $(item).click(function(event){
	      
	       if(readCookie('openMenu') == null || readCookie('openMenu') != $(this).find('a').attr('id')){
                if(readCookie('openMenu')){
    	            $('#'+readCookie('openMenu')).next().fadeOut();
    	        }
                createCookie('openMenu', $(this).find('a').attr('id'), 3);
           }
           
           //reseter
            $(this).find('ul').hide()
            $("#" + menuId + " .lvl0Act").removeClass('lvl0Act')
            $("#" + menuId + " .lvl1Act").removeClass('lvl1Act')
            $("#"+menuId+" #collectionContainer").hide().removeClass('expanded')
            $("#" + menuId + " .lvl0Act").removeClass('lvl0Act')
            //reset timer
            clearPreviewTimers()
            
            $(this).find('ul').first().fadeIn();
            $(this).addClass('lvl0Act')
            showLevel2($(this).find('ul').first());

           
    	    if($(this).parent().attr('id') == 'mega-menu'){
    	        return false;
    	    }
    	        
	        if($(this).parent().hasClass('menuLevel1')){
	            createCookie('openMenu', $(this).parent().prev().attr('id'), 3);
	            event.stopPropagation();

	            return true;
	        }
	        
    	    
	    });
	});
	
	//level 0
	$(this).children().each(function(){
		$(this).hover(
			function(event){

			    return;
				//reseter
				$(this).find('ul').hide()
				$("#" + menuId + " .lvl0Act").removeClass('lvl0Act')
				$("#" + menuId + " .lvl1Act").removeClass('lvl1Act')
				$("#"+menuId+" #collectionContainer").hide().removeClass('expanded')
				$("#" + menuId + " .lvl0Act").removeClass('lvl0Act')
				//reset timer
				clearPreviewTimers()
				
				$(this).find('ul').first().fadeIn();
				$(this).addClass('lvl0Act')
				showLevel2($(this).find('ul').first());
			},
			function(){
			    return;
				$(this).find('ul').first().fadeOut(150)
				$(this).removeClass('lvl0Act')
			}
		)
	})
    
	var showLevel2 = function(level1) {
		$(level1).children().each(function(){
			$(this).hover(
				function(){
					$("#"+menuId+" .menuLevel2 ul").hide()
					$("#"+menuId+" #collectionContainer").hide()
						
					$(this).addClass('lvl1Act')
					$(this).find('ul').first().fadeIn(100)
					$(this).find('#collectionContainer').first().fadeIn(100)
					
				},
				function(){
					$("#" + menuId + " .lvl1Act").removeClass('lvl1Act')
					$(this).find('ul').first().fadeOut(100)
					$(this).find('#collectionContainer').hide().removeClass('expanded')
                    $("#"+menuId+" #promoText").hide();
				}
			)
		})
	}
	
	$("#collectionContainer a").hover(function(){
            $("#collectionContainer .picture img").hide();
            $("#collectionContainer").addClass('expanded');
            $("#pic_" + $(this).attr('id')).show();
        
            $("#"+menuId+" #promoText").slideDown()
	},function(){
            $("#collectionContainer .picture img").hide();
        })
	
	
	var clearPreviewTimers = function(){
		clearTimeout(inPinkMenuTimerOpen)
		clearTimeout(inPinkMenuTimerClose)
	}
    //remove last / from category list
    var c = $("#shopSubmenu > li").length - 1
    $("#shopSubmenu > li:nth-child("+c+")").remove();
    
  };
})( jQuery );

function closePreviewSubmenu() {
	jQuery("#shopSubmenu").fadeOut();
}

function previewSubmenu(){
	jQuery("#shopSubmenu").fadeIn();
	inPinkMenuTimerClose = setTimeout("closePreviewSubmenu()",3000)
}

jQuery(document).ready(function(){
	jQuery("#mega-menu").pinkmenu();
	
        if(jQuery("#homepageContainer").length) {
            //inPinkMenuTimerOpen = setTimeout("previewSubmenu()",5000)
        }
	
})

