﻿



$(document).ready(function () {
    SetupWatermark($('#txtSearch'), "Enter search here");

    //Setup enterkey detection in header search
    $('#txtSearch').keypress(function (e) {
        if (e.which == 13 || e.keyCode == 13) {
            Search();
            return false;
        }
    });

    $(".divSearch .btn").click(Search);

    //Setup Osha Message (if display element is found)
    if ($('#numDaysSinceOshaInjury').length > 0) {
        SetupOshaMessage();
    }
});

function Search() {
    var terms = document.getElementById("txtSearch").value;
    if (terms.length > 0) {
        window.open('search.aspx?q=' + terms, '_self', '');
    }

    return false;
}

function SetupWatermark(jqueryElementObject, waterMarkText) {
    // Define what happens when the textbox comes under focus
    // Remove the watermark class and clear the box
    jqueryElementObject.focus(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not 
            // something actually entered
            return $(this).val() == "" || $(this).val() == waterMarkText

        }).removeClass("watermarkOn").val("");

    });

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    jqueryElementObject.blur(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == ""

        }).addClass("watermarkOn").val(waterMarkText);

    });
}

function SetupOshaMessage() {
    // Store the current date and time
    var current_date = new Date()
    var daysElement = $('#numDaysSinceOshaInjury');
    //Valid date formats: "MM-dd-yyyy", "yyyy/MM/dd", "MM/dd/yyyy", "MMMM dd, yyyy", "MMM dd, yyyy" 
    var lastInjury_date = new Date(daysElement.attr('lastInjuryDate'));

    if (!isNaN(lastInjury_date)) {
        // Call the days_between function
        var days_sinceLastInjury = days_between(lastInjury_date, current_date)

        // Write the result to the page
        daysElement.html(days_sinceLastInjury);
    }
}

function days_between(date1, date2) {
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)

    // Convert back to days and return
    return Math.round(difference_ms / ONE_DAY)
}


function ItemMouseOver(sender, eventArgs) {
    var item = eventArgs.get_item();
    var id = item.get_id();
    var parent = item.get_parentItem();
    if (id != null && parent != null) {
        var siblings = parent.get_items();
        ToggleNeighboringItemDividerClasses(item, siblings,false);
    }
}

function ItemMouseOut(sender, eventArgs) {
    var item = eventArgs.get_item();
    var id = item.get_id();
    var parent = item.get_parentItem();
    if (id != null && parent != null && item.get_lookId() != 'SubItemDividerLook') {
        var siblings = parent.get_items();
        ToggleNeighboringItemDividerClasses(item, siblings, true);
    }
}

//Precondition:item will be a regular menu item (not the first or last item). Item will not be a root item.
function ToggleNeighboringItemDividerClasses(item, siblings, showDivider) {
    var dividerBefore, dividerAfter;
    var numSiblings = siblings.get_length();
    for (var i = 0; i < numSiblings; i++) {
        if (siblings.getItem(i).get_id() == item.get_id()) {
            //check for dividerBefore
            dividerBefore = siblings.getItem(i - 1);
            if (dividerBefore.get_lookId() != 'SubItemDividerLook') {
                dividerBefore = null;
            }
            else {
                var dBeforeItem = $('#' + dividerBefore.get_id());
                if (showDivider) {
                    dBeforeItem.attr("class", "SubItemDivider");
                }
                else {
                    dBeforeItem.attr("class", "SubItemDividerNbrHover");
                }
            }

            //check for dividerAfter
            dividerAfter = siblings.getItem(i + 1);
            if (dividerAfter.get_lookId() != 'SubItemDividerLook') {
                dividerAfter = null;
            }
            else {
                var dAfterItem = $('#' + dividerAfter.get_id());
                if (showDivider) {
                    dAfterItem.attr("class", "SubItemDivider");
                }
                else {
                    dAfterItem.attr("class", "SubItemDividerNbrHover");
                }
            }
            
            //No need to check more than the hovered item's neighbors
            break;
        }
    }
}
