﻿//Global Variable to keep track of map
var map;
//and one to keep track of the mini-map -- maybe we can wire events to this, not sure.
var miniMap;
 var theme = "alphacube";
 
//and a variable for the map's positioning -- just a proxy
var currentMapZoom;
var currentMapLat;
var currentMapLng;
//directions shown on the map
var currentDirections;
var trafficOverlay;
//first, listen to the window load so we can load our map
Event.observe(window, 'load', createMap);
//function to create the google map, initialize all layout stuff here.
function createMap(){
    createSearch();
    //create map
    map = new GMap2($("map"));
    //center map so Florida and most of Atlantic is in view
    map.setCenter(new GLatLng(27.253733339,-82.5249316057), 9);

	// Add the map controls
    map.addControl(new GLargeMapControl());
    map.addControl(new GScaleControl());
    map.addControl(new GMapTypeControl());
    //miniMap = map.addControl(new GOverviewMapControl(new GSize(100,100)));
    //enable zooming animation
    map.enableContinuousZoom();
    //enable new double click feature
    map.enableDoubleClickZoom();
    //and enable the scrollwheel
    map.enableScrollWheelZoom();
    //geocoder for searches
    geocoder = new GClientGeocoder();
    //initialize the tooltips
    initToolTips(map);
    
    homesManager = new HomesManager();
   
    //keep track of the map positioning for ease of access.
    GEvent.addListener(map, "moveend", getMapPosition);
    //and make sure it's right
    getMapPosition();
   
    checkURL();
    
  
}




function hideLoading(){
    $("loading").hide();
}

function showLoading(){
    $("loading").show();
}









//global tooltip variable  
var tooltip;

/**
 * initialies a tooltip within the map. Contains hardcoded styling for now... Probably to fix an IE bug.
 * @param (map): the google map to append the tooltips to. 
 **/
function initToolTips(){
    tooltip = document.createElement("div");
    $("map").appendChild(tooltip);
	tooltip.style.zIndex = "100000000";
	tooltip.style.opacity = "95";
	tooltip.style.filter = "alpha(opacity=95)";
	tooltip.style.color = "#333333";
	tooltip.style.border = "1px solid black";
    tooltip.style.visibility="hidden";
	tooltip.style.position = "absolute";
}
/**
 * shows a tooltip within the map
 * @param (marker): the marker that was hovered over on the map
 * @param (map): the map that the marker is in
 **/
function showTooltip(marker, map) {
    tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	//var anchor=marker.getIcon().iconAnchor;
	var anchor = new Object();
	anchor.x = 3;
	anchor.y = 17;
	//var width=marker.getIcon().iconSize.width;
	width = 5;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip);
	//believe it or not, doing nothing sometimes fixes a weird IE bug. IE sucks.
	randomNothing();
	tooltip.style.visibility="visible";
 }
 
//does nothing
function randomNothing(){
	
} 

var baseURL = "http://www.heraldtribune.com/apps/pbcs.dll/section?CATEGORY=MULTIMEDIA0204";

function getMapPosition(oldLevel, newLevel){
    var center = map.getCenter();
    currentMapLat = center.lat();
    currentMapLng = center.lng();
    currentMapZoom = map.getZoom();
    $("viewLinkTarget").innerHTML = "<a href='"+baseURL+"&lat="+currentMapLat+"&lng="+currentMapLng+"&zoom="+currentMapZoom+
                                      "' target='_top' >Link to this View</a>";
}



/**
 * Tests a .NET dataset coming back from AJAX Pro
 **/
function test(response){
    console.log("response");
    console.log(response);
    for(prop in response){
        console.log(prop + ": " + response[prop]);
    }
    console.log(response.value);
    for(prop in response.value){
        console.log(prop + ": " + response.value[prop]);
    }
    for(prop in response.error){
        console.log(prop + ": " + response.error[prop]);
    }
    var ds = response.value;
    console.log("ds: " + ds);
    for(var j=0; j < ds.Tables.length; j++){
        var nos = ds.Tables[j].Rows.length;
	    console.log("nos: " + nos)
	    console.log(ds.Tables[j].Rows);
	    for( var i=0; i < nos; i++ ){
            var row = ds.Tables[j].Rows[i];
            for(prop in row){
                console.log(prop + ": " +row[prop]);
            }
        }
    }
}







var geocodingReasons=[];
geocodingReasons[G_GEO_SUCCESS]            = "Success";
geocodingReasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
geocodingReasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
geocodingReasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
geocodingReasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
geocodingReasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
geocodingReasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";








 function createSearch(){
    //create new Legend window -- valid properties at: http://prototype-window.xilinus.com/documentation.html
    var legend = new Window({maximizable: false, resizable: true, width:150, height:230, closable:false,  
                            left:800, top:30, className: theme, draggable:true, minHeight:180
                     }); 
    //setContent takes an IDed element and extracts its content into the window. 'legend' div is in main ASPX page.
    legend.setContent("searchBox");
    legend.show(); 

 }
 
 
 
 
 function checkURL(){
    var params = location.search.toQueryParams();
   
    if(params.lat && params.lng){
	    var lat = parseInt(params.lat);
	    var lng = parseInt(params.lng);
		map.setCenter(new GLatLng(params.lat, params.lng));
		
    }else{
         map.setCenter(new GLatLng(27.253733339,-82.5249316057), 9);
    }
	
	if(params.zoom){
	    var z = parseInt(params.zoom);
	    map.setZoom(z);
	}
	
	map.checkResize(); 
}