var documentCarto = null;
var windowCarto = null;
window.onresize = stretchBackground;

function stretchBackground()
{
	var mySize = sizeInnerScreen(),
		bg = document.getElementById('PageBackground');
	
	bg.style.width = (mySize[0]) + "px";
	bg.style.height = (mySize[1]) + "px";
}
function showOpacity(obj, state)
{
	if (state==1)
	{
		obj.style.opacity = '1.0';
		obj.style.filter = 'alpha(opacity=100)';
	}
	else
	{
		obj.style.opacity = '0.3';
		obj.style.filter = 'alpha(opacity=30)';		
	}
}
function startup ()
{
	setInterfaceCarto();
	if (documentCarto == null)
	{
		setTimeout ("startup()", 1000);
	}
	else
	{
		giController.callService('LEGEND');
		cartoEvent();
	}
}
function openCloseNav(obj)
{
	var refStyle = document.getElementById('reference').style;
	if ((refStyle.display == 'block')||(refStyle.display == ''))
	{
		refStyle.display = 'none';
		obj.className=obj.id+'Right';
	}
	else
	{
		refStyle.display = 'block';
		obj.className=obj.id+'Left';
	}
}
function OpenClose(obj,pShowMask,pMasked) {
	var showMasked = pShowMask.split(":"),
		i, lim, e, oc;
	for(i = 0, lim = showMasked.length; i < lim; i++) {
		e = document.getElementById(obj+showMasked[i]);
		// Image "Open/close"
		oc = document.getElementById('OpenClose'+showMasked[0]);
		if (e.style.display != "none")
		{
			e.style.display = "none";
			oc.src = oc.src.replace('close.gif','open.gif'); 
		}
		else
		{
			e.style.display = "";
			oc.src = oc.src.replace('open.gif','close.gif');			
		}
	}
	closeMenu(obj, pMasked);
}
function closeMenu(obj, pMasked) {
	var masked = pMasked.split(":"),
		i, lim, e;
	for(i = 0, lim = masked.length; i < lim; i++){
		if (e = document.getElementById('OpenClose'+masked[i]))
		{
			e.src = e.src.replace('close.gif','open.gif');
		}
		if ((e = document.getElementById(obj+masked[i])) && (e.style.display != "none"))
		{
			e.style.display = "none";
		}	
	}
}
/*-----------------------------------
declaration des evenement souris specialises sur la carte
*/
function cartoEvent()
{
	documentCarto.onmousemove = cartoMouseMove;
	documentCarto.onmouseout = cartoMouseOut;
}
function cartoMouseMove(e){
	windowCarto.TSCoordonnees_mousemove(e);
	windowCarto.TSMinifiche_mousemove(e);
}
function cartoMouseOut(e){
	windowCarto.TSMinifiche_mouseout(e);
}
function	setInterfaceCarto()
{
	if (documentCarto == null || windowCarto == null)
	{
		documentCarto = document.getElementById('CARTO').contentWindow.document;
		windowCarto = document.getElementById('CARTO').contentWindow;
	}
}
/*-----------------------------------
fin declaration des evenement souris specialises sur la carte
*/

function sizeInnerScreen()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return [myWidth,myHeight];
}

function OverToolMenu(obj)
{				
	obj.className = 'menuContentImgActive';			
}
function OutToolMenu(obj)
{
	if (currentTool == obj.id) {
		obj.className = 'menuContentImgActive';
	}
	else
	{
		obj.className = 'menuContentImgUnActive';
	}
}

