﻿var isNurse;
$(document).ready(function () {
    var showNurse = [
                "/nursing.aspx",
                "/healthcare",
                "/bachelor-science-nursing.aspx",
                "/accelerated-rn-msn.aspx",
                "/master-science-nursing-msn.aspx",
                "/associate-allied-health-science.aspx"
            ];

    //Persistent RIF
    $("#persRIF").click(function () {
        toggleRIF();
    });
    $("#ctaRqstInfo").click(function () {
        toggleRIF();
    });

    $(document).click(function (e) {
        var mouseY = e.pageY;
        if ((mouseY > 395) && ($("#persRIF").hasClass("opened"))) {
            toggleRIF();
        };
    });

    $(document).keyup(function (e) {
        if ((e.keyCode == 27) && ($("#persRIF").hasClass("opened"))) { toggleRIF(); }
    });

    $("a[href*='#classroomdemo']").colorbox({ title: 'View Classroom Demo', width: '720px', height: '720px', href: '/request-information/demo-form.aspx', iframe: true, opacity: 0.75 });
    $(".NurseInfoLinkWide").colorbox({ title: 'Let Us Help You Chart Your Course', width: '615px', height: '375px', href: '/modals/nursing-info.html', iframe: true, opacity: 0.75, overlayClose: false });
    $(".libraryLink").colorbox({ title: 'Explore the Online Library', width: '784px', height: '784px', href: '/request-information/library-form.aspx', iframe: true, opacity: 0.75 });

    $(".paylink").colorbox({ width: '868px', height: '720px', href: '/modals/paying-for-school.html', iframe: true, overlayClose: false });
    $('input.textInput').focus(function () { $(this).val(''); });
    //top nav functionality
    $('ul#topnav > li').mouseover(function () { $(this).children('a').siblings('ul').show(); });
    $('ul#topnav > li').mouseout(function () { $(this).children('a').siblings('ul').hide(); });
    //indicate selected page/menu item and parents
    //based on the path structure of current url and menu links
    var path = location.pathname;
    if (path)
    //if this is in the degrees directory, highlight the Academic Programs link
        if (path.indexOf('online-book-store') >= 0) { $('ul#topnav li:nth-child(2)').attr('class', 'selected'); }
        else {
            if (path.indexOf('degrees/') > 0) { $('ul#topnav li:first-child').attr('class', 'selected'); };
            if (path != '/') {
                $('ul#topnav a[href*="' + path + '"]').not('ul#topnav a[href$="online-book-store.aspx#"],ul#topnav a[href$="our-faculty.aspx#"]').parents('li').attr('class', 'selected');
                $('ul#topnav a[href*="' + path + '"]').attr('class', 'selected');
            }
        }
    //Temporary hardcoding or hide/show Student Services/RIF callouts	    
    if ((path.indexOf('student-services') > 0) || (path.indexOf('degrees/') > 0)) { $('#divRIFCallout').removeClass('hidden'); }
    else { $('#divSSCallout').removeClass('hidden'); };

    //Hardcoding hide/show of Nursing info side banner link. I <3 .each!
    $.each(showNurse, function (k, v) { if (path.indexOf(v) > 0) { $('#divNursingInfo').removeClass('hidden'); isNurse = true; }; });

    //Temporary(??) way of handling the background images on the client
    //make sure it isn't the root
    if ((path != '/') && (path != '/Default.aspx')) {
        var strP = path.toString();
        var strPath;
        if (strP.indexOf('aspx') >= 0) { strPath = strP.replace('aspx', 'jpg'); }
        else { strPath = strP + "Default.jpg" };

        if ((strPath.split("/")[2]) && (strPath.split("/")[2].indexOf('articles') >= 0)) { $('#bd').css('background-image', 'url(../../images/Generic.jpg)'); }
        else {
            if ((!(strPath.split("/")[2])) && (strPath.split("/")[1] != "Default.jpg")) { $('#bd').css('background-image', 'url(images/Generic.jpg)') }
            else {
                if ((strPath.split("/")[2]) && (strPath.split("/")[2].indexOf('degrees-by-college') < 0) && (strPath.split("/")[2].indexOf('Default') < 0) && (strPath.split("/")[1] != "area-of-study") && (strPath.split("/")[1] != "degree-level") && (strPath.split("/")[1].indexOf('degrees') < 0)) { strPath = strPath.replace(strPath.split("/")[2], 'Default.jpg'); };

                var pathInfo = {
                    '[1]=="area-of-study"': 'url("../' + strPath.replace(strPath.split("/")[0], 'images') + '")',
                    '[1]=="degrees"': 'url("../' + strPath.replace(strPath.split("/")[0], 'images') + '")',
                    '[1]=="degree-level"': 'url("../' + strPath.replace(strPath.split("/")[0], 'images') + '")',
                    '[1]=="default.jpg"': 'url("../' + strPath.replace(strPath.split("/")[0], 'images') + '")',
                    '[1].indexOf("jpg")>=0': 'url("../' + strPath.replace(strPath.split("/")[0], 'images') + '")',
                    '[2]=="degrees-by-college.jpg"': 'url("../' + strPath.replace(strPath.split("/")[0], 'images') + '")',
                    '[2]==""': 'url("../' + strPath.replace(strPath.split("/")[0], 'images') + 'Default.jpg")'
                };
                $.each(pathInfo, function (k, v) {
                    if (strPath.split("/") + k) { $('#bd').css('background-image', v); return false; }
                });
            };
        };
    };
    /*END Temp Banner Images*/

    $('ul#topnav ul:empty').remove(); //remove the empty menu elements

    //adjust last two menu items to fit within smaller browser windows (<1280px)
    var winWidth = $(window).width();
    //because ff has issues with the window resize timing
    $(window).resize(function () { delay(function () { winWidth = $(window).width(); }); });
    //last two menu items. :nth-child is 1 indexed
    var menuItems = { '5': '1020', '6': '1190' };
    $.each(menuItems, function (k, v) {
        $('ul#topnav>li:nth-child(' + k + ')>a').hover(function () {
            if (winWidth < v) { $(this).next('ul').addClass('zeroRt'); } //set to right:0px
            else { $(this).next('ul').removeClass('zeroRt'); };
        }); //or not
    });

    //$('ul#topnav > li:nth-child(4,5,6)').children('a').siblings('ul').css();

    /*load tabs only if tabbed/combobox controlled data is preset*/
    if ($('div#panels').length > 0) { loadCSS("jquery.tabs.css"); tabs(); }


});

function loadCSS(strCSS) {
    $("head").append("<link>");
    css = $("head").children(":last");
    css.attr({
        rel: "stylesheet",
        type: "text/css",
        href: "/css/" + strCSS
    });
};

//redirect to the parent page from the lightbox
function newPage(strPageURL) {
    window.location.href = strPageURL;
    $.colorbox.close();
}

//delay function, in this context, used for jquery(window).resize
var delay = (function () {
    var timer = 0;
    return function (callback, ms) {
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();

//Toggle the rif open/close
function toggleRIF() {
    $("#persRIF").toggleClass("opened");
    $("#persContent").toggleClass("closed");
    $("form").toggleClass("opened");
};
