﻿// ZOOM CONTROL
function TextualZoomControl() {}
TextualZoomControl.prototype = new GControl();

TextualZoomControl.prototype.initialize = function(map) {  
    var container = document.createElement("div");
    
    var zoomInDiv = document.createElement("div");  
    this.setButtonStyle_(zoomInDiv);  
    container.appendChild(zoomInDiv);  
    zoomInDiv.appendChild(document.createTextNode("Zoom In"));
    GEvent.addDomListener(zoomInDiv, "click", function() {  
        //createTLabel(new GLatLng(245.448218,122.831171),'<nobr>This is my first TLabel</nobr>');  
        map.zoomIn();  
    });
      
    var zoomOutDiv = document.createElement("div");  
    this.setButtonStyle_(zoomOutDiv);  
    container.appendChild(zoomOutDiv);  
    zoomOutDiv.appendChild(document.createTextNode("Zoom Out"));
    GEvent.addDomListener(zoomOutDiv, "click", function() { 
        //map.removeTLabel(mylabel); 
        map.zoomOut();  
    }); 
     
    map.getContainer().appendChild(container);  
    return container;
    
    var n=0;
    var mylabel;
    function createTLabel(point,html) {
        var label = new TLabel();
         label.id = 'Label '+n;
         label.anchorLatLng = point;
         label.anchorPoint = 'bottomLeft';
         label.content = '<div style="width:21em;font-weight:normal;border:1px solid #0000cc;background:#ffffff;color:#0000cc;padding:4px;text-align:center;">'+html+'</div>';
         label.percentOpacity = 70;
         map.addTLabel(label);
         mylabel = label; 
         n++;
    }
}

TextualZoomControl.prototype.getDefaultPosition = function() {  
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));}

TextualZoomControl.prototype.setButtonStyle_ = function(button) {  
    button.style.textDecoration = "underline";  
    button.style.color = "#0000cc";  
    button.style.backgroundColor = "white";  
    button.style.font = "small Arial";  
    button.style.border = "1px solid black";  
    button.style.padding = "2px";  
    button.style.marginBottom = "3px";  
    button.style.textAlign = "center";  
    button.style.width = "6em";  
    button.style.cursor = "pointer";
}


// CONTEXT MENU CONTROL
function ContextMenu(oMap){this.initialize(oMap);}

