function fancybox_close(){
    jQuery('#fancybox-outer').hide();
    jQuery('#fancybox-overlay').hide();
    jQuery('#fancybox-title').hide();
    jQuery('#fancybox-loading').hide();
    //jQuery('#fancybox-wrap').attr('style','');
    jQuery('#fancybox-content').html('');
}

jQuery(document).ready(function(){
    
//    jQuery('.dropdown a .subcategory-dd').css('width',function(){
//        jQuery(this).parent().parent().removeClass('hidden');
//        var width = jQuery(this).parent().width()+22;
//        jQuery(this).parent().parent().addClass('hidden');
//        return width;
//    });
//    jQuery('.dropdown a .subcategory-dd').css('left','-8px');
    
//    jQuery('#dropdown-services .kiosk').fancybox();
//    jQuery('#dropdown-services .private-label').fancybox();
//    jQuery('#dropdown-services .logistics').fancybox();
//    jQuery('#dropdown-services .rnd').fancybox();
    
//    jQuery('.france').mouseover(function(){
//        jQuery('.france .first').fadeOut(500);
//        jQuery('.france .second').fadeIn(500);
//    });
//    
//    jQuery('.france').mouseleave(function(){
//        jQuery('.france .second').fadeOut(500);
//        jQuery('.france .first').fadeIn(500);
//    });
    
    jQuery('.france').mouseenter(function(){
        jQuery('.france .france-container').stop().animate({left:'-110px'},500);
    });
    
    jQuery('.france').mouseleave(function(){
        jQuery('.france .france-container').stop().animate({left:'0'},500);
    });    
    
    jQuery('.france').click(function(){
        return false;
    });
    
//    Активация дропдауна по клику
//    jQuery('.dropdown .link a').click(function(){
//        var dd = jQuery(this).parent().find('.subcategory-dd');
//        if (dd.length) {            
//            if (dd.find(':visible').length) {
//                jQuery('.subcategory-dd').fadeOut(500);
//            } else {
//                jQuery('.subcategory-dd').fadeOut(500);
//                dd.fadeIn(500);
//            }
//            var width_this, width_parent, pos;
//            width_this = dd.width();
//            width_parent = dd.parent().width();
//            pos = (width_parent-width_this)/2;
//            if (dd.parent().hasClass('last')) {
//                dd.css('right','-15px');
//            } else {
//                dd.css('left',pos);
//            }
//        }
//    });
    
    jQuery('.dropdown .link a').mouseenter(function(){
        var dd = jQuery(this).parent().find('.subcategory-dd');
        if (dd.length) {            
            //jQuery('.subcategory-dd').fadeOut(500);
            jQuery('.subcategory-dd').stop().animate({opacity:0},500,function(){
                jQuery(this).hide();
            });
            //dd.fadeIn(500);
            dd.stop().show().animate({opacity:1},500);
            var width_this, width_parent, pos;
            width_this = dd.width();
            width_parent = dd.parent().width();
            pos = (width_parent-width_this)/2;
            if (dd.parent().hasClass('last')) {
                dd.css('right','-15px');
            } else {
                dd.css('left',pos);
            }
        }
    });    
    
    jQuery('.dropdown .link a').mouseleave(function(){
        //        jQuery('.subcategory-dd').fadeOut(500);
        jQuery(this).parent().find('.subcategory-dd').mouseleave(function(){
            jQuery(this).stop().animate({opacity:0},500,function(){
                jQuery(this).hide();
            });
        });
    });
    
    
//    jQuery('.subcategory-dd').each(function(){
//        var need_width;
//        jQuery(this).css('display','block');
//        jQuery(this).css('position','relative');
//        need_width = jQuery(this).width();
//        console.log(need_width);
//        jQuery(this).css('position','absolute');
//        jQuery(this).css('width',need_width);
//        jQuery(this).css('display','none');
//    });

    
    if (window.location.pathname.indexOf('/services') != -1)
    {
        jQuery('#dropdown-services .kiosk').click(function(){
            serviceMenu("kiosk",1);
            return false;
        })
        jQuery('#dropdown-services .private-label').click(function(){
            serviceMenu("private",2);
            return false;
        })
        jQuery('#dropdown-services .logistics').click(function(){
            serviceMenu("logistics",3);
            return false;
        })
        jQuery('#dropdown-services .rnd').click(function(){
            serviceMenu("rnd",4);
            return false;
        })
        jQuery('#dropdown-services .services-home').click(function(){
            showHome();
            return false;
        })
        dropdownMenu("services");
    }
    
    if (window.location.pathname.indexOf('/library') != -1)
    {
        dropdownMenu("library");
    }
    
    if ((window.location.pathname.indexOf('/about') != -1) ||
            window.location.pathname.indexOf('/history') != -1 ||
            window.location.pathname.indexOf('/frozen') != -1)
    {
        dropdownMenu("about");
    }
    
//    if (!jQuery('#dropdown-store').hasClass('hidden')) {
//        dropdownMenu("store");
//    }
//    dropdownMenu("wholesale");
//    dropdownMenu("about");
//    dropdownMenu("library");
    
/*    jQuery(".header .top-menu .store").click(function(){
        if (jQuery("#dropdown-store").hasClass("hidden")) {
            jQuery(".header .dropdown").addClass("hidden");
            jQuery("#dropdown-store").removeClass("hidden");
        } else {
            jQuery(".header .dropdown").addClass("hidden");
        }      
    });
    
    jQuery(".header .top-menu .wholesale").click(function(){
        if (jQuery("#dropdown-wholesale").hasClass("hidden")) {
            jQuery(".header .dropdown").addClass("hidden");
            jQuery("#dropdown-wholesale").removeClass("hidden");
        } else {
            jQuery(".header .dropdown").addClass("hidden");
        }      
    });
    
    jQuery(".header .top-menu .services").click(function(){
        if (jQuery("#dropdown-services").hasClass("hidden")) {
            jQuery(".header .dropdown").addClass("hidden");
            jQuery("#dropdown-services").removeClass("hidden");
        } else {
            jQuery(".header .dropdown").addClass("hidden");
        }      
    });
    jQuery(".header .top-menu .about").click(function(){
        if (jQuery("#dropdown-about").hasClass("hidden")) {
            jQuery(".header .dropdown").addClass("hidden");
            jQuery("#dropdown-about").removeClass("hidden");
        } else {
            jQuery(".header .dropdown").addClass("hidden");
        }      
    }); */
    
});

