$(function() {
    //Menu styling
    $("#topNav li:has(a.selected) div, #topNav li a.selected").hover(function() {
        $("#topNav a.selected").css("color", "#B00932");
    }, function() {
        $("#topNav li a.selected").css("color", "white");
    });
    //Select box
    $("#cbQuicklinksSelect").easySelectBox({speed:100});
    $(".easy-select-box li").click(function() {
        var url = $("a", this).attr('rel');
        if(url != "#") {
            location.href = url;
        }
    });
    
    //ITS

    $("#faqLink").on("click", function() {
        Shadowbox.open({
            content: "http://help.fsugo.com/",
            player: "iframe",
            title: "FSU ITS Knowledge Base",
            options: {
                modal: true
            }
        });
        return false;
    });
    
    $("#googleMap").on("click", function() {
        Shadowbox.open({
            content: "http://www.google.com/maps?q=100+State+St,+Framingham,+MA+01702&hl=en&ll=42.297358,-71.438298&spn=0.010713,0.022724&sll=37.0625,-95.677068&sspn=46.677964,93.076172&oq=100+state+&vpsrc=0&hnear=100+State+St,+Framingham,+Massachusetts+01702&t=m&z=16&output=embed",
            player: "iframe",
            title: "Our Location"
        });
        return false;
    });
    
    $("#eAcademy").on("click", function() {
        Shadowbox.open({
            content: "http://e5.onthehub.com/WebStore/ProductsByMajorVersionList.aspx?ws=2a4aa7c8-db07-dd11-89b0-0030485a6b08&vsro=8",
            player: "iframe",
            title: "Framingham State University | Academic Software Discounts"
        });
        return false;
    });

    var slides = {
        mainContent: true
    };
    var studentOverlays = {};
    var employeeOverlays = {};
    var left = true;
    var right = true;
    
    //Set slides active while setting all the rest to inactive
    function setSlides(settings, bool) {
        //If array is passed, set all the slides in the array to active and set everything else to inactive
        if($.isArray(settings)) {
            for(var i = 0; i < settings.length; i++) {
                slides[settings[i]] = bool;
            }
            for(var slide in slides) {
                if($.inArray(slide, settings) == -1) {
                    slides[slide] = false;
                }
            }
        }
        else {
            //Set single slide to active and set the rest to inactive
            slides[settings] = bool;
            for(var slide in slides) {
                if(settings != slide) {
                    slides[slide] = false;
                }
            }
        }
    }
    
    //Check if any student overlays are active
    function checkStudentOverlays(skipOverlay) {
        var status = false;
        var enabledOverlay;
        for(var overlay in studentOverlays) {
            if(skipOverlay == overlay) {
                continue;
            }
            if(studentOverlays[overlay]) {
                status = true;
                enabledOverlay = overlay;
            }
        }
        if(status) {
            //If an overlay is already active, hide it and show the overlay that was clicked
            $("#"+enabledOverlay.toLowerCase().replace("student", "")+"Overlay").fadeOut(300, function() {
                studentOverlays[enabledOverlay] = false;
                $("#"+skipOverlay.toLowerCase().replace("student", "")+"Overlay").fadeIn(300, function() {
                    studentOverlays[skipOverlay] = true;
                });
            });
        }
        return status;
    }
    
    //Check if any employee overlays are active
    function checkEmployeeOverlays(skipOverlay) {
        var status = false;
        var enabledOverlay;
        for(var overlay in employeeOverlays) {
            if(skipOverlay == overlay) {
                continue;
            }
            if(employeeOverlays[overlay]) {
                status = true;
                enabledOverlay = overlay;
            }
        }
        if(status) {
            //If an overlay is already active, hide it and show the overlay that was clicked
            var oldOverlay = "#"+enabledOverlay.toLowerCase().replace("employee", "")+"Overlay";
            var newOverlay = "#"+skipOverlay.toLowerCase().replace("employee", "")+"Overlay";
            if(newOverlay == "#resourcesOverlay") {
                newOverlay = "#employeeResourcesOverlay";
            }
            if(oldOverlay == "#resourcesOverlay") {
                oldOverlay = "#employeeResourcesOverlay";
            }
            $(oldOverlay).fadeOut(300, function() {
                employeeOverlays[enabledOverlay] = false;
                $(newOverlay).fadeIn(300, function() {
                    employeeOverlays[skipOverlay] = true;
                });
            });
        }
        return status;
    }
    
    //Check if specific student overlay is active
    function isStudentOverlayActive(overlay) {
        return studentOverlays[overlay];
    }
    
    //Check if specific employee overlay is active
    function isEmployeeOverlayActive(overlay) {
        return employeeOverlays[overlay];
    }
    
    //Get all the slide names
    function getSlides() {
        var allSlides = [];
        $.each(slides, function(slide, bool) {
            allSlides.push(slide);
        });
        return allSlides;
    }
    
    //Get only active slide names
    function getActiveSlides() {
        var activeSlides = [];
        $.each(slides, function(slide, bool) {
            if(bool) {
                activeSlides.push(slide);
            }
        });
        return activeSlides;
    }
    
    //Check if a slide is active or inactive for a particular slide
    function isActive(slide) {
        return slides[slide];
    }
    
    //Show the left arrow
    function showLeft() {
        var arrow = $("#slideshow_left");
        arrow.fadeTo(0, 0);
        arrow.on("click", clickLeft);
        arrow.css({"cursor":"pointer"});
        left = true;
    }
    
    //Hide the left arrow
    function hideLeft() {
        var arrow = $("#slideshow_left");
        arrow.fadeTo(0, 0);
        arrow.off("click");
        arrow.css({"cursor":"auto"});
        left = false;
    }
    
    //Show the right arrow
    function showRight() {
        var arrow = $("#slideshow_right");
        arrow.fadeTo(0, 0);
        arrow.on("click", clickRight);
        arrow.css({"cursor":"pointer"});
        right = true;
    }
    
    //Hide the right arrow
    function hideRight() {
        var arrow = $("#slideshow_right");
        arrow.fadeTo(0, 0);
        arrow.off("click");
        arrow.css({"cursor":"auto"});
        right = false;
    }
    
    //Show both arrows
    function showArrows() {
        showLeft();
        showRight();
    }
    
    //Hide both arrows
    function hideArrows() {
        hideLeft();
        hideRight();
    }
    
    //Dynamic breadcrumb nav
    var breadNav = $("#slideShowLabel");
    var breadLinks = {
        main: function(link) {
            if(!link) {
                return "Information Technology Services";
            }
            else {
                return "<a href='#' id='breadNavMain'>Information Technology Services</a>";
            }
        },
        student: function(link) {
            if(!link) {
                return "Student";
            }
            else {
                return "<a href='#' id='breadNavStudents'>Student</a>";
            }
        },
        studentEmployment: function(link) {
            if(!link) {
                return "Employment";
            }
            else {
                return "<a href='#' id='breadNavStudentEmployment'>Employment</a>";
            }
        },
        studentTraining: function(link) {
            if(!link) {
                return "Training";
            }
            else {
                return "<a href='#' id='breadNavStudentTraining'>Training</a>";
            }
        },
        studentOffers: function(link) {
            if(!link) {
                return "Offers";
            }
            else {
                return "<a href='#' id='breadNavStudentOffers'>Student Offers</a>";
            }
        },
        studentLaptop: function(link) {
            if(!link) {
                return "Laptop Program";
            }
            else {
                return "<a href='#' id='breadNavStudentLaptop'>Laptop Program</a>";
            }
        },
        myFramingham: function(link) {
            if(!link) {
                return "myFramingham";
            }
            else {
                return "<a href='#' id='breadNavMyFramingham'>myFramingham</a>";
            }
        },
        blackboard: function(link) {
            if(!link) {
                return "Blackboard Learn";
            }
            else {
                return "<a href='#' id='breadNavBlackboard'>Blackboard Learn</a>";
            }
        },
        employee: function(link) {
            if(!link) {
                return "Employee";
            }
            else {
                return "<a href='#' id='breadNavEmployee'>Employee</a>";
            }
        },
        employeeSpotlight: function(link) {
            if(!link) {
                return "Spotlight";
            }
            else {
                return "<a href='#' id='breadNavEmployeeSpotlight'>Spotlight</a>";
            }
        }
    };
    $("#slideShowIndex").on("click", function() {
        var arrow = " &#8227; ";
        if(isActive("studentContent") && getActiveSlides().length == 1) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.student());
        }
        if(isActive("mainContent")) {
            breadNav.html(breadLinks.main());
        }
        if(isActive("studentEmploymentContent") && isActive("studentContent")) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.student(true)+arrow+breadLinks.studentEmployment());
        }
        if(isActive("studentTrainingContent") && isActive("studentContent")) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.student(true)+arrow+breadLinks.studentTraining());
        }
        if(isActive("studentSoftwareContent") && isActive("studentContent")) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.student(true)+arrow+breadLinks.studentOffers());
        }
        if(isActive("studentLaptopContent") && isActive("studentContent")) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.student(true)+arrow+breadLinks.studentLaptop());
        }
        if(isActive("myframinghamContent")) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.myFramingham());
        }
        if(isActive("blackboardContent")) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.blackboard());
        }
        if(isActive("employeeContent") && getActiveSlides().length == 1) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.employee());
        }
        if(isActive("employeeSpotlight") && isActive("employeeContent")) {
            breadNav.html(breadLinks.main(true)+arrow+breadLinks.employee(true)+arrow+breadLinks.employeeSpotlight());
        }
    });
    
    //Control the slider via the dynamic breadcrumb nav
    $(document).on("click", "#breadNavStudents", function() {
        $("#studentWrapper").trigger("click");
        return false;
    });
    $(document).on("click", "#breadNavEmployee", function() {
        $("#employeeWrapper").trigger("click");
        return false;
    });
    $(document).on("click", "#breadNavMain", function() {
        if(isActive("myframinghamContent")) {
            $("#goDown").trigger("click");
        }
        else if(isActive("blackboardContent")) {
            $("#goUp").trigger("click");
        }
        else if(isActive("studentContent") && getActiveSlides().length == 1) {
            $("#studentWrapper").trigger("click");
            return false;
        }
        else if(isActive("employeeContent") && getActiveSlides().length == 1) {
            $("#employeeWrapper").trigger("click");
            return false;
        }
        else if(isActive("employeeContent") && getActiveSlides().length > 1) {
            var count = getActiveSlides().length;
            for(var i = 0; i < count; i++) {
                $("#employeeWrapper").trigger("click");
            }
            return false;
        }
        else if(isActive("studentContent") && getActiveSlides().length > 1) {
            var count = getActiveSlides().length;
            for(var i = 0; i < count; i++) {
                $("#studentWrapper").trigger("click");
            }
            return false;
        }
    });
    
    //Set both arrows opacity to 0
    $("#slideshow_left, #slideshow_right").fadeTo(0, 0);
    
    //Function for the left arrow click handler
    var clickLeft = function() {
        if(isActive("mainContent")) {
            $("#studentWrapper").trigger("click");
        }
        else if(isActive("employeeContent")) {
            $("#employeeWrapper").trigger("click");
        }
    }
    
    //Function for the right arrow click handler
    var clickRight = function() {
        if(isActive("mainContent")) {
            $("#employeeWrapper").trigger("click");
        }
        else if(isActive("studentContent")) {
            $("#studentWrapper").trigger("click");
        }
    }
    
    //Arrow event handlers
    $("#slideshow_left").on("click", clickLeft);
    $("#slideshow_right").on("click", clickRight);
    
    //Box event handlers
    $("#slideShowWrapper div").on("click", function() {
        var a = $(this);
        if(a.attr("id") == "studentWrapper" && !isActive("studentContent")) {
            setSlides("studentContent", true);
            hideLeft();
            $("#studentContent, #mainContent, #employeeContent").promise().done(function() {
                $("#mainContent").animate({"left":640}, 700);
                $("#studentContent").animate({"left":0}, 700);
            });
        }
        else if(a.attr("id") == "studentWrapper" && isActive("studentContent") && !isActive("studentEmploymentContent")&& !isActive("studentLaptopContent") && !isActive("studentSoftwareContent") && !isActive("studentTrainingContent")) {
            setSlides("mainContent", true);
            showLeft();
            $("#laptopContent, #studentSoftwareContent, #studentTrainingContent, #studentEmploymentContent").promise().done(function() {
                $("#mainContent").animate({"left":0}, 700);
                $("#studentContent").animate({"left":-640}, 700);
            });
        }
        else if(a.attr("id") == "studentWrapper" && isActive("studentContent") && isActive("studentLaptopContent")) {
            setSlides("studentContent", true);
            showRight();
            $("#studentContent").animate({"top":0}, 700);
            $("#laptopContent").animate({"top":360}, 700);
        }
        else if(a.attr("id") == "studentWrapper" && isActive("studentContent") && isActive("studentSoftwareContent")) {
            setSlides("studentContent", true);
            showRight();
            $("#studentSoftwareContent").animate({"top":360}, 700);
            $("#studentContent").animate({"top":0}, 700);
        }
        else if(a.attr("id") == "studentWrapper" && isActive("studentContent") && isActive("studentTrainingContent")) {
            setSlides("studentContent", true);
            $("#studentTrainingContent").animate({"top":360}, 700);
            $("#studentContent").animate({"top":0}, 700);
        }
        else if(a.attr("id") == "studentWrapper" && isActive("studentContent") && isActive("studentEmploymentContent")) {
            setSlides("studentContent", true);
            $("#studentEmploymentContent").animate({"top":360}, 700);
            $("#studentContent").animate({"top":0}, 700);
        }
        else if(a.attr("id") == "myframinghamWrapper" && !isActive("studentContent") && !isActive("employeeContent")) {
            setSlides("myframinghamContent", true);
            $("#studentContent, #mainContent, #employeeContent").promise().done(function() {
                $("#mainContent").animate({"top":360}, 700);
                $("#myframinghamContent").animate({"top":0}, 700);
                hideArrows();
            });
        }
        else if(a.attr("id") == "goDown" && isActive("myframinghamContent")) {
            setSlides("mainContent", true);
            $("#myframinghamWrapper, #mainContent").promise().done(function() {
                $("#mainContent").animate({"top":0}, 700);
                $("#myframinghamContent").animate({"top":-360}, 700);
                showArrows();
            });
        }
        else if(a.attr("id") == "blackboardWrapper" && !isActive("studentContent") && !isActive("employeeContent")) {
            setSlides("blackboardContent", true);
            $("#studentContent, #mainContent, #employeeContent").promise().done(function() {
                $("#mainContent").animate({"top":-360}, 700);
                $("#blackboardContent").animate({"top":0}, 700);
                hideArrows();
            });
        }
        else if(a.attr("id") == "goUp" && isActive("blackboardContent")) {
            setSlides("mainContent", true);
            $("#blackboardWrapper, #mainContent").promise().done(function() {
                $("#mainContent").animate({"top":0}, 700);
                $("#blackboardContent").animate({"top":360}, 700);
                showArrows();
            });
        }
        else if(a.attr("id") == "employeeWrapper" && !isActive("employeeContent") && !isActive("blackboardContent") && !isActive("myframinghamContent")) {
            setSlides("employeeContent", true);
            hideRight();
            $("#mainContent, #employeeContent").promise().done(function() {
                $("#mainContent").animate({"left":-640}, 700);
                $("#employeeContent").animate({"left":320}, 700);
            });
        }
        else if(a.attr("id") == "employeeWrapper" && isActive("employeeContent") && !isActive("employeeSpotlight")) {
            setSlides("mainContent", true);
            showRight();
            $("#employeeSpotlight, #employeeSpotlightContent").promise().done(function() {
                $("#mainContent").animate({"left":0}, 700);
                $("#employeeContent").animate({"left":960}, 700);
            });
        }
        else if(a.attr("id") == "employeeSpotlight" && isActive("employeeContent")) {
            setSlides(["employeeContent", "employeeSpotlight"] , true);
            hideRight();
            $("#mainContent, #employeeContent").promise().done(function() {
                $("#employeeContent").animate({"top":-360}, 700);
                $("#employeeSpotlightContent").animate({"top":0}, 700);
            });
        }
        else if(a.attr("id") == "employeeWrapper" && isActive("employeeContent") && isActive("employeeSpotlight")) {
            setSlides("employeeContent", true);
            hideRight();
            $("#employeeContent").animate({"top":0}, 700);
            $("#employeeSpotlightContent").animate({"top":360}, 700);
        }
        else if(a.attr("id") == "laptopWrapper" && isActive("studentContent")) {
            setSlides(["studentLaptopContent", "studentContent"], true);
            hideArrows();
            $("#studentContent").promise().done(function() {
                $("#studentContent").animate({"top":-360}, 700);
                $("#laptopContent").animate({"top":0}, 700);
            });
        }
        else if(a.attr("id") == "studentSoftware" && isActive("studentContent")) {
            setSlides(["studentContent", "studentSoftwareContent"], true);
            hideArrows();
            $("#studentContent").animate({"top":-360}, 700);
            $("#studentSoftwareContent").animate({"top":0}, 700);
        }
        else if(a.attr("id") == "studentTraining" && isActive("studentContent")) {
            setSlides(["studentContent", "studentTrainingContent"], true);
            hideArrows();
            $("#studentContent").animate({"top":-360}, 700);
            $("#studentTrainingContent").animate({"top":0}, 700);
        }
        else if(a.attr("id") == "studentEmployment" && isActive("studentContent")) {
            setSlides(["studentContent", "studentEmploymentContent"], true);
            hideArrows();
            $("#studentContent").animate({"top":-360}, 700);
            $("#studentEmploymentContent").animate({"top":0}, 700);
        }
    });
    
    //Student Resources Overlay
    $("#studentResources").on("click", function() {
        if(!checkStudentOverlays("studentResources") && !isStudentOverlayActive("studentResources")) {
            $("#resourcesOverlay").fadeIn(300);
            studentOverlays.studentResources = true;
        }
        else if(isStudentOverlayActive("studentResources")) {
            $("#resourcesOverlay").fadeOut(300);
            studentOverlays.studentResources = false;
        }
    });
    
    $("#resourcesOverlay .overlayClose").on("click", function() {
        $("#studentResources").trigger("click");
    });
    
    //Student Network Overlay
    $("#studentNetwork").on("click", function() {
        if(!checkStudentOverlays("studentNetwork") && !isStudentOverlayActive("studentNetwork")) {
            $("#networkOverlay").fadeIn(300);
            studentOverlays.studentNetwork = true;
        }
        else if(isStudentOverlayActive("studentNetwork")) {
            $("#networkOverlay").fadeOut(300);
            studentOverlays.studentNetwork = false;
        }
    });
    
    $("#networkOverlay .overlayClose").on("click", function() {
        $("#studentNetwork").trigger("click");
    });
    
    //Student Printing Overlay
    $("#studentPrinting").on("click", function() {
        if(!checkStudentOverlays("studentPrinting") && !isStudentOverlayActive("studentPrinting")) {
            $("#printingOverlay").fadeIn(300);
            studentOverlays.studentPrinting = true;
        }
        else if(isStudentOverlayActive("studentPrinting")) {
            $("#printingOverlay").fadeOut(300);
            studentOverlays.studentPrinting = false;
        }
    });
    
    $("#printingOverlay .overlayClose").on("click", function() {
        $("#studentPrinting").trigger("click");
    });
    
    //Employee Enrichment Overlay
    $("#employeeEnrichment").on("click", function() {
        if(!checkEmployeeOverlays("employeeEnrichment") && !isEmployeeOverlayActive("employeeEnrichment")) {
            $("#enrichmentOverlay").fadeIn(300);
            employeeOverlays.employeeEnrichment = true;
        }
        else if(isEmployeeOverlayActive("employeeEnrichment")) {
            $("#enrichmentOverlay").fadeOut(300);
            employeeOverlays.employeeEnrichment = false;
        }
    });
    
    $("#enrichmentOverlay .overlayClose").on("click", function() {
        $("#employeeEnrichment").trigger("click");
    });
    
    //Employee Program Overlay
    $("#employeeProgram").on("click", function() {
        if(!checkEmployeeOverlays("employeeProgram") && !isEmployeeOverlayActive("employeeProgram")) {
            $("#programOverlay").fadeIn(300);
            employeeOverlays.employeeProgram = true;
        }
        else if(isEmployeeOverlayActive("employeeProgram")) {
            $("#programOverlay").fadeOut(300);
            employeeOverlays.employeeProgram = false;
        }
    });
    
    $("#programOverlay .overlayClose").on("click", function() {
        $("#employeeProgram").trigger("click");
    });
    
    //Employee Technology Overlay
    $("#employeeTechnology").on("click", function() {
        if(!checkEmployeeOverlays("employeeTechnology") && !isEmployeeOverlayActive("employeeTechnology")) {
            $("#technologyOverlay").fadeIn(300);
            employeeOverlays.employeeTechnology = true;
        }
        else if(isEmployeeOverlayActive("employeeTechnology")) {
            $("#technologyOverlay").fadeOut(300);
            employeeOverlays.employeeTechnology = false;
        }
    });
    
    $("#technologyOverlay .overlayClose").on("click", function() {
        $("#employeeTechnology").trigger("click");
    });
    
    //Employee Resources Overlay
    $("#employeeResources").on("click", function() {
        if(!checkEmployeeOverlays("employeeResources") && !isEmployeeOverlayActive("employeeResources")) {
            $("#employeeResourcesOverlay").fadeIn(300);
            employeeOverlays.employeeResources = true;
        }
        else if(isEmployeeOverlayActive("employeeResources")) {
            $("#employeeResourcesOverlay").fadeOut(300);
            employeeOverlays.employeeResources = false;
        }
    });
    
    $("#employeeResourcesOverlay .overlayClose").on("click", function() {
        $("#employeeResources").trigger("click");
    });
    
    //Employee Initiatives Overlay
    $("#employeeInitiatives").on("click", function() {
        if(!checkEmployeeOverlays("employeeInitiatives") && !isEmployeeOverlayActive("employeeInitiatives")) {
            $("#initiativesOverlay").fadeIn(300);
            employeeOverlays.employeeInitiatives = true;
        }
        else if(isEmployeeOverlayActive("employeeInitiatives")) {
            $("#initiativesOverlay").fadeOut(300);
            employeeOverlays.employeeInitiatives = false;
        }
    });
    
    $("#initiativesOverlay .overlayClose").on("click", function() {
        $("#employeeInitiatives").trigger("click");
    });
    
    //Left arrow opacity and hover image
    $("#slideshow_left").hover(function() {
        if(left) {
            $(this).fadeTo(100, 1);
            $("#slideshow_left img").attr("src", "images/redesign/arrow_l_h.png");
        }
    }, function() {
        if(left) {
            $(this).fadeTo(100, 0);
            $("#slideshow_left img").attr("src", "images/redesign/arrow_l.png");
        }
    });
    
    //Right arrow opactiy and hover image
    $("#slideshow_right").hover(function() {
        if(right) {
            $(this).fadeTo(100, 1);
            $("#slideshow_right img").attr("src", "images/redesign/arrow_r_h.png");
        }
    }, function() {
        if(right) {
            $(this).fadeTo(100, 0);
            $("#slideshow_right img").attr("src", "images/redesign/arrow_r.png");
        }
    });
    
    //Down arrow hover image
    $("#goDown").hover(function() {
        $("img", this).attr("src", "images/redesign/arrow_d_h.png");
    }, function() {
        $("img", this).attr("src", "images/redesign/arrow_d.png");
    });
    
    //Up arrow hover image
    $("#goUp").hover(function() {
        $("img", this).attr("src", "images/redesign/arrow_u_h.png");
    }, function() {
        $("img", this).attr("src", "images/redesign/arrow_u.png");
    });
    
    //X button hover image
    $(".overlayClose").hover(function() {
        $(this).css({"background":"url(images/redesign/closeMe_b.png) no-repeat"});
    }, function() {
        $(this).css({"background":"url(images/redesign/closeMe.png) no-repeat"});
    });

});
