///////////////////////////////////////////////////////////////////////////////
//
//   (c) Pitney Bowes MapInfo Corporation, 2008.  All rights reserved.
//
//   The source code below is provided as sample code only. The end user of the
//   Licensed Product that contains this code may use the code below for
//   development purposes. This software is provided by Pitney Bowes MapInfo
//   "as is" and any express or implied warranties, including, but not limited
//   to, the implied warranties of merchantability and fitness for a particular
//   purpose are disclaimed.  In no event shall Pitney Bowes MapInfo be liable
//   for any direct, indirect, incidental, special, exemplary, or consequential
//   damages (including, but not limited to, procurement of substitute goods or
//   services; loss of use, data or profits; or business interruption) however
//   caused and whether in contract, strict liability, or tort (including
//   negligence) arising in any way out of the use of this software, even if
//   advised of the possibility of such damage.
//
///////////////////////////////////////////////////////////////////////////////

function toggle2(node) {

    var nextDIV = node.nextSibling;
    while(nextDIV.nodeName != "DIV") {
    nextDIV = nextDIV.nextSibling;
    }

    if (nextDIV.style.display == 'none') {

    if (node.childNodes.length > 0) {

        if (node.childNodes.item(0).nodeName == "IMG") {
        node.childNodes.item(0).src = getImgDirectory(node.childNodes.item(0).src) + "minus.gif";
        }
    }
    nextDIV.style.display = 'block';
    }
    else {

    if (node.childNodes.length > 0) {
        if (node.childNodes.item(0).nodeName == "IMG") {
            node.childNodes.item(0).src = getImgDirectory(node.childNodes.item(0).src) + "plus.gif";
        }
    }
    nextDIV.style.display = 'none';
    }
    
    
}

function getImgDirectory(source) {
    return source.substring(0, source.lastIndexOf('/') + 1);
}

function selectLeaf(title, url) {
    alert("You just clicked on title = " + title + ":: url = " + url);
}

// Due to problems in setting outerhtml in NetScape/FireFox this is required
function SetOuterHTML(doc, ele, html)
{
	if (BrowserType() == NS) {
		var range = doc.createRange();
		range.setStartBefore(ele);
		var frag = range.createContextualFragment(html);
		ele.parentNode.replaceChild(frag, ele);
	} else {
		ele.outerHTML = html;
	}
}

function IMapXtremeEventHandler()
{
}

IMapXtremeEventHandler.prototype.layerEventHandler=function(a){
		var b=this;
		b=b;
		return function() 
		{
			if (a != null) return b[a]();
		}
	}

// Muuttujat tasojen hallintaa varten
var mainMapControlName = "mapMain";
var mainMapAlias = "Map1";
var legendAlias = "Legend1";

var mapLoadDelay = 500;

var refreshMapUrl;
var refreshMapImage;


function LayerInfo(uniqueID, mapControlID, resourcePath, xsltFile)
{
	this.uniqueID = uniqueID;
	this.mapControlID = mapControlID;
	this.resourcePath = resourcePath;
	this.xsltFile = xsltFile;
	window.onload = this.layerEventHandler("UpdateLayerHTML");
}
LayerInfo.prototype = new IMapXtremeEventHandler();
LayerInfo.prototype.constructor = LayerInfo;
LayerInfo.superclass = IMapXtremeEventHandler.prototype;

LayerInfo.prototype.UpdateLayerHTML = function()
{
	this.mapImage = FindElement(this.mapControlID+"_Image");
	if (!this.mapImage.mapAlias) this.mapImage.mapAlias = this.mapImage.attributes["mapAlias"].value;
	if (!this.mapImage.exportFormat) this.mapImage.exportFormat = this.mapImage.attributes["exportFormat"].value;
	this.mapImage.onload = this.layerEventHandler("UpdateLayerHTML");
	
	this.mapAlias = this.mapImage.mapAlias;
	var me = document.getElementById(this.uniqueID);
	
	var url = "LayerController.ashx?Command=GetHTML"+
					"&UniqueID=" + this.uniqueID +
					"&MapAlias=" + this.mapAlias +
					"&XsltFile=" + this.xsltFile +
					"&R=" + Math.random();
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("POST", url, false);
	xmlHttp.send(this.resourcePath);
    var layerHTML = xmlHttp.responseText;
    
    // tässä päivitetään legenda, jos ei ladata sivua 1.kertaa
	if (me.innerHTML != "") {
        paivitaLegendanFrame("LegendControl1");
    }
	me.innerHTML = layerHTML;
	   
//	SetOuterHTML(document, me, layerHTML);
//	me.id = this.uniqueID+"_div";
}


