// seteaza div-ul cu map pe mijlocul td-ului in care se afla
var mapOldYPosition = null;
var headerHeight = 195;

function autoMoveMapDiv(){
	var mapDiv = document.getElementById("map");
	if (mapDiv ==null) return;
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
	var scrollTop =document.all? iebody.scrollTop : pageYOffset

	// daca m-am mutat 30 px in sus sau in jos mut map-ul
	var parentHeight = document.all? mapDiv.parentElement.clientHeight : mapDiv.parentNode.clientHeight;
	var hasMoved = scrollTop > headerHeight || mapOldYPosition == null || (mapOldYPosition - scrollTop >30) || (mapOldYPosition- scrollTop <-30);
	if(hasMoved){
		var moveTo = scrollTop - headerHeight;// cu cat s-a deplasat de fapt fereastra
		if(moveTo <0) moveTo = 0;
		if(moveTo>parentHeight-mapDiv.clientHeight)
			moveTo = parentHeight-mapDiv.clientHeight;
		//var moveTo = parentHeight-mapDiv.clientHeight-headerHeight;
		
		// daca sunt mai jos decat bottom - atunci merg pe bottom
		//if(moveTo + mapDiv.clientHeight > parentHeight)  		moveTop = parentHeight - mapDiv.clientHeight;
		// daca sunt mai sus - ma opresc
		//if(scrollTop-headerHeight>0)	moveTo = scrollTop-headerHeight;
		mapOldYPosition = moveTo;
		mapDiv.style.top = moveTo+"px";
	}
}

// global map pointer
var map = null;
var mapMarkers = [];

// init la map - in viitor poate fi si yahoo - nu numai google
function mapInit(){
	document.body.onscroll = function(){autoMoveMapDiv();}
	window.onscroll = function(){autoMoveMapDiv();}
	
	if (GBrowserIsCompatible()) {
	  	if(document.getElementById("map") == null) return;
    	map = new GMap2(document.getElementById("map"));
    	map.enableScrollWheelZoom();
		//map.addControl(new GSmallMapControl());  
		map.addControl(new GMapTypeControl());  
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(51.179045, -1.826752), 10);   
	}
	if(map == null) return;
	// show markers 
	bounds =new  GLatLngBounds();
	for(var idx=0;idx<mapMarkers.length;idx++){
		map.addOverlay(mapMarkers[idx]);
		map.addOverlay(mapMarkers[idx]);
		bounds.extend(mapMarkers[idx].getPoint());
	}
	var autoZoomLevel = map.getBoundsZoomLevel(bounds);
	map.setCenter(bounds.getCenter(),autoZoomLevel);
	autoMoveMapDiv();
}

function mapUnload(){
	GUnload();
}

function getIcon(iconIdx){
	//var i = Math.floor(IMAGES.length*Math.random());
	//if (!ICONS[i]) {
	  var icon = new GIcon();
	  icon.image = "/images/map.pointer."+iconIdx+".png";
	  icon.iconAnchor = new GPoint(19, 31);
	  icon.infoWindowAnchor = new GPoint(17, 9);// de unde incepe tool tipul
	  icon.iconSize = new GSize(32, 32);
	  //icon.shadow = "markermanager_" + IMAGES[i] + "-shadow.png";
	  //icon.shadow= "/work/nabooko/images/map.pointer.shadow.png";
	  //icon.shadowSize = new GSize(45, 32);
	  //ICONS[i] = icon;
	  return icon;
	//}
	//return ICONS[i];
}
// adauga un marker cu continut HTML la harta
function mapAddMarker(iconIdx, lat, long, content){
	if(lat == '' || long == '') return;
	if(lat == 0 && long == 0) return; // presupun ca nu am nici un hotel pe 0,0 asa vine cand nu stie unde este
	if(typeof(GEvent) =="undefined") return;
	var point = new GLatLng(lat, long);  
	var marker = new GMarker(point,{icon:getIcon(iconIdx)}); // GMarker(getRandomPoint(), { icon: getWeatherIcon() })
	GEvent.addListener(marker, "click", function() {    
		marker.openInfoWindowHtml(content);  });  
	if(map != null)
		map.addOverlay(marker);
	mapMarkers.push(marker);
}

// auto Zome la marker-ul specificat
function mapAutoZoom(lat, long){
/*
	for (var idx = 0; idx< mapMarkers.length; idx++)
		if(mapMarkers[idx]
*/
	if(map == null) return;
	map.setCenter(new GLatLng(lat, long));		
}
