
// Hook pour Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub GTScope_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call GTScope_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}

var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var GTScopeObj;
var iZoom = 0;
var maxRes = 1200;
var minRes = 0.15;
var curTab = 'divGeo';
var bWheelActive = true;
var myDate = new Date();
var iObjectId = myDate.getTime(); // identifiant unique à un instant t
var bIsMiniOverview = false;

// Code pour Navigateurs Mozilla : récupération de l'evt scroll molette
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
// Code pour Navigateurs Opera et IE : récupération de l'evt scroll molette
//if (window.opera)
window.onmousewheel = document.onmousewheel = wheel;

// Activation / désactivation du zoom molette
function activateWheel(bActivate)
{
	bWheelActive = bActivate;
}

// fonction de zoom in/out pour navigateur Mozilla (connexion à l'evt scroll avec molette)
function wheel(event)
{
	if (!bWheelActive)
		return ;
	var delta = 0;
	
	if (!event) // IE
		event = window.event;
	if (event.wheelDelta) // IE / Opera
	{
		delta = -event.wheelDelta / 40;
		if (window.opera)
			delta = -delta;
	}
	else if (event.detail) // Mozilla
	{
		delta = event.detail;
	}
	if (delta)
		zoomOffset(delta);
}


// zoom par offset : active le zoom progressif
function zoomOffset(offSet)
{
	var sValue;
	
	if (!bWheelActive)
		return ;
	sValue = iZoom;
	sValue *= 1.0 + offSet / 40.0;
	// arrondi à 2 chiffres après la vigule
	sValue = fixRes(sValue);
	iZoom = (sValue > minRes) ? sValue : minRes;
	if (iZoom != minRes)
		iZoom = (sValue < maxRes) ? sValue : maxRes;
	GTScopeObj.SetVariable("zoomTo", iZoom + ":1");
	//checkOverviewVisibility(iZoom);
	sValue = Math.sqrt((iZoom - minRes) / (maxRes - minRes));
	//$('debug1').innerHTML='Res : '+ iZoom;
	zoomCursorJS.setValue(sValue);
}


// Cette méthode force la résolution avec 2 chiffres après la virgule
// si elle n'est pas entière.
// NOTE : attention, il y a tout de même des erreurs de virgules flottantes.
function fixRes(vRes)
{
	return (Math.round(vRes * 100.0) / 100.0);
}


function processZoomEvent(vRes)
{
	var sValue;

	// arrondi à 2 chiffres après la virgule
	sValue = fixRes(vRes);
	if ((iZoom <= (sValue + 0.01)) && (iZoom >= (sValue - 0.01)))
		return ;
	iZoom = sValue;
	//checkOverviewVisibility(iZoom);
	sValue = Math.sqrt((iZoom - minRes) / (maxRes - minRes));
	//$('debug1').innerHTML ='Res : '+ iZoom;
	zoomCursorJS.setValue(sValue);
}


// fonction zoomToRes :
// appelée par la zoomBar
// Cette fonction désactive le zoom progressif
function zoomToRes(vRes)
{
	var sValue;
	
	// arrondi à 2 chiffres après la virgule
	sValue = fixRes(vRes);
	if ((iZoom <= (sValue + 0.01)) && (iZoom >= (sValue - 0.01)))
		return ;
	iZoom = sValue;
	GTScopeObj.SetVariable("zoomTo", iZoom);
	//checkOverviewVisibility(iZoom);
}


function checkOverviewVisibility(vRes)
{
	if (vRes > 400)
	{
		if (!bIsMiniOverview)
		{
			if ($('Overview').style.visibility == "visible")
			{
				$('Overview').style.visibility = "hidden";
				$('overviewalpha').style.visibility = "hidden";
			}
		}
		else
		{
			$('overviewminimized').style.visibility = "hidden";
		}
	}
	else
	{
		if (!bIsMiniOverview)
		{
			if ($('Overview').style.visibility == "hidden")
			{
				$('Overview').style.visibility = "visible";
				$('overviewalpha').style.visibility = "visible";
			}
		}
		else
		{
			$('overviewminimized').style.visibility = "visible";
		}
	}
}

function maximizeOverview()
{
	bIsMiniOverview = false;
	document.getElementById("overviewminimized").style.visibility = "hidden";
	document.getElementById("Overview").style.visibility = "visible";
	document.getElementById("overviewalpha").style.visibility = "visible";
}

function minimizeOverview()
{
	bIsMiniOverview = true;
	document.getElementById("Overview").style.visibility = "hidden";
	document.getElementById("overviewalpha").style.visibility = "hidden";
	document.getElementById("overviewminimized").style.visibility = "visible";
}

