﻿function open_dropdown() {
    $(".filter-select-wrap select").selectbox("detach").selectbox("attach");
    var width = $('.dropdown-wrapper').children('div').outerWidth();
    var height = $('.dropdown-wrapper').children('div').outerHeight();
    $('.dropdown-wrapper').stop();
    $('.dropdown-wrapper').animate({
            width: width,
            height: height
        }, 500, function() {
            $('.dropdown-head').addClass('open');
    });
};

function close_dropdown() {
    
    $('.dropdown-wrapper').stop();
    $('.dropdown-wrapper').animate({
            width: 0,
            height: 0
        }, 500, function() {
            $('.dropdown-head').removeClass('open');
        });
};

$(document).ready(function() {
    
    $('#dropdown-mega').append('<ul class="M2"></ul>').append('<ul class="M2"></ul>').append('<ul class="M2"></ul>').append('<ul class="M2"></ul>').append('<ul class="M2 last"></ul>');

    $('#dropdown-mega > ul > li').each(function(i){
        function calc_i(i){
            if (i > 6) {
                i -= 6;
                i = calc_i(i);
            } else {
                i;
            };
            return i;
        };
        
        i++;
        $(this).addClass('item-'+calc_i(i));
    });
    
    $('#dropdown-mega').data('li',$('#dropdown-mega > ul > li').detach());
    
    $('#dropdown-mega > ul').each(function(i){
        var li = $('#dropdown-mega').data('li');
        var height = $('#dropdown-mega').data('height');
        
        $(this).html(li);
        i++;
        li = $(this).children('li').not('.item-'+i).detach();
        $('#dropdown-mega').data('li',li);
        
        var new_height = $(this).height();
        if (height) {
            if (height < new_height) {
                $('#dropdown-mega').data('height',new_height);
            }
        } else {
            $('#dropdown-mega').data('height',new_height);
        };
        
        $('#dropdown-mega .M2').css('min-height',$('#dropdown-mega').data('height'));
        
    });
    
    
    $('.dropdown-small > ul > li:first-child').addClass('hovered');
    
    $('.dropdown-small > ul > li > a').prepend('<span class="jsbullet"></span>');
    $('.dropdown-small > ul > li').hover(function(){
        $('.dropdown-small *').removeClass('hovered');
        $(this).addClass('hovered');
    });
   
    $('.dropdown-wrapper').each(function(){
        var width = $(this).children('div').outerWidth();
        var height = $(this).children('div').outerHeight();
        $(this).data('width',width);
        $(this).data('height',height);
    });

    $('.dropdown-head').live('click',
        function(){
            var width = $('.dropdown-wrapper').data('width');
            var height = $('.dropdown-wrapper').data('height');
            open_dropdown();
            $('select').selectbox("close");
            search_container.close();
        }
    );

    $('.dropdown-head.open').live('click',
        function(e){
            e.preventDefault();
            close_dropdown();
            $('select').selectbox("close");
            search_container.close();
        }
    );

    $('.dropdown-wrapper').mouseleave(function(){
        close_dropdown();
    });
});
