//****************************************************************************************
//*** This JavaScript code was generated by SVGMapper 2 for use by Interactive SVG Map ***
//***                        Copyright: Uros Preloznik                                 ***
//****************************************************************************************
var press;
function init()
{
layer = new Array;

//  Disable layers on startup with 'OFF'
layer[0] = 'ON';					//comprensorio
layer[1] = 'ON';					//confine comunale
layer[2] = 'ON';					//quadro unione mappe catastali
layer[3] = 'ON';					//valori medi microzone
layer[4] = 'ON';					//microzone censuarie
layer[5] = 'ON';					//text
// **************************************************************************************

svgMainDoc = document.svg_main.getSVGDocument();
svgMainObj = svgMainDoc.getDocumentElement();
svgLegDoc = document.svg_legend.getSVGDocument();
svgArrowDoc = document.svg_arrows.getSVGDocument();
svgOverDoc = document.svg_overview.getSVGDocument();
svgOverObj = svgOverDoc.getDocumentElement();
svgOver = svgOverDoc.getElementById('overview');
svgOverWidth = svgOver.getAttribute('width');
svgOverHeight = svgOver.getAttribute('height');
overpixWidth = svgOverObj.getAttribute('width');
var overbox = new String(svgOverObj.getAttribute('viewBox'));
var overboxes = overbox.split(' ');
overviewx = parseFloat(overboxes[0]);
overviewy = parseFloat(overboxes[1]);
overwidth = parseFloat(overboxes[2]);
overheight = parseFloat(overboxes[3]);
overpixSize = overwidth / overpixWidth;
svgX = svgArrowDoc.getElementById('coordx').getFirstChild();
svgY = svgArrowDoc.getElementById('coordy').getFirstChild();
var viewbox = new String(svgMainObj.getAttribute('viewBox'));
var viewboxes = viewbox.split(' ');
ulXCorner = viewboxes[0];
ulYCorner = viewboxes[1];
width = viewboxes[2];
height = viewboxes[3];
var pixWidth = svgMainObj.getAttribute('width');
var pixHeight = svgMainObj.getAttribute('height');
origPixSize = width / pixWidth;
for (i=0;i<layer.length;i++) {
if (layer[i]=='OFF') {
layeroff = svgMainDoc.getElementById(i);
glist = layeroff.getElementsByTagName('g');
layeroff.setAttribute('style','visibility:hidden');
legoff = svgLegDoc.getElementById(i)
legoffstyle = legoff.getStyle();
legoffstyle.setProperty('fill','white');
}
}
legrect = svgLegDoc.getElementById('all');
textid = svgArrowDoc.getElementById('idObj').getFirstChild();
origscale = 150000;
newscale = document.form1.scaletext;
newscale.value = origscale;
shift_y = 4469139;
shift_x = 2497211;
top_y = 48605.04;
resetCoords();
}
function resetCoords() {
scale = svgMainObj.getCurrentScale();
trans = svgMainObj.getCurrentTranslate();
transx = trans.getX();
transy = trans.getY();
pixSize = origPixSize / scale;
offsetX = parseFloat(ulXCorner) - transx * pixSize;
offsetY = parseFloat(ulYCorner) - transy * pixSize;
midpointX = offsetX + (width / scale  / 2);				//dolocitev sredine okna
midpointY = offsetY + (height / scale  / 2);
svgOver.setAttribute('x',offsetX);
svgOver.setAttribute('y',offsetY);
svgOver.setAttribute('width',svgOverWidth / scale);
svgOver.setAttribute('height',svgOverHeight / scale);
}
function zooming()
{
resetCoords();
mapscale = Math.round(origscale / scale);
newscale.value = mapscale;
}
function help() {
 var helpwin = window.open('./data/help.htm','HelpWin', 'width=480,height=350,top=300,left=50,toolbar=no,menubar=no,location=no,hotkeys=no,resizable=yes,scrollbars=yes,dependent=yes,status=no');
}
function scrolling() {
resetCoords();
}
function showCoords(evt) {
resetCoords();
svgX.setData(Math.round(offsetX + evt.getClientX() * pixSize)+shift_x)
svgY.setData(Math.round(top_y-(offsetY + evt.getClientY() * pixSize)+shift_y))
}
function onoff(id)
{
mainobj = svgMainDoc.getElementById(id);
mainstyle = mainobj.getStyle();
mainvis = mainstyle.getPropertyValue('visibility');
legobj = svgLegDoc.getElementById(id);
legstyle = legobj.getStyle();
legstylefill = legstyle.getPropertyValue('fill');
if ((legstylefill == 'yellow') || (legstylefill == ''))
{
legstyle.setProperty('fill','white');
mainstyle.setProperty('visibility','hidden');
}
else
{
legstyle.setProperty('fill','yellow');
mainstyle.setProperty('visibility','visible');
}
}
function navclick(id)
{
leg = svgLegDoc.getElementById('leg');
legViewBox = leg.getAttribute('viewBox');
field = legViewBox.split(' ');
newY = parseFloat(field[1]);
newHeight = parseFloat(field[3]);
switch(id){
case 'down': newY = newY + 0.1*newHeight;
if (newY > (430.00-newHeight)){
newY = 430.00-newHeight;
}
if (newY < 0){
newY = 0;
}
break;
case 'up': newY = newY - 0.1*newHeight;
if (newY < 0){
newY = 0;
}
break;
}
newViewBox = field[0]+' '+newY+' '+field[2]+' '+field[3];
leg.setAttribute('viewBox',newViewBox);
}
function NewWin(id)
{
var theme = id.split('_');
var XMLDoc = new ActiveXObject('microsoft.xmldom');
XMLDoc.async='false';
XMLDoc.load('./data/i_'+theme[1]+'.xml');
var XMLEl = XMLDoc.documentElement;
var attwin = open('','new','width=300,height=300,locationbar=yes,menubar=no,scrollbars=yes,status=no,resizable=yes,screenX=700,screenY=330');
attwin.document.open();
attwin.focus();
attwin.document.writeln('<HTML><HEAD><TITLE>SVGMapper</TITLE></HEAD><BODY><TABLE border="1">');
attwin.document.writeln('<link rel="stylesheet" href="./data/style.css">');
var svgatt = XMLEl.getElementsByTagName(id).item(0);
var obj = svgatt.childNodes.item(0);
for(var i=0; i<obj.childNodes.length; i++) {
var field = obj.childNodes.item(i).nodeName;
var value = obj.childNodes.item(i).text;
if ((value.indexOf('www') > - 1) || (value.indexOf('http://') > - 1)) {
attwin.document.writeln('<TR><th class="color1">'+field+'</th><th class="color2"><a href="'+value+'"target="blank">'+value+'</a></th></TR>');
}
else {
attwin.document.writeln('<TR><th class="color1">'+field+'</th><th class="color2">'+value+'</th></TR>');
}
}
attwin.document.writeln('</TABLE></HTML>');
}
function Triple(id,maptip)
{
var theme = id.split('_');
var index = parseFloat(theme[1]);
var mapField = new Array;
mapField[0] = 'Comune';
mapField[1] = 'Comune';
mapField[2] = 'Foglio';
mapField[3] = 'Val_med';
mapField[4] = 'Microzona';
var value = mapField[index]+' '+maptip;
textid.setData(value);
svgObj = svgMainDoc.getElementById(id);
svgStyle = svgObj.getStyle();
svgStyle.setProperty('fill-opacity','0.5');
}
function Out(id)
{
textid.setData(' ');
svgObj = svgMainDoc.getElementById(id);
svgStyle = svgObj.getStyle();
svgStyle.setProperty('fill-opacity','1.0')
}
function info(theme)
{
var themewin = open('./data/'+theme+'.xml','data','width=500,height=500,locationbar=yes,menubar=no,scrollbars=yes,status=no,resizable=yes,screenX=700,screenY=330');
themewin.focus();
}
function changescale()
{
oldmidpointX = midpointX;
oldmidpointY = midpointY;
svgMainObj.setCurrentScale(origscale / newscale.value);
trans.setX(((midpointX - oldmidpointX) / pixSize) + transx);	//zumiranje iz sredine
trans.setY(((midpointY - oldmidpointY) / pixSize) + transy);
}
function searchIn(id)
{
svgObj = svgMainDoc.getElementById(id);
svgStyle = svgObj.getStyle();
vrsta =svgObj.nodeName;
switch(vrsta) {
case 'path': 
dolzina = svgObj.getTotalLength; 		//dolzina celega objekta = obseg
zacetna = svgObj.getPointAtLength(0);   //prva tocka objekta
Xmax = zacetna.getX;
Xmin = Xmax;
Ymax = zacetna.getY;
Ymin = Ymax;
for (i=0; i<dolzina; i++) {
tocka = svgObj.getPointAtLength(i);
tockaX = tocka.getX;
tockaY = tocka.getY;
if (tockaX > Xmax) {
Xmax = tockaX;
}
if (tockaX < Xmin) {
Xmin = tockaX;
}
if (tockaY > Ymax) {
Ymax = tockaY;
}
if (tockaY < Ymin) {
Ymin = tockaY;
}
}
Xsr = (Xmax + Xmin) / 2;
Ysr = (Ymax + Ymin) / 2;
break;
case 'text': 
Xsr = svgObj.getStartPositionOfChar(0).getX;
Ysr = svgObj.getStartPositionOfChar(0).getY;
break;
case 'circle': 
Xsr = svgObj.getAttribute('cx');
Ysr = svgObj.getAttribute('cy');
break;
case 'use': 
Xsr = svgObj.getAttribute('x');
Ysr = svgObj.getAttribute('y');
break;
}
trans.setX(((midpointX - Xsr) / pixSize) + transx);		//izbrani objekt premaknemo na sredino
trans.setY(((midpointY - Ysr) / pixSize) + transy);
stroke = svgStyle.getPropertyValue('stroke');
sw = svgStyle.getPropertyValue('stroke-width');
svgStyle.setProperty('fill-opacity','0.4');
}
function searchOut(id)
{
svgStyle.setProperty('fill-opacity','1.0');
}
function filter(search_string) {
// naloadam XML fajl v IE-jev XML parser (preko ActiveX objekta)
var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
var menuIndex = document.form2.select.selectedIndex
xmlDoc.async='false';
searchField = new Array;
th_id = new Array;
searchField[0] = 'Comune';
th_id[0] = '00';
searchField[1] = 'Comune';
th_id[1] = '01';
searchField[2] = 'Foglio';
th_id[2] = '02';
searchField[3] = 'Valore_med';
th_id[3] = '03';
searchField[4] = 'Microzona';
th_id[4] = '04';
xmlDoc.load('data/i_'+th_id[menuIndex]+'.xml');
path='//DATA['+searchField[menuIndex]+'="'+search_string+'"]';
searchnodes=xmlDoc.selectNodes(path);
for (i=0; i<searchnodes.length; i++) {
searchIn(searchnodes.item(i).parentNode.nodeName);
}
alert('Number of results: '+searchnodes.length);
for (i=0; i<searchnodes.length; i++) {
searchOut(searchnodes.item(i).parentNode.nodeName);
}
}
function check(Obj,proc) {
// proc=1 (do search)
// proc=2 (do change scale)
if (Obj.event.keyCode == 13) {
if (proc==1) document.form1.do_search.onclick()
else changescale();
}
}
function start(evt) {
press = 1;
startX = evt.getClientX;
startY = evt.getClientY;
overx = parseFloat(svgOver.getAttribute('x'));
overy = parseFloat(svgOver.getAttribute('y'));
if ((svgOverWidth / scale) >= overwidth) {
	press = 0;
}
}
function move(evt) {
if (press ==1) {
	endX = evt.getClientX;
	endY = evt.getClientY;
	moveX = overx + ((endX-startX) * overpixSize);
	moveY = overy + ((endY-startY) * overpixSize);
	if (moveX < overviewx) {
		svgOver.setAttribute('x',overviewx);
	}
	else if ((moveX + (svgOverWidth) / scale) > (overviewx + overwidth)) {
		svgOver.setAttribute('x',overviewx + overwidth - (svgOverWidth / scale));
	}
    else {
		svgOver.setAttribute('x',moveX);
	}
	if (moveY < overviewy) {
		svgOver.setAttribute('y',overviewy);
	}
    else if (moveY + (svgOverHeight / scale) > (overviewy + overheight)) {
		svgOver.setAttribute('y',overviewy + overheight - (svgOverHeight / scale));
	} 
    else {
      	svgOver.setAttribute('y',moveY);
    }
    overxnew = parseFloat(svgOver.getAttribute('x'));
	overynew = parseFloat(svgOver.getAttribute('y'));
	deltax = overx - overxnew;
	deltay = overy - overynew;
}
}
function end() {
if (press==1) {
trans.setX((deltax / pixSize) + transx);
trans.setY((deltay / pixSize) + transy);
press = 0;
}
}
function layers() {
selectXml = new Array;
selectXml[0] = 'comprensorio';
selectXml[1] = 'confine comunale';
selectXml[2] = 'quadro unione mappe catastali';
selectXml[3] = 'valori medi microzone';
selectXml[4] = 'microzone censuarie';
for (j=0;j<selectXml.length;j++) {
document.writeln('<option value='+j+'>'+selectXml[j]+'</option>');
}
}
function layout() {
document.writeln('<div align="center"><embed width="660" height="500" name="svg_main"')
document.writeln('src="view.svg" type="image/svg+xml" pluginspage="http://www.adobe.com/svg/viewer/install/">')
document.writeln('</embed></div>')
document.writeln('</td>')
document.writeln('<td valign="top" colspan="3" rowspan="2">')
document.writeln('<div align="center"><embed width="200" height="350.00" name="svg_legend"')
document.writeln('src="legend.svg" type="image/svg+xml" pluginspage="http://www.adobe.com/svg/viewer/install/">')
document.writeln('</embed></div>')
}
