$(document).ready(function(){
    $('li.dropdown').hover( 
        function(event) {
            var $dd_menu = $(this).children('ul:first');
            $dd_menu.toggleClass("open");
            if ( ! $dd_menu.data("initial_height") ) {
                $dd_menu.data("initial_height", $dd_menu.height());
                $dd_menu.css( { "height": "0px", "display": "block" } );
            }
            $dd_menu.stop();
            var new_opacity, new_height;
            if ( event.type == 'mouseenter' ) {
                new_opacity = 1;
                new_height = $dd_menu.data("initial_height");
            } else {
                new_opacity = 0;
                new_height = 0;            
            }
            $dd_menu.animate({
                "height": new_height, "opacity": new_opacity
                }, 400
            );
        }
    );
    
    /* bind search functionality */
    $('#site_search').keyup( function(e) {
        if ((e.keyCode | e.which) == 13) 
            window.location = '/search.php?q=' + $(this).val();

    });
    $('#mag_glass').click( function() {
        window.location = '/search.php?q=' + $('#site_search').val();
    });
    
    
    /* bind expand/collapse functionality */
    $('.expand').click( function() {
        $(this).css('display','none');
        $(this).next('.expandable').slideDown('slow', function() {
            $(this).next('.collapse').fadeIn('slow');
        });
    });
    $('.collapse').click( function() {
        $(this).css('display','none');
        $(this).prev('.expandable').slideUp('slow', function() {
            $(this).prev('.expand').fadeIn('slow');
        });
    });
});

/**
* this function displays the amazing darkview box with the given content.
* if no content is given, the box is hidden.
*
* @dependency - jQuery UI ( for 'clip' effect )
*/
function darkview(title,content, width, height, callback) {

    if (typeof(content) != "undefined")
    {
        var $dv = '<div id="overlay"></div><div id="dv_outer"><div id="dv_middle"><div id="darkview" style="width:' + width + 'px;"><div id="dv_inner"><div id="dv_header"><span id="dv_title">' + title + '</span><div onclick="darkview();" id="dvExit"></div></div><div id="dv_content_wrapper" style="width:100%;height:' + height + (isNaN(height)?'':'px') + ';"><div id="dv_content">' + content + '</div></div></div></div></div></div>';
        $('body').append($dv);

        var leftMargin, topMargin, dvHeight, dvWidth, contentHeight;
        dvHeight = $('#dv_outer').height();
        dvWidth = $('#darkview').outerWidth();
        topMargin = -(dvHeight / 2);
       
        // hack for auto height
        contentHeight = $('#dv_content_wrapper').height();
        $('#dv_content_wrapper').css('height', contentHeight);
        
        $('#darkview').css('display', 'none');
        $('#dv_outer').css('visibility', 'visible');
        $('#dv_outer').css('height', dvHeight);
        $('#dv_outer').css('top', '50%');
        $('#dv_outer').css('margin-top', topMargin);
        
        $('#dv_middle').css('width', dvWidth);
        $('#dv_content').css("display", "none");
        
        var height = $('body').height();
        $('#overlay').css('height', height);
        $('#overlay').fadeTo(200, .85);
        $('#darkview').show("clip", { direction: "vertical" }, 600, function() {
            $('#dv_content').show("slide", { direction: "left" }, 300);
        });
    }
    else
    {
        $('#darkview').hide('clip', { direction: "vertical" }, 300);
        $('#overlay').fadeTo(400, 0, function() {
            // remove darkview and overlay!
            $('#dv_outer').remove();
            $('#overlay').remove();
        });
    }
    if (typeof(callback) != 'undefined')
        setTimeout(callback, 400);
}
