//NOTE:  ORDERING OF THESE CONSTANTS IS CRITICAL.  ALSO, INCREMENTS MUST BE BY 1.
//NEVER LET THE MINIMUM NUMBER OF THIS CONSTANT BE <1, OR UNDESIRABLE BEHAVIOR WILL RESULT.
//Content1.gif = Job Description
//Content2.gif = Steps to Becoming
//Content3.gif = Competencies
//Content4.gif = Resources
 var JOB_DESC = 1;
 var STEPS = 2;
 var COMPS = 3;
 var RES = 4;
 
 //This Variable is TRUE if it is an Internet build.  FALSE if it is an INTRANET build.  
 //This enables dynamic management of builds, since content can differ across the sites.
 var isInternet = true;
 
 //maximum number of competencies and resources on a page at a given time
 var MAXCOMPSPERPAGE = 5;
 var MAXRESPERPAGE = 5;

//Use this variable to manage the current selected position
 var currentJobDesc = ''; 
 
 window.onload = function() {onStart();};
 
 //These only work with IE5; Other browsers will just have to print "well enough"
 window.onbeforeprint = function() {beforePrint()};
 window.onafterprint = function(){afterPrint();};

//these variables are used to manage state only in the managePrint function
//the function manage the visibility of the overlay images, which the print.css cannot handle alongside the scripts.
 var objTop;
 var objHiDisplay;
 var objHereDisplay;
 function beforePrint(){
		var shiftTop =  -415; 	//the number of pixels that the top of the image changes between printing and browsing
		var objImg = $("imgRHS");
		objTop = objImg.style.top;
		objHiDisplay = $('imgMenuHighlight').style.display;
		objHereDisplay = $('imgHere').style.display;
		removeMenuHighlight();
		$('imgHere').style.display = 'none';	
		objImg.style.top = (parseInt(objImg.style.top.substr(0,3)) + shiftTop) + 'px';
	
}

function afterPrint(){
		$("imgRHS").style.top = objTop;
		$('imgMenuHighlight').style.display = objHiDisplay;
		$('imgHere').style.display = objHereDisplay;
}

 //Then, menu selection uses a query string with position.  This function manages that accordingly.
 //if no string, then we are on the default page, and don't want some of the interactive features, such as the lightbox popup
 //last step is to download and cache map images for better user experience.
 function onStart()
 {
		var url = document.location.toString();
		var idxPos = url.indexOf('Position=');
		if (idxPos > 0) {
			var jobDesc = url.substr(idxPos + 9);
			initLightbox();
			changeJob(jobDesc);
		}
		else{
			var imgContent = getContentImg();
			imgContent.src = 'images/ContentPanel/ContentDefault.gif';
		}
		
		preloadMapImages();
 }



//Returns the number of competencies associated with a given job.
//If job not found, returns 0
function getCompetencyCount(jobDesc)
{
	var compCount = 0;

	switch (jobDesc) {
		case 'H': compCount = 5; break;
		case 'C': compCount = 5; break;
		case 'S': compCount = 5; break;
		case 'BA': compCount = 5; break;
		case 'G': compCount = 5; break;
		case 'P': compCount = 5; break;
		case 'F': compCount = 5; break;
		case 'M': compCount = 5; break;
		case 'B': compCount = 5; break;
		case 'E': compCount = 5; break;
		case 'NE': compCount = 7; break;
		case 'CT': compCount = 7; break;
		case 'AE': compCount = 7; break;
		case 'KE': compCount = 10; break;
		case 'TL': compCount = 10; break;
		case 'FM': compCount = 9; break;
		case 'AM': compCount = 9; break;
		case 'AKM': compCount = 9; break;
		case 'KM': compCount = 9; break;
		case 'AGM': compCount = 9; break;
		case 'GM': compCount = 9; break;
		case 'TGM': compCount = 9; break;
		case 'EGM': compCount = 9; break;
		case 'AD': compCount = 10; break;
		case 'RM': compCount = 11; break;
		case 'RVP': compCount = 11; break;
	}

	return compCount;
}

