﻿$(document).ready(function() {

    // enable nth-child selection in IE
    $('#content .category-table span').each(function(index) {
        if (index % 3 == 0) { $(this).addClass('child-3n1'); };
        if (index % 2 == 0) { $(this).addClass('child-2n1'); }
    });

    // add pseudo-link to home page over the background Lebturf header logo 
    $('<div class="pseudoHome" onclick="window.location.href = \'/\';"><img src="/images/lebturf-print-logo-sm.png" id="pseudoHomeLogo" /></div>').insertBefore('.header.page div:eq(0)');

    // When the document is ready, initialize the link so that when it is clicked, the printable area of the page will print.

    // Hook up the print links.
    //$('#content div.printable').append('<p><a href="javascript:void(0)" class="printable">Print this region.</a></p>');
    $('#content div.printable')
        .append('<div class="section-tools" style="display:none;"><a href="#content" >top</a><a href="javascript:void(0)" class="print-section">print section</a></div>')
        .hover(function() {
            $('#content div.printable div.section-tools').removeClass('active');
            $(this).find('.section-tools').addClass('active');
        });
    var pageHead = $('#content h1:first').html();
    $('#content a.print-section').click(
        function() {
            // Print the DIV.
            PrintContentSection(this, pageHead);

            // Cancel click event.
            return (false);
        });


    // set up overlay / popup triggers
    $("#content a.popup[rel]").overlay({

        mask: 'darkred',
        //effect: 'apple', -- need the 'apple' effects plugin for this, requires extra bandwidth
        fixed: false,
        onBeforeLoad: function() {
            try {
                // to fix IE and overlay within relatively positioned #content
                // TODO: Now that overlay is outside of #wrap, we need to redefine styles for content in the overlay!!
                var that = this;
                $(this.getOverlay()).remove().appendTo("body");
                $(".close", this.getOverlay()).click(function(e) { that.close(); });

                // grab wrapper element inside content
                var wrap = this.getOverlay().find(".contentWrap");

                // load the page specified in the trigger
                if (wrap != null) { wrap.load(this.getTrigger().attr("href")); };

            } catch (e) {
                alert("oops!");
            }
        }

    });


    // Adjust the guaranteed analysis tables
    $('.dot-leader:not(.guaranteed-analysis) td').each(function() {
        td = $(this).html();
        td = td.replace(/\u00a0/g, ''); // remove &nsbp;
        td = td.replace(/&nbsp;/g, ''); // remove &nsbp;
        td = td.replace(' x ', ' &times; ');
        $(this).html(td);
    });
    $('.dot-leader:not(.guaranteed-analysis) td.left').removeClass('left');
    $('.dot-leader *[class=""]').removeAttr('class');
    $('.dot-leader.guaranteed-analysis td').each(function() {
        td = $(this).html();
        td = td.replace('P2O5', 'P<sub>2</sub>O<sub>5</sub>');
        td = td.replace('K2O', 'K<sub>2</sub>O');
        $(this).html(td);
    });
    $('.dot-leader tr > td.right:not(.no-leader)').each(function() {
        $(this).parents('tr:first').find('td:not(:has(span))').each(function() {
            td = '<span>' + $(this).html() + '</span>';
            $(this).html(td);
        });
    });

    if (autoPrint == true) { window.print(); };
});
