$(document).ready(function() {

    $('#gallery a').lightBox();
    // Generate toc list p#toc, ul#toc, p#toc-h3, ul#toc-h3
    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');
    };
    // Generate toc list
    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();
    }
    //Format the contacts listing in right column
    var contacts = $('div#format-contacts');
    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++) {
            if (ps[j].className != 'text') {
                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>';
            };
        };
    };
    // Expand and collapse function for listings
    //Adds expand and collapse span [+/-] to items with a description and hides description
    var expands = $('p.expand');
    var collapses = $('div.collapse');
    for (var i = 0; i < expands.length; i++) {
        if (collapses[i].innerHTML.length > 7) {
            $(collapses[i]).hide();
            expands[i].innerHTML = '<span title=\"Show and hide description\" class=\"expandable\">[+]</span> ' + expands[i].innerHTML;
            collapses[i].className = 'collapseListing';
        } else {
            $(collapses[i]).hide();
            collapses[i].className = 'collapseListing';
        };
    };
    //expands and collapses description divs
    $('span.expandable').each(function(i) {
        $(this).bind('click', function() {
            var parentTag = $(this).parent().next();
            if ($(this).html() == '[-]') {
                $(this).html('[+]');
                parentTag.hide();
            } else {
                $(this).html('[-]');
                parentTag.show();
            }
        });
    });
    //expands and collapses description divs
    $('span.plus').each(function(i) {
        $(this).bind('click', function() {
            if ($(this).siblings('div').length) {
                var sibTag = $(this).siblings('div');
            } else if ($(this).siblings('p').length) {
                var sibTag = $(this).siblings('p');
            }
            if ($(this).html() == '[-]') {
                $(this).html('[+]');
                sibTag.hide();
            } else {
                $(this).html('[-]');
                sibTag.show();
            }
        });
    });
    //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 = $('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];
    } /* Adds .even to alternate rows for tiger striping. Ignores tables marked .no-tiger or .notiger */
    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');
        }
    }
    $('.aside').each(function() {
        if ($(this).html().length < 108) {
            $(this).hide();
        }
    })
    $('#content h2,#toc a,select option').each(function() {
        if ($(this).html() == 'Code of practices') {
            $(this).html('Codes of practice');
        };
        if ($(this).html() == 'Codes 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() == 'Memorandum of understandings') {
            $(this).html('Memoranda of understanding');
        };
        if ($(this).html() == 'Legislations') {
            $(this).html('Legislation');
        };
    })
    if ($('#staf-form-wrapper').length == 0) {
        $('.share-by-email').hide();
    }else{
        if ($('#name ul').length>0){
            $('#name ul').append('<li class="staf"><a class="share-by-email" href="#" title="Send to a friend"><span>Send to a friend</span></a></li>');
        }else{
            $('#name .max-width').append('<ul><li class="staf"><a class="share-by-email" href="#" title="Send to a friend"><span>Send to a friend</span></a></li></ul>');
        };
    };
/* Display send to a friend */
$('.share-by-email').click(function() {
    $('#staf-form-bg,#staf-form-wrapper').css('display', 'block');
    return false;
});
/*! Hide send to a friend */
$('.staf-close').click(function() {
    $('#staf-form-bg,#staf-form-wrapper').css('display', 'none');
    return false;
});
if ($("#type").length > 0){
  var theGroups = $('#content .group');
   $('#type').append('<option value=\"all\">-- Show all --</option>');
   $('#content .group').each(function(i){
      $('#type').append('<option value=\"'+i+'\">'+$(this).children('h2').html()+'</option>');
   });
   $('#type')[0].onchange = function(){
      filterList(this.selectedIndex-1);
   }
};
function filterList(theGroup){
   if(theGroup < 0){
      $('#content .group').show();
   }else{
      $('#content .group').each(function(i){
         if(i != theGroup){
            $(this).hide();
         }else{
            $(this).show();
         }
      });
   }
}
$('.accordion:even').addClass('even');var locations=new Array();$(".location").each(function(){var tempLocation=this.innerHTML;var duplicate=false;for(var i=0;i<locations.length;i++){if(locations[i]==tempLocation){duplicate=true;}}if(duplicate!=true){locations.push(tempLocation);}});locations.sort();for(var i=0;i<locations.length;i++){$("#filter-box").append("<option value=\""+locations[i]+"\">"+locations[i]+"</option>");}function filterEvents(){var filterLocation=$("#filter-box option:selected").html();$(".location").each(function(){if(filterLocation=="All"){$(this).parent().parent().show();}else if(filterLocation!=this.innerHTML){$(this).parent().parent().hide();}else{$(this).parent().parent().show();}});};if($("#up-view").html()==''){$("#month-view").show();};function checkView(theRadioValue){if(theRadioValue=='all'){$("#upcoming-events").slideUp('normal',function(){$("#calendar-page").slideDown('medium');});}else{$("#calendar-page").slideUp('medium',function(){$("#upcoming-events").slideDown('medium');});};};
});