function dropdownMenu(link)
{
    jQuery(".header .top-menu ."+link).click(function() {
        jQuery(".header .dropdown").addClass("hidden");
        jQuery("#dropdown-"+link).removeClass("hidden");
        
        var itemName = jQuery(this).attr('class').split(' ');
        itemName = itemName[0];
        highlightCategoryMenu(itemName);
        return false;
    });
}

function highlightCategoryMenu(link)
{
    jQuery(".header .top-menu a").removeClass("active"); 
    jQuery(".header .top-menu ."+link).addClass("active");
}

function hideBanner()
{
    jQuery('#header-discount-banner').hide();
}

function showBanner()
{
       jQuery('#header-discount-banner').show();
       jQuery('#header-discount-banner').stop().css({'display':'block', 'opacity':0, 'height':0}).animate({height: 37, opacity: 1},500);
}

function showBannerAdditionalText()
{
       jQuery('#header-discount-banner-additional-text').show();
       jQuery('#header-discount-banner-additional-text').stop().css({'display':'block', 'opacity':0}).animate({opacity: 1},500);
}

function showSubAbout()
{
    jQuery('.top-menu .about').trigger('click');
}

function clickRecipes()
{
    jQuery('.top-menu .recipes').addClass("active");
}

function clickLibrary()
{
    jQuery('.top-menu .library').addClass("active");
}

//function highlightSubCategory(title, iswholesale) 
//{
//  jQuery(".header .dropdown").addClass('hidden');
//  jQuery(".header #dropdown-"+((iswholesale) ? 'wholesale' : 'store' )+" a:contains('"+title+"')").addClass("active");
//  jQuery('.top-menu .'+((iswholesale) ? 'wholesale' : 'store' )).trigger('click'); 
//}

function highlightSubCategory(title, num)
{
    
    switch(num)
    {
        case 0:
            temp = "store";
            break;
        case 1:
            temp = "wholesale";
            break;
        case 2:
            temp = "services";
            break;
        case 3:
            temp = "about";
            break;
        case 4:
            temp = "library";
            break;
    }
    
    // Вот не дано было по порядку все? Ну ладно, теперь поздно менять. library будет 4.
    
    jQuery(".header .dropdown a").removeClass('active');
    jQuery(".header .dropdown").addClass('hidden');
    jQuery(".header #dropdown-"+temp+" a:contains('"+title+"')").addClass("active");
    jQuery('.top-menu .'+temp).trigger('click');
}

function serviceMenu(name, n)
{
    if (jQuery('.service-detail').is(':visible')) {
        switchingExpanded(name,n);
    } else {
        showExpanded(name,n);
    }
}

function libShowCategory(category) 
{
   
   jQuery(".library-"+category).click(function() {
        jQuery ('.library-main').css({'display':'none'});
        jQuery(".library-main #"+category).css({'display':'block'});
   });
   
}
