/*
 * variables/functions for menu dropdowns
 */

menu_active = new Array();
menu_timeoutArray = new Array();
menu_current = 0;
menu_last = 0;

function menu_getCoord(thisObj, axis) {
        // set coord to zero (default offset is zero for any object)
        var thisCoord = 0;
        while (thisObj.offsetParent) {
                if (axis == "x") {
                        thisCoord += parseInt(thisObj.offsetLeft);
                } else if (axis == "y") {
                        thisCoord += parseInt(thisObj.offsetTop);
                }
                thisObj = thisObj.offsetParent;
        }
        return thisCoord;
}

function menu_activateElement(menu_id) {
        // define the element locally
        var thisElement = document.getElementById('menu' + menu_id);
        if (!thisElement) {
                alert('menu' + menu_id);
        }
        // turn cell on
        newClass = thisElement.className.substr(0, thisElement.className.lastIndexOf('_')) + '_on';
        thisElement.className = newClass;
}

function menu_activateChild(menu_id) {
        // set as active
        menu_active[menu_id] = 1;
        // redefine cell
        var thisElement = document.getElementById('menu' + menu_id);
        // get submenu, if one exists
        var thisSubmenu = document.getElementById('menu_span' + menu_id);
        if (thisSubmenu) {
                var y_offset = 0;
                var x_offset = 0;
                // we manually move sub-submenus to the right of the submenu dropdown
                if (menu_id.indexOf('_') != menu_id.lastIndexOf('_')) {
                        x_offset = parseInt(thisElement.clientWidth) + 1; // add 1 for border width (js can't see borders)
                        y_offset = -parseInt(thisElement.clientHeight);
                }
                // change the offset based on browser (ff/ie/sf render borders differently)
                var myBrowser = browser_detect();
                if (myBrowser == "ff") {
                        x_offset -= 1;
                } else if (myBrowser == "ie") {
                        y_offset += 1;
                }
                // first set the coords
                thisX = parseInt(menu_getCoord(thisElement, 'x')) + x_offset;
                // TODO: y coord has stopped working due to js error
                /*
                if (menu_id.indexOf('_') != menu_id.lastIndexOf('_')) {
                var test = "";
                for (i in thisElement) { test += i + ":" + thisElement[i] + "<br>"; }
                document.write(test);
                return;
                }
                */
                thisY = parseInt(menu_getCoord(thisElement, 'y')) + parseInt(thisElement.clientHeight) + y_offset;
                // assign to the cell
                thisSubmenu.style.position = "absolute";
                thisSubmenu.style.display = "block";
                thisSubmenu.style.left = thisX + "px";
                thisSubmenu.style.top = thisY + "px";
        }
}

function menu_rollover(menu_id) {
        if (menu_last != menu_id) {
                clearInterval(menu_timeoutArray[menu_last]);
                menu_timeout(menu_last);
        }
        //alert("rollover");
        // activate element
        menu_activateElement(menu_id);
        // set element as current and last
        menu_current = menu_id;
        menu_last = menu_id;
        // activate child
        menu_activateChild(menu_id);
        // activate parents
        menu_activateParents(menu_id);
        // alert("Final coord: " + menu_getCoord('cell' + cell_id, 'y'));

}

function menu_rollout(menu_id) {
        //alert("rollout");
        // erase the current
        menu_current = 0;
        // set the item as inactive
        menu_active[menu_id] = 0;
        // begin timeout for rollout
        menu_timeoutArray[menu_id] = setTimeout("menu_timeout('" + menu_id + "')", 100);
}

function menu_deactivateElement(menu_id) {
        // define the element locally
        var thisElement = document.getElementById('menu' + menu_id);
        // turn cell on
        newClass = thisElement.className.substr(0, thisElement.className.lastIndexOf('_')) + '_off';
        thisElement.className = newClass;
}

