﻿//variable declaration
var frameWidth, frameHeight;

function UnloadEverything(){
    GUnload();
    frameHeight = undefined;
    frameWidth = undefined;
    map = undefined;
    mapName = undefined;
    geocoder = undefined;
    bounds = undefined;
    trafficInfo = undefined;
    trafficVisible = undefined;
    _mFlags = undefined;
    mapLoaded = undefined;
    Array_IconImage = undefined;
    Array_IconImportance = undefined;
    baseIcon = undefined;
    utilityWidth = undefined;
    contextmenu = undefined;
    clickedPixel = undefined;
    MyLocationMarker = undefined;
    MyLocationChangeDiv = undefined;
    MyLocationDiv = undefined;
    MyLocationAddressDiv = undefined;
    MyLocationAddressTextDiv = undefined;
    MyLocationSearchVal = undefined;
    oldHTML = undefined;
    Array_LocationTypeTranslation = undefined;
    directions = undefined;
    directionsPanel = undefined;
    yourLocale = undefined;
    Array_Marker = undefined;
    mgr = undefined;
    Array_GMarker = undefined;
    Array_HTML_Address = undefined;
    Array_HTML_Hours = undefined;
    Array_HTML_AMS = undefined;
    Array_FullAddress = undefined;
    Array_LatitudeLongitude = undefined;
    Array_LocationTypeTranslation = undefined;
    Array_AddressFailureReasons = undefined;
    i = undefined;
    useLatLngFlag = undefined;
    dirTypeFlag = undefined;
    currLocID = undefined;
}

function GetFrameWidth(){
    if (self.innerWidth){
        return self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth){
        return document.documentElement.clientWidth;
    }
    else if (document.body){
        return document.body.clientWidth;
    }
}

function GetFrameHeight(){
    if (self.innerWidth){
        return self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth){
        return document.documentElement.clientHeight;
    }
    else if (document.body){
        return document.body.clientHeight;
    }
}

function fixMapSize(){
    if (mapLoaded == true){
        map2Div = document.getElementById("map");
        map2Div.style.width = (GetAvailableWidth()).toString() + "px";
        map2Div.style.left = (GetUtilityWidth()).toString() + "px";
        map.checkResize();
    }
    mapLoaded = true;
}

function GetUtilityWidth(){
    w = 0;
    utilDiv = document.getElementById("messages");
    
    if (utilDiv.style.display == "" || utilDiv.style.display == "block"){
        w += utilityWidth;
    }
    return w;
}

function GetAvailableWidth(){
    w = GetFrameWidth() - GetUtilityWidth();
    if (w < 0 ){
        w = 0;
    }
    return w;
}
   
function hideUtilityBar(){
    utilityDiv = document.getElementById("messages");
    utilityDiv.style.display = "none";
    hiderDiv = document.getElementById("hideMessage");
    hiderDiv.style.display = "none";
    showerDiv = document.getElementById("showMessage");
    showerDiv.style.display = "block";
    map2Div = document.getElementById("map");
    map2Div.style.width = (GetAvailableWidth()).toString() + "px";
    map2Div.style.left = (GetUtilityWidth()).toString() + "px";
    map.checkResize();
}

function showUtilityBar(){
    utilityDiv = document.getElementById("messages");
    utilityDiv.style.display = "block";
    hiderDiv = document.getElementById("hideMessage");
    hiderDiv.style.display = "block";
    showerDiv = document.getElementById("showMessage");
    showerDiv.style.display = "none";
    map2Div = document.getElementById("map");
    map2Div.style.width = (GetAvailableWidth()).toString() + "px";
    map2Div.style.left = (GetUtilityWidth()).toString() + "px";
    map.checkResize();
}

function AddDoubleQuotes(value){
    return '"' + value + '"';
}

function AddSingleQuotes(value){
    return "'" + value + "'";
}

function importanceOrder (marker,b) {
    return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}

function trace(strType){
	if (strType == 'center'){
		document.getElementById("message").innerHTML = map.getCenter();
	}else if (strType == 'zoom'){
		document.getElementById("message").innerHTML = map.getZoom();
	}
}

function getGeo(address){
	geocoder.getLatLng(address,function(point) {
	  return point.toString();
	});
}

function CheckForOnScreenLocations(inLoop){
    var intCount = 0;
    var LLBounds = new GLatLngBounds();
    var newZoom = 0;
    var gb = new GLatLngBounds();

    LLBounds = map.getBounds();
    if (Array_Points.length > 0){
        for (x=0;x<Array_Points.length;x++){
            if (LLBounds.contains(Array_Points[x]) == true){
                intCount++;
                gb.extend(Array_Points[x]);
            }
        }
        gb.extend(MyLocationPoint);
        
        if (intCount == 0){
            if (inLoop != undefined){
                map.zoomOut();
                setTimeout('CheckForOnScreenLocations(1)', 300);
            }else{
                var doZoom = confirm("There are no locations on the screen.\n\n Is it ok for the map to zoom out until one is visible?");
                if (doZoom == true){
                    map.zoomOut();
                    setTimeout('CheckForOnScreenLocations(1)', 300);
                }
            }
        }else{
            newZoom = map.getBoundsZoomLevel(gb)-1;
            if (newZoom > map.getZoom()){
                map.setZoom(newZoom);
            }
            map.panTo(gb.getCenter());
            
            /*setTimeout(function(){
                LLBounds = map.getBounds();
                for (x=0;x<Array_Points.length;x++){
                    if (LLBounds.contains(Array_Points[x]) == true){
                        map.addOverlay(new GPolyline([MyLocationPoint,Array_Points[x]]));
                    }
                }
                map.addOverlay(new GPolyline([new GLatLng(gb.getNorthEast().lat(),gb.getSouthWest().lng()),gb.getNorthEast()]));
                map.addOverlay(new GPolyline([gb.getNorthEast(),new GLatLng(gb.getSouthWest().lat(),gb.getNorthEast().lng())]));
                map.addOverlay(new GPolyline([new GLatLng(gb.getSouthWest().lat(),gb.getNorthEast().lng()),gb.getSouthWest()]));
                map.addOverlay(new GPolyline([gb.getSouthWest(),new GLatLng(gb.getNorthEast().lat(),gb.getSouthWest().lng())]));           
            },500);*/
            
            setTimeout(function(){
                MyLocationRepositionedPoint = map.getCenter();
                MyLocationRepositionedZoom = map.getZoom();
            },500);
        }
    }
}

function ClearAllPolylines(){
    map.clearOverlays();
    if (mgr != undefined){
        mgr.refresh();
    }
    if (MyLocationMarker != undefined){
        map.addOverlay(MyLocationMarker);
    }  
}

function isInteger (s){
	var i;
	if (isEmpty(s))
	if (isInteger.arguments.length == 1) return 0;
	else return (isInteger.arguments[1] == true);
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}

function isEmpty(s){
	return ((s == null) || (s.length == 0))
}

function isDigit (c){
	return ((c >= "0") && (c <= "9"))
}