var g_username = "Gast";

function closeChat()
{
	var popup = getDivPopup("webcamPopup");
	var flash = getFlash(popup);
	flash.CallFunction("<invoke name=\"userLeaves\" returntype=\"javascript\"></invoke>");
	updateAvailability();
}

function authorizedCallback(name, success)
{
	if (success)
	{
		var popup = divPopup("webcamPopup", null, null, "Chat", null,
		{
			onClose: closeChat,
			noIframe: true,
			noOuterDiv: true
		});
		
		var innerPopup = popup.firstChild.childNodes[1];
		createFlash(innerPopup, "player.swf", 660, 625, {username: g_username, streamServer: g_streamServer, sessionId: g_streamSessionId, client: true});
	}
	else
	{
		alert("Webcam is niet beschikbaar");
	}
}

var onUsername;
function openChat()
{
	var inputs = [];
	var popup = divPopup("usernamePopup", null, null, "Chat", null,
	{
		noOuterDiv: true
	});
	
	onUsername = function()
	{
		if (inputs[0].value == "")
		{
			alert("U heeft geen naam opgegeven.");
			inputs[0].focus();
		}
		else
		{
			g_username = inputs[0].value;
			hideDivPopup("usernamePopup");
			xajax_authorize("webcam", "authorizedCallback");
			updateAvailability();
		}
	}
	
	dom_getElementsByTagName(popup, "INPUT", inputs);
	inputs[0].focus();
	return;
}

function serverLeft()
{
	return;
	
	hideDivPopup("webcamPopup");
	updateAvailability();
}

function isAvailableCallback(name, status)
{
	var div = document.getElementById(name);	
	if (!div)
		return;
	var available = status == "free";
	div.childNodes[0].style.display = available ? "" : "none";
	div.childNodes[1].style.display = !available ? "" : "none";
	var message = "";
	switch(status)
	{
		case "free":
			message = "De webcam is Online";
			break;
		case "occupied":
			message = "Momenteel helaas Bezet";
			break;
		case "not_available":
			message = "Helaas Offline";
			break;
	}
	div.childNodes[2].innerHTML = message;
}

var availabilityTimer = null;
function updateAvailability()
{
	if (availabilityTimer == null)
	{
		availabilityTimer = window.setInterval(updateAvailability, 5000);	
	}
	
	xajax_isAvailable("webcam", "isAvailableCallback");	
}

function resizeWinTo() {

	if( !document.images.length )

				{ document.images[0] = document.layers[0].images[0]; }

	var oH = document.images[0].height, oW = document.images[0].width;

   

	if( !oH || window.doneAlready )

				{ return; } //in case images are disabled

	window.doneAlready = true; //for Safari and Opera

	var x = window;

	x.resizeTo( oW + 200, oH + 200 );

	var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;

   

	if( x.innerWidth )

				{ myW = x.innerWidth; myH = x.innerHeight; }

	else if( d && d.clientWidth )

				{ myW = d.clientWidth; myH = d.clientHeight; }

	else if( b && b.clientWidth )

				{ myW = b.clientWidth; myH = b.clientHeight; }

	if( window.opera && !document.childNodes )

				{ myW += 16; }

	x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );

	var scW = screen.availWidth ? screen.availWidth : screen.width;

	var scH = screen.availHeight ? screen.availHeight : screen.height;

	if( !window.opera )

				{ x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }

}


