$(document).ready(function () {
	
	// Hide the H1 if none is specified.	
	var h1Count = 1;
	var charLength = 0;

	$("#ahBodyCopy h1").each(function(index) {
		if(h1Count == 1){
		 charLength = $(this).text().length;	
		}
	   h1Count++;
	//alert(h1Count);
	});
	
	if (charLength < 60 && h1Count < 3) { 
	    $("h1").text($("#ahBreadcrumb span span:last-child").text());
  	}


    // More Aultman Sites Drop Down
    $("#ahMoreSites > li > a").mouseover(function () {
        $(this).parent().find("#ahSiteList").slideDown();
        $(this).css({ "background-position": "-34px -97px" });
    });

    $("#ahMoreSites > li").mouseleave(function () {
        $(this).find("#ahSiteList").hide();
        $("#ahHeaderRight #ahMoreSites > li > a").css({ "background-position": "-34px -66px" });
    });

    $("#ahMoreSites > li > a").click(function (e) {
        e.preventDefault();
    });

    // More Phone Numbers Drop Down
    $("#ahPhoneList > li > a").mouseover(function () {
        $(this).parent().find("ul").slideDown();
        $(this).css({ "background-position": "-34px -159px" });
    });

    $("#ahPhoneList > li").mouseleave(function () {
        $(this).find("ul").hide();
        $("#ahHeaderRight #ahPhoneList > li > a").css({ "background-position": "-34px -128px" });
    });

    $("#ahPhoneList > li > a").click(function (e) {
        e.preventDefault();
    });

    $("#ahEducation > li > a").mouseover(function () {
        $(this).parent().find("ul").slideDown();
        $(this).css({ "background-position": "-34px -599px" });
    });

    $("#ahEducation > li").mouseleave(function () {
        $(this).find("ul").hide();
        $("#ahHeaderRight #ahEducation > li > a").css({ "background-position": "-34px -128px" });
    });

    $("#ahEducation > li > a").click(function (e) {
        e.preventDefault();
    });

    // More Aultman Sites Drop Down - ADAM HEALTH
    $("#ahMoreSites2 > li > a").mouseover(function () {
        $(this).css({ "background-position": "-34px -97px" });
    });

    $("#ahMoreSites2 > li").mouseleave(function () {
        $("#ahMoreSites2 > li > a").css({ "background-position": "-34px -66px" });
    });

    // More Phone Numbers Drop Down - ADAM HEALTH
    $("#ahPhoneList2 > li > a").mouseover(function () {
        $(this).css({ "background-position": "-34px -159px" });
    });

    $("#ahPhoneList2 > li").mouseleave(function () {
        $("#ahPhoneList2 > li > a").css({ "background-position": "-34px -128px" });
    });

	$("#ahEducation2 > li > a").mouseover(function () {
        $(this).parent().find("ul").slideDown();
        $(this).css({ "background-position": "-34px -532px" });
    });

    $("#ahEducation2 > li").mouseleave(function () {
        $("#ahHeaderRight #ahEducation2 > li > a").css({ "background-position": "-34px -128px" });
    });


    // Wire up job application links to modal
    $(".jobApplicationLink").live("click", function (e) {
        e.preventDefault();
        radopen("https://www.aultman.org/employment/Aultman-Job-Opportunities/application.aspx?id=" + $(this).attr('href').replace('#', ''), "JobAppWindow");
    });

    // Immediate Care Wait Times
    $(".waitTimes").live("click", function (e) {
        e.preventDefault();
        radopen("/waittimes.aspx", "WaitTimes");
    });

 // Immediate Care Wait Times
    $(".waitTimes2").live("click", function (e) {
        e.preventDefault();
        radopen("/waittimes.aspx", "WaitTimes");
    });

    // SIde Nav
    $("#ahSideNav .active").parents("ul").addClass("active").show();

    $(".active").prev("a").find("span").addClass("active");

    $("#ahSideNav > ul > li.group").mouseover(function () {
        $(this).find("> ul").slideDown();

        $(this).find("a:first").css({ "border-bottom": "0" });
        $(this).find("a:first > span").addClass("active");

        $("#ahSideNav > ul > li > ul > li").mouseover(function () {
            $(this).find("ul").slideDown();
            $(this).find("a:first").css({ "border-bottom": "0" });
            $(this).find("a:first span:first").addClass("active");
        });

        $("#ahSideNav").mouseleave(function () {
            $("#ahSideNav ul li").each(function () {
                if ($(this).contents(".active").length === 0) {
                    $(this).find("ul").not(".active").slideUp();
                    $(this).find("a span").removeClass("active");
                }
            });
        });

    });


    // NAVIGATION
    $(".noGo").live("click", function (e) {
        e.preventDefault();
    });


    // Calculate the Width of the Dropdown

    $("#ahNavigation > li > ul").each(function () {
        var groupWidth = 0;
        var groupHeight = 0;
        var liCount = 0;
        var beginningWidth = 0;

        $(this).find("li .section a").each(function () {
            if ($(this).outerWidth() > beginningWidth) {
                beginningWidth = $(this).outerWidth();
            }
        });

        liCount = $(this).find("li").length;
        marginTotal = liCount * 22;

        $(this).css({ "width": (beginningWidth * liCount) + (marginTotal) });
        $(this).find("li").css({ "width": beginningWidth });
        $(this).find("li:last-child").css({ "margin-right": "0" });


        marginTotal = 0;
        beginningWidth = 0;
        groupWidth = 0;
        groupHeight = 0;
        liCount = 0;
    });

    if ($.browser.msie && $.browser.version == 8) {
        $("ul#ahNavigation > li > a").css({
            "padding": "12px 13px 9px"
        });

        $("#ahNavigation > li > ul").css({ 
            "border-left": "1px solid #666",
            "border-right": "1px solid #666"    
        });
    }


    // OPEN CLOSE FOOTER LINKS
	var $openClose = $(".openCloseLinks");
		
	$openClose.live("click", function (e) {
        e.preventDefault();

        if ($(this).find("div").text() === "Open Links") {
            $(this).find("div").text("Close Links");
            $(this).find("span").css({ "background": "url(/App_Themes/Aultman/images/buttons_SPRITE.png) no-repeat -58px 0" });
        }
        else {
            $(this).find("div").text("Open Links");
            $(this).find("span").css({ "background": "url(/App_Themes/Aultman/images/buttons_SPRITE.png) no-repeat -34px 0" });
        }

        $("#ahSiteDirectory").slideToggle();
    });
	
	
	$("#ahSiteDirectory .column:last-child").css({"margin-right":"0"});

    $("#ahBreadcrumb span span:last-child").css({ "font-weight": "bold" });

    $("a[href^='http']").attr('target', '_blank');
	$("a[href^='.pdf']").attr('target', '_blank');
	
});

$(function () {

    var config = {
        sensitivity: 333, // number = sensitivity threshold (must be 1 or higher)    
        interval: 200,  // number = milliseconds for onMouseOver polling interval    
        over: doOpen,   // function = onMouseOver callback (REQUIRED)    
        timeout: 1,   // number = milliseconds delay before onMouseOut    
        out: doClose    // function = onMouseOut callback (REQUIRED)    
    };

    function doOpen() {
        $(this).find("a").addClass("active");
        $('ul:first', this).css('visibility', 'visible');
    }

    function doClose() {
        $(this).find("a").removeClass("active");
        $('ul:first', this).css('visibility', 'hidden');
    }

    $("ul#ahNavigation li").hoverIntent(config);
});

