﻿$(document).ready(function() {

    $("#imgVideoAudio").click(function(event) {
        $("#divVideoAudioOptions").toggle();
    });

    var $w = $(window);

    jQuery('ul.sf-menu').superfish({ onBeforeShow: function() {
        this.each(function() {
            var $u = $(this);
            $u.css('display', 'block');
            var menuWidth = $u.width(),
						    parentWidth = $u.parents('ul').width(),
						    totalRight = $w.width() + offsetFix('x'),
						    menuRight = $u.offset().left + menuWidth;
            if (menuRight > totalRight) {
                $u.css('margin-left', ($u.parents('ul').length == 1 ? totalRight - menuRight : -(menuWidth + parentWidth)) + 'px');
            }

            var windowHeight = $w.height(),
						    offsetTop = $u.offset().top,
						    menuHeight = $u.height(),
						    baseline = windowHeight + offsetFix('y');
            var expandUp = (offsetTop + menuHeight > baseline);
            if (expandUp) {
                $u.css('margin-top', baseline - (menuHeight + offsetTop));
            }
            $u.css('display', 'none');
        });
    }
    });

    $("ul.sf-menu > li:has(ul) > a").click(function() {
        return false;
    });

});

function offsetFix(dir) {
	return window[dir == 'y' ? 'pageYOffset' : 'pageXOffset']
	|| document.documentElement && document.documentElement[dir=='y' ? 'scrollTop' : 'scrollLeft']
    || document.body[dir=='y' ? 'scrollTop' : 'scrollLeft'];
}


