﻿//topBar Script
$(document).ready(function () {
    //On Hover Over
    function megaHoverOver() {
        //$("#topBarRoot").find(".topBarSub").hide();
        //$("#topBarRoot").find(".topBarRoot-on").toggleClass('topBarRoot-on topBarRoot-off');

        $(this).find(".topBarRoot-off").toggleClass('topBarRoot-off topBarRoot-on');

        $(this).find(".topBarSub").stop().fadeTo('fast', 0.9).show(); //Find sub and fade it in
        (function ($) {
            //Function to calculate total width of all ul's
            jQuery.fn.calcSubWidth = function () {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function () { //for each ul...
                    rowWidth += $(this).width(); //Add each ul's width together
                });
            };
        })(jQuery);

        if ($(this).find(".topBarRow").length > 0) { //If row exists...

            var biggestRow = 0;

            $(this).find(".topBarRow").each(function () {	//for each row...
                $(this).calcSubWidth(); //Call function to calculate width of all ul's
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });

            $(this).find(".topBarSub").css({ 'width': biggestRow }); //Set width
            $(this).find(".topBarRow:last").css({ 'margin': '0' });  //Kill last row's margin

        } else { //If row does not exist...

            $(this).calcSubWidth();  //Call function to calculate width of all ul's
            $(this).find(".topBarSub").css({ 'width': rowWidth }); //Set Width

        }
    }
    //On Hover Out
    function megaHoverOut() {
        $(this).find(".topBarRoot-on").toggleClass('topBarRoot-on topBarRoot-off');

        $(this).find(".topBarSub").stop().fadeTo('fast', 0, function () { //Fade to 0 opactiy
            $(this).hide();  //after fading, hide it
        });
    }

    //Set custom configurations
    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
        timeout: 500, // number = milliseconds delay before onMouseOut
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };

    $("ul#topBarRoot li .topBarSub").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
    $("ul#topBarRoot li").hoverIntent(config); //Trigger Hover intent with custom configurations
    //$("ul#topBarRoot li").click(null, megaHoverOver); //Expands menu on touch device

});


//script writes email addresses in mailto format
function writeMailTo(handle, dname) {
    document.write("<a href=" + "mail" + "to:" + handle + "@" + dname + ">" + handle + "@" + dname + "</a>");
}

function writeMailToDB(address) {
    document.write("<a href=" + "mail" + "to:" + address + ">" + address + "</a>");
}

function writeMailToCustText(handle, dname, linktext) {
    document.write("<a href=" + "mail" + "to:" + handle + "@" + dname + ">" + linktext + "</a>");
}

function getTimeStamp() {
    var aDate = new Date();
    var stamp = aDate.getTime();

    return stamp;
}