//Returns the number of resources associated with a given job.
//If job not found, returns 0
function getResourceCount(jobDesc)
{
	var resCount = 0;

	switch (jobDesc) {
		case 'H': resCount = 17; break;
		case 'C': resCount = 17; break;
		case 'S': resCount = 17; break;
		case 'BA': resCount = 17; break;
		case 'G': resCount = 17; break;
		case 'P': resCount = 17; break;
		case 'F': resCount = 17; break;
		case 'M': resCount = 17; break;
		case 'B': resCount = 17; break;
		case 'E': resCount = 17; break;
		case 'NE': resCount = 17; break;
		case 'CT': resCount = 17; break;
		case 'AE': resCount = 17; break;
		case 'KE': resCount = 17; break;
		case 'TL': resCount = 17; break;
		case 'FM': resCount = 33; break;
		case 'AM': resCount = 33; break;
		case 'AKM': resCount = 33; break;
		case 'KM': resCount = 33; break;
		case 'AGM': resCount = 33; break;
		case 'GM': resCount = 33; break;
		case 'TGM': resCount = 33; break;
		case 'EGM': resCount = 33; break;
		case 'AD': resCount = 24; break;
		case 'RM': resCount = 24; break;
		case 'RVP': resCount = 24; break;
	}

	return resCount;
}

//This function manages the navigation of the content panel on the details page.
//It increments the Content.gif file by the value in the variable imgAdd
function changeContentPanel(imgAdd){
	var minImg = JOB_DESC;
	var maxImg = RES;
	var objImg = getContentImg();
	var imgCurrent = getContentSrc();
	var imgNo = getImgNo() + imgAdd;
	
	
	//if on minimum image page or on the default content page, the "back" button constitutes home page
	if (imgAdd == -1 && (imgCurrent.indexOf("ContentDefault") > 0 || imgNo == minImg - 1)){
		window.location = "Default.aspx";
	}
	else if (imgNo >= minImg && imgNo <= maxImg)
	{
		removeMenuHighlight();
		removeRHSText();
		
		if (imgNo == JOB_DESC)
			addLightbox(true, imgNo);
		else
			addLightbox(false, imgNo);
			
		dissolveImages(objImg,"images/ContentPanel/Content" + currentJobDesc + imgNo + ".gif",'12');
		
	} 
} 

//return the image object that represents the content panel
function getContentImg(){
	var img = $("imgContent");
	return img;
}

//parses which content panel image index we are using in this situation.  See comments for changeContentPanel.
//if unparsable/error, returns -1
function getImgNo(){
	var contentImg = getContentSrc();
	var myInt = parseInt(contentImg.charAt(contentImg.indexOf(".gif") - 1));
	if (isNaN(myInt))
		myInt = -1;
	
	return myInt;

}

//get the source property of the current content panel image
function getContentSrc(){
	var contentImg =  $("imgContent").src;
	return contentImg;
}


//changes the map panel so as to manage rollover effects
function changeMap(jobDesc){  
	if (jobDesc == '')
	{
		$("imgMap").src = "images/MapPanel/MapPanel.gif";
	}
	else
	{
		$("imgMap").src = "images/MapPanel/MapPanelRollover" + jobDesc + ".gif";   
	}
} 

//this function manages all changes in the current position selected
//topOffset and leftOffset refer to the position of You are Here, RELATIVE TO THE UPPER LEFT CORNER OF THE TABLE.
//I.E.  THE topOffset and leftOffset should align with one of the data points of the corresponding area map.
function changeJob(jobDesc){
	var imgContent = getContentImg();
	removeMenuHighlight();
	removeRHSText();
	currentJobDesc = jobDesc;
	$('imgCol').style.backgroundImage = "url('Images/ContentPanel/ContentPanelBG" + jobDesc + ".gif')";
	imgContent.src = "images/ContentPanel/Content" + jobDesc + "1.gif";
	
	//manage You Are Here location
	//These offset variables are designed to baseline the You Are Here ARROW with the top left corner of the table
	var topBaseline = -823;
	var leftBaseline = 0;
	var objArea = $("area" + jobDesc);
	var leftOffset = parseInt(getCoordinate(objArea,3));
	var topOffset = parseInt(getCoordinate(objArea,4));
	var topAdjust = mapAdjustment(jobDesc,false);
	var leftAdjust = mapAdjustment(jobDesc,true);
	var objHereImg = $("imgHere");
	overlayImage(objHereImg, leftBaseline + leftOffset + leftAdjust, topBaseline + topOffset + topAdjust);
	
	//manage benefits and development plan overlays
	addLightbox(true,JOB_DESC);
}

