/*******************************************************************
  Copyright eSolutions Limited
  Licensed for eSolutions customer usage only.
  For enquiries, please email info@esol-group.com.

  NAVHISTORY OBJECT
  ar1 = ['lev0_title|lev0_name'], ..., ['levN_title|levN_name']
  nav.getParentTitle(0) returns 'lev0_title' (e.g. root)
  nav.getParentTitle('lev{i}_name') returns 'lev{i}_title'
  nav.getParentTitle() returns 'level{i-1}_title' (e.g. parent of last child}
*******************************************************************/
function navHistory(ar,pfx){
  if(typeof pfx=='undefined') pfx = 'default.asp?xid='
  this.ar = ar
  this.pfx = pfx
  this.skipSingleLevel = true
  this.className = 'navHistory'
  this.className_on = 'navHistory_on'
  this.getTitle=function(s){
    if(typeof s!='string')s=s[0] // if array item is passed
    return s.split('|')[0]
  }
  this.getName=function(s){
    if(typeof s!='string')s=s[0] // if array item is passed
    return s.split('|')[1]
  }
  // to display title of parent section
  // => Doesn't work for if s is in sub-level
  this.getParentTitle=function(s){
    if(!ar.length) return ''
    if(typeof s=='number')return this.getTitle(ar[s])
    // if(typeof s=='undefined') s='~LEVEL0~'
    if(typeof s=='undefined' && ar.length>=2)return this.getTitle(ar[ar.length-2])
    return this.getTitle(ar[Math.max(0,this.indexOf(s))])
  }
  this.indexOf=function(s){
    for(var i=ar.length-1; i>=0; i--){
      if(this.getName(ar[i])==s)
        return i
    }
    return ar.length-1
  }
  this.history=function(r, format){ // to display hierarchy in document.body
    if(typeof format == 'undefined') format = 'html'
    if(ar.length <= 1 && this.skipSingleLevel && format == 'html') return '' // print nothing if only one for html format
    if(typeof r == 'undefined') r = ''
    for(var i=0; i<ar.length; i++){
      r+=r?' > ':''
      switch (format){
        case 'html':
          if(i<ar.length-1){
            if(this.getName(ar[i])){
              r+='<A href="'+this.pfx+this.getName(ar[i])+'">'+this.getTitle(ar[i])+'</A>'
            }else{
              r+=this.getTitle(ar[i])
            }
          }else{
            r+='<font class='+this.className_on+'>'+this.getTitle(ar[i])+'</font>'
          }
          break
        case 'text':
          r+=this.getTitle(ar[i]).replace(/&#039;/g,"'")
          break
      }
    }
    if(format == 'html') r = '<font class='+this.className+'>'+ r + '</font>'
    return r
  }
}
