var map, box;
var draggable = false, resizable = false;
var mouseX, mouseY, drawnX, drawnY, diffX, diffY;

window.onload = function() {
 // Define global variable box
 box = document.getElementById("drag");
 // Register mouse move listener
 document.onmousemove= watchMouse;
}

 // Mouse move listener
function watchMouse(e) {

  // Include possible scroll values
  var sx = window.scrollX || document.documentElement.scrollLeft|| 0;
  var sy = window.scrollY || document.documentElement.scrollTop|| 0;

  if(!e) e = window.event; // IEs event definition
  mouseX = e.clientX + sx;
  mouseY = e.clientY + sy;

  /* Direction of mouse movement
  *  deltaX: -1 for left, 1 for right
  *  deltaY: -1 for up, 1 for down
  */
  var deltaX = mouseX - diffX;
  var deltaY = mouseY - diffY;
  // Store difference in global variables
  diffX = mouseX;
  diffY = mouseY;

  if(resizable) { // The resize button is being held
   changeMapSize(deltaX, deltaY);
  }

  else if(draggable) { // The box is being dragged
   box.style.left= (mouseX - drawnX) + "px";
   box.style.top = (mouseY - drawnY) + "px";
  }
  return false;
}


function dragstart(e) { // Calculate mouse position for dragging

 draggable = true;
 drawnX = mouseX - parseInt(box.style.left);
 drawnY = mouseY - parseInt(box.style.top);

 /* Avoid selecting the content
  * of the box while dragging
 */
 if(e.cancelable) { e.preventDefault(); }
 if(window.event) { window.event.returnValue = false; }

 // The box is being dropped
 box.onmouseup = function() { draggable = false; }
}

/*
function createResizeControl(controlDiv, map) {
	var resizeButton = document.createElement('div');
	resizeButton.style.width = "20px";
	resizeButton.style.height = "20px";
	resizeButton.style.backgroundImage = "url('http://localhost/book-travel.cz/_js_css/_images/resize.gif')";
	
	controlDiv.appendChild(resizeButton);

	google.maps.event.addDomListener(resizeButton, 'mousedown', function() {
		resizable = true; return false;
	});
	document.onmouseup = function() { resizable = false; }
 	return resizeButton;
}

// Resizes the map's width and height by the given increment
function changeMapSize(dx, dy) {

  var mapdiv = gebi('google_map');
  var width = parseInt(mapdiv.style.width);
  var height =  parseInt(mapdiv.style.height);

  if(width < 100) { width = 100; }
  if(height < 50) { height = 50; }
 
  mapdiv.style.width = (width + dx) + "px";
  mapdiv.style.height= (height + dy) + "px";
}
*/

/***** GOOGLE MAPS */
// http://gmaps-samples.googlecode.com/svn/trunk/spreadsheetsmapwizard/exampleoutput.htm
// http://www.wolfpil.de/map-in-a-box.html
function hideMap() {
	box.style.visibility = 'hidden';
}
	
var google_marker, google_map;