//Construct the DOM tree of the menu
ContextMenu.prototype.initLink = function(oMap) {
    var that = this;
    a_link = document.createElement("li");
    a_link.innerHTML = "<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Zoom In</a>";
    GEvent.addDomListener(a_link, 'click', function() {
        that.map.zoomIn();
        that.contextmenu.style.visibility = "hidden";
    });
    this.ul_container.appendChild(a_link);

    a_link = document.createElement("li");
    a_link.innerHTML = "<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Zoom Out</a>";
    GEvent.addDomListener(a_link, 'click', function() {
        that.map.zoomOut();
        that.contextmenu.style.visibility = "hidden";
    });
    this.ul_container.appendChild(a_link);

    a_link = document.createElement("li");
    a_link.innerHTML = "<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Zoom Here</a>";
    GEvent.addDomListener(a_link, 'click', function() {
        var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
        that.map.zoomIn(point, true);
        that.contextmenu.style.visibility = "hidden";
    });
    this.ul_container.appendChild(a_link);

    a_link = document.createElement("li");
    a_link.innerHTML = "<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Center Map Here</a>";
    GEvent.addDomListener(a_link, 'click', function() {
        var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
        that.map.panTo(point);
        that.contextmenu.style.visibility = "hidden";
    });
    this.ul_container.appendChild(a_link);

    a_link = document.createElement("li");
    var imgBul = '<img src="Images/pinned.gif" border="0"  align="absmiddle" />&nbsp;'
    a_link.innerHTML = "<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Refresh Map</a>";
    GEvent.addDomListener(a_link, 'click', function() {
        LoadMap();resetUrlBar();ShowSearchByAddress();CoordsOnIdle(); 
        that.contextmenu.style.visibility = "hidden";
    });
    this.ul_container.appendChild(a_link);

    a_link = document.createElement("li");
    var imgBul = '<img src="Images/pinned.gif" border="0"  align="absmiddle" />&nbsp;'
    a_link.innerHTML = "<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Find Antipode</a>";
    GEvent.addDomListener(a_link, 'click', function() {
        var center = that.map.getCenter();
        that.map.panTo(new GLatLng(center.y * -1, center.x + 180));
        var newcenter = that.map.getCenter();
        that.map.setMapType(G_NORMAL_MAP);
        that.map.setZoom(7);
        var marker = new GMarker(newcenter);
        that.map.addOverlay(marker);
        marker.openInfoWindowHtml('<b>The Antipode coords are:</b><br><br><font color=#D41818><u>Latitude:</u></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + newcenter.y + '<br><font color=#D41818><u>Longitude:</u></font>&nbsp;&nbsp;' + newcenter.x);
        that.contextmenu.style.visibility = "hidden";
    });
    this.ul_container.appendChild(a_link);

    a_link = document.createElement("li");
    a_link.innerHTML = "<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Get Altitude</a>";
    GEvent.addDomListener(a_link, 'click', function() {
        var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
        //var url = 'get_altitude.php?alt_lat='+point.lat()+'&alt_lng='+point.lng();
        var url = 'Services/Get_Altitude.aspx?alt_lat=' + point.lat() + '&alt_lng=' + point.lng();
        StartLoading();
        that.contextmenu.style.visibility = "hidden";
        GDownloadUrl(url, function(data, responseCode) {
            var retval = data;
            if (that.activeLabel != null || that.timer != null) {
                map.removeTLabel(that.activeLabel);
                window.clearTimeout(that.timer);
                that.timer = null;
            }
            var label = new TLabel();
            label.id = 'tlabel_altezza';
            label.anchorLatLng = point;
            label.percentOpacity = 75;
            label.content = '<div style="width:21em;font-weight:normal;border:1px solid #0000cc;background:#ffffff;color:#0000cc;padding:4px;text-align:center;">' + retval + '</div>';
            that.map.addTLabel(label);
            FinishLoading();
            var cb = that.bind(function() {
                that.map.removeTLabel(that.activeLabel);
                that.activeLabel = null;
                that.timer = null;
            });
            that.timer = window.setTimeout(cb, 5000);
            that.activeLabel = label;
        });
    });
    this.ul_container.appendChild(a_link);

    a_link = document.createElement("li");
    a_link.innerHTML = "<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Get Weather</a>";
    GEvent.addDomListener(a_link, 'click', function() {
        var point = that.map.fromContainerPixelToLatLng(that.clickedPixel);
        var url = 'Services/Get_Weather.aspx?alt_lat=' + point.lat() + '&alt_lng=' + point.lng();
        StartLoading();
        that.contextmenu.style.visibility = "hidden";
        GDownloadUrl(url, function(data, responseCode) {
            var retval = data;
            if (that.activeLabel != null || that.timer != null) {
                map.removeTLabel(that.activeLabel);
                window.clearTimeout(that.timer);
                that.timer = null;
            }
            var label = new TLabel();
            label.id = 'tlabel_altezza';
            label.anchorLatLng = point;
            label.percentOpacity = 75;
            //label.content = '<img alt="" src="Images/ImgBox/icon_close.gif" style="float:right; padding-right:5px; padding-top:5px; cursor:crosshair" onclick="javascript:closeTLabel();" /><div style="width:30em;font-weight:normal;border:1px solid #0000cc;background:#ffffff;color:#0000cc;padding:4px;text-align:left;">' + retval + '</div>';
            label.content = '<div style="width:30em;font-weight:normal;border:1px solid #0000cc;background:#ffffff;color:#0000cc;padding:4px;text-align:left;">' + retval + '</div>';
            that.map.addTLabel(label);
            FinishLoading();
            var cb = that.bind(function() {
                that.map.removeTLabel(that.activeLabel);
                that.activeLabel = null;
                that.timer = null;
            });
            that.timer = window.setTimeout(cb, 10000);
            that.activeLabel = label;
        });
    });
    this.ul_container.appendChild(a_link);

    //	a_link = document.createElement("li");
    //	a_link.innerHTML="<a href='javascript:void(0);'>&raquo;&nbsp;&nbsp;Add Marker</a>";
    //	GEvent.addDomListener(a_link, 'click', function() {
    //	    var center = that.map.getCenter();
    //	    var zml = that.map.getZoom();
    //        var type = that.map.getCurrentMapType().getName();
    //        var group = document.getElementById("cboGroupList").selectedIndex;
    //	    var point = new GLatLng(center.y,center.x)
    //	    var marker = new GMarker(point);
    //        that.map.addOverlay(marker);
    //        var addMapPointPath = 'AddMapPoint.aspx?';
    //        var adlnk = '<a href=""#"" onclick=showPopWin(\''+ addMapPointPath + '&Group=' + group + '&Longitude=' + center.x + '&Latitude=' + center.y + '&Zoom=' + zml + '&Type=' + type +'\'\)>Save Map Point</a>';
    //        marker.openInfoWindowHtml("<font color=#D41818><u>Latitude:</u></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + center.y + "<br><font color=#D41818><u>Longitude:</u></font>&nbsp;&nbsp;" + center.x + "<br><br><br>" + adlnk);
    //	    that.contextmenu.style.visibility="hidden";
    //	});
    //	this.ul_container.appendChild(a_link);
}

ContextMenu.prototype.bind = function(method) {
	var self = this;
	var opt_args = [].slice.call(arguments, 1);
	return function() {
		var args = opt_args.concat([].slice.call(arguments));
		return method.apply(self, args);
	}
}

//The object 'constructor'
ContextMenu.prototype.initialize = function(oMap){
	this.map = oMap;
	var that=this;
	
	this.contextmenu = document.createElement("div");
	this.contextmenu.style.visibility="hidden";
//CSS class name of the menu
	this.contextmenu.className="contextmenu";
	this.ul_container = document.createElement("ul");
	this.ul_container.id="context_menu_ul";
	this.contextmenu.appendChild(this.ul_container);	
	this.initLink();
	this.map.getContainer().appendChild(this.contextmenu);	

//Event listeners that will interact with our context menu
	GEvent.addListener(oMap,"singlerightclick",function(pixel,tile) {
		that.clickedPixel = pixel;
		var x=pixel.x;
		var y=pixel.y;
//Prevents the menu to go out of the map margins, in this case the expected menu size is 150x110
		if (x > that.map.getSize().width - 160) { x = that.map.getSize().width - 160 }
		if (y > that.map.getSize().height - 120) { y = that.map.getSize().height - 120 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
		pos.apply(that.contextmenu);
		that.contextmenu.style.visibility = "visible";
	});	
	GEvent.addListener(oMap, "move", function() {
		that.contextmenu.style.visibility="hidden";
	});
	GEvent.addListener(oMap, "click", function(overlay,point) {
		that.contextmenu.style.visibility="hidden";
	});	
}

// REFRESH CONTROL
function TextualRefreshControl() {}
    TextualRefreshControl.prototype = new GControl();
    TextualRefreshControl.prototype.initialize = function(map) {
        var container = document.createElement("div");
        var refreshDiv = document.createElement("div");
        this.setButtonStyle_(refreshDiv);
        container.appendChild(refreshDiv);
        refreshDiv.appendChild(document.createTextNode("Refresh"));

        GEvent.addDomListener(refreshDiv, "click", function() {
            LoadMap();resetUrlBar();ShowSearchByAddress();CoordsOnIdle(); 
        });

        map.getContainer().appendChild(container);
        return container;
    }

TextualRefreshControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(360, 7));
}

