var hoveredTopmenuBlocks = {};
var hoveredSubmenuBlocks = {};
var hoveredProductBlocks = {};


$(document).ready(function() {
    
    // Top menu
    $('ul#nav > li').hover(function() {
        if($(this).find('ul').length > 0) {
            var submenuId = $(this).attr('id');
            hoveredTopmenuBlocks[submenuId] = true;
            setTimeout(function() {
                if (hoveredTopmenuBlocks[submenuId]) {
                    $('#' + submenuId).find('a').addClass('expanded');
                    $('#' + submenuId).find('ul').stop().slideDown(300);
                }
            }, 300);
        }
    }, 
    function() {
        if($(this).find('ul').length > 0) {
            var submenuId = $(this).attr('id');
            hoveredTopmenuBlocks[submenuId] = false;
            setTimeout(function() {
                if (!hoveredTopmenuBlocks[submenuId]) {
                    $('#' + submenuId).find('ul').stop().slideUp(300, function(){
                        $('#' + submenuId).find('a').removeClass('expanded');
                    });
                }
            }, 300);
            
            
        }
    });
    
    // Categories menu
    $('.sublink').hover(function() {
        var submenuId = $(this).attr('id');
        hoveredSubmenuBlocks[submenuId] = true;
            
        setTimeout(function() {
            if (hoveredSubmenuBlocks[submenuId]) {
                $('#' + submenuId).find('a').addClass('expanded');
                $('#' + submenuId + ' .sublink_dropdown').stop().slideDown(300);
            }
        }, 300);
    },
    function() {
        var submenuId = $(this).attr('id');
        hoveredSubmenuBlocks[submenuId] = false;
            
        setTimeout(function() {
            if (!hoveredSubmenuBlocks[submenuId]) {
                    
                var link = $('#' + submenuId).find('a');
                if ($('#' + submenuId).find('.sublink_dropdown').length > 0)
                    $('#' + submenuId).find('.sublink_dropdown').stop().slideUp(300, function() {
                        link.removeClass('expanded')
                    });
                else
                    link.removeClass('expanded');
            }
        }, 300);
    });
    
    // Submit search form
    $('.submitButton').click(function() {
        $(this).parent('form').submit();
    });
    
    // Coupon block on main page
    $('#pinned_coupon #pinned_close_btn').click(function() {
        $(this).parent('#pinned_coupon').slideUp();
    });
    
    // Featured categories
    $('.featuredproducts_header .featured_list a').click(function() {
        
        // Set current button
        $('.featuredproducts_header .featured_list a').removeClass('current');
        $(this).addClass('current');
        
        // Show block
        var priority = $(this).attr('rel');
        $('.featured_container ul.grid').addClass('hidden');
        $('.featured_container ul.grid[rev="' + priority + '"]').removeClass('hidden');
    });
    
    // Product block
    $('.block').hover(function() {
        var productId = $(this).attr('product-id');
        hoveredProductBlocks[productId] = true;
        setTimeout(function() {
            if (hoveredProductBlocks[productId])
                $('.userdeal_contents[product-id="' + productId + '"]').stop().animate({
                    'width': '203px'
                }, 500);
        }, 500);
    },
    function() {
        var productId = $(this).attr('product-id');
        hoveredProductBlocks[productId] = false;
        $('.userdeal_contents[product-id="' + productId + '"]').stop().animate({
            'width': '0px'
        }, 500);
    });
    
    // Enlarge image on product page
    $('#coupon_bg .deal_image_container .deal_img_container a.view_image').fancybox();
});