function ApplyChanges(obj, uniqueID, command)
{
	if (!obj.layerAlias) obj.layerAlias = obj.attributes["layerAlias"].value;
	if (!obj.layerType) obj.layerType = obj.attributes["layerType"].value;
	
	var me = eval(uniqueID + "LayerInfo");
	var url = "MapController.ashx?Command="+ command + 
					"&MapAlias=" + me.mapAlias +
					"&LayerAlias=" + obj.layerAlias +
					"&LayerType=" + obj.layerType +
					"&Visible=" + obj.checked +
					"&Width=" +me.mapImage.width +
					"&Height=" + me.mapImage.height +
					"&ExportFormat=" + me.mapImage.exportFormat +
					"&Left=0"+
					"&Top=0"+
					"&Right="+ me.mapImage.width +
					"&Bottom="+ me.mapImage.height +
					"&R=" + Math.random();
	
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);

// tässä pitäisi päivittää kartta uudelleen
   refreshMapUrl = "MapController.ashx?Command=RefreshMapCommand" +
				   "&Width=" + me.mapImage.width +
				   "&Height=" + me.mapImage.height +
				   "&ExportFormat=" + me.mapImage.exportFormat +
				   "&Ran=" + Math.random() +
				   "&MapAlias=" + me.mapAlias +
				   "&GetMapImage=true";

    refreshMapImage = me.mapImage;
//  tehtävä tällä, muuten ei kartta päivity
    setTimeout("refreshMapImage.src = refreshMapUrl", mapLoadDelay);
}

function SelectedNodeChanged(node, name, code, lyrCntrlID, mapid, lyrid)
{
}

function AppendLayerScriptToForm(script)
{
	var scr = document.getElementById('LayerActivationScript');
	if (!scr) scr = document.createElement('script');
	scr.id = 'LayerActivationScript';
	scr.type = 'text/javascript';
	scr.text = script;
	document.forms[0].appendChild(scr);
}

function paivitaLegendanFrame(legendControlId) {
    try {
        var legendImageId = legendControlId + "_Image";
        
        var legendControl = document.getElementById(legendControlId);
        legendControl.style.visibility = "visible";
        if (legendControl.parentNode.tagName == "DIV")
        {
            legendControl.parentNode.style.visibility = "visible";
        }
        legendImage = FindElement(legendImageId);
        
        if(legendImage) {
            var indexMap = FindElement("mapIndex_Image");
            var w = indexMap.width;
            var h = indexMap.height;
//            var w = legendImage.width;
//            var h = legendImage.height;
            var exportFormat = "Gif";
            var url = null;
            
            if (BrowserType() == IE) {
                if (w < 1) {
					//get width from the SPAN tags "style" property
					w = legendImage.parentElement.style.pixelWidth;
				}
				//get height from the SPAN tags "style" property
				if (h < 1) {
					h = legendImage.parentElement.style.pixelHeight;
				}
				if (legendImage.legendAlias == "") {
					legendImage.legendAlias = legendImage.attributes["legendAlias"].value;
				}
				if (legendImage.exportFormat == "") {
					legendImage.exportFormat = legendImage.attributes["exportFormat"].value;
					exportFormat = legendImage.exportFormat;
				}           
            }
            legendImage.width = w;
			legendImage.height = h;

			//...set the image source to a new URL to get a new map image
			// of the proper pixel dimensions
			url = "MapController.ashx?Command=GetLegend" + 
			    "&MapAlias=[object]" +
				"&LegendExportFormat=" + exportFormat + 
				"&Ran=" + Math.random();
				
			//If the map has an alias defined, append it to the url
			if (legendImage.legendAlias != "" || legendImage.legendAlias != null) {
				url += "&LegendAlias=" + legendAlias;
			}
			legendImage.style.left = 0;
			legendImage.style.top = 0;
			legendImage.style.clip = 'rect(' + 0 + ' ' + w + ' ' + h + ' ' + 0 +')';
			
			//Set the image object src attribute to the url to get a new map image
			legendImage.src = url;
        }
        else {
			alert("Image could not be found on page");	
		}
    } catch (e) {
        //alert("Exception in 'GetLegend' function (Command.js)");
    }
}
