// Constructor function
function BreadCrumbs(_dataSource, _targetDiv) {
    //datasource path
    var obj = this;
    
    this.dataSource = _dataSource;

    //targetDIV id
    this.targetDiv = _targetDiv;

    //root node styleclass
    this.NodeStyleCSS = "jt_BreadCrumbs_Node";

    //current node styleclass
    this.currentNodeStyleCSS = "jt_BreadCrumbs_CurrentNode";

    this.SeparatorTemplate = "->";

    this.outputHtmlStr = "";


    this.getNode = function(currentNode) {
        
        if (this.outputHtmlStr != "") {
            this.outputHtmlStr = "<span class='" + this.NodeStyleCSS + "'><a href='" + currentNode.attr('url') + "'>" + sentenceUpperCase(currentNode.attr('name')) + "</a></span>" + this.SeparatorTemplate + this.outputHtmlStr; //currentNode.attr('name');
        }
        else {
            this.outputHtmlStr = "<span class='" + this.currentNodeStyleCSS + "'>" + sentenceUpperCase(currentNode.attr('name')) + "</span>";
        }
        
        var tempnode = currentNode.parent();
        var name = tempnode.attr('name');
        
        if ( name != null || name != undefined) {
            
            this.getNode( tempnode );
        }
    };
    this.getfilePath = function()
    {
        var wholeurl = window.location.href.toLowerCase();
        var filepath_withoutparm = document.location.pathname.toLowerCase();
        
        var index = wholeurl.indexOf("?");
        var ret_filepath = "";
        //alert();
        if( index == -1 )
        {
            ret_filepath = filepath_withoutparm;
        }
        else
        {
            ret_filepath = filepath_withoutparm + wholeurl.substring(index,wholeurl.length);
        }
        
        return ret_filepath.toLowerCase();
    };
    this.outputHtml = function() {
        
        requestUrl = obj.getfilePath();
        //alert( requestUrl );
        //requestUrl = document.location.pathname.toLowerCase();
        jQuery.ajax({
            type: "GET",
            url: this.dataSource,
            dataType: "xml",
            success: function(xml) {
	            jQuery(xml).find("menuItem[url='"+requestUrl+"'][type!='link']").each(function() {
                        obj.getNode(jQuery(this));
                        jQuery("#"+obj.targetDiv).html(obj.outputHtmlStr);
                    });
            }
        });
    }
}

// 
 function wordUpperCase(word){
  var wLength = word.length;
  if (word=="NJ") return word
  if(wLength > 0){
   var ss = (word.substring(0,1).toUpperCase()) + (word.substring(1,wLength).toLowerCase()+" ");
  }
  return ss;
 }
 
 // 
 function sentenceUpperCase(sentence){
  var upperSentence = '';
  var array = sentence.split(' ');
  for(i = 0 ;i < array.length ; i++){
   upperSentence += wordUpperCase(array[i]);     
  }
  return upperSentence; 
 } 

