// JavaScript Document
var flashResizeTimeoutId = null;
var flashIsResizing = false;
var intervalId = setInterval("alertFromFlash", 1000, "...");

function getFlashMovie(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 { // if (navigator.appName.indexOf("Microsoft Internet")!=-1) 
		return document.getElementById(movieName);
	}
}

function selectInstitution(institutionName) {
	getFlashMovie("MediaBox").tellActionscriptToSelectInstitution(institutionName);
}

function setFlashVideoHeight(height) {
	if (flashIsResizing == false) {
		flashIsResizing = true;
		
		var swf = getFlashMovie("MediaBox");
		var steps = 20;
		var heightDifference = height - swf.offsetHeight;
		var startHeight = swf.offsetHeight;
		var radians;
		var radianStep = Math.PI / steps;
		var areaUnderCurve;
		var duration = 500;
		var durationDelta = duration / steps;
		var i = 0
		
		for (radians = 0; radians <= Math.PI; radians += radianStep) {
			i++;
			areaUnderCurve = heightDifference*(-1*Math.cos(radians) + 1)/2;
			setTimeout("setFlashVideoHeightTo(" + (startHeight + areaUnderCurve) + ");", durationDelta*i);
		}
		
		setTimeout("flashIsResizing = false;", duration);
	}
}

function setFlashVideoHeightTo(height) {
	var swf = getFlashMovie("MediaBox");	
	swf.height = height + "px";
}

function getFlashVideoHeight() {
	return getFlashMovie("MediaBox").offsetHeight;
}

function alertFromFlash(msg) {
	if (document.getElementById("receiveInput").checked == true) {
		var fa = document.getElementById("FlashAlerts");
		var msg = "<p>" + msg + "</p>";
		fa.innerHTML += msg;
		fa.scrollTop = 0;
		fa.scrollTop = fa.scrollHeight;
	}
}

function cookiesSupported() {
	return navigator.cookieEnabled;
}

function getConnectionSpeedCookie() {
	var connectionSpeed = readCookie("user_connection_speed");
	//if (connectionSpeed != "") setConnectionSpeeedCookie(connectionSpeed); //lengthen the cookie expiration date
	return connectionSpeed;
}

function setConnectionSpeedCookie(speed_kbps) {
	createCookie("user_connection_speed", speed_kbps, 60);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
