$(document).ready(function () {
    $(".main_image").each(function () { loadCSS("mastslider.css"); });
    /*Clear out the Search box on user click*/
    $('input.textInput').focus(function () { $(this).val(''); });

    /*Getting and breaking down the pathing, for nav functionality and skinning*/
    var aiopath = location.pathname; /*Glean the path (after the domain)*/
    if (aiopath) {
        /*Check to see if this is a program (note that programs are nested urls)*/
        var isProgram = false;
        if ((location.href.indexOf("degrees") > 1) || (location.href.indexOf("-degree") > 1) || (location.href.indexOf("associates") > 1) || (location.href.indexOf("program") > 1) || (location.href.indexOf("degrees-in") > 1) || (location.href.indexOf("certificate") > 1)) {
            isProgram = true; /*This will come in handy later on*/
        };

        /*If it is nested, and not a program*/
        if ((aiopath.split("/")[2] != "") && (isProgram == false)) {
            newpath = aiopath.split("/")[1] + "-" + aiopath.split("/")[2];
            if (isNaN(newpath.charAt(0))) { $('html').addClass(newpath) } /*Make sure the URL doesn't start with a number (CSS stipulation)*/
            else { $('html').addClass('x' + newpath) };
            aiopath = aiopath.split("/")[1] + "/" + aiopath.split("/")[2];
        }
        /*Just one directory in*/
        else {
            if (aiopath.split("/")[1] != "") {
                aiopath = aiopath.split("/")[1];
                if (isNaN(aiopath.charAt(0))) { $('html').addClass(aiopath) } /*Make sure the URL doesn't start with a number (CSS stipulation)*/
                else { $('html').addClass('x' + aiopath); };
            };
        };

        /*select the default(current) nav elements*/
        if (isProgram) {
            /*Is this the all encompassing degrees page???*/
            if (aiopath != "degrees") { var linkSelected = $(".btm a[href*='" + aiopath + "']").parents("ul#nav li"); }
            else { var linkSelected = $("ul#nav>li>a[href*='/" + aiopath + "/']").parents("ul#nav li"); };
            $.getScript("/Scripts/toggleCourse.js"); /*If this is a program, load the script to toggle the curriculum tabs*/
        }
        else {
            if (aiopath == "student-services/community") {/*If this is the student community page, only highlight the link under student services*/
                var linkSelected = $("ul#nav a[href$='" + aiopath + "/']").parents("ul#nav li:nth-child(5)");
            }
            else { /*This is just a regular old article page*/
                var linkSelected = $("ul#nav a[href$='" + aiopath + "/']").parents("ul#nav li");
            };
        };
        /*Add selected class to the default(current) nav elements*/
        linkSelected.attr("class", "selected");

    };

    /*Show correct subnav on mouseover*/
    /*    $("ul#nav > li").mouseover(function () { $(this).addClass("selected"); $(this).children("a").siblings("div").show(); });*/
    /*Hide subnav on mouseover*/
    /*    $("ul#nav > li").mouseout(function () { $(this).not(linkSelected).removeClass("selected"); $(this).children("a").siblings("div").hide(); });
    */

    /*Show correct subnav on mouseover*/
    $("ul#nav > li").hover(function () { $(this).addClass("selected"); $(this).children("a").siblings("div").show(); }, function () { $(this).not(linkSelected).removeClass("selected"); $(this).children("a").siblings("div").hide(); });
    /*Show the subnav child menu*/
    $("ul#nav .btm > ul > li").hover(function () { $(this).children("a").siblings("div").show(); }, function () { $(this).children("a").siblings("div").hide(); });

    /*load tabs only if tabbed/combobox controlled data is preset*/
    if ($("div#panels").length > 0) { loadCSS("jquery.tabs.css"); tabs(); };

    // LETS TWEET THIS!!
    $.ajaxSetup({ cache: true });
    $.getJSON("twittar.axd",
                 function (data) {
                     $.each(data, function (i, item) {
                         ct = item.text;
                         ct = ct.replace(/http:\/\/\S+/g, '<a href="$&" target="_blank">$&</a>');
                         ct = ct.replace(/\s(@)(\w+)/g, ' @<a href="http://twitter.com/$2" target="_blank">$2</a>');
                         ct = ct.replace(/\s(#)(\w+)/g, ' #<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
                         $("#jstweets").append('<p>' + ct + '</p><a class=lnkRead href=http://www.twitter.com/aicampuscommon>read more</a>');
                     });
                 });

    if (typeof jQuery.colorbox != 'undefined') {
        //all links to the campus commons should show a warning
        $("a[href*='http://aiconnect.thecampuscommon.com']").colorbox({ title: 'Visit Ai Connections', width: '850px', height: '700px', href: '/modals/connections.html', iframe: true, opacity: 0.75 });
    }
    $("#btnContactAdm").colorbox({ title: 'View Classroom Demo', width: '720px', height: '750px', href: '/request-information/demo-form.aspx', iframe: true, opacity: 0.75 });
    $(".btnOnlineDemo").colorbox({ title: 'View Online Library Demo', width: '720px', height: '750px', href: '/request-information/online-library-demo-form.aspx', iframe: true, opacity: 0.75 });
    $(".paylink").colorbox({ width: '868px', height: '720px', href: '/modals/paying-for-school.html', iframe: true, overlayClose: false });
    $(".gallery").colorbox();
});

/*Flexible lightbox call*/
function callLightbox(w, h, u, i, o) {
    /* w(width) is required*/
    /* h(height) is required*/
    /* u(url) is required*/
    /* i (iframe) is optional*/
    /* o (overlayClose-disable click out of modal close) is optional*/

    i = i || false;
    o = o || true;
    $.colorbox({ width: w, height: h, href: u, iframe: i, overlayClose: o });
};

function newPage(strPageURL) {
    window.location.href = strPageURL;
    $.colorbox.close();

};
/*Load any css file, by name*/
function loadCSS(strCSS) {
    $("head").append("<link>");
    css = $("head").children(":last");
    css.attr({
        rel: "stylesheet",
        type: "text/css",
        href: "/css/" + strCSS
    });
};

// Isolated functions
var OHE = (function (window) {

    var cookies = {
        setCookie: function (c_name, value, exdays) {
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
            document.cookie = c_name + "=" + c_value;
        },
        getCookie: function (c_name) {
            var i, x, y, ARRcookies = document.cookie.split(";");
            for (i = 0; i < ARRcookies.length; i++) {
                x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
                y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
                x = x.replace(/^\s+|\s+$/g, "");
                if (x == c_name) {
                    return unescape(y);
                }
            }
        }
    },

    Student = {
        isStudent: function () {
            return cookies.getCookie('isStudent') == 'true';
        },
        setAsStudent: function () {
            cookies.setCookie('isStudent', 'true', 360);
        }
    }
    return {
        cookies: cookies,
        Student: Student
    }
} (window));
