﻿jQuery(function() {
    jQuery('[x-modweb-designer-breadcrumbs-container]').each(function() {
        var html = "";
        var seperator = jQuery(this).attr("x-seperator");
        var displayMode = parseInt(jQuery(this).attr("x-displaymode"));
        var hideUrlRewriterHandlers = jQuery.jqModweb.toBool(jQuery(this).attr("x-hideurlrewritehandlers"));
        var hideAnchorIfIsCurrent = false; // darren wants this disabled by default, because he can't style on the <a> anymore if it's not there for the last node!


        var renderSingleNodeLink = function(divNode) {
            var text = divNode.attr('x-text');
            var navigateUrl = divNode.attr('x-navigateurl');
            var isCurrent = jQuery.jqModweb.toBool(divNode.attr('x-iscurrent'));
            var a = "";
            if (hideAnchorIfIsCurrent == true && isCurrent == true) {
                a = text;
            } else {
                a = "<a href=\"" + navigateUrl + "\">" + text + "</a>";
            }
            return a;
        }


        if (displayMode == 0) { // ChatactorSeperated
            //alert("ChatactorSeperated");
            jQuery(this).find('div[x-modweb-designer-breadcrumbs-nodes] div').each(function() {
                var a = renderSingleNodeLink(jQuery(this));
                html += a + " " + seperator;
            });


            // trim the ending ", " off, or whatever the charactor seperator is.
            html += "##FINDME##";
            html = html.replace(new RegExp(" / ##FINDME##", "g"), "");
            html = html.replace(new RegExp("##FINDME##", "g"), ""); // this line is just incase the seperator char is not a '/'
            //alert(html);
            
            
        }
        else if (displayMode == 1) { // BulletedList
            //alert("BulletedList");
            html += "<ul>";
            jQuery(this).find('div[x-modweb-designer-breadcrumbs-nodes] div').each(function() {
                var isCurrent = jQuery.jqModweb.toBool(jQuery(this).attr('x-iscurrent'));
                var a = renderSingleNodeLink(jQuery(this));
                var li = "<li>" + a + "</li>";
                if (isCurrent == true)
                    li = "<li class=\"current\">" + a + "</li>";
                html += li;
            });
            html += "</ul>";
        }
        else {
            alert("ERROR: Breadcrumbs control has invalid DisplayMode.");
            console.log("ERROR: Breadcrumbs control has invalid DisplayMode.");
        }





        // render to page
        jQuery(this).append(html);
    });
});