TextualRefreshControl.prototype.setButtonStyle_ = function(button) {
    button.style.textDecoration = "none";
    button.style.color = "#CC3333";
    button.style.backgroundColor = "white";
    button.style.fontFamily = "Verdana";
    button.style.fontWeight = "Bold";
    button.style.border = "1px solid black";
    button.style.padding = "2px";
    button.style.margin = "0px";
    button.style.textAlign = "center";
    button.style.width = "6em";
    button.style.fontSize = "11px";
    button.style.cursor = "pointer";
}

// DRAG ZOOM OPTIONS
var boxStyleOpts = {
    opacity:.2,
    border:"1px solid #CC3333"
}
var otherOpts = {
    buttonStartingStyle: { display:'block',color:'black',background:'white',width:'7em',textAlign:'center',padding:'2px',
        fontFamily: 'Verdana', fontSize: '11px', fontWeight:'bold',color:'#CC3333',border:'1px solid black',cursor:'pointer'},
    buttonHTML: 'Drag Zoom',
    buttonZoomingHTML: 'Drag a region on the map (click here to reset)',
    buttonZoomingStyle: {background:'beige'},
    backButtonHTML: 'Drag Zoom Back',
    backButtonStyle: {display:'none',marginTop:'3px',background:'#FFFFC8'},
    backButtonEnabled: false,
    overlayRemoveTime: 1000
}

// GOOGLE BAR OPTIONS
var gbOptions = {
onIdleCallback: function() { CoordsOnIdle(); },
onSearchCompleteCallback: function() { CoordsOnSearch(); }
};