var dragObject;
var posDifference;

function showMap (event, city) {
	var dojoBuc = new google.maps.LatLng(44.420573, 26.140668);
	var dojoBv = new google.maps.LatLng(45.655277, 25.583503);
	
	var mapOptions = {
		      zoom: 16,
		      center: city == 1 ? dojoBuc : dojoBv,
		      mapTypeId: google.maps.MapTypeId.ROADMAP,
		      backgroundColor: '#ffffff',
		      disableDoubleClickZoom: true,
		      maxZoom: 18,
		      minZoom: 13,
		      panControl: false,
		      mapTypeControl: false
		    };
	
    var dojoMap = new google.maps.Map(document.getElementById('mapDiv'),
    		mapOptions);
    
    var markerOptionsBuc = {
        	map: dojoMap,
        	position: dojoBuc,
        	visible: true,
        	title: 'Mokushozen-Ji'
    	};
    
    var markerOptionsBv = {
        	map: dojoMap,
        	position: dojoBv,
        	visible: true,
        	title: 'Dojo Bra\u015fov'
    	};
    		
    var dojoMarker = new google.maps.Marker (city == 1 ? markerOptionsBuc : markerOptionsBv);
    
    var infoWindowOptionsBuc = {
        content: '<b>Templul Mokushozen-Ji</b><br/>str. Complexului, nr. 2bis'
    };
    
    var infoWindowOptionsBv = {
            content: '<b>Mokusho Zen Dojo Bra\u015fov</b><br/>str. Lung\u0103, nr. 130'
        };
        
	var dojoInfoWindow = new google.maps.InfoWindow (city == 1 ? infoWindowOptionsBuc : infoWindowOptionsBv);
    
	google.maps.event.addListener(dojoMarker, 'click', function() {
		dojoInfoWindow.open(dojoMap, dojoMarker);
	});
	
	dojoInfoWindow.open(dojoMap, dojoMarker);
	
	document.getElementById("mapDivContainer").style.visibility = "visible";
	
	event = event || window.event;
	var mseX = event.clientX;
	var mseY = event.clientY;
	
	document.getElementById("mapDivContainer").style.top = (mseY + 10) + "px";
	document.getElementById("mapDivContainer").style.left = (mseX - 250) + "px";
}

function hideMap() {
	document.getElementById("mapDivContainer").style.visibility = "hidden";
	
	clearElement (document.getElementById("mapDiv"));
}

function clearElement (element) {
	if (element != null) {
		while (element.hasChildNodes()) {
			element.removeChild(element.firstChild);
		}
	}
}

function startDragMap (div, event) {
	event = event || window.event;
	
	if (event.button != 2) {
		var mseX = event.clientX;
		var mseY = event.clientY;
		var position = getElementPosition(div.parentNode);
		posDifference = {dx: mseX - position.x, dy: mseY - position.y};
		
		dragObject = div;
		document.onmousemove = dragMap;
		document.onmouseup = dragMapEnd;
	}
}


function dragMap (event) {
	if (dragObject != null) {
		event = event || window.event;
		var mseX = event.clientX;
		var mseY = event.clientY;
		
		dragObject.parentNode.style.top = mseY - posDifference.dy + "px";
		dragObject.parentNode.style.left = mseX - posDifference.dx + "px";
		
		return false;
	}
}

function dragMapEnd () {
	if (dragObject != null) {
		dragObject.style.cursor = "move";
	}
	
	dragObject = null;
	document.onmousemove = null;
	document.onmouseup = null;
}

function getElementPosition (object) {
	var left = 0;
	var top = 0;
	
	while (object.offsetParent){
		left += object.offsetLeft +
				(object.currentStyle?(parseInt(object.currentStyle.borderLeftWidth)).NaN0():0);
		top += object.offsetTop  +
				(object.currentStyle?(parseInt(object.currentStyle.borderTopWidth)).NaN0():0);
		object = object.offsetParent;
	}


	left += object.offsetLeft + (object.currentStyle?(parseInt(object.currentStyle.borderLeftWidth)).NaN0():0);
	top  += object.offsetTop  + (object.currentStyle?(parseInt(object.currentStyle.borderTopWidth)).NaN0():0);

	return {x:left, y:top};
}

Number.prototype.NaN0 = function() {
	return isNaN (this) ? 0 : this;
}
