
/**
 * Flash communication functions
 */
var flashMovieName = '';
var flashAnimationStatusEnd = false;
var flashAnimationStatusStop = false;
var flashLocationInitiated = false;
var flashActive = false;

function setMovieName(name) {
	flashMovieName = name;
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1 && window[movieName]) {
		return window[movieName];
	} 
	else {
		return document[movieName];
	}
}

function flashReady() {
	
	if(!flashActive) {
		flashActive = true;
		if(document.getElementById('flashworld-wrapper')) {
			document.getElementById('flashworld-wrapper').style.backgroundImage = 'none';
			document.getElementById('flashworld-wrapper').style.backgroundColor = '#60070E';
		}
	}	
}

function sendToJavaScript(type, value) {
	
	flashReady();
	
	var dbgmsg = '';
	switch(type.toLowerCase()) {
		case 'animation':
			if(value.toLowerCase() == 'end' && flashAnimationStatusStop == false) {
				flashAnimationStatusEnd = true;
				//TODO: Animation end event handler (call next world)
			}
			else if(value.toLowerCase() == 'stop' && flashAnimationStatusStop == false) {
				flashAnimationStatusStop = true;
				//TODO: User-initiated break handler - stop all automatic handlers
			} else {
				dbgmsg = "[previously stopped by user interaction] ";
			}
			dbgmsg += value;
			break;
		case 'location':
			var newUrl = '';
			var newNodeID = 0;
			if(value[0]) {
				newURL = value[0];
			}
			if(value[1]) {
				newNodeID = value[1];
			}
			if(value[1] > 0) {
				dbgmsg = 'url: ' + value[0] + ' - id: ' + value[1];
				flashLocationInit = true;
				top.location.href = value[0];
			}
			break;
		default:
			return false;
			break;
	}
	flashDebug('sendToJavaScript: ' + type + ": " + dbgmsg);
	return true;
}

function sendToActionScript(value) {
	var movie = thisMovie(flashMovieName);
	flashDebug('sendToActionScript: ' + value);
	if (movie && movie.sendToActionScript != null) {
		movie.sendToActionScript(value);
		flashReady();
	}
}

function notifyFlashworldMovie(value) {
	setMovieName('phantasialand');
	sendToActionScript(value);
}

function flashDebug(string) {
	return;
	if(window.addEventListener) {
		if(console) {
			console.log(string);
		}
	}
	else {
		
		document.getElementById('result').style.display = "inline";
		document.getElementById('result').style.visibility = "visible";
		document.getElementById('result').innerHTML += string + "<br/>";
	}
}
/**
 * END::Flash communication functions
 */
