/*
** jQuery document load function.
*/
$(function() {
    
    // Image galleries.
    $("a.lightbox").lightBox();
    if(!$('#sidebarR').length){
        $('#mainContent').css('margin-right','0px');
    }
    
// Hack to clear the search box - 26/11/2010
    if (document.search_f.search_i.value == "Search our site here") {
       document.search_f.search_i.value = "";
    }
    // If the current page is a top level ('topic home') section page
    // then highlite menu and hide main h1 content title.
    if ($('li.i0').length === 0) {
        $('ul.lev1').prepend('<li class="i0 e1 hilite first parent"><a href="#">Topic Home</a></li>');
        $('#main h1.pagetitle').hide();
    }

    if ($('#sidebarR .article h3.maps').length != 0) {
        $('#sidebarR .article h2').first().append('<h3><a href="http://www.ga.gov.au/oracle/agsocat/textonly.jsp">Search All Products</a></h3>');
    }
       
    adjustRightColumn();
    
    adjustGalleryBoxHeights();

    
}); // End jQuery doc load.

/*
** Auto adjust right col top minus margin to compensate for wrapping errors and cross browser issues.
** By default right column will use CSS min height on breadcrumbs and main h1 to calculate minus top margin.
*/
function adjustRightColumn(){
    
    // Do on initial load.

    var totalHeight;
    var $crumbs = $('#crumbs');
    var $pageTitle = $('#main h1.pagetitle');

    function fixMargin() {
        totalHeight = 0;
        if($crumbs.length && $crumbs.css('display') !== 'none'){
            totalHeight += $crumbs.outerHeight(true);
        }
        if($pageTitle.length && $pageTitle.css('display') !== 'none'){
            totalHeight += $pageTitle.outerHeight(true);
        }
        $('#sidebarR').css('margin-top','-'+totalHeight+'px');
    }
        
    // Do after resize.
    $(window).resize(function() {
        fixMargin();
    }); // End resize.

    fixMargin();
    
} // End adjustRightColumn().

/*
** Auto adjust image gallery LI heights to compensate for authors entering too much content.
** By default CSS uses min height and content should be limited to short summaries.
*/
function adjustGalleryBoxHeights(){
    
    var $galleryBoxes = $('#main .img-gallery li');
    var $highest = null;
    var height = 0;
    if($galleryBoxes.length){
        $galleryBoxes.each( function(i){
            var $self = $(this);
            var myHeight = $self.outerHeight(true);
            if(myHeight > height){
                height = myHeight;
            }
        }); // End each.
        $galleryBoxes.height(height);
    }
    
} // End adjustRightColumn().