function showHotelOnMap(object) {
	if (!(object.lat || object.lng))
		return false;
	box.style.visibility = 'visible';
	centerObject('drag');
  var latlng = new google.maps.LatLng(object.lat, object.lng);
  var myOptions = {
  	zoom: 14,
    center: latlng,
		scaleControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	if (!map) {
  	map = new google.maps.Map(gebi("google_map"), myOptions);
/*		
		var resizeControlDiv = document.createElement("div");
		createResizeControl(resizeControlDiv, map);
		resizeControlDiv.index = 1;
		map.controls[google.maps.ControlPosition.BOTTOM_RIGHT].push(resizeControlDiv);
		google.maps.event.trigger(map, 'resize');
*/
	}
  if (!google_marker) {
  	google_marker = new google.maps.Marker({
  		position: latlng, 
    	map: map,
    	title: object.title
		});
  } else {
  	google_marker.setPosition(latlng);
  	google_marker.setTitle(object.title);
  }
  map.setCenter(latlng);
}

/************ GOOGLE MAP USECASE ***/
	function initializeMap(latlng, zoom, sMarkerTitle) {
  	var opt = {
  		zoom: zoom,
    	center: latlng,
			scaleControl: true,
    	mapTypeId: google.maps.MapTypeId.ROADMAP
		};
  	google_map = new google.maps.Map(document.getElementById("google_map"), opt);
  	var ngeocoder = new google.maps.Geocoder();
		if (!isEmpty(sMarkerTitle)) {
  		var google_marker = new google.maps.Marker({
  			position: latlng, 
    		map: google_map,
    		title: sMarkerTitle
			});
		}
	}

	function search_address() {
		var address = gebi('address').value;
		if (address=='')
			return;
    if (geocoder) {
      geocoder.geocode( { 'address': address + ', Prague, CZ'}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
            google_map.setCenter(results[0].geometry.location);
            google_map.setZoom(15);
            var marker = new google.maps.Marker({ map: google_map, position: results[0].geometry.location, title:results[0].formatted_address });
						var infoWindow = new google.maps.InfoWindow({ content:results[0].formatted_address });
						infoWindow.open(google_map, marker);
          } else {
            alert(g_t['no-address-found']);
          }
        } else {
          alert(g_t['no-address-found']);
        }
      });
    }    
  }
  
	function createLodgMarker(latlng, title, content, icon, visibility, bOpen) {
		var marker = new google.maps.Marker({ position:latlng, title:title, visible:visibility, icon:icon, map:google_map, shadow:new google.maps.MarkerImage('/images/marker-shadow.png',
      																																																			new google.maps.Size(27,14), new google.maps.Point(0,0), new google.maps.Point(0,14)) });
		var infoWindow = new google.maps.InfoWindow({ content:content}); //, maxWidth:300 
		google.maps.event.addListener(marker, 'click', function() { infoWindow.open(google_map,marker); });
		return marker;
	}    
	
	function showOnlyFolderLodgs(folderId) {
		for (var id in lm)
			lm[id].setVisible(false);
		var latlngbounds = new google.maps.LatLngBounds();
		var fitBounds = false;
		if (folders[folderId].lodgs && folders[folderId].lodgs.length>0) {
			for (var i=0, c=folders[folderId].lodgs.length; i<c; i++) {
				var lodgId = folders[folderId].lodgs[i]; 
				if (lm[lodgId]) {
					lm[lodgId].setVisible(true);
					latlngbounds.extend(lm[lodgId].position);
					if (!fitBounds && !google_map.getBounds().contains(lm[lodgId].position))
						fitBounds = true;
				}
			}
		}
		if (fitBounds)
			google_map.fitBounds(latlngbounds);					
	}
	
	function showOnlyMcastLodgs(mcastId) {
		for (var id in lm)
			lm[id].setVisible(false);
		var latlngbounds = new google.maps.LatLngBounds();
		var fitBounds = false;
		for (i=0, c=mcast[mcastId].length; i<c; i++) {
			lm[mcast[mcastId][i]].setVisible(true);
			latlngbounds.extend(lm[mcast[mcastId][i]].position);
			if (!fitBounds && !google_map.getBounds().contains(lm[mcast[mcastId][i]].position))
				fitBounds = true;
		}
		if (fitBounds)
			google_map.fitBounds(latlngbounds);
	}
	
	function showOnlyCobceLodgs(cobceId) {
		for (var id in lm)
			lm[id].setVisible(false);
		var latlngbounds = new google.maps.LatLngBounds();
		var fitBounds = false;
		for (i=0, c=cobce[cobceId].length; i<c; i++) {
			lm[cobce[cobceId][i]].setVisible(true);
			latlngbounds.extend(lm[cobce[cobceId][i]].position);
			if (!fitBounds && !google_map.getBounds().contains(lm[cobce[cobceId][i]].position))
				fitBounds = true;
		}
		if (fitBounds)
			google_map.fitBounds(latlngbounds);
	}
	
	function showLodgMarker(lodgId, content) {
		var infoWindow = new google.maps.InfoWindow({ content:content});
		var listener = google.maps.event.addListener(google_map, 'tilesloaded', function() { 
			infoWindow.open(google_map, lm[lodgId]); 
			google.maps.event.removeListener(listener);
		});
	}

