$(document).ready(function() {
// Remove last pipe on second level menu
if(document.getElementById('menu-2') != null){
   var menuPipes = document.getElementById('menu-2').getElementsByTagName('span');
   menuPipes[menuPipes.length-1].style.display='none';
}

// Generate toc list
if($('#toc').is('p')){
  toc('p','h2','p#toc');
}else if($('#toc').is('ul')){
  toc('ul','h2','ul#toc');
};
if($('#toc-h3').is('p')){
  toc('p','h3','p#toc-h3');
}else if($('#toc-h3').is('ul')){
  toc('ul','h3','ul#toc-h3');
};

function toc(theEle,theHeading,theId){
var element = '#content '+theHeading;
var lastSpan = theId + ' ' + 'span:last';
  $(element).each(function(){
    if($(this).children('a').length > 0){
      var tempHTML = $(this).children('a:eq(0)').html();
    }else{
      var tempHTML = $(this).html();
    }
    if(theEle == 'p'){
      $(theId).append('<a href=\"#' + tempHTML + '\">' + tempHTML + '</a> <span>|</span> ');  
    }else if(theEle == 'ul'){
      $(theId).append('<li><a href=\"#' + tempHTML + '\">' + tempHTML + '</a></li>');
    }
    $(this).attr('id',tempHTML);
  });
  $(lastSpan).hide(); 
}

// Display font swap tool
document.getElementById('toggle-font').className = '';
document.getElementById('toggle-font').innerHTML = '<a title=\"Toggle font size\" href=\"javascript:fontBigger()\"><span>Toggle font size</span></a>';
// Switch Font Size
var fontSizes = new Array();
fontSizes[0] = '69%';
fontSizes[1] = '74%';
fontSizes[2] = '79%';
var currentSize = 0;
function fontBigger() {
   if(currentSize < 2){
      currentSize++;
      document.getElementById('body').style.fontSize = fontSizes[currentSize];
   }else if(currentSize==2){
      document.getElementById('body').style.fontSize = fontSizes[0];
      currentSize=0;
   }
}
function fontSmaller() {
   if(currentSize > 0){
      currentSize--;
      document.getElementById('body').style.fontSize = fontSizes[currentSize];
   }
}

//Initiates the service links in the left col if they are present
if(document.getElementById('service-links')!=null){
      var uls = document.getElementById('service-links').getElementsByTagName('ul');
      var lis = document.getElementById('service-links').getElementsByTagName('li');
      var ulCount = 0;
      for(var i=0;i<lis.length;i++){
         if(lis[i].innerHTML.indexOf('<ul>')>0||lis[i].innerHTML.indexOf('<UL>')>0){
            ulCount++;
            var currentLi = lis[i].innerHTML;
            lis[i].innerHTML = '<span id=\"span-'+i+'\" onclick=\"expand('+ulCount+','+i+')\">[+]</span> ' + currentLi;
            lis[i].className = 'expandable';
         }
      }
}
function expand(theUl,theLi){
   var spanID = 'span-'+theLi;
   if(uls[theUl].style.display == 'block'){
      uls[theUl].style.display = 'none';
      lis[theLi].className='expandable';
      document.getElementById(spanID).innerHTML='[+]';
   }else{
      uls[theUl].style.display = 'block';
      lis[theLi].className='expandable open';
      document.getElementById(spanID).innerHTML='[-]';
   }
}

//Check if calendar picker is available, if it is, print heading
if(document.getElementById('cal-wrapper') != null){
if(document.getElementById('cal-wrapper').innerHTML.indexOf('table')>1||document.getElementById('cal-wrapper').innerHTML.indexOf('TABLE')>1){
   if(document.getElementById('page').className == 'purple'){
      document.getElementById('cal-heading').innerHTML = '<h2>Justice services calendar</h2>';
   }else if(document.getElementById('page').className == 'blue'){
      document.getElementById('cal-heading').innerHTML = '<h2>Events calendar</h2>';
   }else if(document.getElementById('page').className == 'green'){
      document.getElementById('cal-heading').innerHTML = '<h2>Fair and safe work calendar</h2>';
   }else{
      document.getElementById('cal-heading').innerHTML = '<h2>Calendar</h2>';
   }
}
}

//Expand collapse function for news listings with teaser texts
function expandCollapse(theSpan, theIntro){
   var theIntroObject = document.getElementById(theIntro);
   if(theIntroObject.style.display == 'none'){
      theIntroObject.style.display = 'block';
      theSpan.innerHTML = '[-]';
   }else if(theIntroObject.style.display == 'block'){
      theIntroObject.style.display = 'none';
      theSpan.innerHTML = '[+]';
   }else{
      theIntroObject.style.display = 'block';
      theSpan.innerHTML = '[-]';
   }
}

//Replaces any span tags with classname "convert-date" to correct date format
var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var dates = getElementsByClassName(document.getElementById('container'), 'span', 'convert-date');
var currentDate = new Array();
for(var k=0;k<dates.length;k++){
   currentDate = dates[k].innerHTML.split('-');
   var theDay = parseInt(currentDate[2],10);
   var theMonth = months[currentDate[1]-1];
   if(theDay > 0){theDay=theDay+' '}else{theDay=''};
   if(theMonth==undefined){theMonth=''}else{theMonth=theMonth+' '};
   dates[k].innerHTML = theDay+''+theMonth+''+currentDate[0];
}

// Hide or show acrobat link
if(document.getElementById('container').innerHTML.indexOf('.pdf')>0 || document.getElementById('toggle-font').innerHTML.indexOf('.PDF')>0){
   document.getElementById('acrobat-script').style.display='block';
}

// Make a href on text only links hashed #
if(document.getElementById('text-only-on')!=null){
   document.getElementById('text-only-on').href='#';
   document.getElementById('text-only-off').href='#';
}

// Add classes to even rows in all tables within the content
var tables = $('table');
for (var i=0;i<tables.length;i++){
   if($(tables[i]).attr('class')=='no-tiger' || $(tables[i]).attr('class')=='notiger'){
      /*Do not stripe*/
   }else{
      $(tables[i]).find('tr:visible:even').addClass('even');
   }
}


//Format the contacts listing in right column
var contacts = $('div.contacts-content');
  if(navigator.appName == 'Microsoft Internet Explorer'){
     var breakTag = '<BR>';
  }else{
     var breakTag = '<br>';
  }
for (var i=0;i<contacts.length;i++){
   var ps = contacts[i].getElementsByTagName('p');
   for (var j=0;j<ps.length;j++){
      var pContents = ps[j].innerHTML.split(breakTag);
      ps[j].innerHTML = '<strong class=\"contact-heading\">'+pContents[0]+'</strong>';
      var k = 1;
      var spanContent = '';
      while(pContents[k]!=null){
         spanContent = spanContent + pContents[k]+'<br/>';
         k++;
      }
      ps[j].innerHTML = ps[j].innerHTML + '<span class=\"contact-span\">' + spanContent + '</span>';
   }
}

$('div.collapse').each(function(i){
   var thisHTML = $(this).html();
   var headingTag = 'h2';
   if(this.className.indexOf('h3')>0){
      var headingTag = 'h3';
   }
   if(this.className.indexOf('h4')>0){
      var headingTag = 'h4';
   }
   if(this.className.indexOf('h5')>0){
      var headingTag = 'h5';
   }
   this.className = 'expand';
   $(this).html('<'+headingTag+'><span class=\"expand-collapse\">[+]</span> ' + $(this).attr('desc') + '</'+headingTag+'><div class=\"collapse\" >'+thisHTML+'</div>');
});
$('div.collapse').hide();
$('span.expand-collapse').each(function(i){
   $(this).bind('click', function(){
      if($(this).html() == '[-]'){
         $(this).html('[+]');
         $(this).parent().nextAll('.collapse:eq(0)').hide();
      }else{
         $(this).html('[-]');
         $(this).parent().nextAll('.collapse:eq(0)').show();
      }
   });
});
$('#content h2,#toc a').each(function(){
   if($(this).html() == 'Code of practices'){
      $(this).html('Codes of practice');
   };
   if($(this).html() == 'Terms of references'){
      $(this).html('Terms of reference');
   };
if($(this).html() == 'Memorandum of agreements'){
      $(this).html('Memoranda of agreement');
   };
if($(this).html() == 'Legislations'){
      $(this).html('Legislation');
   };
});
});
