﻿// JScript File
var map = null;
         //pass xml dataset in hidden field, create points from that
        //holds xml field arrays
         var colAddress;
         var colProgram;
         var colWork_type;
         var colX;
         var colY;
         
         var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
         xmlDoc.async="false";


function GetMap()
         {
            loadXML();
            //alert(document.getElementById("hvXMLText").value);
           
            map = new VEMap('myMap');
            map.LoadMap(); 
            map.AttachEvent("onclick",MouseHandler);
            // This only works in 2D mode 
            if (map.GetMapMode() != VEMapMode.Mode2D)
            {
               alert("This only works in 2D mode");
               return;
            }
            var latLong01 = new VELatLong(document.getElementById("hvCenterLat").value, document.getElementById("hvCenterLon").value); 
            map.SetCenterAndZoom(latLong01, document.getElementById("hvZoomlevel").value);
            map.SetMapStyle(VEMapStyle.Hybrid);
            //map.AttachEvent("onclick", ShapeInfo);

            map.ClearInfoBoxStyles();
            AddShapes();
         }  
     
         function AddShapes()
         {
                   
            CreateLayer(); 

             //try to put it in 1 loop using the xml dataset
          var shapes = new Array(); // pushpin array
          for (var i = 0; i < colLat.length; i++)
          {
                var latLon = new VELatLong(colLat[i].text, colLon[i].text);
                shapes[i] = new VEShape(VEShapeType.Pushpin, latLon); 
                shapes[i].SetTitle(colName[i].text);
                shapes[i].SetDescription('Not specified');
                shapes[i].SetPhotoURL(colPic1[i].text);
                try {
                    shapes[i].SetDescription(colStory[i].text);
                }
                    catch(err)
                {
                    //Skip errors
                }
                //custom info box
                //if (colType[i].text == 'Shop') {
                    shapes[i].SetDescription('<iframe src="http://urbanboatbuilders.org" width="65%" height="50%"></iframe>');
                //} else {
                    var picFile = ""
                    var description = ""
                    try {
                         picFile1 = "'images/" + colPic1[i].text + "'";
                         //picFile1 = "'" + colPic1[i].text + "'";
                        if (colStory[i].text.length > 50) {
                            var tmparray = colStory[i].text.split(" ");
                            var tmpstr = "";
                            for (var j = 0; j < tmparray.length; j++) {
                            //Insert a line break every 10 words
                                if (j != 10 && j != 20 && j != 30 && j != 40 && j != 50 && j != 60 && j !== 70) {
                                   tmpstr = tmpstr + tmparray[j] + " ";
                                } else {
                                    tmpstr = tmpstr + tmparray[j] + "<br>" + " ";
                                }      
                            }
                          colStory[i].text = tmpstr;  
                        }
                        description = colStory[i].text; 
                    } catch(err) {
                    
                     }
                    try {
                        picFile2 = "'images/" + colPic2[i].text + "'";
                    } catch(err)
                    { }
                    if (colPic1[i].text != " ") {  
                        description = description + "<p>" + "<img src=" + picFile1 + " width='300px'></img></p>";
                        //If 2 pictures, make them smaller
                        if (colPic1[i].text != '' && colPic2[i].text != '') {
                            description = colStory[i].text; 
                            description = description + "<p><img src=" + picFile1 + " height='200px'></img></p>";
                            description = description + "<p><img src=" + picFile2 + " height='200px'></img></p>";
                        }
                    }
                    shapes[i].SetDescription(description);
                //}
                       
                         
               //Show labels only if checkbox is checked
             var label = '';  
             var backColor = '';
             if (document.getElementById("ChkLabels").checked) {
                label = colName[i].text;  
                backColor = 'background-color:white; '
             }
             
             if (document.getElementById("ChkMarkers").checked != 1) {
                return;
             }
                  
             //Change marker color for different types         
                if (colType[i] != null) {
                    if (colType[i].text == "Shop" && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "color:Red; font-weight:bold; " + backColor + "'>" + 
                        "<img src='images/TheShop.gif'/>" + label + "</span>");
                    } else if (colType[i].text == "Start"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/boatlaunch.gif'/><b>" + label + "</b></span>");
                   } else if (colType[i].text == "Finish"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/boatlaunch.gif'/><b>" + label + "</b></span>");     
                   } else if (colType[i].text == "Scary"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/scared.gif'/><b>" + label + "</b></span>");     
                   } else if (colType[i].text == "Campsite"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/tent.gif'/><b>" + label + "</b></span>"); 
                   } else if (colType[i].text == "Lunch"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/lunch.gif'/><b>" + label + "</b></span>");
                   } else if (colType[i].text == "Fun"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/Laugh2.gif'/><b>" + label + "</b></span>"); 
                     } else if (colType[i].text == "Dam"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/damAnimated.gif'/><b>" + label + "</b></span>");   
                     } else if (colType[i].text == "General"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/RedCircle.gif'/><b>" + label + "</b></span>");  
                     } else if (colType[i].text == "Historic"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/YellowCircle.gif'/><b>" + label + "</b></span>"); 
                     } else if (colType[i].text == "Pollution"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/spill.gif'/><b>" + label + "</b></span>");      
                     } else if (colType[i].text == "WaterTest"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/watertest.gif'/><b>" + label + "</b></span>");       
                     } else if (colType[i].text == "Sad"  && colType[i] != null) {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Blue; " + backColor + "'>" + 
                        "<img src='images/Frown.gif'/><b>" + label + "</b></span>");                                      
                
                    } else {
                        shapes[i].SetCustomIcon(
                        "<span style='font-family:Arial; font-size:xx-small;" +
                        "font-weight:bold; color:Gray; " + backColor + "'>" + 
                        "<img src='images/GrayCircle.gif'/><b>" + colName[i].text + "</b></span>");
                    }  
                } else {
                   shapes[i].SetCustomIcon(
                  "<span style='font-family:Arial; font-size:xx-small;" +
                  " font-weight:bold; color:Gray; " + backColor + "'>" + 
                  "<img src='images/GrayCircle.gif'/><b>" + colName[i].text + "</b></span>");
                }
            // }               
                
                
                layer.AddShape(shapes[i]);
          } // end of for loop

         } 
     
     function MouseHandler(e)      
     {         
        var msg;         
        if (e.eventName == "onclick")         
        {            
            if (e.leftMouseButton) {              
              //  msg = "onclick (left mouse button)  event"; 
               DoPixelToLL(e.clientX, e.clientY);          
            } else if (e.rightMouseButton)  {             
                msg = "onclick (right mouse button)  event";            
            } else if (e.leftMouseButton)  {             
                msg = "onclick (middle mouse button)  event";         
            }   
            } else  {                   
                msg = e.eventName + " event.";         
            }         
           // document.getElementById('resultDiv').innerHTML=msg;      
     }
     
     
     function DoPixelToLL(x,y) 
     { 
        var ll = map.PixelToLatLong(new VEPixel(x,y)).toString()  
        //round the numbers to 100,000ths
        var temparr = ll.split(",");
        temparr[0] = Math.round(temparr[0] * 100000)/100000;
        temparr[1] = Math.round(temparr[1] * 100000)/100000; 
        document.getElementById('Lat').value = temparr[0];  
        document.getElementById('Lon').value = temparr[1];        
        //alert("The latitude,longitude of the pixel at ("+x+","+y+") is: "+ll)         
     }