//manage benefits and development plan lightbox  overlays
//isBenefits = true if we are to show the benefits lightbox; false otherwise
//imgNo is the image number of the active content panel.  Not used in all cases.
function addLightbox(isBenefits, imgNo){
	var lBox = $('areaLB')
	if (isBenefits){
		var strPath = "Images/Benefits/";
		if (isManager(currentJobDesc)){
				lBox.href = strPath + currentJobDesc + "Benefits.gif";
		}
		else{
			lBox.href = strPath + "HourlyBenefits.gif";
		}
	}
	else{
		//EGM Steps to becoming is a special link to the EGM program
		if (currentJobDesc == 'EGM' && imgNo  == STEPS )
			lBox.href = "Documents/EGMProgram.pdf"
		else{
			var href = "Images/DevelopmentPlan";
			
			if (isManager(currentJobDesc)){
				if (currentJobDesc == "FM" || currentJobDesc == "AM" || currentJobDesc == "AKM")
					href = href + "Hourly";
				else
					href = href + "Mgr";
			}
			else
				href = href + "Hourly";
				
			if (isInternet)
				href = href + "Ext";
			
			lBox.href = href + ".gif"
		}
	}
}

//this just "fine tunes" the position of the image overlay, based on the job Desc.  It will return a value that should alter the overlay adjustment by the value returned.
//if isLeft = true, then left adjustment.  Else top adjustment
function mapAdjustment(jobDesc, isLeft){
	var leftAdjust = 0;
	var topAdjust = 0;
	var adjustment;
	
	switch(jobDesc)
	{
		case "G":
			topAdjust = 10;
			leftAdjust = -5;
			break;
		case "P":
			topAdjust = 10;
			leftAdjust = -5;
			break;
		case "F":
			topAdjust = 10;
			leftAdjust = -5;
			break;
		case "M":
			topAdjust = 10;
			leftAdjust = -5;
			break;
		case "B":
			topAdjust = 10;
			leftAdjust = -5;
			break;
		case "E":
			topAdjust = 10;
			leftAdjust = -5;
			break;
		case "CT":
			topAdjust = 5;
			leftAdjust = -10;
			break;
		case "AE":
			leftAdjust = -25;
			break;
		case "KE":
			leftAdjust = -25;
			break;
		case "TL":
			topAdjust = 5;
			leftAdjust = -30;
			break;
		case "FM":
			topAdjust = 5;
			leftAdjust = 5;
			break;
		case "AM":
			topAdjust = 10;
			leftAdjust = 10;
			break;
		case "AKM":
			leftAdjust = 10;
			break;
		case "KM":
			leftAdjust = 10;
			break;
		case "AGM":
			topAdjust = -20;
			leftAdjust = 10;
			break;
		case "GM":
			topAdjust = 10;
			leftAdjust = 5;
			break;
		case "TGM":
			topAdjust = -5;
			leftAdjust = 5;
			break;
		case "EGM":
			topAdjust = 5;
			leftAdjust = 10;
			break;
		case "AD":
			topAdjust = -10;
			leftAdjust = 5;
			break;
		case "RM":
			topAdjust = -15;
			leftAdjust = 10;
			break;
		case "RVP":
			topAdjust = 5;
			leftAdjust = 10;
			break;

	}
	
	if (isLeft) 
		adjustment = leftAdjust;
	else
		adjustment = topAdjust;
		
	return adjustment;
}	


