// aimsLegend.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js
*/

aimsLegendPresent=true;

var drawLegendOnly=false;
var defaultLegTitle=legTitle;

/*
***************************************************************************************

Legend functions

***************************************************************************************
*/


// send request to create graphic legend
function getLegend() {
	legendVisible=true;
	drawLegendOnly=true;
	var theString=writeXML();
	//alert(theString);
	showRetrieveMap();
	sendToServer(imsURL,theString,98);
}

//INIZIO Help-Legenda statica: HLS
function hls_csi_showLegendHTM(){
  //var Win1 = open("./legend_hls_csi/pagCliente.html","LegendWindow","width=190,height=300,scrollbars=yes,resizable=yes");
  var Win2 = open( doc_loc_sub +"legend_hls_csi/pagCliente.html","LegendWindow","width=300,height=300,scrollbars=yes,resizable=yes");
  Win2.focus();
}
//FINE Help-Legenda statica: HLS

// write out the legend display
function showLegend() {
	if (legInt) {
		parent.TOCFrame.document.open();
		parent.TOCFrame.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>' + titleList[3] + '</title>');
		parent.TOCFrame.document.writeln('<style type="text/css">a {text-decoration:none;}</style>');
		parent.TOCFrame.document.writeln('</head>');
		parent.TOCFrame.document.writeln('<body BGCOLOR="White" text="Black" leftmargin=0 topmargin=0 rightmargin=0 link="Black" vlink="Black" alink="Black">');
		parent.TOCFrame.document.writeln('<center>');
		parent.TOCFrame.document.writeln('<IMG SRC="' + legendImage + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="' + titleList[3] + '"></center>');
		parent.TOCFrame.document.writeln('</body></html>');
		parent.TOCFrame.document.close();

	} else {

		var Win1 = open("","LegendWindow","width=270, height=500, scrollbars=yes, resizable=yes");
		Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head><title>' + titleList[3] + '</title>');
		Win1.document.writeln('<link rel="stylesheet" href="'+pagStile+'" type="text/css">');

		// Inizio Dichiarazione delle funzioni Javascript
		Win1.document.writeln('<script language="Javascript">');

			Win1.document.writeln('	var t;');
			Win1.document.writeln('	var tName = "";');
			Win1.document.writeln('	if (opener) {');
			Win1.document.writeln('		if (opener.name=="MapFrame") {');
			Win1.document.writeln('			t = opener.parent.MapFrame;');
			Win1.document.writeln('			tName = "opener.parent.MapFrame.";');
			Win1.document.writeln('		} else {');
			Win1.document.writeln('			t = opener;');
			Win1.document.writeln('			tName = "opener.";');
			Win1.document.writeln('		}');
			Win1.document.writeln('	} else {');
			Win1.document.writeln('		if (parent.MapFrame!=null) {');
			Win1.document.writeln('			t = parent.MapFrame;');
			Win1.document.writeln('			tName = "parent.MapFrame.";');
			Win1.document.writeln('		} else {');
			Win1.document.writeln('			t=document;');
			Win1.document.writeln('		}');
			Win1.document.writeln('	}');

			Win1.document.writeln(' function closeIt() {');
			Win1.document.writeln('	  t.legendVisible=false;');
			Win1.document.writeln('   window.close();');
			Win1.document.writeln('	}');

			//INIZIO Help-Legenda statica: HLS
			Win1.document.writeln('	function hls_csi_showLegendHTM(){ ');
			Win1.document.writeln('  var Win3 = window.open("' + doc_loc_sub + 'legend_hls_csi/pagCliente.html","StaticLegend","width=640,height=480,scrollbars=yes,resizable=yes"); ');
			Win1.document.writeln('  Win3.focus();');
			Win1.document.writeln('	}');
			//FINE Help-Legenda statica: HLS

			// Inizio : funzioni di swap delle immagini per effetto di rollover
			Win1.document.writeln(' function MM_swapImgRestore() { ');
			Win1.document.writeln('   var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;');
			Win1.document.writeln(' } ');

			Win1.document.writeln(' function MM_preloadImages() { ');
			Win1.document.writeln('   var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); ');
			Win1.document.writeln('   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) ');
			Win1.document.writeln('  	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} ');
			Win1.document.writeln(' } ');

			Win1.document.writeln(' function MM_findObj(n, d) { ');
			Win1.document.writeln('   var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { ');
			Win1.document.writeln('   d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} ');
			Win1.document.writeln('   if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; ');
			Win1.document.writeln('   for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); ');
			Win1.document.writeln('   if(!x && d.getElementById) x=d.getElementById(n); return x; ');
			Win1.document.writeln(' } ');

			Win1.document.writeln(' function MM_swapImage() { ');
			Win1.document.writeln('   var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) ');
			Win1.document.writeln('   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} ');
			Win1.document.writeln(' } ');
			// Fine : funzioni di swap delle immagini per effetto di rollover

		Win1.document.writeln('</script>');
		// Fine Dichiarazione delle funzioni Javascript

		Win1.document.writeln('</head>');
		//Win1.document.writeln('<body BGCOLOR="White" text="#ff0000" leftmargin=0 topmargin=0 rightmargin=0 link="Gray" vlink="Gray" alink="Gray" onclose="closeIt()">');

		//Correzione baco - GEDEONE 1.3
		//La pop-up relativa alla legenda puņ ora essere definitivamente chiusa
		//anche utilizzado il pulsante di chiusura (x) della pop-up stessa
		//Win1.document.writeln('<body BGCOLOR="White" text="#ff0000" leftmargin=0 topmargin=0 rightmargin=0 link="Gray" vlink="Gray" alink="Gray" onclose="closeIt()">');
    	Win1.document.writeln('<body BGCOLOR="#999999" onbeforeUnload="javascript:t.legendVisible=false;" text="#ff0000" leftmargin=0 topmargin=0 rightmargin=0 link="Gray" vlink="Gray" alink="Gray" onclose="closeIt()">');

		Win1.document.writeln('<table width="250" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">');
			Win1.document.writeln('<tr><td>');

				Win1.document.writeln('<table align="left" border="0" cellpadding="0" cellspacing="0" width="250">');
				Win1.document.writeln('<tr>');
					Win1.document.writeln('<td width="13"><img name="angolo_legenda_sx" src="' + doc_loc_sub + '/images/angolo_legenda_sx.gif" width="13" height="20" border="0" alt=""></td>');
					Win1.document.writeln('<td height="20" background="' + doc_loc_sub + '/images/sfondo_titolo_legenda.gif" class="testo12whitebold">LEGENDA</td>');
					Win1.document.writeln('<td width="59"><img name="angolo_legenda_dx" src="' + doc_loc_sub + '/images/angolo_legenda_dx.gif" width="59" height="20" border="0" alt="chiudi" style="cursor:hand;" onMouseOver="MM_swapImage(\'angolo_legenda_dx\',\'\',\'' + doc_loc_sub + '/images/angolo_legenda_dx_roll.gif\',1);" onMouseOut="MM_swapImgRestore()" onMouseDown="closeIt();"></a></td>');
				Win1.document.writeln('</tr>');
				Win1.document.writeln('</table>');

			Win1.document.writeln('</td></tr>');

			//Win1.document.writeln('<tr height="10"><td width="8" style="border-left:1px solid #000000; border-right:1px solid #000000"><img name="spazio" src="' + doc_loc_sub + '/images/pixel.gif" width="1" height="1"></td></tr>');

			// Link alla Legenda Dinamica
			//Win1.document.writeln('<tr height="10"><td style="border-left:1px solid #000000; border-right:1px solid #000000" class="testo11black"><div align="center"><a href="javascript:hls_csi_showLegendHTM();">&nbsp;&nbsp;Dettaglio soggetti venatori &gt;&gt;&nbsp;&nbsp;</a></div></td></tr>');
			//

			Win1.document.writeln('<tr>');

				Win1.document.writeln('<td style="border-left:1px solid #000000; border-right:1px solid #000000">');

				//INIZIO Help-Legenda statica: HLS
				if (hls_csi_useStaticLegendImg == true)
					{
					//viene utilizzata la legenda fornita dall' utente.
					//alert(doc_loc_sub + hls_csi_fileStaticLegend);
					Win1.document.writeln('<IMG SRC="' + doc_loc_sub + hls_csi_fileStaticLegend + '" HSPACE=0 VSPACE=0 BORDER=1 ALT="' + titleList[3] + '">');
				} else {
					//legenda default di ArcIms
					//alert(legendImage);
					Win1.document.writeln('<IMG SRC="' + legendImage + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="' + titleList[3] + '">');
				}
				//FINE Help-Legenda statica: HLS

				Win1.document.writeln('</td>');

			Win1.document.writeln('</tr>');
			Win1.document.writeln('<tr>');
			Win1.document.writeln('<td><img name="legenda_sotto" src="' + doc_loc_sub + '/images/angolo_legenda_sotto.gif" width="250" height="20" border="0" alt=""></td>');
			Win1.document.writeln('</tr>');

		Win1.document.writeln('</table>');

		Win1.document.writeln('</body></html>');
		Win1.document.close();
		Win1.focus();
		Win1=null;
	}
}

	// add Legend to XML request
	function addLegendToMap() {
	//	var legString = '<LEGEND title="' + legTitle + '" font="' + legFont + '" titlefontsize="16" width="' + legWidth + '" height="' + legHeight + '" ';

		var legString = '<LEGEND font="' + legFont + '" titlefontsize="16" width="' + legWidth + '" height="' + legHeight + '" ';

		legString += 'autoextend="true" backgroundcolor="255,255,255" cellspacing="20"';

		if (hideLayersFromList) {
			legString += '>\n<LAYERS>\n';
			for (var legvar=0;legvar<noListLayer.length;legvar++) {
				if (noListLayer[legvar]) legString += '<LAYER id="' + LayerID[legvar] + '" />\n';
			}
			legString += '</LAYERS>\n';
			legString += '</LEGEND>\n';
		} else {
			legString += ' />\n';
		}
		if (drawLegendOnly) legString = legString + '<DRAW map="false" />\n';
		return legString;
	}