//         function ShapeInfo(e)
//         {
//            if(e.elementID != null)
//            {
//               shape = map.GetShapeByID(e.elementID);
//             
//               //Respond to ctrl-click event (toggle polygon icon visibility).
//               //Note that pushpin shapes ignore ShowIcon and HideIcon.
//               if(e.ctrlKey == 1)
//               {
//                  shape.HideIcon();
//               }
//               else
//               {
//                  shape.ShowIcon();
//               }
//             
//               //Display information for the selected shape.
//               var info = "";
//               info += "ID (event object): " + e.elementID + "<br />";
//               info += "ID (GetID method): " + shape.GetID() + "<br />";
//               info += "Type: " + shape.GetType() + "<br />";
//               info += "Title: " + shape.GetTitle() + "<br />";                 
//               icon = shape.GetCustomIcon();
//               info += "Icon: " + icon + "<br />";
//                
//               //Note that the Icon Anchor value for pushpin shapes is always
//               //the latitude and longitude of the pin itself.
//               info += "Icon Anchor: " + shape.GetIconAnchor() + "<br />";
//               label.innerHTML = info;
//            }
//         }
         
         function loadXML()
        {
         var t = document.getElementById("hvXMLText").value;
         document.getElementById("hvXMLText").value = '';
            
            
            
        // code for IE
        if (window.ActiveXObject)
          {
          xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.async=false;
          xmlDoc.loadXML(t);
          xmlObj=xmlDoc.documentElement; 
//alert(xmlObj.childNodes.length);
          parseXML();
          }
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation &&
        document.implementation.createDocument)
          {
          xmlDoc=document.implementation.createDocument("","",null);
          xmlDoc.loadXML(t);
          xmlDoc.onload=parseXML;
          }
        else
          {
          alert('Your browser cannot handle this script');
          }
        }
        
        function parseXML()
            {
            var sXPath = "/NewDataSet/Waypoints/Lat";
            colLat = xmlDoc.selectNodes(sXPath);
            var sXPath = "/NewDataSet/Waypoints/Lon";
            colLon = xmlDoc.selectNodes(sXPath);
            sXPath = "/NewDataSet/Waypoints/Type";
            colType= xmlDoc.selectNodes(sXPath);
            sXPath = "/NewDataSet/Waypoints/Name";
            colName= xmlDoc.selectNodes(sXPath);
            sXPath = "/NewDataSet/Waypoints/Story";
            colStory = xmlDoc.selectNodes(sXPath);
            sXPath = "/NewDataSet/Waypoints/Pic1";
            colPic1 = xmlDoc.selectNodes(sXPath);
            sXPath = "/NewDataSet/Waypoints/Pic2";
            colPic2 = xmlDoc.selectNodes(sXPath);
            }
            
        function chkLabels_Changed()
            {
                DeleteLayer();
                AddShapes();  
            }    
       
         function chkMarkers_Changed()
            {
                DeleteLayer();
                AddShapes();  
            }       
      
      function CreateLayer()
      {
         layer = new VEShapeLayer();
         map.AddShapeLayer(layer);
      }
      
      function DeleteLayer()
      {
         if(layer != null)
         {
            map.DeleteShapeLayer(layer);
            layer = null;
         }
      }
      
      function DeleteShape()
      {
         if(shape != null)
         {
            layer.DeleteShape(shape);
            shape = null;
         }
      }
     
     
