// Function to get the Flash movie object so I can runactionscript functions in Flash from javascript.
function getFlashMovieObject(movieName) {
	if (window.document[movieName])
		return window.document[movieName];
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	} else {
		return document.getElementById(movieName);
	}
}

// Function to open up a window with a list of sub specialties.
function openSubSpecs(specialtyCategoryId) {
	var ps = new proxyService();
	
	// Setup ajaxproxy to get the count of the specialty area selected.
	ps.setHTTPMethod("post");
	ps.setCallbackHandler(retrieveJobCount);
	ps.setErrorHandler(myErrorHandler);
	try {
		ps.GetJobSpecialtyCount(specialtyCategoryId);
	}
	catch(err){
		window.alert(err);
		window.alert("An unknown error prevented the GetJobSpecialtyCount request from being processed.");
	}
		
	return false;
}

// Resizes cfwindow cfWinName to the size of cfWinDIVName, constrained by the min/max set inside function
function resizeToFit(cfWinName, cfWinDIVName) {
	var maxWidth = 820, maxHeight = 600;
	var minWidth = 450, minHeight = 100;
	var displayDiv, displayHeight, displayWidth;
	var win = ColdFusion.Window.getWindowObject(cfWinName);
	
	if (arguments.length > 2) {
		minHeight = arguments[2];
		if (arguments.length > 3) {
			minWidth = arguments[3];
			if (arguments.length > 4) {
				maxHeight = arguments[4];
				if (arguments.length > 5) {
					maxWidth = arguments[5];
				}
			}
		}
	}
	win.resizeTo(minWidth,minHeight);

	displayDiv = document.getElementById(cfWinDIVName);
	if (displayDiv) {
		displayHeight = displayDiv.offsetHeight + 50;
		displayHeight = Math.min(maxHeight,Math.max(minHeight,displayHeight));
		displayWidth = displayDiv.scrollWidth + 50;
		displayWidth = Math.min(maxWidth,Math.max(minWidth,displayWidth));

		win.resizeTo(displayWidth,displayHeight);
	}
}

// Function to open up a window with an OSG video.
function showOsgVideo(videoId) {
	var ps = new proxyService();
	
	// Setup ajaxproxy to get the count of the specialty area selected.
	ps.setHTTPMethod("post");
	ps.setCallbackHandler(retrieveOsgVideo);
	ps.setErrorHandler(myErrorHandler);
	try {
		ps.GetOsgVideo(videoId);
	}
	catch(err){
		window.alert(err);
		window.alert("An unknown error prevented the GetOsgVideo request from being processed.");
	}
		
	return false;
}

// Function to show the full personal photo in a modal window.
function showPhoto() {
	resizeToFit("photoWindow", "photoCfWindow");
	ColdFusion.Window.show("photoWindow");

	return false;
}

// Function to show the full quote of the testimonial in a modal window.
function showTestimonial() {
	resizeToFit("testimonialWindow", "testimonialCfWindow", 100, 450, 600, 600);
	ColdFusion.Window.show("testimonialWindow");
	ColdFusion.Window.onHide("testimonialWindow", cleanupTestimonial);

	return false;
}

//Function to create a graphical mouse rollover.
function swapImage(imageName, graphicSource) {
	document.getElementById(imageName).src = graphicSource;
}

function uploadPhoto() {
	// TODO:Dislpay busy time graphic.
	return false;
}

// Handlers
function cleanup() {
    ColdFusion.Window.destroy("showSpecialties", true);
}
function cleanupOsg() {
    ColdFusion.Window.destroy("showVideo", true);
}
function cleanupTestimonial() {
    var flashMovie = getFlashMovieObject("testimonial");
	flashMovie.resetFlash();
}

retrieveJobCount = function(result) { // result[0] is the SpecialtyCategoryID, result[1] is the record count, and result[2] is the Specialty.
	if (result[1] == 1) { // Go directly to career center.
		location.href="/CareerCenter/FindSpecialty/" + result[2];
	} else { // Display window.
		try {
			ColdFusion.Window.destroy("showSpecialties", true);
		} catch(e) { }
		
		ColdFusion.Window.create("showSpecialties", "Select a Specific Specialty", "_jobs.cfm?specCatId=" + result[0],
			{callbackHandler:specialtyBreakdownCreated,center:true,draggable:false,modal:true,resizable:false,width:815,destroyOnClose:true});
		ColdFusion.Window.onHide("showSpecialties", cleanup);
	}
}

retrieveOsgVideo = function(result) { // result[0] is the Name, result[1] is the Length, and result[2] is the Title.
	// Display window.
	try {
		ColdFusion.Window.destroy("showVideo", true);
	} catch(e) { }
	
	ColdFusion.Window.create("showVideo", result[2], "_video.cfm?name=" + result[0] + "&len=" + result[1],
		{callbackHandler:function(){osgVideoCreated(result[0], result[1])},center:true,draggable:false,modal:true,resizable:false,width:400,height:400,destroyOnClose:true});
	ColdFusion.Window.onHide("showVideo", cleanupOsg);
}

//Error handler for the asynchronous functions.
myErrorHandler = function(statusCode, statusMsg) {
	window.alert('Status: ' + statusCode + ', ' + statusMsg);
}

osgVideoCreated = function(vidName, vidLen) {
	showVid(vidName, vidLen);
}

specialtyBreakdownCreated = function() {
	resizeToFit("showSpecialties", "specialtyBreakdown");
}

closeOsgVideo = function() {
	ColdFusion.Window.destroy("showVideo");

	return false;
}

function toggleDiv(divId) {
	var el = document.getElementById(divId);

	if (el.style.display == "none") {
		el.style.display = "block";
	} else {
		el.style.display = "none";
	}
	
	return false;
}

// Function to submit contact us pages through a link instead of a form button to reduce bot submits.
function submitContactForm() {
	var form = document.getElementById("contactForm");
	
	form.submit();
	
	return false;
}

// Function for custom sourcing free - plan hide div for radio button
function showPosition(){
	document.getElementById("position").style.display="block";
}
function hidePosition(){
	document.getElementById("position").style.display="none";
}

