﻿$(document).ready(function () {

    buildMenu();
    $("#main #header #topmenu ul li:last").attr("style", "border-right: none; padding-right: 0px !important");
    $("#main #header #topmenu ul li:first").attr("style", "padding-left: 0px !important");
});

function buildMenu() {
    var mainMenuId = "#mainmenu";
    $(mainMenuId + " > ul > li").each(function (i) {

        var menuItemTitle = $(this).children(':first-child').html();
        var menuContent = $(this).children(':first-child ul').html();

        var menuItemId = "mainmenu-item-" + i;
        var menuItemIcon = ''; // (menuContent != null) ? "<span class='ui-icon ui-icon-circle-triangle-s'></span> " : "";
        var menuItemCssClass = "fg-button  ui-widget ui-corner-all ui-state-default";


        // clear content before rebuilding the menu:
        if (i == 0) {
            $(mainMenuId).empty();
        }

        menuHref = $(this).find("a").attr("href");
        menuItem = "<li> <a href='" + menuHref + "' id='" + menuItemId + "' class='" + menuItemCssClass + "' >" + menuItemIcon + menuItemTitle + "</a></li>";
        $(mainMenuId).append(menuItem);

        // ...generate the acutal menu:
        if (menuContent != null) {
            // Create link to parent - "Forside"
            //menuFrontpage = "<li> <a href='" + menuHref + "' id='" + menuItemId + "' class='" + menuItemCssClass + "' >" + "Forside (" + menuItemTitle + ")" + "</a></li>";
            //menuContent = menuFrontpage + menuContent;
            $('#' + menuItemId).fgmenu({ content: "<ul>" + menuContent + "</ul>", flyOut: true, showOnHover: true, useRedirect: true });
        }
    })

    $(mainMenuId).wrapInner("<ul id='menu1'></ul>");

    // BUTTON
    $('.fg-button').hover(
    		function () { $(this).removeClass('ui-state-default').addClass('ui-state-hover'); },
    		function () { $(this).removeClass('ui-state-hover').addClass('ui-state-default'); }
    	);

}
