﻿//client info command to control client behavior for info tool.
function InfoCommand(name, interaction)
{
	if (arguments.length > 0) {
		this.Init(name, interaction);
	}
}
InfoCommand.prototype = new MapCommand();
InfoCommand.prototype.constructor = InfoCommand;
InfoCommand.superclass = MapCommand.prototype;
InfoCommand.prototype.Execute = function()
{
	this.CreateUrl();
	this.AddParamToUrl("PixelTolerance", this.pixelTolerance);
	//create an XMLHttp obj to send request to server
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("GET", this.url, false);
	xmlHttp.send(null);
	//get response back
	this.result = xmlHttp.responseText;	
	var div2;
	var div = FindElement("infoDIV");
	div.innerHTML="";
        var col_array=this.result.split("_XML_");
        var part_num=0;
        var tablo="<table class=infoTablo border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
        while (part_num < col_array.length)
        {
            if (col_array[part_num].length>0)
            {
                tablo+="<tr><td class=infoTablo id=\"td"+part_num+"\"></td></tr>";
            }
            part_num=part_num+1;
        }
        tablo+="</table>";
        div.innerHTML += tablo;
        var elementID;
        
    if (IsGC())
    {
        //alert("ameleeeeeeeeeee");
    }    	
    else 
    if (IsIE())
    {
	    // Load XML 	    	    
        var xml;
        var xsl;
        part_num=0;
                xml = new ActiveXObject("Microsoft.XMLDOM");
                xml.async = false;
                xsl = new ActiveXObject("Microsoft.XMLDOM");
                xsl.async = false;
		xsl.load("XSL/infoTool2.xsl");
        while (part_num < col_array.length)
        {            
            if (col_array[part_num].length>0)
            {
            
                xml.loadXML(col_array[part_num]);
        //it's often a good idea to view your result before trying out the xslt, 
        //just so you know what you're dealing with(just during development, i meant)
        
        // Load XSL
              //  if (part_num>0)
              //      xsl.load("XSL/infoTool2.xsl");
              //  else
              //      xsl.load("XSL/infoTool.xsl");
    	        elementID="td"+part_num;
	    //display the response at client html by transforming the xml with an xslt
	    //div.innerHTML = xml.transformNode(xsl);	 	    
	    //document.getElementById(elementID).innerHTML="<iframe src=\"http://www.google.com\"></iframe>" 
	            document.getElementById(elementID).innerHTML=xml.transformNode(xsl);
	        }    	    
	        part_num+=1;
        }        
	div.innerHTML="<div style=\"text-align: right;\"><img src=\"images/InfoTool/cross.gif\" onclick=\"HideElement('infoDIV')\"/></div>"+div.innerHTML;	
	}
	
	else if (IsFF())
	{
	    part_num=0;
	    var amele =col_array[0];
		var onloadx;
	    var processor = new XSLTProcessor();
	    var testTransform; //= document.implementation.createDocument("", "test", null);
	    var domToBeTransformed = document.implementation.createDocument("", "test"+part_num, null);
	    var parser=new DOMParser();
        var domToBeTransformed;
        var newDocument;
        var serializer = new XMLSerializer();
        var xml = "";
        var i;
        
                testTransform = document.implementation.createDocument("", "test"+part_num, null);

	        testTransform.addEventListener('load', function() {        	    	        
	        processor.importStylesheet(testTransform);
	        
        for (var j = 0; j < col_array.length; j++)
        {            
            if (amele.length>0)
            {
	        
	        elementID="td"+j;
	        amele=col_array[j];
	       domToBeTransformed=parser.parseFromString(amele ,"text/xml");
            
            div = FindElement(elementID);
            newDocument = processor.transformToDocument(domToBeTransformed);            
            xml="";
            for (i = 0; i < newDocument.childNodes[0].childNodes.length; i++)
                xml += serializer.serializeToString(newDocument.childNodes[0].childNodes[i]);
            div.innerHTML = xml;
            
            }
            part_num=part_num+1;
        }
            
            
        }, false);
    
        testTransform.load("XSL/infoTool2.xsl");
//            
	    div=document.getElementById("infoDIV");
	    div.innerHTML="<div style=\"text-align: right;\"><img src=\"images/InfoTool/cross.gif\" onclick=\"HideElement('infoDIV')\"/></div>"+div.innerHTML;
	}	
    div=FindElement("infoDIV");
 
        
        
	if(div.style.visibility != "visible")
	{	
		if (col_array.length>0)
		div.style.visibility = "visible";	
	}
    		
	//div positioning	
    div.style.left = this.interaction.PointsData.Points[0].x;
    div.style.top = this.interaction.PointsData.Points[0].y;
    
    
};

function CollapseExpandLayer(name)  //Hide or display the additional info for a map feature
{
    var div = FindElement(name);
    if (div.style.display != "block")
    {
        div.style.display = "block";
    }
    else
    {
        div.style.display = "none";
    }
}

function HideElement(name)          //Hide the info popup when the close button is clicked
{
    var element = FindElement(name);
    if (element.style.visibility != "hidden") element.style.visibility="hidden";
}
