﻿/*****************************************************************/
/*********** script for generating product popup *****************/
/*********** By: Steffen Mortensen html 24       *****************/
/*********** 02-12-2011 sm@html24.dk             *****************/
/*****************************************************************/

function hidePopup(e) {

    $('#menu-popup').animate({
        'left': '-631px'
    },500, function() {
        $(e).parent('li').removeClass('popup-active');
    }); 
}

function showPopup() {

    $('#menu-popup').animate({
        'left': '180px'
    },500, function() {
        //animation done
    }); 
    
}

$(document).ready(function(){
    
    var hover = false;
    
    //handle first and last childs
    $('.popup-content > ul > li:last-child').addClass('last-item');
    $('ul.M2 > li:last-child').addClass('last-item');
        
    //add class to li product item and change href attribute
    $('#primarymenu ul li').each(function(){      
        if($(this).children('a').attr('href') == '/Produkter-1.aspx'){            
          
          $(this).addClass('popup-parent');                                         
          
          $(this).children('a').attr('href', 'javascript:void(0)');            
        }         
    }); //end each - primarymenu ul li  
      
       
    //set click functions on products item to show and hide menu 
    //depending on the menu status
    $('.popup-parent > a').click(function() {                    
        
        if($('#menu-popup').css('left') == "-631px"){
          
          if(!$(this).hasClass('inpath')){
            $(this).parent('li').addClass('popup-active');
          }    
          
            showPopup();            
        } else {
            hidePopup($(this));            
        }           
    }); // end click - popup-parent  
    
    //Get meta values from hidden divs and append to item
    $('.popup-content > ul > li').each(function(i){        
        var metaText = $('#meta-values div:nth-child(' + (i + 1) + ')').text();
        //$().appendTo($(this));
        $(this).children('a').after('<span>' + metaText + '</span>');
    });  
    //remove the div when done
    $('#meta-values').remove();
    
    
    //create hover effects for submenu-dropdown
    $('.popup-content ul li > ul.M2 > li').hover(
       function(){
           if(!$(this).hasClass('active-item')){
               $(this).addClass('active-item');
           }
           hover = true;
        },
        function(){
            if($(this).children('ul.M3').css('display') !== 'block'){
                $(this).removeClass('active-item');
            }
            hover = false;
        }
    ); // end hover effects
    
    //handle clickevent when clickin on hide menu
    $('.hide-popup').click(function(){        
        hidePopup($('li.popup-parent'));        
    }); // end hide-popup-click
    
    $('.popup-content ul li > ul.M2 > li > a').each(function(){
        //if the item has no sub-menu post org. link instead of void
        if($(this).siblings().length > 0){
            $(this).attr('href', 'javascript:void(0)');
        }       
    }); // end a each
    
    $('.popup-content ul li > ul.M2 > li > a').click(function(){       
        if($(this).siblings('ul.M3').css('display') == 'none'){
          $('ul.M3').slideUp(function(){
              $(this).parent('li').removeClass('active-item');
          });
            $(this).parent('li').addClass('active-item');
          
            $(this).siblings('ul.M3').slideDown();
        } else {
            $(this).siblings('ul.M3').slideUp(function(){
                if(!hover){
                    $(this).parents('li').removeClass('active-item');
                }                                
            });
            
            if(!hover){
                $(this).parents('li').removeClass('active-item');
            }             
        }        
    }); // end .popup-content ul li > ul.M2 > li > a click
        
}); //end ready

