// cb eval_heights.js version 1.4

// Only do anything if jQuery isn't defined
var thisPageUsingOtherJSLibrary;
if (typeof jQuery == 'undefined') {

        if (typeof $ == 'function') {
                // warning, global var
                thisPageUsingOtherJSLibrary = true;
        }

        function getScript(url, success) {

                var script     = document.createElement('script');
                     script.src = url;

                var head = document.getElementsByTagName('head')[0],
                done = false;

                // Attach handlers for all browsers
                script.onload = script.onreadystatechange = function() {

                        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {

                                done = true;

                                // callback function provided as param
                                success();

                                script.onload = script.onreadystatechange = null;
                                head.removeChild(script);

                        };

                };

                head.appendChild(script);

        };

        getScript('https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', function() {

                if (typeof jQuery=='undefined') {
                        // Super failsafe - still somehow failed...
                } else {
                        // jQuery loaded! Make sure to use .noConflict just in case

                        if (thisPageUsingOtherJSLibrary) {
                                column_fix(jQuery);
                        } else {
                                // Use .noConflict(), then run your jQuery Code
                                column_fix(jQuery);

                        }

                }

        });

} else { 
        column_fix(jQuery);
};

function column_fix($)
{
    // If jQuery is not yet loaded do nothing and try again later
    if (typeof window['jQuery'] == 'undefined') {
        setTimeout('column_fix(jQuery)', 1000);
        return;
    }

    branchesPage = window.location.href.search('branch');
 
    // We need to wait for all of the images to load before we can calculate the heights
    install_batch_img_plugin();
    
    //taking this out to test branch.php - client complaining that it 

    $(document).ready(function() {
    
        // For the new equipment pages
        $('.zd').hide();
        $('.zd:first').show();
        
        $('.zd_nav_overview').click(function() {
            $('#left_spacer').height(1);
            $('#right_spacer').height(160);
        
            $('.zd').hide();
            $('#zd_overview').show();
            resizer($);
            return false;
        });

        $('.zd_nav_specs').click(function() {
            $('#left_spacer').height(1);
            $('#right_spacer').height(160);
        
            $('.zd').hide();
            $('#zd_specs').show();
            resizer($);
            return false;
        });

        $('.zd_nav_features').click(function() {
            $('#left_spacer').height(1);
            $('#right_spacer').height(160);
        
            $('.zd').hide();
            $('#zd_features').show();
            resizer($);
            return false;
        });       
        
        $('.zd_nav_standard').click(function() {
            $('#left_spacer').height(1);
            $('#right_spacer').height(160);
        
            $('.zd').hide();
            $('#zd_standard').show();
            resizer($);
            return false;
        });  
        // -- end for new equipment pages --      
    
        // frontpage slider.. doing it like this to prevent
        if (branchesPage == -1){
          
          //don't run this on the branches page.. since it takes forever to preload all these images
          // which then delays the flash
          $('img').batchImageLoad({
              loadingCompleteCallback: function() {
                // Fix that silly gap at the end of the middle column that occurs on many pages
                  $('div#company_history_bg div.white12_arial:last > p:last')
                    .parent().after('<div style="clear: both;">&nbsp;</div>');   
                //console.log('completed');
                  resizer($); 
              }
          });
          if (jQuery.browser.msie){ //some reason the batchImageLoad isn't working so callback never called
            resizer($); 
            
          }
        }else{  
          // alert('else'); 
          jQuery('#slider1').css('display','block');
          resizer($);
        }
        
    });
}

function resizer($)
{
    // Identify middle column container
    var mcc = $('#middle_column');
    if (!$('#middle_column').length)
    {              
        // The middle column is different for OCR
        mcc = $('#middle-column-bg');
    }

    // Get column heights
    var middle_height = mcc.height();
    var left_height   = $('#left_container').height();
    var right_height  = $('#right_container').height();

    // Find largest column height
    var largest = Math.max(middle_height, left_height, right_height);

    // Adjust left column height
    var l_makeup = largest - left_height;
    if (l_makeup > 0) {
        var sd = $('div#left_container div:last').prev();
        var f_offset = $('.forestry').length ? 16 : 0;
        sd.height(sd.height() + l_makeup - f_offset).addClass('colfixed');
    }

    // Adjust middle column height
    var m_makeup = largest - middle_height;
    if (m_makeup > 0) {
        var sd = $('div#company_history_bg');
        sd.height(sd.height() + m_makeup).addClass('colfixed');
    }

    // Adjust right column height
    var r_makeup = largest - right_height;
    if (r_makeup > 0) {
        var sd = $('div#right_container div.new_links_bg:last > div:last');
        sd.height(sd.height() + r_makeup - 2)
            .addClass('colfixed')
            .parent().css('height', 'auto');
    }

}

function install_batch_img_plugin()
{
    (function($) {
    $.fn.batchImageLoad = function(options) {
        var images = $(this);
        var originalTotalImagesCount = images.size();
        var totalImagesCount = originalTotalImagesCount;
        var elementsLoaded = 0;

        // Init
        $.fn.batchImageLoad.defaults = {
                loadingCompleteCallback: null,
                imageLoadedCallback: null
        }
        var opts = $.extend({}, $.fn.batchImageLoad.defaults, options);

        // Start
        images.each(function() {
                // The image has already been loaded (cached)
                if ($(this)[0].complete) {
                        totalImagesCount--;
                        if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);
                // The image is loading, so attach the listener
                } else {
                        $(this).load(function() {
                                elementsLoaded++;

                                if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);

                                // An image has been loaded
                                if (elementsLoaded >= totalImagesCount)
                                        if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
                        });
                        $(this).error(function() {
                                elementsLoaded++;

                                if (opts.imageLoadedCallback) opts.imageLoadedCallback(elementsLoaded, originalTotalImagesCount);

                                // The image has errored
                                if (elementsLoaded >= totalImagesCount)
                                        if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
                        });
                }
        });
   
        // There are no unloaded images
        if (totalImagesCount <= 0)
                if (opts.loadingCompleteCallback) opts.loadingCompleteCallback();
    };
    })(jQuery);
}