function ToggleTool(obj)
{
	if (obj.className == 'toolUnActive') {
		obj.className = 'toolActive';
	}
	else
	{
		obj.className = 'toolUnActive';
	}
}
function ActiveTool(obj)
{
	if (currentTool != '') {
		oldTool = currentTool;
		document.getElementById(currentTool).className = 'toolUnActive';
	}
	if (obj == '')
	{
		currentTool = '';
	}
	else if (obj != null)
	{
		currentTool=obj.id;
		document.getElementById(currentTool).className = 'toolActive';
	}	
}
function verifToolEdit()
{
	giController.callService('CARTO_EDITCLEAR'); 
	
	if (currentTool == 'EditAdd')
	{
		giController.callService('CARTO_EDITADD');
	}
}	
function ActiveEditTool(obj)
{
	interfaceCartoDoc = document.getElementById('CARTO').contentWindow.document;
	
	lstObjEdit = interfaceCartoDoc.getElementsByName('PS_LST_OBJ_EDI_MAPCITE')[0].value;	
	
	if (obj.id == 'EditSelAdd' || obj.id == 'EditSelAddPp' || obj.id == 'EditSelAddMm')
	{
		oneNew = false;
				
		if (lstObjEdit != '')
		{
			tabObjEdit = lstObjEdit.split('@');
			for(i=0;i<tabObjEdit.length;i++)
			{
				if (tabObjEdit[i] == '-1!-1')
				{
					oneNew = true;
					break;
				}
			}
		}
		
		choice = true;
		
		if (oneNew == true)
		{
			choice = confirm('Il existe des objets en cours de création. Voulez-vous interrompre la création ?\nTous les objets en crées seront perdus.');		
		}
		
		if (choice == true)
		{
			if (oneNew == true)
			{
				giController.callService('CARTO_EDITCLEAR');
			}
			if (obj.id == 'EditSelAdd')
			{
				ActiveTool(obj);
				giController.callService('CARTO_EDITSELADD');
			}
			else if (obj.id == 'EditSelAddPp')
			{
				ActiveTool(obj);
				giController.callService('CARTO_EDITSELADDPP');
			}
			else if (obj.id == 'EditSelAddMm')
			{
				ActiveTool(obj);
				giController.callService('CARTO_EDITSELADDMM');
			}
		}
	}
	else if (obj.id == 'EditAdd')
	{
		oneOld = false;
		
		if (lstObjEdit != '')
		{
			tabObjEdit = lstObjEdit.split('@');
			for(i=0;i<tabObjEdit.length;i++)
			{
				if (tabObjEdit[i] != '-1!-1')
				{
					oneOld = true;
					break;
				}
			}
		}
		
		choice = true;
		
		if (oneOld == true)
		{
			choice = confirm('Il existe des objets en cours de modification. Voulez-vous interrompre la modification ?\nTous les modifications seront perdu seront perdus.');		
		}
		
		if (choice == true)
		{
			if (oneOld == true)
			{
				giController.callService('CARTO_EDITCLEAR');
			}
			ActiveTool(obj);
			giController.callService('CARTO_EDITADD');
		}
	}
	else
	{
		alert('l\'outil choisit n\'est pas un outil d\'edition');
	}
}
function RestorTool()
{
	if (currentTool != '') {
		document.getElementById(currentTool).className = 'toolUnActive';
		if (oldTool != '') {
			ActiveTool(document.getElementById(oldTool));
		}
		else
		{
			currentTool = '';
		}
	}
}
function OverTool(obj)
{				
	obj.className = 'toolActive';			
}
function OutTool(obj)
{
	if ((currentTool != '')&&(currentTool == obj.id)) {
		obj.className = 'toolActive';
	}
	else
	{
		obj.className = 'toolUnActive';
	}
}
var currentTool = '';
var oldTool = '';



