/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {
    $("#main_menu").fixMenu({'main':$("#main")});
});

jQuery.fn.fixMenu = function(options) {
    var ul = jQuery(this).find("ul:first");
    if (ul.length == 0) return;
    // Calculate Width
    var w = 10;
    ul.children().each(function () {
        w += $(this).outerWidth();
    });
    ul.width(w);
    var main = options.main;
    var left = ul.position().left + ul.offset().left;
    var margin = (main.position().left + main.outerWidth()) - (left+w);
    //var margin = main.outerWidth() - (ul.offset().left + w);
    if (margin < 0) {
        ul.css('margin-left', margin);
    }
}
