// Wait till document is ready
$(document).ready(function(){

    //$.preloadCssImages();
    
    // Initialize navigtion menus
    initMenu();
});

function initMenu(){
    $("div.headlink").each(function(){
        var menu = $('div.grayShadowBox2', this);
        $(this).hover(function(){ // onhover
            showMenu(menu);
        }, function(){ // onmouseout
            hideMenu(menu);
        });
    });
}

function showMenu(menu){
    var link = $('a.navMenu', menu.parent());
    var tab = $('div.grayBoxTab', menu.parent());
    var coords = getXY(menu.parent().get(0));
    menu.css("left", coords.x + "px");
    tab.css("left", coords.x + 20 + "px");
    tab.css("width", link.get(0).offsetWidth + "px");
    tab.css("top", coords.y - 22 + "px");
    
    tab.show();
    menu.show();
}

function hideMenu(menu){
    var tab = $('div.grayBoxTab', menu.parent());
    var link = $('a.navMenu', menu.parent());
    
    tab.hide();
    menu.hide();
}

function getXY(obj){
    var curleft = 0;
    var curtop = obj.offsetHeight;
    var border;
    if (obj.offsetParent) {
        do {
            // XXX: If the element is position: relative we have to add borderWidth
            if (getStyle(obj, 'position') == 'relative') {
                if (border = _pub.getStyle(obj, 'border-top-width')) 
                    curtop += parseInt(border);
                if (border = _pub.getStyle(obj, 'border-left-width')) 
                    curleft += parseInt(border);
            }
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while (obj = obj.offsetParent)
    }
    else 
        if (obj.x) {
            curleft += obj.x;
            curtop += obj.y;
        }
    return {
        'x': curleft,
        'y': curtop
    };
}

function getStyle(obj, styleProp){
    if (obj.currentStyle) 
        return obj.currentStyle[styleProp];
    else 
        if (window.getComputedStyle) 
            return document.defaultView.getComputedStyle(obj, null).getPropertyValue(styleProp);
}