function ActiveTabs(tabOn, tabsOff) {
		
	currentTabs = tabOn;
	
	tabs = tabsOff.split(',');
	for(i=0;i<tabs.length;i++)
	{
		OutTabs(tabs[i]);
		document.getElementById('PanelMenuContent'+tabs[i]).className = 'menuContentHidden';
	}
	
	document.getElementById('PanelMenuContent'+tabOn).className = 'menuContentVisible';
	OverTabs(tabOn);
	
}
function OverTabs(tab)
{	
	document.getElementById('tbl0_Row0_Cell'+tab+'A').className = 'menuTabsBeforeActive';							
	document.getElementById('tbl0_Row0_Cell'+tab).className = 'menuTabsContentActive';
	document.getElementById('tbl0_Row0_Cell'+tab+'B').className = 'menuTabsAfterActive';
}
function OutTabs(tab)
{
	if (currentTabs == tab) {
		document.getElementById('tbl0_Row0_Cell'+tab+'A').className = 'menuTabsBeforeActive';							
		document.getElementById('tbl0_Row0_Cell'+tab).className = 'menuTabsContentActive';
		document.getElementById('tbl0_Row0_Cell'+tab+'B').className = 'menuTabsAfterActive';
		
		document.getElementById('tbl0_Row0T_Cell'+tab+'AA').className = 'menuContentTLActive';							
		document.getElementById('tbl0_Row0T_Cell'+tab).className = 'menuContentTActive';
		document.getElementById('tbl0_Row0T_Cell'+tab+'BB').className = 'menuContentTRActive';			
	}
	else
	{
		document.getElementById('tbl0_Row0_Cell'+tab+'A').className = 'menuTabsBeforeUnActive';							
		document.getElementById('tbl0_Row0_Cell'+tab).className = 'menuTabsContentUnActive';
		document.getElementById('tbl0_Row0_Cell'+tab+'B').className = 'menuTabsAfterUnActive';	
		
		document.getElementById('tbl0_Row0T_Cell'+tab+'AA').className = 'menuContentT';							
		document.getElementById('tbl0_Row0T_Cell'+tab).className = 'menuContentT';
		document.getElementById('tbl0_Row0T_Cell'+tab+'BB').className = 'menuContentT';			
		
	}
}
var currentTabs = '1';

function	RecentreByRefMap(e)
{
	var		evt = e?e:window.event,
			obj,
			l = 0,
			h = 0,
			X,
			Y;
	
	if (navigator.appName == "Netscape")
		evt = e;
	else
		evt = window.event;
		
	if (navigator.appName == "Netscape")
	{
		obj = e.target;
		while (obj != document)
		{
			if ((obj.tagName != "TR") && (obj.tagName != "TBODY"))
			{
				l += obj.offsetLeft;
				h += obj.offsetTop;
			}
			obj = obj.parentNode;
		}
		X = e.pageX - l;
		Y = e.pageY - h;
	}
	else
	{
		X = e.offsetX;
		Y = e.offsetY;
	}

	giController.callService('CARTO_CENTREBYREF',X,Y);
}

function userList()
{
	window.location.href = "./index.php?service=global.ulist";
}

function GI()
{
	win = window.open('http://www.generale-infographie.fr');
}

function hideSplashScreen()
{
	var objDiv = document.getElementById("splashScreen");
	objDiv.style.display = 'none';
}

function showSplashScreen(time)
{
	var objDiv = document.getElementById("splashScreen");
	objDiv.style.display = 'block';
	if (time)
	{
		setTimeout("hideSplashScreen()", time);
	}
}

function hideSplashScreen2()
{
	var objDiv = document.getElementById("splashScreen2");
	objDiv.style.display = 'none';
}

function showSplashScreen2(time)
{
	var objDiv = document.getElementById("splashScreen2");
	objDiv.style.display = 'block';
	if (time)
	{
		setTimeout("hideSplashScreen2()", time);
	}
}
function requeteur(File,Type,Id,Param,Id_Utilisateur,Param_Utilisateur)
{
	if (!File || File == '') {
		File = "../../../style/spec/requeteur/SSRequeteur.xml";
	}
	if (!Type || Type == '') {
		Type = "";
	}
	if (!Id || Id == '') {
		Id = "";
	}
	if (!Param || Param == '') {
		Param = "";
	}
	if (!Id_Utilisateur || Id_Utilisateur == '') {
		Id_Utilisateur = "";
	}
	if (!Param_Utilisateur || Param_Utilisateur == '') {
		Param_Utilisateur = "";
	}
			
	giController.callService('REQUETEUR',File,Type,Id,Param,Id_Utilisateur,Param_Utilisateur);
}


