﻿/// <reference path="../../Common/Js/jquery-1.3.2-vsdoc.js" />

var lis = "";
var children = 0;
var capo = "\n";
var Left = "<img src='/99/images/mL.png' border='0' alt='' />";
var Right = "<img src='/99/images/mR.png' border='0' alt='' />";
function itemPropertiesArray(item, itemNext) {
    if (itemNext != undefined) {
        if (itemNext.get_items().get_length() > 0) {
            lis += "</div></li>" + capo;
        }
    }
    var subItems = item.get_items();
    //alert(item.getProperty("Text"));

    if (item.get_navigateUrl() != undefined) {
        lis += "<li id='" + item.getProperty("Text").replace(/ /g, "") + "'><div>" + Left + "</div><div class='itemActive' onmouseover=this.className='item' onmouseout=this.className='itemActive' onclick=document.location='" + (item.get_navigateUrl()).replace(/ /g, "-") + "'><a href='" + (item.get_navigateUrl()).replace(/ /g, "-") + "'>" + item.getProperty("Text") + "</a></div><div>" + Right + "</div>";
    } else {
        lis += "<li id='" + item.getProperty("Text").replace(/ /g, "") + "'><div>" + Left + "</div><div class='item'>" + item.getProperty("Text") + "</div><div>" + Right + "</div>";
    }
    if (subItems.get_length() > 0) {
        // apro DIV senza chiusura /li

    } else {
        lis += "</li>" + capo;
    }

    if (subItems.get_length() > 0) {

        for (var e = 0; e < subItems.get_length(); e++) {

            var subsub = subItems.getItem(e).get_items();
            //itemPropertiesArray(subItems.getItem(e));

            //alert(subsub.get_length());
            if (subsub.get_length() > 0) {

                if (e == 0) {
                    lis += "<div class='sub' style='width: 600px; display: none;'>" + capo
                };
                lis += "<ul>" + capo;

                //alert(subItems.getItem(e).getProperty("Text") + " ha " + subsub.get_length() + "figli");
                itemPropertiesArray(subItems.getItem(e));



                for (var x = 0; x < subsub.get_length(); x++) {

                    if (x == 0) {
                        // alert(e + " " + subItems.getItem(e).getProperty("Text"));
                        lis += "</li>"
                    }
                    //alert(subsub.getItem(x).getProperty("Text"));
                    itemPropertiesArray(subsub.getItem(x));
                }
                lis += "</ul>" + capo;
                //alert("x: " + x + "e: " + e);



            }

        }
    }

}

function buildMenu() {
    lis += "<ul id='topnav'>";
    var topItems = Menu1.get_items();

    for (var i = 0; i < topItems.get_length(); i++) {
        itemPropertiesArray(topItems.getItem(i), topItems.getItem(i - 1));
    }
    lis += "</ul>";

    //alert(lis);
    $("#menuContainer").html("");
    $("#menuContainer").append(lis);

    $(document).ready(function() {

        function megaHoverOver() {
            var subElement = 0;
            $(this).find(".sub").stop().fadeTo('fast', 1).show();

            (function($) {
                jQuery.fn.calcSubWidth = function(mySub) {
                    var HrowW = 100;
                    mySub.find("li").each(function(index) {
                        var thisWidth = $(this).outerWidth();
                        if (thisWidth > HrowW) {
                            HrowW = thisWidth;
                        }
                    });
                    mySub.css("width", HrowW);

                };
            })(jQuery);
            $(this).find(".sub ul").each(function(index) {
                $(this).calcSubWidth($(this));

            });
            $(this).find(".sub ul").each(function(index) {
                subElement += $(this).outerWidth();

            });

            $(this).find(".sub").css("width", subElement);

        }

        function megaHoverOut() {

            $(this).find(".sub").stop().fadeTo('fast', 0, function() {
                $(this).hide();
            });

        }


        var config = {
            sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
            interval: 100, // number = milliseconds for onMouseOver polling interval    
            over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
            timeout: 200, // number = milliseconds delay before onMouseOut    
            out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
        };

        $("ul#topnav li .sub").css({ 'opacity': '0' });
        $("ul#topnav li").hoverIntent(config);
        // add h2 class
        $(this + ".sub ul").each(function() {
            var content = "<h2>" + $(this).find("li:first").text() + "</h2>";
            $(this).find("li:first").html(content);

        });


    });

    return;




}    