function activateFlashPan(bActivate)
{
	var iValue;
	
	if (bActivate)
		iValue = 1;
	else
		iValue = 0;
	GTScopeObj.SetVariable("enablePan", iValue);
}

function selectItems(strItems)
{
	GTScopeObj.SetVariable("selectItems", strItems);
}

function centerOnItem(iValue)
{
	GTScopeObj.SetVariable("centerOnItem", iValue);
}

function move(direction)
{
	//GTScopeObj.SetVariable("giveMeTheZoom", "");
	GTScopeObj.SetVariable("translateTo", direction);
}

// Gérer tous les messages FSCommand d'une animation Flash.
function GTScope_DoFSCommand(command, args)
{
	//alert("->" + command + " - " + args);
	switch (command)
	{
		case "debug":
			//alert("debug : "+args);
			break;
		case "zoomEvent":
			processZoomEvent(args);
			break;
		/*case "itemClicked":
			processClick(args);
			break;*/
		case "infoItem":
			showInfoItem(args);
			break;
		case "itemsList":
			refreshItemsList(args);
			break;
		case "showmenu":
			window.status="";
			break;
		case "aboutdlg":
			showAboutDlgWindow();
			break;
		default:
			alert("commande inconnue '" + command + "' - '" + args + "'");
	}
	return ;
}

function jumpTo(pos) {
//alert("calling jumpto");
	GTScopeObj.SetVariable("jumpTo", pos);
}

function layerSwitch(layerConf) {
//alert("calling layerSwitch");
	GTScopeObj.SetVariable("layerSwitch", layerConf);
}

function sourceSwitch(srcConf) {
//alert("calling layerSwitch");
	GTScopeObj.SetVariable("srcSwitch", srcConf);
}

function appearance(config) {
//alert("calling config");
	GTScopeObj.SetVariable("appearance", config);
}

function init() {
	GTScopeObj = isInternetExplorer ? document.all.GTScope : document.GTScope;
	initGraphicsComponents(true);
	GTScopeObj.SetVariable("giveMeTheZoom", "");
	//processZoomEvent(iZoom);
	setSplashWndVisibility(true);
	document.getElementById("ifdivGeo").src = "./TreeViewViews.htm";
}

function initGraphicsComponents(bInitLoad)
{
	var iHeight;
	var iWidth;
	var iMiddleH;
	var strHref;

	iHeight = getWindowHeight();
	iWidth = getWindowWidth();
	iMiddleH = iHeight / 2 - 20;
	document.getElementById('divHideButton').style.top = iMiddleH.toString() + "px";
	document.getElementById('divShowButton').style.top = iMiddleH.toString() + "px";
	$("if" + curTab).height = iHeight - 130;
	if (curTab == 'divList')
	{
		document.getElementById('ifdivList').contentWindow.resizeList(iHeight - 160);
	}
	if (bInitLoad)
	{
		GTScopeObj.SetVariable("scaleOffsetX", 300);
		strHref = document.location.search;
		strHref = strHref.substr(1, strHref.length - 1);
		// Si l'url contient des paramètres il faut essayer de mettre à jour la vue initiale
		if (strHref.length)
			setInitViewFromURLParams(strHref);
	}
	iWidth -= 220;
	iHeight -= 200;
	/*document.getElementById('Overview').style.left = iWidth.toString() + "px";
	document.getElementById('Overview').style.top = iHeight.toString() + "px";
	if (bInitLoad)
		document.getElementById('Overview').style.visibility = "visible";*/
}

function reinit()
{
	initGraphicsComponents(false);
	if (document.getElementById('divSplashWnd').style.visibility == "visible")
		setSplashWndVisibility(true);
}

function setInitViewFromURLParams(strParams)
{
	var urlParams;
	var curParam;
	var i;
	var strCenterX;
	var strCenterY;
	var strRes;

	strCenterX = "";
	strCenterY = "";
	strRes = "";
	urlParams = strParams.split('&');
	for (i = 0; i < urlParams.length; i++)
	{
		if (urlParams[i].indexOf("="))
		{
			curParam = urlParams[i].split("=");
			switch (curParam[0])
			{
				case "x":
					strCenterX = curParam[1];
					break;
				case "y":
					strCenterY = curParam[1];
					break;
				case "res":
					strRes = curParam[1];
					break;
			}
		}
	}
	if ((!strCenterX.length) || (!strCenterY.length) || (!strRes.length))
	{
		alert("aucun ou mauvais paramètre");
		return (false);
	}
	GTScopeObj.SetVariable("jumpTo", strCenterX + "," + strCenterY + "," + strRes);
	return (true);
}

