﻿(function($) {
    $.fn.extend({
        sbreadcrumb: function(options) {
            var settings = $.extend({
                sitemapurl: '',
                separator: '&nbsp;',
                style: 1
            }, options);
            return this.each(function() {
                var wrapper = $(this);
                var currenttab = $('<div></div>');
                currenttab.addClass('current-tab');
                wrapper.append(currenttab);
                var element = $('<div></div>');
                element.addClass('breadcrumb');
                wrapper.append(element);
                if (settings.style == 2)
                    element.addClass('breadcrumb-02');
                else {
                    wrapper.html('');
                    wrapper.append(element);
                    if (settings.style == 1)
                        element.addClass('breadcrumb-01');
                    if (settings.style == 3)
                        element.addClass('breadcrumb-03');
                    if (settings.style == 4)
                        element.addClass('breadcrumb-04');
                    if (settings.style == 5)
                        element.addClass('breadcrumb-05');
                }
                try {
                    $.get(settings.sitemapurl, function(xml) {
                        var url = window.location.protocol + "//" + window.location.host;
                        var resource = window.location.pathname;
                        var resourceParts = window.location.pathname.split('/');
                        var virtualDirectory = '';
                        var currentResource = resource;
                        if (window.location.host == 'localhost') {
                            virtualDirectory = resourceParts[1];
                            currentResource = resource.replace('/' + virtualDirectory + '/', '');
                        }
                        if (currentResource.split('/').length > 0) //its not the root
                        {
                            var nodePath = null;
                            $(xml).find('page').each(function(index, p) {
                                if ($(p).attr('url').toLowerCase() == currentResource.toLowerCase() || ("/" + $(this).attr('url').toLowerCase()) == currentResource.toLowerCase()) {
                                    nodePath = $(this);
                                }
                            });
                            if (nodePath != null) {
                                var strPath = '';
                                var pathsArray = new Array();
                                if (virtualDirectory != '')
                                    url += '/' + virtualDirectory;
                                while (true) {
                                    var node = nodePath.get(0);
                                    if (node != null && node != undefined) {
                                        var icon = '';
                                        if (node.attributes.length > 2)
                                            icon = '<img src="' + url + '/' + node.attributes[2].value + '" alt="' + node.attributes[0].value + '"/>';
                                        if (settings.style != 5)
                                            icon = '';
                                        pathsArray[pathsArray.length] = node.attributes[0].value + '|' + node.attributes[1].value + '|' + icon;
                                        if (node.nodeName == 'root') break;
                                        nodePath = $(node.parentNode);
                                    }
                                    else break;
                                }
                                var html = '';
                                var tabcount = 0;
                                for (var counter = pathsArray.length - 1; counter > 0; counter--) {

                                    var pageUrl = pathsArray[counter].split('|')[1];
                                    var title = pathsArray[counter].split('|')[0];
                                    var icon = pathsArray[counter].split('|')[2];
                                    var url = "#";
                                    if (pageUrl != "#") {
                                        url = window.location.protocol + '//' + window.location.host + '/' + pageUrl;
                                        if (virtualDirectory != '')
                                            url = window.location.protocol + '//' + window.location.host + '/' + virtualDirectory + '/' + pageUrl;
                                    }
                                    var pageNode = $(xml).find('page[url="' + pageUrl + '"]');
                                    var subPages = getSubMenu(pageNode, virtualDirectory);
                                    var cls = 'class="top"';
                                    if (settings.style == 1) {
                                        if (settings.separator == '&nbsp;')
                                            settings.separator = '';
                                    }
                                    if (settings.style == 2) {
                                        if (settings.separator == '&nbsp;')
                                            settings.separator = '';
                                    }
                                    if (settings.style == 3) {
                                        if (settings.separator == '&nbsp;')
                                            settings.separator = '&nbsp;/&nbsp;';
                                    }
                                    if (settings.style == 4) {
                                        if (settings.separator == '&nbsp;')
                                            settings.separator = '<div class="arrow"></div>';
                                    }
                                    if (settings.style == 5) {
                                        if (settings.separator == '&nbsp;')
                                            settings.separator = '';
                                    }
                                    if (settings.style != 4)
                                        cls = '';
                                    var tabcls = '';
                                    if (settings.style == 1)
                                        tabcls = 'class="tab' + (++tabcount) + '"';
                                    var separatorul = '';
                                    if (settings.separator != '&nbsp;' && settings.separator != '')
                                        separatorul = '<ul><li><span>' + settings.separator + '</span></li></ul>';
                                    html += '<ul ' + tabcls + ' ><li><a ' + cls + ' href=' + url + ' title="' + title + '" >' + icon + '' + title + '</a>' + subPages + '</li></ul>' + separatorul + '';
                                }

                                if (pathsArray.length > 0) {
                                    var cls = 'class ="breadcrumb-selected"';
                                    if (settings.style != 3) cls = '';
                                    if (settings.style == 1) tabcls = 'class="tab' + (++tabcount) + '"';
                                    html += '<ul  ' + tabcls + '><li><span ' + cls + '>' + pathsArray[0].split('|')[0] + '</span></li></ul>';
                                    element.html(html);
                                    currenttab.html(pathsArray[0].split('|')[0]);
                                }
                                else element.html('');

                                element.find('ul li').hover(
                                function() {
                                    $(this).find('ul:eq(0)').fadeIn("fast");
                                    return;
                                }, function() {
                                    $(this).find('ul').fadeOut("fast");
                                }
                            );
                            }
                            return wrapper; //for chanining
                        }
                    }, 'xml');
                }
                catch (ex) {
                    throw ex;
                }

                function getSubMenu(node, virtualDirectory, strHtml) {
                    var html = '<ul class="dropdown">';
                    try {
                        node.find('page').each(function() {
                            var url = window.location.protocol + '//' + window.location.host + '/' + $(this).attr('url');
                            if (virtualDirectory != '')
                                url = window.location.protocol + '//' + window.location.host + '/' + virtualDirectory + '/' + $(this).attr('url');
                            html += '<li><a href="' + url + '">' + $(this).attr('title') + '</a></li>';

                        });
                        html += '</ul>';
                        if (html.length < 27) html = '';
                    }
                    catch (ex) { }
                    return html;
                }
            });

        }
    });
})(jQuery);
