// Constructor function
function BreadCrumbs(_dataSource, _targetDiv) {
    //datasource path
    var obj = this;
    this.dataSource = _dataSource;
    //targetDIV id
    this.targetDiv = _targetDiv;
    //root node styleclass
    this.NodeStyleCSS = "BreadCrumbs_Node";
    //current node styleclass
    this.currentNodeStyleCSS = "BreadCrumbs_CurrentNode";
    this.SeparatorTemplate =  "<SPAN>&nbsp; <IMG alt='' src='/SiteCollectionImages/sitemap_arrow.gif'>&nbsp;</SPAN>";
    this.outputHtmlStr = "";
    
    this.getNode = function(currentNode) {

        if (this.outputHtmlStr != "") {
            this.outputHtmlStr = "<span class='" + this.NodeStyleCSS + "'><a href='" + currentNode.attr('url') + "'>" + currentNode.attr('name') + "</a></span>" + this.SeparatorTemplate + this.outputHtmlStr; //currentNode.attr('name');
        }
        else {
            this.outputHtmlStr = "<span class='" + this.currentNodeStyleCSS + "'>" + currentNode.attr('name') + "</span>";
        }

        var tempnode = currentNode.parent();
        var name = tempnode.attr('name');

        if (name != null || name != undefined) {
            this.getNode(tempnode);
        }
    };

    this.getPagePath = function() {
        var url = window.location.href.toLowerCase();
        var index = url.indexOf("?");
        if( index != -1 )
        {
        	url = url.substring(0, index);
        }
        var host = "";
        if (url.indexOf("http") != -1) {
            host = "http://" + window.location.host;
        }
        if (url.indexOf("https") != -1) {
            host = "https://" + window.location.host;
        }
        return url.replace(host, "");

    };
	
    this.render = function() {
        requestUrl = obj.getPagePath();
        jQuery.ajax({
            type: "GET",
            url: this.dataSource,
            async:false,
            dataType: "xml",
            success: function(xml) {
                jQuery(xml).find("menuItem[Key='" + requestUrl + "']").each(function() {
                    obj.getNode(jQuery(this));
                    jQuery("#" + obj.targetDiv).html(obj.outputHtmlStr);
                });
            }
        });
    };
}

