$(document).ready(function () {
    // link marking
    var exitLinks = $('a[href^=http]').not('.noIcon, a[href$=".pdf"], [href*=".eidebailly.com"], ul.nav a'),
        pdfLinks = $('a[href$=".pdf"]').not('.noIcon'),
        docLinks = $('a[href$=".doc"], a[href$=".docx"]').not('.noIcon'),
        xlsLinks = $('a[href$=".xls"], a[href$=".xlsx"], a[href$=".xlsm"]').not('.noIcon');

    // exit links
    $.each(exitLinks, function (i, val) {
        // all exit links
        $(val).attr({ title: 'This link takes you to an external website.', target: '_blank' });
        // exit links that are not images or buttons
        $(val).not($('a[href^=http]').children('img').parent()).not($('a[class$=button]')).append("<img src='/images/external.png' class='exitIcon' alt='This link takes you to an external website.' width='9' height='9' />");
    });

    // pdf links
    $.each(pdfLinks, function (i, val) {
        // all pdf links
        $(val).attr({ title: 'This link opens a PDF document.', target: '_blank' });
        // pdf links that are images or buttons
        $(val).not($('a[href$=".pdf"]').children('img').parent()).not($('a[class$=button]')).append("<img src='/images/pdf.png' class='pdfIcon' alt='This link opens a PDF document.' width='9' height='9' />");
    });

    // doc links
    $.each(docLinks, function (i, val) {
        // all doc links
        $(val).attr({ title: 'This link opens a Word document.', target: '_blank' });
        // doc links that are images or buttons
        $(val).not($('a[href$=".doc"], a[href$=".docx"]').children('img').parent()).not($('a[class$=button]')).append("<img src='/images/doc.png' class='docIcon' alt='This link opens a Word document.' width='9' height='9' />");
    });

    // xls links
    $.each(xlsLinks, function (i, val) {
        // all xls links
        $(val).attr({ title: 'This link opens an Excel document.', target: '_blank' });
        // xls links that are images or buttons
        $(val).not($('a[href$=".xls"], a[href$=".xlsx"], a[href$=".xlsm"]').children('img').parent()).not($('a[class$=button]')).append("<img src='/images/xls.png' class='xlsIcon' alt='This link opens a Excel document.' width='9' height='9' />");
    });

    // table stripes and hover - add table-stripe class to <table>
    $(".table-stripe tr:even").addClass("even");
    $(".table-stripe tr").hover(function () {
        $(this).addClass("table-over");
    },
	function () {
	    $(this).removeClass("table-over");
	});
    // table hover without stripes - add table-hover class to <table>
    $(".table-hover tr").mouseover(function () {
        $(this).addClass("table-over");
    }).mouseout(function () {
        $(this).removeClass("table-over");
    });
    // <td> Highlight - Add td-hover class to <table>
    $(".td-hover td").hover(function () {
        $(this).addClass("td-over");
    },
	function () {
	    $(this).removeClass("td-over");
	});

    // breadcrumb nav - addClass to last-child
    $('#breadcrumb ul li:last-child').addClass('last-child');

    // show page-option if js in enabled
    $('#page-options').removeClass('invisible');

    // catch enter key and trigger submit action on closest button
    $('input:text').keydown(function (event) {
        // validate on enter keydown
        if (event.keyCode == 13) {
            event.preventDefault();
            $(event.currentTarget).parent().parent().find('[type="submit"]').click();
        }
    });

    //remove empty paragraph tags that the rich text editor injects sometimes
    $('p:empty').remove();
    

    /////////////////////////////////////////////////////////////////////////
    // plugins
    /////////////////////////////////////////////////////////////////////////

    // input helper text plugin
    $.fn.inputHelper = function () {
        return this.each(function () {
            var inputText = $(this).attr('value');
            var helperText = $(this).attr('alt');

            $(this).attr("value", helperText);

            // on focus clear input
            $(this).focus(function () {
                if ($(this).val() === helperText) {
                    $(this).attr('value', '');
                    $(this).val('');
                } else {
                    return;
                }
            });

            // on blur, replace empty input with helper text if nothing was entered
            $(this).blur(function () {
                if ($(this).val() === "") {
                    $(this).attr("value", helperText);
                } else {
                    return;
                }
            });
        });
    };
    $('.input-helper').inputHelper();

    // clear helper text on button click
    $('.input-helper').siblings('.btn-submit').click(function () {
        var $inputHelperClosest = $(this).siblings('.input-helper');
        jQuery.each($inputHelperClosest, function (i, val) {
            var iVal = $(val).val();
            var iAlt = $(val).attr('alt');
            if (iVal === iAlt) {
                $(val).attr('value', '');
            }
        });
    });

    // input reset plugin
    $.fn.inputReset = function () {
        return this.each(function () {
            // clear text
            $(this).val('');
            // blur will cause the helper text to reappear
            $(this).blur();
        });
    };

    // pause plugin			   
    $.fn.pause = function (milli, type) {
        milli = milli || 1000;
        type = type || "fx";
        return this.queue(type, function () {
            var self = this;
            setTimeout(function () {
                $.dequeue(self);
            }, milli);
        });
    };

    // console.log plugin
    window.log = function () { log.history = log.history || []; log.history.push(arguments); if (this.console) { console.log(Array.prototype.slice.call(arguments)) } };

});
