// ed_utils.js.php

var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

var editorLoaded = false;
var editorVisible = false;
var editorCanLiveConnect = true;
var editorCanReallyLiveConnect = false;
// Hook for Internet Explorer Windows
/*
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 
	  navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	  editorCanReallyLiveConnect = true;
}
*/

// NetScape 6.2.3 / OS X
// http://wp.netscape.com/download/archive/client_archive62x.html

function getFlashObj()
{
	var flashObj = null;
	if(document.embeds)
		flashObj = document.embeds['flash_editor'];
	if (!flashObj && document.all)
		flashObj = document.all['flash_editor'];
	if (!flashObj) {
		try {
			flashObj = InternetExplorer ? flash_editor : document.flash_editor;
		} catch(exception) {
			flashObj = null;
		}
	}
	if (!flashObj && document.getElementById)
		flashObj = document.getElementById('flash_editor');
	return flashObj;
}

function showHide_editor(showIt) {
	if(showIt) {
		if (!(layer = MM_findObj('content'))) return false;
		if (navigator.appName=="Netscape") {
			layer.style.height = '0px';	// must remain displayed
		} else {
			layer.style.display = 'none';
		}
	}
	if(showIt == editorVisible) return false;
	if (!(layer = MM_findObj('editor'))) return false;
	var edTitleDiv = MM_findObj('editor_title');
	var flashObj = getFlashObj();

	if(showIt) {
		if (navigator.appName=="Netscape" || !editorLoaded) {
			layer.style.width = layer.width = '100%';
			layer.style.height = layer.height = '500px';
			layer.style.marginBottom = '40px';
			if(flashObj) {
				flashObj.style.width = flashObj.width = '100%';
				flashObj.style.height = flashObj.height = '100%';
			}
		} else {
			layer.style.display = 'block';
		}
		if(edTitleDiv)
			edTitleDiv.style.display = 'block';
		if(flashObj) {
			if(flashObj.scrollIntoView)
				flashObj.scrollIntoView();
			if(flashObj.focus)
				flashObj.focus();
		}
	} else {
		if(edTitleDiv)
			edTitleDiv.style.display = 'none';
		if (navigator.appName=="Netscape") {
			layer.style.width = layer.width = '0px';
			layer.style.height = layer.height = '0px';
			layer.style.marginBottom = '0px';
			if(flashObj) {
				flashObj.style.width = flashObj.width = '0px';
				flashObj.style.height = flashObj.height = '0px';
			}
		} else {
			layer.style.display = 'none';
		}
	}
	editorVisible = showIt;
	return false;
}

// hideEditorLoadButtons is called repeatly on loading editor
var hideEditorLoadButtonsRepeat=0;	// to limit nb of repeated calls
function hideEditorLoadButtons() {
	var flashObj = getFlashObj();
	if(flashObj) if(typeof(flashObj.PercentLoaded)!="undefined") if(flashObj.PercentLoaded() == 100) if(typeof(flashObj.SetVariable)!="undefined") if(typeof(flashObj.TCallLabel)!="undefined")
	{
		flashObj.SetVariable("canLiveConnect", "true");
		flashObj.TCallLabel("/","hideLoadButtons");	// flash editor will call fscommand("liveconnect","ok");
//  alert('editor loaded');
		return;
	}
	if(hideEditorLoadButtonsRepeat > 60) {	// if did not load after 30 sec, stop
		return;
	}
	hideEditorLoadButtonsRepeat++;
	window.setTimeout("hideEditorLoadButtons()",500);	// retry every half sec
}

function load_editor() {
	if (!(IFrameObj = MM_findObj('RS_iframe'))) return false;
	if (!(IFrameDoc = getIFrameDoc(IFrameObj))) return false;

	showHide_editor(true);

	if(editorLoaded && !getFlashObj())
		editorLoaded = false;

	if (!editorLoaded) {
		editorLoaded = true;
		var args=load_editor.arguments;
		var url='/load_editor.php';
		var sep='?';
		url += sep+'PHPSESSID=6cd2115393ab226374c1a7cf330e89e8'; sep='&';
		if(args.length > 0)
			url += sep+args[0];
// alert("load_editor url=\'"+url+"\'");
		IFrameDoc.location.replace(url);
//		if (!(navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&  navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1))
		{
			hideEditorLoadButtonsRepeat=0;
			window.setTimeout("hideEditorLoadButtons()",500);	// will also test liveconnect between JS & flash
		}
	}
	return false;
}

// Handle all the the FSCommand messages in a Flash movie

function flash_editor_DoFSCommand(command, args) {
	//
	// call from SWF
	//
	var flashObj = getFlashObj();
	if (!flashObj) {
		return;
	}
	
	if(command == "liveconnect") {
		// msg from editor to test live connect is supported by browser & flash plugin
		editorCanReallyLiveConnect = true;
	}
}

// Hook for Internet Explorer Windows
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 
	  navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<scr' + 'ipt type="text/vbscript" language="VBScript"\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('Sub flash_editor_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call flash_editor_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('<\/scr' + 'ipt\> \n');
}

function placeImg(logoId,flashLabel) {
	if(!editorVisible) {
		load_editor();
	}
	var flashObj = getFlashObj();
	if (!flashObj) {
		window.setTimeout("placeImg('"+logoId+"','"+flashLabel+"')",200);
		return false;
	}
	if(flashObj.scrollIntoView)
		flashObj.scrollIntoView();
	if(flashObj.focus)
		flashObj.focus();
	if(typeof(flashObj.SetVariable)!="undefined") if(typeof(flashObj.TCallLabel)!="undefined")
	{
		flashObj.SetVariable("placedLogoId", logoId);
		flashObj.TCallLabel("/",flashLabel);
	}
	return false;
}

function placeLogo(logoId) {
	if (!(IFrameObj = MM_findObj('RS_iframe2'))) return false;
	if (!(IFrameDoc = getIFrameDoc(IFrameObj))) return false;
	IFrameDoc.location.replace('ed_setObjId.php?type=logo&objId='+logoId);
	return placeImg(logoId,"placeLogo");
}

function placeBackground(logoId) {
	if (!(IFrameObj = MM_findObj('RS_iframe2'))) return false;
	if (!(IFrameDoc = getIFrameDoc(IFrameObj))) return false;
	IFrameDoc.location.replace('ed_setObjId.php?type=vehicule&objId='+logoId);
	if (!editorLoaded) {
		return load_editor('backgroundId=' + logoId);
	}
	return placeImg(logoId,"placeBackground");
}

function chooseFont(fontId) {
	if (!(IFrameObj = MM_findObj('RS_iframe2'))) return false;
	if (!(IFrameDoc = getIFrameDoc(IFrameObj))) return false;
	IFrameDoc.location.replace('ed_setObjId.php?type=font&objId='+fontId);
	return placeImg(fontId,"chooseFont");
}

function editJob(jobId) {
	if (!editorCanReallyLiveConnect) {
		editorLoaded = false;	// force reload
	}
	if (!editorLoaded) {
		return load_editor('jobId=' + jobId);
	}
	return placeImg(jobId,"openJob");
}

function setEditorJobId(jobId) {
	var flashObj = getFlashObj();
	if (!flashObj) 	return false;
	if(typeof(flashObj.SetVariable)!="undefined")
		flashObj.SetVariable("jobId", jobId);
	return false;
}

function change_lang() {
	if(editorLoaded)
		return confirm('Warning, when changing the language, you will loose your composition');
	return true;
}
