$(document).ready(function() { 

    // 
    // Init current cursor position for DROPDOWN menus.
    var _current = 0;

    //
    // Bind toggle events to DROPDOWN selection
    $('.acts_as_dropdown_327x31').toggle(

    //
    // Function for DROPDOWN activation
        function() {
            $(this)
                .children('.dropdown_node_327x31')
                .addClass('dropdown_active_327x31')
                .bind('mouseover', function(i) {
                    _current = $(this).attr('rel');
                });
        },

    //
    // Function for DROPDOWN selection
    // |-> Deactivate DROPDOWN
    // |-> Apply selection class
    // '-> Scroll window to matching item
        function() {

            $(this).children('.dropdown_node_327x31').each(function(index, elem) {
                $(elem).removeClass('dropdown_active_327x31').removeClass('dropdown_selected_327x31');
                if ($(elem).attr('rel') == _current) {
                    $(elem).addClass('dropdown_selected_327x31');
                    if ($('#' + _current).length > 0) {
                        $(window).scrollTop($('#' + _current).offset().top);
                    } else {
                        $(this).children('.dropdown_node_327x31:first').addClass('dropdown_selected_327x31');
                    }
                } else {
                    $(this).children('.dropdown_node_327x31:first').addClass('dropdown_selected_327x31');
                }
            });

        }
    );

    /* ALINA */
    /* this is my dropdown implementation */

    $('.dropdown_trigger_327x31').click(function() {
        var holder = $(this).parent();
        var title = $(this);
        if ($(this).hasClass("dropdown_header_327x31")) {
            $(this).removeClass("dropdown_header_327x31");
            $(this).addClass("dropdown_header_active_327x31");
            holder.children('ul.dropdown_body_327x31').eq(0).slideDown('medium');
            holder.children('ul.dropdown_body_327x31').eq(0).children().bind('mouseover', function(i) {
                _current = $(this).children('a').eq(0).attr('href');
              
            });
            holder.children('ul.dropdown_body_327x31').eq(0).children().children('a').bind('click', function(){
                var _string = $(this).html();
                if ( $('label', holder).length > 0 ) {
                    $('label', holder).html(_string);
                } else {
                    holder.children().eq(0).html(_string);
                }
                $('input[type=hidden]',holder).eq(0).val(_string);
                holder.children('ul.dropdown_body_327x31').eq(0).slideUp('medium', function() {
                    title.removeClass("dropdown_header_active_327x31");
                    title.addClass("dropdown_header_327x31");
                });
                return false;
            });
            holder.children('ul.dropdown_body_327x31').eq(0).children().bind('click', function(i) {
                var _string = $('a', this).length > 0 ? $('a', this).eq(0).html() : $(this).html();
                if ( $('label', holder).length > 0 ) {
                    $('label', holder).html(_string);
                } else {
                    holder.children().eq(0).html(_string);
                }
                $('input[type=hidden]',holder).eq(0).val(_string);
                holder.children('ul.dropdown_body_327x31').eq(0).slideUp('medium', function() {
                    title.removeClass("dropdown_header_active_327x31");
                    title.addClass("dropdown_header_327x31");

                });
            });
             /*
                Bind roll-up on mouse-leave
            */
            holder.bind('mouseleave', function(){
                holder.children('ul').eq(0).slideUp('medium', function() {
                    title.removeClass("dropdown_header_active_327x31");
                    title.addClass("dropdown_header_327x31");
                });
            });
        }
        else { 

            holder.children('ul.dropdown_body_327x31').eq(0).slideUp('medium', function() {
                title.removeClass("dropdown_header_active_327x31");
                title.addClass("dropdown_header_327x31");

            });

        }


    });


    /* end of ALINA */

}); 
