// No part of this document may be reproduced, stored in or introduced into a retrieval system,
// or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise),
// or for any purpose, without the express written permission of the State Of West Virginia.
// Individuals or corporate entities who fail to comply with this notice are subject to being prosecuted
// under ARTICLE 3C. WEST VIRGINIA COMPUTER CRIME AND ABUSE ACT and/or any other applicable state or federal law. 
//
// © 2004 State of West Virginia. All rights reserved.

/*
--------------------   Google Map  functions here-----------------
*/

var map;
var Gmap;
var baseIcon;
var geocoder;
var xmlhttp

  
    // set a reference to the page's map object
   function setMap(thisMap, thisGeocoder)
   {
        map = thisMap; 
       geocoder =  thisGeocoder;        
   }   

    function SetMapCenter(newLat, newLon, zoomLevel)
    {
           map.setCenter(new GLatLng(newLat, newLon), zoomLevel);
    }  
   
  function setCountyOverlay()
  {
          // === Create the tilelayer ===
        var tilelayer = new GTileLayer(new GCopyrightCollection(), 0, 17); // 0 and 17 is scale range
       
        // === Set the getTileUrl to use the Google KML tile processor with our compressed KMZ file ===
        tilelayer.getTileUrl = function(a,b)
        {
            b=this.maxResolution()-b;
            	            
	        return "http://maps.google.com/mapsdt?id=http://www.comparecarewv.gov/counties_line_clean.kmz&x="+a.x+"&y="+a.y+"&zoom="+b;
        }
        
        tilelayer.getOpacity = function()
        {
            return 0.4;
        }
        //-- add a GTileLayerOverlay to it ===
        var counties = new GTileLayerOverlay(tilelayer)
        map.addOverlay(counties);
  
  }
  
    
   
  function processXMLDocument(thisXMLDoc)
  {
        var xmlDoc = GXml.parse(thisXMLDoc);          
        var x=xmlDoc.documentElement;
        var y=x.childNodes;
//        var hospitalCount = 0;
        var switchNode; 
               
	    var myLat, myLon, myFacText, myLine1Text, myCityText, myStateText, myZipText
	    var  myMainPhoneText, myFAXPhoneText, myTypeIDText, myDescriptionText
	    var myImageText, myClassTitleText, myContactTitleText, myPhoneTitleText, myURLText
        
        
        for (i=0;i<y.length;i++)
        {
            if(y[i].nodeType ==1)
            {              
//                hospitalCount ++
                
                //document.write("<br />Begin Hospital Node: " + hospitalCount  + "<br /><br />");
                
                for (z=0;z<y[i].childNodes.length;z++)
                {                 
                    if(y[i].childNodes[z].nodeType ==1)
                    {
                        switchNode = y[i].childNodes[z].nodeName;
                                                                            
                        // this is hospital data in here!
                        //document.write("nodeName: " + y[i].childNodes[z].nodeName); 
                        //document.write(" =  " + y[i].childNodes[z].firstChild.nodeValue + "<br />")
                       switch(switchNode)
                        {
                            case "lat":   myLat = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "lon":   myLon = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "facilityname":   myFacText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "line1":   myLine1Text = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "city":   myCityText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "state":   myStateText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "zip":   myZipText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "main":   myMainPhoneText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "fax":   myFAXPhoneText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "typeid":   myTypeIDText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "description":   myDescriptionText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "image":   myImageText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "contactTitle":   myContactTitleText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "classTitle":   myClassTitleText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "phoneTitle":   myPhoneTitleText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            case "URL":   myURLText = y[i].childNodes[z].firstChild.nodeValue;
                            break;                                
                            default: //code to be executed if n is   different from case 1 and 2
                        } // end switch               
                    }   //  end if(y[i].childNodes[z].nodeType ==1)
                }  //  end for
               
     		    var point = new GPoint(parseFloat(myLon), parseFloat(myLat)); 	     		          		         		         		    		    
      		    var thisAddrContent =  "<b>" + myFacText + "</b>" + "<br/>"  + myLine1Text + "<br/>" + myCityText + ',' + myStateText + ' ' + ' ' + myZipText + "<br/>"+ "<a href='" + myURLText + "'>" + myURLText + "</a>";
      		    var thisPhoneContent = "Main Number: " + myMainPhoneText + "<br/>"  + "FAX Number: " +  myFAXPhoneText;
      		          		    
      		   // go create the markers from the file content 
      		   var marker =   createClassificationTabbedMarker (point,
      		                                                                    myContactTitleText,
      		                                                                    thisAddrContent,
      		                                                                    myPhoneTitleText,
      		                                                                    thisPhoneContent,
      		                                                                    myClassTitleText,
      		                                                                    myDescriptionText);
      		                                                                    
                map.addOverlay(marker);
               
                switchNode = "";
                myLat = "";
	            myLon= "";
	            myFacText= "";
	            myLine1Text= "";
	            myCityText= "";
	            myStateText= "";
	            myZipText= "";
	            myMainPhoneText= "";
	            myFAXPhoneText= "";
	            myTypeIDText= "";
	            myDescriptionText= "";
	            myImageText= "";
	            myClassTitleText= "";
	            myContactTitleText= "";
	            myPhoneTitleText= "";
	            myURLText= "";
               
                                
                
            }  //  end  if(y[i].nodeType ==1)
        }  //  end   for (i=0;i<y.length;i++)
  }
   
  
    function selectBrowserType(url)
    {
        xmlhttp=null
        
        // code for Mozilla, etc.
        if (window.XMLHttpRequest)
        {
            //alert("Using Mozilla....") 
            xmlhttp=new XMLHttpRequest()
        }
        // code for IE
        else if (window.ActiveXObject)
        {
            //alert("Using IE....")
            setClassMarkers(url);          
            //xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
        }
         
        //  if using mozilla just fall thru and get the XML
        //  from the callback and branch off and process it   
        if (xmlhttp!=null)
        {
            xmlhttp.onreadystatechange=state_Change
            xmlhttp.open("GET",url,true)
            xmlhttp.send(null)
        }
      
        }function state_Change()
        {
            // if xmlhttp shows "loaded"
            if (xmlhttp.readyState==4)
            {      
                // if "OK"
                if (xmlhttp.status==200)
                {
                    var myXML =  xmlhttp.responseText;            
                    processXMLDocument(myXML);       
                }
                else
                {
                alert("Problem retrieving XML data")
            }
        }
    }
  


    // use an AJAX call to get the XML file from the server down to the client 
    function setClassMarkers(thisXMLFile)
    { 
       
        var myDoc;       
        GDownloadUrl(thisXMLFile,
        function(data, responseCode)
        {
            var myXMLDoc = GXml.parse(data);
                        
            // get all the elements for the hospitals(n)
            var myLat  = myXMLDoc.documentElement.getElementsByTagName("lat");             
            var myLon  = myXMLDoc.documentElement.getElementsByTagName("lon"); 
            var myFacName = myXMLDoc.documentElement.getElementsByTagName("facilityname"); 
            var myLine1 = myXMLDoc.documentElement.getElementsByTagName("line1"); 
            var myCity = myXMLDoc.documentElement.getElementsByTagName("city"); 
            var myState = myXMLDoc.documentElement.getElementsByTagName("state"); 
            var myZip = myXMLDoc.documentElement.getElementsByTagName("zip"); 
            var myMainPhone = myXMLDoc.documentElement.getElementsByTagName("main"); 
            var myFaxPhone  = myXMLDoc.documentElement.getElementsByTagName("fax"); 
            var myTypeID = myXMLDoc.documentElement.getElementsByTagName("typeid"); 
            var myDescription = myXMLDoc.documentElement.getElementsByTagName("description");             
            var myImage = myXMLDoc.documentElement.getElementsByTagName("image"); 
            var myContactTitle = myXMLDoc.documentElement.getElementsByTagName("contactTitle"); 
            var myClassTitle = myXMLDoc.documentElement.getElementsByTagName("classTitle"); 
            var myPhoneTitle = myXMLDoc.documentElement.getElementsByTagName("phoneTitle"); 
            var myURL = myXMLDoc.documentElement.getElementsByTagName("URL"); 
                        
            // Use the lat elements as an iterator. If there are no lat/lon elements/attributes then you dont want it anyway
            for (var i = 0; i < myLat.length; i++)
	    	{
	    		    	    	    		 	    	
	    	    if(myFacName[i] != null){var myFacText = myFacName[i].text;}	    	   
	    	    if(myLine1[i] != null){var myLine1Text = myLine1[i].text;}
	    	    if(myCity[i] != null){var myCityText = myCity[i].text;}
	    	    if(myState[i] != null){var myStateText = myState[i].text;}
	    	    if(myZip[i] != null){var myZipText = myZip[i].text;}
	    	    if(myMainPhone[i] != null){var myMainPhoneText = myMainPhone[i].text;}
	    	    if(myFaxPhone[i] != null){var myFAXPhoneText = myFaxPhone[i].text;}
	    	    if(myTypeID[i] != null){var myTypeIDText = myTypeID[i].text;}
	    	    if(myDescription[i] != null){var myDescriptionText = myDescription[i].text;}
	    	    if(myImage[i] != null){var myImageText = myImage[i].text;}
	    	    if(myContactTitle[i] != null){var myContactTitleText = myContactTitle[i].text;}
	    	    if(myClassTitle[i] != null){var myClassTitleText = myClassTitle[i].text;}
	    	    if(myPhoneTitle[i] != null){var myPhoneTitleText = myPhoneTitle[i].text;}
	    	    if(myURL[i] != null){var myURLText = myURL[i].text;}
	    	   
     		    var point = new GPoint(parseFloat(myLon[i].text), parseFloat(myLat[i].text)); 	     		          		         		         		    		    
      		    var thisAddrContent =  "<b>" + myFacText + "</b>" + "<br/>"  + myLine1Text + "<br/>" + myCityText + ',' + myStateText + ' ' + ' ' + myZipText + "<br/>"+ "<a href='" + myURLText + "'>" + myURLText + "</a>";
      		    var thisPhoneContent = "Main Number: " + myMainPhoneText + "<br/>"  + "FAX Number: " +  myFAXPhoneText;
      		          		    
      		   // go create the markers from the file content 
      		   var marker =   createClassificationTabbedMarker (point,
      		                                                                    myContactTitleText,
      		                                                                    thisAddrContent,
      		                                                                    myPhoneTitleText,
      		                                                                    thisPhoneContent,
      		                                                                    myClassTitleText,
      		                                                                    myDescriptionText);
      		                                                                    
                map.addOverlay(marker);
      		                                                                    
                  
    		}    		
        }); //  end callback function
   } // end method
  
  
  // pass in a typeID to select which xml file to load markers from
  function setAHAClassificationMarker(typeID)
  {  
        selectBrowserType("./XML/hospitals" + typeID + ".xml");  
  }
  
  
    // Create a base icon for all markers that specifies the shadow, icon dimensions, etc.   
  function setBaseIcon()
  {
        baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
       
        return baseIcon; 
  
  }       

    function createClassificationTabbedMarker (point,
                                                                                    tabAddrTitle, tabAddrContent, 
                                                                                    tabPhoneTitle, tabPhoneContent,
                                                                                    tabClassTitle, tabClassContent)
  {
        // build the tabs here....
//        var infoTabs = [
//                                      new GInfoWindowTab(tabAddrTitle, tabAddrContent),
//                                      new GInfoWindowTab(tabPhoneTitle, tabPhoneContent),
//                                      new GInfoWindowTab(tabClassTitle, tabClassContent)
//                                    ];

        var infoTabs = [
                                      new GInfoWindowTab(tabAddrTitle, tabAddrContent),
                                      new GInfoWindowTab(tabPhoneTitle, tabPhoneContent)
                                      //new GInfoWindowTab(tabClassTitle, tabClassContent)
                                    ];


        // set tthe icon for the marker
        var myShadow = setBaseIcon();
         var icon = new GIcon(myShadow);
        icon.image = "./_img/hospital-sign.png";
       
       
        tabAddrContent = null;
        tabPhoneContent = null;
        tabClassContent = null;

        // now build the marker and hook the onclick to it
        //  to bring up the tabbed dialog 
        var marker = new GMarker(point, icon);
        GEvent.addListener(marker, "click",
        function()
        {
            marker.openInfoWindowTabsHtml(infoTabs);
        }
        );
       
       return marker; 
//        map.addOverlay(marker);
        
         
  }

    function PopulateGenericPrintingWindow()
     {
      // This routine expect the caller to have a DIV named PrintContent
      // and the desination template to have a DIV named Content 
      // which can then be surrounded by all of the formatting you want
      // The original page also needs a hidden IFRAME called 
      // HiddenPrintFrame to receive the focus and print
      var PrintFrame;

      this.document.all['Content'].innerHTML = parent.document.all['PrintContent'].innerHTML;
      PrintFrame = parent.frames['HiddenPrintFrame'];
      if (PrintFrame)
       {
	        // IE requires you to set focus first
	        PrintFrame.focus();
	        PrintFrame.print();
      }
    }