function processClick(id) {
	window.status = "clic souris sur l'item " + id;
}

function showLayers(iState, layers){
    var layersArray = layers.split(",");
    var i;
    var lSwitch="";
	
      lSwitch = layersArray[0] + ":" + iState;
	  for (i = 1; i < layersArray.length; i++)
	  {
        lSwitch += "," + layersArray[i] + ":" + iState;
      }/**/
      layerSwitch(lSwitch);
}

function showSources(iState, sources){
	var sourcesArray = sources.split(",");
	var i;
	var lSwitch="";
	
	lSwitch = sourcesArray[0] + ":" + iState;
	for (i = 1; i < sourcesArray.length; i++)
	{
		lSwitch += "," + sourcesArray[i] + ":" + iState;
	}/**/
	sourceSwitch(lSwitch);
}

function showMenu(varMenu)
{
	//var divArray = new Array('divGeo', 'divSources', 'divLayers', 'divList');
	//var ifArray = new Array('./TreeViewViews.htm', './TreeViewSources.htm', './TreeViewLayers.htm', './TreeViewList.htm');
	var divArray = new Array('divGeo', 'divSources');
	var ifArray = new Array('./TreeViewViews.htm', './TreeViewSources.htm');
	var iHeight;
	
	for (i = 0; i < divArray.length; i++)
	{
		if (divArray[i] == varMenu)
		{
			// redimensionnement
			iHeight = getWindowHeight();
			$("if" + divArray[i]).height = iHeight - 130;
			if (varMenu == 'divList')
			{
				document.getElementById('ifdivList').contentWindow.resizeList(iHeight - 160);
			}
			if (document.getElementById("if" + divArray[i]).src.indexOf("loading.htm") > 0)
				document.getElementById("if" + divArray[i]).src = ifArray[i];
			// Affichage
			$(divArray[i]).style.visibility = "visible";
			$(divArray[i]).style.display = "block";
		}
		else
		{
			$(divArray[i]).style.visibility = "hidden";
			$(divArray[i]).style.display = "none";
		}
	}
	curTab = varMenu;
}

function getWindowHeight()
{
	var iHeight;

	if (navigator.appName.indexOf("Explorer") > 0)
	{
		// Internet Explorer
		if (document.documentElement && document.documentElement.clientHeight) // IE 6
			iHeight = document.documentElement.clientHeight;
	    else
			if (document.body && document.body.clientHeight) // IE 4+
				iHeight = document.body.clientHeight;
	}
	else
	{
		// !Internet Explorer -> Firefox, ...
		iHeight = window.innerHeight;
	}
	return (iHeight);
}

function getWindowWidth()
{
	var iWidth;

	if (navigator.appName.indexOf("Explorer") > 0)
	{
		// Internet Explorer
		if (document.documentElement && document.documentElement.clientWidth) // IE 6
			iWidth = document.documentElement.clientWidth;
	    else
			if (document.body && document.body.clientWidth) // IE 4+
				iWidth = document.body.clientWidth;
	}
	else
	{
		// !Internet Explorer -> Firefox, ...
		iWidth = window.innerWidth;
	}
	return (iWidth);
}

function setMenuVisibility(bVisible)
{
	if (bVisible)
	{
		if (document.getElementById('NavMenu').style.left == "10px")
			document.getElementById('NavMenu').style.left = "300px";
		document.getElementById("divMenu").style.visibility = "visible";
		document.getElementById("headerLeft").style.visibility = "visible";
		$(curTab).style.visibility = "visible";
		document.getElementById("divHideButton").style.visibility = "visible";
		document.getElementById("divShowButton").style.visibility = "hidden";
		// déplacement de l'échelle
		GTScopeObj.SetVariable("scaleOffsetX", 300);
	}
	else
	{
		document.getElementById("divMenu").style.visibility = "hidden";
		document.getElementById("headerLeft").style.visibility = "hidden";
		$(curTab).style.visibility = "hidden";
		document.getElementById("divHideButton").style.visibility = "hidden";
		document.getElementById("divShowButton").style.visibility = "visible";
		// déplacement de l'échelle
		GTScopeObj.SetVariable("scaleOffsetX", 10);
		if (document.getElementById('NavMenu').style.left == "300px")
			document.getElementById('NavMenu').style.left = "10px";
	}
}

