var vmsMarkers = new Array();
var existingVMSArray = new Array();
var addVMSFlag = true;
var checkExistingVMS = false; //flag to check existing array - only changes if we've already gotten data
var vmsExist = false;
var vmsVisible = false;

//constants for VMS directions, stored as integers in the DB
var directionArray = new Array('unknown', 'Northbound', 'Southbound', 'Eastbound', 'Westbound', 'Both', 'NorthSouth', 'EastWest');

function createVMSPopups(elem, xmlDoc) {
	//create popups from XML data
	
	
	//initialize markers to display false - send type to display false
	initializeEventMarkers(vmsMarkers);
	
	var vmsChecked = document.main.vmsCheck.checked;
	
	//alert("vmsChecked: " + vmsChecked);
	for (var i = 0; i < elem.length; i++) {

	//loop through and create markers	
		
		//since we have layer checkboxes, we wont want to display the marker if the
		//checkbox is unchecked, or layer is toggled off - we can store the marker 
		//for later use - just don't add the overlay
		var displayMarker = true;
		
		var VMS_ID = xmlDoc.getElementsByTagName("VMS_ID")[i].firstChild.nodeValue;
		var direction = xmlDoc.getElementsByTagName("Direction")[i].firstChild.nodeValue.toLowerCase();
		var location = xmlDoc.getElementsByTagName("Location")[i].firstChild.nodeValue;
		var lat = parseFloat(xmlDoc.getElementsByTagName("Latitude")[i].firstChild.nodeValue);
		var lon = parseFloat(xmlDoc.getElementsByTagName("Longitude")[i].firstChild.nodeValue);
		var vmsMessage = xmlDoc.getElementsByTagName("VMS_Message")[i].firstChild.nodeValue;
		var lastUpdated = "";
//		var lastUpdated = xmlDoc.getElementsByTagName("LastUpdated")[i].firstChild.nodeValue;
		var status = xmlDoc.getElementsByTagName("Status")[i].firstChild.nodeValue;
		
		var textColor = "";
			
//		if (vmsMessage == "null") {
//			vmsMessage = "BLANK";
//		}

		if (checkExistingVMS) {
			var addToExistingArray = false;
			//we already have data in the array, now check to see if we need to remove markers
			for (var n = 0; n < vmsMarkers.length; n++) {
				var existingVMSID = vmsMarkers[n].getId();
				if (VMS_ID == existingVMSID){
					//event already on map - do not display twice
	                
					//now set display flag based on checkbox status --- ugghh
					addToExistingArray = true;
					
					if (addToExistingArray) {
						existingVMSArray[existingVMSArray.length] = VMS_ID;
					}
					addVMSFlag = false;
					break;
				}
				else {
					//marker does not exist - set add flag to true to notify that
					//we have to add it to the map
					addVMSFlag = true;  
				}
			}        
		}
    
	    if (addVMSFlag) {	
        //create new markers
    			
    		var point = new GLatLng(lat,lon);	
    			
    		//if (parseInt(status) == 4) {
                if (vmsMessage==null || vmsMessage=='' || vmsMessage=='null') {
    			icon = vmsInactiveIcon;
    			textColor = "#990000";
			vmsMessage = "SIGN BLANK";
    		}
    		else {
    		    icon = vmsActiveIcon;
    		    textColor = "#000099";
    		}
    		
    		//alert("icon:" + icon.image + "vms message="+vmsMessage);
    		vmsVisible = true;
    		var vmsMarker = new PdMarker(point, icon);
    		
    		
    		if (!document.main.vmsCheck.checked) {
    		    displayMarker = false;
    		}
    		
    		//GLog.write("displayMarker: " + displayMarker + " , point: " + point);	
    				
    		vmsMessage = '<b><font style="color: ' + textColor + ';">' + vmsMessage + '</font></b><br />';
    										
    		// BUILD HTML MARKUP for info window
            var infoHtmls = '<div class="markerinfo" style="width:260px; font-size: 11px;">';
            infoHtmls += '<b><font style="color: ' + textColor + ';">' + location.toUpperCase() + '</font></b>';
    		infoHtmls += '<br /><br />';	
    		infoHtmls += vmsMessage;
            infoHtmls += '<br />';
         	infoHtmls += lastUpdated;
            infoHtmls += '</div>';		
    				
    		vmsMarker.tooltip = '<div class="markerTooltip" style="color:'+textColor+'; font-weight:bold;">'
    		vmsMarker.tooltip += vmsMarker.tooltip + location + '<br>';
    		vmsMarker.tooltip += directionArray[parseInt(direction)] + '</div>';
    
    		createVMSMarker(point, vmsMarker, infoHtmls);
    		
            var length = vmsMarkers.length;
    		
    		vmsMarkers[length] = vmsMarker;
    		//need to append vms_ to id because some vms and cameras shared id
            vmsMarkers[length].setId("vms_"+VMS_ID); // over-ride internal id
            googleMap.addOverlay(vmsMarker);
            
            //GLog.write("vmsMarkers[" + length + "]: " + vmsMarkers.length);
            if (displayMarker) {
                //only display the marker if the particular layer checkbox is checked
                vmsMarker.display(true);
                //GLog.write("display true");
            }
            else {
			    vmsMarker.display(false);
			    //GLog.write("display false");
            }
            vmsMarker = null;
        } 
   // addFlag = true;
		
	}//end for loop looping through XML elements      
        
    //GLog.write("checkExistingVMS: " + checkExistingVMS);
	//function to display markers true, make sure something is in the array first
	if (checkExistingVMS) {
	    for (var n = 0; n < existingVMSArray.length; n++) {
            var markerToDisplay = googleMap.getMarkerById(existingVMSArray[n]);
            GLog.write("vms markerToDisplay: " + markerToDisplay.getId());
            markerToDisplay.display(true);
        }
	}
    
    //now that we've gotten data, we need to set a flag to let us know to 
    //check existing next time the user updates the map
    checkExistingVMS = true;

    	
	//hide please wait screen
	//functions are in javascript/showHidePleaseWaitScreen.js
	hidePleaseWaitScreen();
	
    //reset existing marker array
    existingVMSArray = [];
    
    
}

function createVMSMarker(point, marker, html) {

	//alert(point + " , " + marker + " , " + html);
	
  	GEvent.addListener(marker, "click", function() {
  	 // if (CCTVPopupDisplayed) {
     //     closeCCTVPopupWindow();
     // }
      marker.openInfoWindowHtml(html);
  	});

    GEvent.addListener(marker,"mouseover", function() {
    	showTooltip(marker);
    });        
    GEvent.addListener(marker,"mouseout", function() {
		markerTooltip.style.visibility="hidden"
    });        
}