function	executeSymbologie(idxLayer)
{
	interfaceCartoDoc = document.getElementById('CARTO').contentWindow.document;
	
	var target = 'popupSYMBOLOGY';
	var url = 'Contenu=TSSymbologieCouche.php';
	url += "&couche=" + idxLayer;
	url += "&PN_MAP_FILE=" + interfaceCartoDoc.status.PN_MAP_FILE.value;
	url += "&service=" + interfaceCartoDoc.status.PS_SERVICE_IDX.value;
			
	giController.callService('CARTO_POPUP',url,target, "Symbologie");
}

function	executeEchelle(idxLayer)
{
	interfaceCartoDoc = document.getElementById('CARTO').contentWindow.document;
	
	var target = 'popupSYMBOLOGY';
	var url = 'Contenu=TSEchellesCouche.php';
	url += "&couche=" + idxLayer;
	url += "&PN_MAP_FILE=" + interfaceCartoDoc.status.PN_MAP_FILE.value;
	url += "&service=" + interfaceCartoDoc.status.PS_SERVICE_IDX.value;
			
	giController.callService('CARTO_POPUP',url,target, "Plage d'\u00e9chelle");
}

function	executeEtiquette(idxLayer)
{
	interfaceCartoDoc = document.getElementById('CARTO').contentWindow.document;
	
	
	var target = 'popupSYMBOLOGY';
	var url = 'Contenu=TSEtiquettes.php';
	url += "&couche=" + idxLayer;
	url += "&PN_MAP_FILE=" + interfaceCartoDoc.status.PN_MAP_FILE.value;
	url += "&service=" + interfaceCartoDoc.status.PS_SERVICE_IDX.value;
			
	giController.callService('CARTO_POPUP',url,target, "Etiquettes");
}

var out_div = 'false';
function cacheThema(numThema)
{
	if (out_div == "true"){
		var objDiv;
		objDiv = document.getElementById(numThema);
		objDiv.style.display = "none";
	}
}
function clicDroit(nomLayer, e)
{
	evt = e;
	// Recherche du navigateur sur lequel on est, pour le positionnement de la souris
	if (navigator.appName == "Netscape") {
		posLeft = evt.pageX;
		posTop = evt.pageY;
	}
	else {
		posLeft = evt.offsetX;
		posTop = evt.offsetY;
	}
	
	
	var objDiv;
	var nomDiv = "thema_"+nomLayer;
	var i = 0;
	
	//On cache tous les  div "thema_x", au cas ou il y en ait un déjà d'ouvert avant d'ouvrir celui demandé
	var objetsDiv;
	objetsDiv = document.getElementsByTagName("div");
	while (i < objetsDiv.length){
		if(objetsDiv[i].id.match("thema_")){
			objetsDiv[i].style.display = "none";
		}
		i = i +1;
	}

	//Puis on affiche le div demandé (par rapport au nomLayer)
	objDiv = document.getElementById(nomDiv);
	objDiv.style.left = posLeft + 10 + "px";
	objDiv.style.top = posTop + 173 + "px";
	objDiv.width = 200 + "px";
	objDiv.height = 250 + "px";
	objDiv.style.cursor = "default";
	objDiv.style.display = "block";
}

function IsNumericScale(sText)
{
	var ValidChars = "0123456789/",
		IsNumber = true,
		Char, i;
	
	for (i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
			break;
		}
	}
	return IsNumber;
} 

function changeScaleCarto(namePrompt, valPrompt)
{
	if (valPrompt != "")
	{		
		if (IsNumericScale(valPrompt) == true)
		{
			var strPrp;
			if (parseFloat(eval(valPrompt)) < 1)
			{
				strPrp = (1 / parseFloat(eval(valPrompt))).toString();
			}
			else
			{
				strPrp = (parseFloat(eval(valPrompt))).toString();		
			}
			
			giPopupHandler.doClose(namePrompt);
		
			giController.callService('CARTO_CHANGESCLAE',parseInt(strPrp));			
		}
		else
		{
			if (valPrompt != "*")
			{
				alert("Saisie incorrecte, veuillez recommencer.");
			}
			else
			{
				giPopupHandler.open(namePrompt,'index.php?service=dialog.prompt&message=Rapport attendu (Exemple: 1/200 ou 200) : &fctjs=parent.changeScaleCarto&width=300&height=80');
			}				
		}
	}	 
}
