////////////////////////////////////////////////////////////////////////////////
//
//  PLANNER
//      Andrea Pradarelli Feb - 2009
//
///////////////////////////////////////////////////////////////////////////////

var map;
var snapToRoute;
var routeOverlay;
var lookingGlass;
var mapDiv = null;
var OW = 0;
var DW = 0;
var bounds = new GLatLngBounds();
var boundsMod = false;

var lat=44.4942016602;//44.21353994108407;//44.4942016602;
var lon=11.3467998505;//12.389588360128982;//11.3467998505;

function adjust(p) {
    p[0] += 0;// (44.171616146784615 - 44.1733244832708);
    p[1] += 0;// (12.031574249267578 -  12.032668590545654);
}

function load() {
    map = new GMap2($("map"));
    //map.enableScrollWheelZoom();
    map.setCenter(new GLatLng(lat,lon), 12);
    map.addControl(new GLargeMapControl3D());
    map.addControl(new GMapTypeControl());
    mapDiv = window.document.getElementById("map");
    OW = getWindowSize();
    DW = getObjSize(mapDiv);
    mapResize();
}


var images = [
"http://maps.google.com/mapfiles/marker.png",
"http://maps.google.com/mapfiles/dd-start.png",
"http://maps.google.com/mapfiles/dd-end.png"
];

function follow(imageInd){
    var marker;
    var dog = true;
    var noMore = false;

    var mouseMove = GEvent.addListener(map, 'mousemove', function(cursorPoint){
        if(!noMore){
            marker = new GMarker(cursorPoint,{
                draggable:true,
                autoPan:false
            });
            map.addOverlay(marker);
            marker.setImage(images[imageInd]);
            noMore = true;
            // This function deletes the marker when dragged outside map
            GEvent.addListener(marker, 'drag', function(markerPoint){
                if(!map.getBounds().containsLatLng(markerPoint)){
                    map.removeOverlay(marker);
                }
            });
        }
        if(dog){
            marker.setLatLng(cursorPoint);
        }
    });
    var mapClick = GEvent.addListener(map, 'click', function(){
        dog = false;
        // 'mousemove' event listener is deleted to save resources
        GEvent.removeListener(mouseMove);
        GEvent.removeListener(mapClick);
    });
}

function makeMarker(point, html, toHTMLElement, toHTMLElementValue, customIcon, overlayArray) {
    var icon = new GIcon();
    icon.shadow = '';
    if (customIcon) {
    	    dimH=31;
    	    dimW=24;
   	    ancX=12;
    	    ancY=31;
            if ((customIcon.indexOf('_green')>0) || (customIcon.indexOf('_orange')>0) || (customIcon.indexOf('_red'))>0)  {
    	    	dimH=38;
    	    	ancY=38;
   	    }
	    icon.iconSize = new GSize(dimW, dimH);
	    icon.shadowSize = new GSize(22, 20);
	    icon.iconAnchor = new GPoint(ancX, ancY);
	    icon.infoWindowAnchor = new GPoint(16, 2);
	    icon.image = customIcon;
    } else {
	    icon.iconSize = new GSize(12, 20);
	    icon.shadowSize = new GSize(22, 20);
	    icon.iconAnchor = new GPoint(6, 20);
	    icon.infoWindowAnchor = new GPoint(5, 1);
            icon.image = images[0];
    }
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
        if (toHTMLElement) {
        	toHTMLElement.value=toHTMLElementValue;
        }
    });
    map.addOverlay(marker);
    if (overlayArray) {
    	overlayArray[overlayArray.length] = marker;
    }
    return marker;
}

function clearOverlays() {
    if ($('indicazioni')) {$('indicazioni').innerHTML='';}
    map.clearOverlays();
    bounds = new GLatLngBounds();
    boundsMod = false;
}

function  init() {
    load();
}

function mapResize(){
	if (mapDiv) {
		var NS = getWindowSize();
		mapDiv.style.width=((NS.width) -530) + 'px';
		mapDiv.style.height=((NS.height) -200) + 'px';
		map.checkResize();
	}
};

window.onresize = mapResize;

function getObjSize(obj){
	if(obj.style.width && obj.style.height){
		var e = new Object();
		e.width = parseInt(obj.style.width.replace(/px/, ''));
		e.height = parseInt(obj.style.height.replace(/px/, ''));
		return e;
	}
	return null;
}

function getWindowSize(){
	var e = new Object();
	if(window.self && self.innerWidth){
		e.width = self.innerWidth;
		e.height = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight){
		e.width = document.documentElement.clientWidth;
		e.height = document.documentElement.clientHeight;
	}else{
		e.width = document.body.clientWidth;
		e.height = document.body.clientHeight;
	}
	return e
}