function menu_deactivateChild(menu_id) {
        // get submenu, if one exists
        var thisSubmenu = document.getElementById('menu_span' + menu_id);
        if (thisSubmenu) {
                // assign style to the cell
                thisSubmenu.style.position = "absolute";
                thisSubmenu.style.display = "none";
        }
}

function menu_activateParents(menu_id) {
        var cellParents = menu_id.split('_');
        var thisParent = "";
        for (i in cellParents) {
                if (cellParents[i] != "") {
                        thisParent += '_' + cellParents[i];
                        var nextParent = thisParent + '_' + cellParents[parseInt(i)+1];
                        // if (nextParent == menu_id) {
                                menu_activateChild(thisParent);
                                menu_activateElement(thisParent);
                        // }
                }
        }
}

function menu_deactivateFull(menu_id) {
        // deactivate child
        menu_deactivateChild(menu_id);
        // deactivate the element
        menu_deactivateElement(menu_id);
}

function menu_timeout(menu_id) {
        if (menu_active[menu_id] == 0) {
                // deactive this element in full
                menu_deactivateFull(menu_id);
                if (menu_current == 0) {
                        menu_deactivateParents(menu_id);
                        // alert('no active element');
                }
        }
}

function menu_deactivateParents(menu_id) {
        var allCells = menu_id.split("_");
        var thisCell = "";
        for (var x = 1; x < allCells.length; x++) {
                thisCell += "_" + allCells[x];
                if (menu_id != thisCell) {
                        menu_deactivateFull(thisCell);
                }
        }
}

/*
 * variables/functions for main rollovers
 */

function main_rollover(imageName, instructionBox, x_offset, y_offset) {
        // get src
        thisSrc = document.images[imageName].src;
        thisPath = thisSrc.substring(0, thisSrc.indexOf('_off'));
        thisType = thisSrc.substr(thisSrc.lastIndexOf('.') + 1, 3);
        // set element
        thisElement = document.images[imageName];
        // switch image
        thisElement.src = thisPath + '_on.' + thisType;
        if (instructionBox != "") {
                thisBox = document.getElementById(instructionBox);
                if (thisBox) {
                        thisBox.style.display = "inline";
                        thisBox.style.position = "absolute";
                        // first get the coords
                        thisX = parseInt(menu_getCoord(thisElement, 'x')) + parseInt(thisElement.width) + x_offset;
                        thisY = parseInt(menu_getCoord(thisElement, 'y')) + parseInt(thisElement.height) + y_offset;
                        // now assign coords to box
                        thisBox.style.left = thisX;
                        thisBox.style.top = thisY;
                }
        }
}

function main_rollout(imageName, instructionBox) {
        // get src
        thisSrc = document.images[imageName].src;
        thisPath = thisSrc.substring(0, thisSrc.indexOf('_on'));
        thisType = thisSrc.substr(thisSrc.lastIndexOf('.') + 1, 3);
        // set element
        thisElement = document.images[imageName];
        // switch image
        thisElement.src = thisPath + '_off.' + thisType;
        if (instructionBox != "") {
                thisBox = document.getElementById(instructionBox);
                if (thisBox) {
                        thisBox.style.display = "none";
                }
        }
}

/*
 * basic browser detection
 */

function browser_detect() {
        var thisBrowser = navigator.userAgent;
        if (thisBrowser.indexOf("Firefox") != -1) {
                return "ff";
        } else if (thisBrowser.indexOf("MSIE") != -1) {
                return "ie";
        } else if (thisBrowser.indexOf("Safari") != -1) {
                return "sf";
        }
}

/*
 * variables/functions for word hunt
 */

function wordhunt_roll(dir) {
        // set id
        thisElement = document.getElementById('wordhunt_claimletter');
        thisElement.className = "wordhunt_claimletter_" + dir;
}

/*
 * message center
 */

