$(function(){
    var initHomeMenu = function (){
        function initNewsScroller(){
            if($(".homemenu .news").length > 0){
                $(".news")
                    .scrollable({circular: true, vertical: true, mousewheel: true })
                    .autoscroll({ autoplay: true });
            }
        }
        $('.homemenu .news').stop().delay(1000).animate({height:50}, {complete: initNewsScroller});        
    }();
    
    var initNavigationMenu = function(){
        var mainContent = $('.maincolumn');
        var mainContentOrigMargin = $('.maincolumn').css('margin-left');
        var mainContentTargetMargin = 500;
        
        function hasSubmenu(e){
            var menuItem = $(e.currentTarget);
            if(menuItem.find('ul').length > 0){
                return true;
            }else{
                return false;
            }
        }
        
        function moveContentAndShowSubmenu(e){
            if(hasSubmenu(e)){
                var menuItem = $(e.currentTarget);
                mainContent.stop().animate({opacity: 0, 'margin-left':mainContentTargetMargin});
                menuItem.find('ul').stop().animate({width: 210});
            }
        }
        function hideSubmenuAndRestoreContent(e){
            if(hasSubmenu(e)){
                var menuItem = $(e.currentTarget);
                menuItem.find('ul').stop().animate({width: 0});
                mainContent.stop().animate({opacity: 1, 'margin-left':mainContentOrigMargin});
            }
        }
        $('.leftcolumn > ul > li').hover(moveContentAndShowSubmenu, hideSubmenuAndRestoreContent);
    }();
    
    var initProductGalleryPopup = function(){
        var isShowing = false;
        var slider = $('.struttura .contents_wrapper');
        var gallery = slider.find('.gallery');
        var galleryHeight = gallery.height();
        
        function showGallery(){
            isShowing = true;
            gallery.stop().animate({height: galleryHeight}, {queue: true});
            slider.stop().animate({'margin-left':-567}, {queue: true});
        }
        function hideGallery(){
            isShowing = false;
            slider.stop().animate({'margin-left':0}, {queue: true})
            gallery.stop().animate({height: 0}, {queue: true});
        }
        function toggleGallery(e){
            var a = $(e.currentTarget);
            var label = a.html();
            a.html(a.attr('data-label'));
            a.attr('data-label', label);
            
            if(isShowing){
                hideGallery();
            }else{
                showGallery();
            }
        }
        
        gallery.height(0);
        
        if(gallery.length > 0){
            $('a.gallerylink').click(toggleGallery);
        }else{
            $('a.gallerylink').hide();
        }
    }(); 
});
