﻿//sInstructors should have the full name of each instructor
//  It should be initialized in the instructors file, not in this file.
var sInstructors = [];
var iCurrentInstructor;

//Call this function after all of the instructor div blocks 
//  have been instantiated.  An instructor can be called for
//  in the query string with the "instructor" key.
function initialize() {
    var args = [];
    var pair = [];
    var sCurrentInstructor = -1;
    
    //location.search has the query string including the '?'
    var q = location.search.substring(1, location.search.length);
    //check querystring and set the current instructor to that
    if (q.length > 0) {
        args = q.split('&');
        for (var i = 0; i < args.length; i++) {
            pair = args[i].split('=');
            if (pair[0] == "instructor" && pair.length == 2) {
                sCurrentInstructor = pair[1];
                iCurrentInstructor = getIndexFromID(sCurrentInstructor);
                break;
            }
        }
    }
    if (sCurrentInstructor == -1 ) {
        iCurrentInstructor = 0;
        sCurrentInstructor = getID(sInstructors[0]);
    }
    
    showInstructor(sCurrentInstructor);
}

//Show the bio and information of the instructor with the last name sName
function showInstructor(sName) {
    hideBio(getID(sInstructors[iCurrentInstructor]));
    iCurrentInstructor = getIndexFromID(sName);
    document.getElementById(sName).style.visibility = "visible";
    document.getElementById(sName).style.display = "block";
    document.getElementById("pic" + sName).style.border = "medium solid white";
}

//Hides the biography of the instructor
function hideBio(sName) {
    document.getElementById(sName).style.visibility = "hidden";
    document.getElementById(sName).style.display = "none";
    document.getElementById("pic" + sName).style.border = "medium solid #899498";
}

//returns the index of the instructor
//  whose id is sID (the last name)
function getIndexFromID(sID) {
    var names;  //holds title, first, and last names of one instructor
    for (var i = 0; i < sInstructors.length; i++) {
        names = sInstructors[i].split(" ");
        //if the last name matches the sID, the index has been found
        if (sID.toLowerCase() == names[names.length - 1].toLowerCase()) {
            return i;
        }
    }
    return 0;
}

//returns the last name of the instructor, which
//  should be the id of the instructor
function getID(strName) {
    var names = strName.split(" ");
    return names[names.length - 1].toLowerCase();
}

//moves all the images one to the left
function moveLeft() {
    hideBio(iCurrentInstructor);
    iCurrentInstructor--;
    if( iCurrentInstructor < 0 ) {
        iCurrentInstructor = sInstructors.length - 1;
    }
    setImages();
}

//moves all the images one to the right
function moveRight() {
    hideBio(iCurrentInstructor);
    iCurrentInstructor = Math.abs(iCurrentInstructor + 1) % sInstructors.length;
    setImages();
}

//determines the instructor who is on the left
function getLeftInstructor() {
    var l = iCurrentInstructor - 1;
    if( l < 0 ) {
        return sInstructors.length - 1;
    } 
    return l;                
}

//determines the instructor who is on the right
function getRightInstructor() {
    return Math.abs(iCurrentInstructor + 1) % sInstructors.length;
}

//sets the images for the entire picture-banner
//  Also displays the biography of the current instructor.
function setImages() {
    setImage("left",  getLeftInstructor() );
    setImage("center", iCurrentInstructor);
    setImage("right", getRightInstructor());

    document.getElementById(getID(sInstructors[iCurrentInstructor])).style.visibility = "visible";
    document.getElementById(getID(sInstructors[iCurrentInstructor])).style.display = "block";
}

//Sets the image of the instructor.  The image id is specified,
//  along with the index of the instructor.
function setImage(id, index) {
    document.getElementById(id).src = "images/instructors/".concat(getID(sInstructors[index])).concat(".jpg");
    document.getElementById(id).alt = sInstructors[index];
    document.getElementById(id).title = sInstructors[index];
}

