var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var isSaved = "true";
var ignoreExit = false;
var returnVal = null;
var colorToggle = 0;
var flashProxy = new FlashProxy(uid, '/jswf/JavaScriptFlashGateway.swf');

window.onbeforeunload = confirmExit;

var wx = null;
function openWin(url,Width,Height) {
	wx = window.open(url,"","width="+Width+",height="+Height);
	setTimeout("checkPopup()", 1000);
}

function checkPopup() {
	if ( (wx == null) || (!wx) || (wx.closed) ) {
		alert("A popup blocker is not allowing toufee to work properly!\\n\\nPlease disable your popup blocker (or allow popups from toufee.com) for this function to work");
	}
}

function confirmExit()
{
	if ( ( !ignoreExit ) && (isSaved != "true") ) {
		if (window.confirm("Your Project is not currently saved. Do you want to save it now?")) {
			flashProxy.call("fnbtnCode", false, false, "on Page Unload");
			return "Please click *CANCEL*\nif you want to stay on this page and save your project";
		}
	}
}

var hintDiv = null;
var loadingBar = null;

function resetIgnoreExit() { 
	ignoreExit = 0;
}

function hidePopWin(e) {
	var x = document.getElementById('onPopupWin');
	var y = document.getElementById('imgadd');
	var z = document.getElementById('signupForm');

	if ( y ) y.src = '/cgi-bin/library.pl?screen=loading';
	if ( x ) x.style.display = 'none'; else alert('Cannot hide popup!');
	if ( z ) z.style.display = 'none'; else alert('Cannot hide popup!');

	window.onscroll = null;
	if ( !isIE5 ) { 
	   addEvent(window, "resize", null);
	   addEvent(window, "scroll", null);
	}
}

function abcd(args){
	var argz = args.split('::'); 
	var pointTo = argz[0];
	var width   = argz[1];
	var height  = argz[2];

	var ret = new Array();
	for (i=3;i<argz.length;i++) ret.push(argz[i]);
	returnVal	= ret.join('::');

	var x = document.getElementById('onPopupWin');
	var y = document.getElementById('imgadd');
	var w = parseInt(width) + 100;
	var h = parseInt(height) + 20;
	var T = document.body.clientWidth ? parseInt(document.body.clientWidth) : screen.width ? parseInt(screen.width) : 0;
	var S = document.body.clientHeight ? parseInt(document.body.clientHeight) : screen.height ? parseInt(screen.height) : 0;

	if ( x ) x.style.display = ''; else alert('Cannot create popup!');
	if ( y ) {
		y.src = '/cgi-bin/'+pointTo+'?sess='+Math.random();
		if ( w > 500 ) {
			y.style.width =  w + 'px';
			y.style.height = h + 'px';

			if ( T > 0 ) 
				y.style.left = Math.round( (T - w ) / 2 ) + 'px';

			if ( S > 0 )
				y.style.top = ( ( S < ( h + 100 ) ) ? ( S - ( h + 100) ) : '5' ) + 'px';						
		}
	}
	else 
		alert('cannot find popup frame');

	window.onscroll = noscroll;
	if ( !isIE5 ) { 
	   addEvent(window, "resize", noscroll);
	   addEvent(window, "scroll", noscroll);
	}
}

function doSignup(){
	var x = document.getElementById('signupForm');
	if ( x ) x.style.display = ''; else alert('Cannot create popup!');

	window.onscroll = noscroll;
	if ( !isIE5 ) { 
	   addEvent(window, "resize", noscroll);
	   addEvent(window, "scroll", noscroll);
	}
}

function noscroll() {
	document.body.scrollTop = 0;
	document.body.scrollLeft = 0;
	return false;
}

function instructionChange() {
	var x = document.getElementById('hintLine');
	if (x) {
		x.style.color = ( colorToggle++ % 2 ) == 0 ? 'blue' : 'red';
		setTimeout('instructionChange()', colorToggle > 4 ? 10000 : 350);
		if (colorToggle > 4) colorToggle = 0;
	}
}

function UITextLayers_DoFSCommand(command, args) {
	if(command=='hint') {
		if(!loadingBar) {
			loadingBar = document.getElementById('loadingBar'); 
			if (loadingBar) 
				loadingBar.style.display = 'none';
		}

		window.scrollTo(0,0);
		hintDiv = document.getElementById('tutorialBar');
		hintDiv.innerHTML = '<table border="0" align="left" cellpadding="0" cellspacing="0" width="733"><tr><td align="center" bgcolor="#E5ECF9"><font size=3><b>Tutorial Mode</b></font></td></tr><tr><td bgcolor="white"><img src="/otherimages/blank.gif" width="1" height="5"></td></tr><tr><td align=center style="font-size:20px;color:blue;text-decoration:underline;font-weight:bold" bgcolor="#E5ECF9" id="hintLine" name="hintLine">'+args+'</td></tr></table>';
		
		colorToggle = 0;
		setTimeout('instructionChange()', 350);
	}

	if(command=="saveMovie"){
		isSaved = args;
	}

   if(command=="tryintopop"){
		ignoreExit = 1;
		setTimeout('resetIgnoreExit()', 1000);
	}

	if(command=="focus"){
		window.focus();
	}

	if(command=="hidePopupLayer") {
		hidePopWin(false);
	}

	if(command=="showPopupLayer") {
		abcd(args);
	}

	if(command=="alert") {
		 if ( (args.toLowerCase().indexOf('saved') != -1 ) && dowarn ) {
			args = "Your project was saved successfully\n\nProjects saved in trial mode are purged two days after their creation (to save disk space). Please signup if you want to make this permanent.\n- Signup is free and effortless and only takes a minute.\n\nDo you want to save this project permanently?";
			if ( confirm(args) ) {
				doSignup()
			}
		 }
		 else {
				alert(args);
		 }
	}
}

function bb(imageID) {
	flashProxy.call("addImageToStage", imageID);
}

function whatif(wx) {
	flashProxy.call("addSanEvent", wx, 'click' );
}