//used on the resources and competencies windows to scroll down along a large number of items in the Left Hand navigation
//If isMore = True, then the "More" Button is clicked. else the "Back" button is clicked
function  manageLeftNavScroll(isMore){
	var imgNo = getImgNo();
	var contentImg = getContentImg();
	var lhsCount;
	var numScreens;
	var maxPerPage;
	
	if (imgNo == COMPS || imgNo == RES)
	{
		if (imgNo == COMPS) {
			lhsCount = getCompetencyCount(currentJobDesc);
			maxPerPage = MAXCOMPSPERPAGE;
		}
		else{
			lhsCount = getResourceCount(currentJobDesc);
			maxPerPage = MAXRESPERPAGE;
		}
		numScreens = Math.ceil(lhsCount/maxPerPage);
		if (numScreens > 1){
			var srcContent = getContentSrc();
			var screenNum = lhsScreenNum();
			var rhsIndex = getRHSIndex();

			if (screenNum == 0){
				if (isMore){
					removeMenuHighlight();
					dissolveImages(contentImg, "images/ContentPanel/ContentMore1" + currentJobDesc + imgNo + ".gif",'5'); //5 = Wipe Down
					screenNum = screenNum + 1;
				}
			}
			else {
				if (numScreens > (screenNum + 1) && isMore){
					removeMenuHighlight();
					dissolveImages(contentImg,"images/ContentPanel/ContentMore" + (screenNum + 1) + currentJobDesc + imgNo + ".gif",'5');//5 = Wipe Down
					screenNum = screenNum + 1;
				}
				else if (!(isMore)){
					removeMenuHighlight();
					if (screenNum == 1)
						dissolveImages(contentImg, "images/ContentPanel/Content" + currentJobDesc + imgNo + ".gif",'4'); //4 = Wipe up
					else
						dissolveImages(contentImg, "images/ContentPanel/ContentMore" + (screenNum - 1) + currentJobDesc + imgNo + ".gif",'4'); //4 = Wipe up						
					
					screenNum = screenNum - 1;
				}

			}
			//if select an item, then click more, then click back,(for example) want menu highlight to reappear in it's current position
			//wait 1 second to let transition effects do their thing.
			rhsIndex = getRHSIndex();
			if (rhsIndex > (maxPerPage*screenNum) && rhsIndex <= (maxPerPage*(screenNum + 1)))
				setTimeout("$('imgMenuHighlight').style.display = 'block'",1000);

		}
	}

}

//returns the (0-indexed!) screen number of either the Competency or Resources window.  This is to manage "More" and "Back" left-hand nav features.
//first competencies panel returns 0, when you click more, then this returns 1, if more again, then returns 2, etc....
//If not on the correct screen, returns -1
function lhsScreenNum(){
	var screenNum = -1;
	var srcContent = getContentSrc();
	var imgNo = getImgNo();
	if (imgNo == COMPS || imgNo == RES)
	{
		if (srcContent.indexOf("More") < 0)
			screenNum = 0;
		else
			screenNum = parseInt(srcContent.charAt(srcContent.indexOf("More") + 4));
	}

	return screenNum;
}

//removes the menu highlight from the screen
function removeMenuHighlight(){
	$("imgMenuHighlight").style.display = 'none';	
}

//removes the rhs text from the screen
function removeRHSText(){
	$("imgRHS").style.display = 'none';
}

//overlays the selected menu highlight on the selected menu item in the content panel
function overlayMenuHighlight(leftOffset, topOffset, imgWidth, imgHeight){
		var imgNo = getImgNo();
		if (imgNo == COMPS || imgNo == RES)
		{
			var topBaseline = -1090;
			var leftBaseline = 5;
			var objHiImg = $("imgMenuHighlight");
			objHiImg.height = imgHeight;
			objHiImg.width = imgWidth;
			changeOpacity(50, "imgMenuHighlight");
			overlayImage(objHiImg,leftBaseline + leftOffset,topBaseline + topOffset);
		}
}