function snapNavMenu()
{
	if (document.getElementById("divMenu").style.visibility == "visible")
		document.getElementById('NavMenu').style.left = "300px";
	else
		document.getElementById('NavMenu').style.left = "10px";
	document.getElementById('NavMenu').style.top = "10px";
}

function snapOverview()
{
	var iWidth;
	var iHeight;
	
	iHeight = getWindowHeight() - 200;
	iWidth = getWindowWidth() - 220;
	document.getElementById('Overview').style.left = iWidth.toString() + "px";
	document.getElementById('Overview').style.top = iHeight.toString() + "px";
}

function showInfoItem(args)
{
	if (args.indexOf("http://") > -1)
	{
		window.open(args);
	}
	else
	{
		document.getElementById("ifInfoItem").src = args;	
		setInfoVisibility(true);
	}
}

function setInfoVisibility(bVisible)
{
	if (bVisible)
	{
		document.getElementById("divInfos").style.right = 20;
		document.getElementById("divInfos").style.top = 60;
		document.getElementById("divInfos").style.visibility = "visible";
	}
	else
	{
		document.getElementById("divInfos").style.visibility = "hidden";
		document.getElementById("ifInfoItem").src = "./loading.htm";
	}
}

function refreshItemsList(args)
{
	if (document.getElementById('ifdivList'))
		document.getElementById('ifdivList').contentWindow.refreshItemsList(args);
}

// Affiche la fenêtre "A propos..."
function showAboutDlgWindow()
{
	setAboutWndVisibility(true);
}

function setAboutWndVisibility(bVisible)
{
	var iHeight;
	var iWidth;
	
	if (bVisible)
	{
		if (document.getElementById("ifAboutWnd").src.indexOf("loading.htm") > 0)
			document.getElementById("ifAboutWnd").src = "./AboutWnd.htm";
		iHeight = getWindowHeight() / 2 - 200;
		iWidth = getWindowWidth() / 2 - 300;
		document.getElementById("divAboutWnd").style.top = iHeight + "px";
		document.getElementById("divAboutWnd").style.left = iWidth + "px";
		
		//document.getElementById("divAboutWnd").Left = 500;
		document.getElementById("divAboutWnd").style.visibility = "visible";
	}
	else
	{
		document.getElementById("divAboutWnd").style.visibility = "hidden";
		document.getElementById("ifAboutWnd").src = "./AboutWnd.htm";
	}
}

function setSplashWndVisibility(bVisible)
{
	var iHeight;
	var iWidth;
	
	if (bVisible)
	{
		if (document.cookie == 'displaySplashScreen=0')
		{
			// Affichage du menu de contrôle
			document.getElementById('NavMenu').style.visibility = "visible";
			return ;
		}
		iHeight = getWindowHeight() / 2 - 200;
		iWidth = getWindowWidth() / 2 - 300;
		document.getElementById("divSplashWnd").style.top = iHeight + "px";
		document.getElementById("divSplashWnd").style.left = iWidth + "px";
		if (document.getElementById("ifSplashWnd").src.indexOf("loading.htm") > 0)
			document.getElementById("ifSplashWnd").src = "./splashWnd.htm";
		document.getElementById("divSplashWnd").style.visibility = "visible";	
	}
	else
	{
		// Sauvegarde de l'option d'affichage dans le cookie
		if (document.getElementById('ifSplashWnd').contentWindow.displayAtStartUp())
			SetCookie("displaySplashScreen", "1", 1);
		else
			SetCookie("displaySplashScreen", "0", 1);
		document.getElementById("divSplashWnd").style.visibility = "hidden";
		// Affichage du menu de contrôle
		document.getElementById('NavMenu').style.visibility = "visible";
	}
}

function SetCookie(cookieName, cookieValue, nDays)
{
	var today = new Date();
	var expire = new Date();
	
	if (nDays==null || nDays==0)
		nDays=1;
	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function showHelp()
{
	document.getElementById("ifInfoItem").src = 'help.htm';
	setInfoVisibility(true);
}

function showAlphaOverview(bShow)
{
	if (bIsMiniOverview)
		return;
	if (bShow)
	{
		if (document.getElementById("overviewalpha").style.visibility != "visible")
		{
			document.getElementById("overviewalpha").style.visibility = "visible";
			document.getElementById("overviewalpha").style.display = "block";
		}
	}
	else
	{
		if (document.getElementById("overviewalpha").style.visibility != "hidden")
		{
			document.getElementById("overviewalpha").style.visibility = "hidden";
			document.getElementById("overviewalpha").style.display = "none";
		}
	}
}