function toggle_message_center() {
        messageCenter = document.getElementById('message_center');
        messageOn = document.getElementById('message_on');
        messageOff = document.getElementById('message_off');
        if (!messageCenter) {
                alert('There was an error loading the message center.\n\nPlease reload the page you are viewing.');
                return;
        } else {
                thisDisplay = messageCenter.className;
                if (thisDisplay == "messages_on") {
                        messageCenter.className = "messages_off";
                        messageOn.className = "message_main_off";
                        messageOff.className = "message_main_on";
                } else if (thisDisplay == "messages_off") {
                        messageCenter.style.position = "absolute";
                        messageCenter.style.top = 166;
                        messageCenter.style.left = 390;
                        messageCenter.className = "messages_on";
                        messageOn.className = "message_main_on";
                        messageOff.className = "message_main_off";
                }
        }
}

/*
 * quicklinks
 */

function myLinksAdd() {
        var thisPageName = document.mylinks_form.myLinks_pageName.value;
        if (thisPageName != "") {
                var requestData = "newpage=" + thisPageName + "&newurl=" + document.mylinks_form.myLinks_url.value;
                if (!ajax_sendRequest("_mylinks_add", requestData, "mylinks_success", "mylinks_failure")) {
                        alert("There was a problem adding your quicklink... please refresh this page and try again!");
                }
        } else {
                alert("Please enter a name for this page in the field shown.");
                document.mylinks_form.myLinks_pageName.focus();
        }
}

function myLinksToggle(newStyle) {
        var myLinks = document.getElementById('mylinks_bar');
        myLinks.style.display = newStyle;
}

/*
 * OLD JAVASCRIPT
 */

function rollover(img) {
        return;
	// document.images[img].src = "<?=$_TP?>/<?=$_TPSITE?>_btn_" + img + "_on.gif";
}

function rollout(img) {
        return;
	// document.images[img].src = "<?=$_TP?>/<?=$_TPSITE?>_btn_" + img + "_off.gif";
}

function confirmer(message) {
	if (confirm(message)) {
		return true;
	}
	return false;
}

function textLimit(field, maxlimit) {
	if (field.value.length > maxlimit) {
		// if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		alert("Please limit your text to less than " + maxlimit + " characters.");
	}
}

function displayScore(id) {
	window.open('http://www.student.com/scoreinfo.php?id='+id,'scoreinfo','scrollbars=no,location=no,width=500,height=300');
}

function showProfileWindow(profileName) {
	showProfilePage('profile.php',profileName);
	// window.profileWindow.focus();
}

function showProfilePage(page,profileName) {
	// campusHook = <?=SITE_CAMPUSHOOK?>;
	campusHook = false;
	if (campusHook) {
                return;
		// strip off the ".php" from the page name for the location bar
		// newLocation = "<?=$SCN_SITES['main_url']?>/profile/" + page + "?the_profile_name=" + profileName;
		// alert(newLocation);
		// document.location.href = newLocation;
	} else {
		profileWindow = window.open('http://www.student.com/profile/' + page + '?the_profile_name=' + profileName,'profileWindow','width=1005,height=610,scrollbars=yes,screenX=0,screenY=0,top=0,left=0');
		window.profileWindow.focus();
	}
}

function showControlPage(page) {
	showProfilePage(page,'');
	// showProfilePage(page,'<?=$the_login_name?>');
	// profileWindow = window.open('<?=$SCN_SITES['main_url']?>/profile/' + page,'profileWindow','width=1005,height=610,scrollbars=yes,screenX=0,screenY=0,top=0,left=0');
	// window.profileWindow.focus();
}

function whosonline() {
	window.open("http://www.student.com/whosonline.php","instaclient","width=750,height=550,scrollbars=no,screenX=0,screenY=0,top=0,left=0");
}

function whereiseveryone() {
        window.open("http://www.student.com/whereami.php","","width=525,height=500,status=yes,scrollbars=yes");
}

function toggleItem(id) {
	element = document.getElementById(id);
	element.className = (element.className.toLowerCase() == 'expanded'?'collapsed':'expanded');
}