//Shows the content in the RHS of the content panel (where applicable), based on the selected index (1-based) of the LHS navigation.
//if successful, it then overlays the selected menu highlight on the selected menu item in the content panel, based on the parameters given
function showRHSText(lhsIndex,hiLeftOffset, hiTopOffset, hiWidth, hiHeight){
	var screenNum = lhsScreenNum();
	var imgIndex;
	var lhsCount;
	
	if (screenNum >= 0){
		var imgNo = getImgNo();
		var imgRHS = $("imgRHS");
		var leftOffset = 278;
		var topOffset = -643;
		if (imgNo == COMPS){
			imgIndex = (MAXCOMPSPERPAGE * screenNum) + lhsIndex;
			lhsCount = getCompetencyCount(currentJobDesc);
			if (lhsCount >= imgIndex){
				imgRHS.src = "images/Competencies/CompText" + currentJobDesc + imgIndex + ".gif";
				overlayImage(imgRHS, leftOffset,topOffset);
				overlayMenuHighlight(hiLeftOffset, hiTopOffset, hiWidth, hiHeight);
			}
		}
		else if (imgNo == RES){
			imgIndex = (MAXRESPERPAGE * screenNum) + lhsIndex;
			lhsCount = getResourceCount(currentJobDesc);
			if (lhsCount >= imgIndex){
				imgRHS.src = "images/Resources/ResText" + currentJobDesc + imgIndex + ".gif";
				overlayImage(imgRHS, leftOffset,topOffset);
				overlayMenuHighlight(hiLeftOffset, hiTopOffset, hiWidth, hiHeight);
			}
		}
	}
}

//this function returns the image index of the current content in the RHS Text
//If RHS is not currently selected,or we are in the wrong spot on the content page, this returns -1.
function getRHSIndex(){
	var rhsIndex = -1;
	var imgRHS = $("imgRHS");
	
	if (imgRHS.style.display != 'none')
	{
		rhsIndex =  parseInt(imgRHS.src.substr(imgRHS.src.indexOf(".gif") - 2, 2));
		if (isNaN(rhsIndex))
			rhsIndex = parseInt(imgRHS.src.charAt(imgRHS.src.indexOf(".gif") - 1));
	}
	return rhsIndex;
}

//create the menu that shows all of the available positions and positions it accordingly
//left coordinate is determined with the following formula:
//(screenWidth - tableWidth)/2 + LeftCoordAreaMap + LeftIndent + 2 * BorderWidth + MapImageWidth
//=(screenWidth - 1000)/2 + 441 + 34 + (2 * 6) + 335 = (screenWidth - 1000)/2 + 822
function createMenu() {
	var wid = document.body.clientWidth;
	var left = (wid - 1000) / 2 + 822;
	var objMenu = $('menuPosition');
    objMenu.style.top = '158';       
    objMenu.style.left = left;
    objMenu.style.width = '150';
	objMenu.style.display='inline';
}	

function hideMenu()
{
	$('menuPosition').style.display='none';
}
	
//returns true if the job description is a manager or multi-unit manager, false otherwise
//if job desc not known, also returns false.
function isManager(jobDesc){
	var blnMgr = false;
	var pos = jobDesc.length - 1;
	
	if (pos > 0 && (jobDesc.charAt(pos) == 'M' || jobDesc == 'RVP' || jobDesc == 'AD'))
		blnMgr = true;
		
	return blnMgr;
}

//Show all of the resources or competencies in downloadable/savable format.
function showAll(){
	var imgNo = getImgNo();
	var docPath = "Documents/"
	
	if (imgNo == JOB_DESC)
		window.location = docPath + "LQPositionProfiles.pdf";
	else if (imgNo == STEPS)
		window.location = docPath + "LQSteps.pdf";
	else if (imgNo == COMPS){
		//remove everything but the line of code beneath the else statement when you finally get your 
		//positional competencies.
		if (currentJobDesc == 'CT' || currentJobDesc == 'AE' || currentJobDesc == 'KE' || currentJobDesc == 'NE')
			window.location = docPath + "LQCompetenciesCT.pdf";
		else if (isManager(currentJobDesc)){
			if (currentJobDesc == 'RVP' || currentJobDesc == 'RM' || currentJobDesc == 'AD') 
				window.location = docPath + "LQCompetencies" + currentJobDesc + ".pdf";
			else
				window.location = docPath + "LQCompetenciesMGR.pdf";
		}
		else
			window.location = docPath + "LQCompetencies" + currentJobDesc + ".pdf";
	} 
	else if (imgNo == RES){
		if (isManager(currentJobDesc)) {
			if (currentJobDesc == 'RVP' || currentJobDesc == 'RM' || currentJobDesc == 'AD')
				window.location = docPath + "LQMultiUnitResourceGuide.pdf";
			else
				window.location = docPath + "LQMgrResourceGuide.pdf";
		}
		else
			window.location = docPath + "LQHourlyResourceGuide.pdf";
	}
}

