Project = {};
Project.activeMarker = null;
Project.icon = null;
Project.gmap = null;
Project.markers = new Array;
Project.mapTypes = {mapType:G_PHYSICAL_MAP};
Project.zoomLevel = 6;
Project.latitude = 52.165538;
Project.longitude = 5.305303;

Project.loadMap = function (el) {
	if (GBrowserIsCompatible()) {
     	Project.gmap = new GMap2(el, this.mapTypes);
      	Project.gmap.setCenter(new GLatLng(this.latitude, this.longitude), this.zoomLevel);
        // Project.gmap.addControl(new GLargeMapControl());
        // Project.gmap.addControl(new GMapTypeControl());
        //Project.gmap.addControl(new GScaleControl());
      	Project.gmap.setMapType(G_PHYSICAL_MAP);

      	Project.icon1 = new GIcon (G_DEFAULT_ICON);
		Project.icon1.image = '/image/pink.png';
		Project.icon1.shadow = '';
		Project.icon1.iconSize = new GSize(17, 23);
		Project.icon1.iconAnchor = new GPoint(9, 23); //prutsed by Kras
    }
}

Project.addProjectMarker = function (id, title, lat, lng, status, discipline, hRef) {
	var marker = Project.addMarker(id, title, lat, lng, eval("Project.icon"+ status), discipline);
	marker.state = status;
	marker.discipline = discipline;
	marker.hRef = hRef;
	Project.markers.push(marker);
}

Project.addMarker = function (id, title, lat, lng, icon) {
    var marker = new GMarker(new GLatLng(lat, lng), icon );
    GEvent.addListener(marker,"click", function () {
      	document.location = marker.hRef;
    });

    GEvent.addListener(marker, "mouseover", function () {
    	Project.activeMarker = this;
    	this.overlay = new InfoBox(this, 24, -7, 10, title);
    	Project.gmap.addOverlay(this.overlay);
    });

    GEvent.addListener(marker, "mouseout", function () {
    	if (Project.activeMarker){
			Project.gmap.removeOverlay(Project.activeMarker.overlay);
		}
    });

    Project.gmap.addOverlay(marker);
    return marker;
}

Project.clearMap = function () {
	for (var i = 0; i < Project.markers.length; i++) {
		Project.gmap.removeOverlay(Project.markers[i]);
	}
}

Project.updateGMap = function (states, disciplines){
	for (var i = 0; i < Project.markers.length; i++) {
		if(states && states.length > 0 && disciplines && disciplines.length > 0){
			for(var j = 0; j < states.length; j++){
				if(states[j] == Project.markers[i].state){
					for(var k = 0; k < disciplines.length; k++){
						if(disciplines[k] == Project.markers[i].discipline){
							Project.markers[i].show();
							break;
						}else{
							Project.markers[i].hide();
						}
					}
					break;
				}else{
					Project.markers[i].hide();
				}
			}
		}else{
			Project.markers[i].hide();
		}
	}
}


/*** INFOBOX - START ***/
function InfoBox(marker,divHeight, anchorOffsetX, anchorOffsetY, divHtml) {
    this.marker = marker;
    this.divHeight = divHeight;
    this.anchorOffsetX = anchorOffsetX;
    this.divHtml = divHtml
    if (null == this.anchorOffsetX){
        this.anchorOffsetX = 0;
    }

    this.anchorOffsetY = anchorOffsetY;

    if (null == this.anchorOffsetY){
        this.anchorOffsetY = 0;
    }
    this.anchorOffsetY -= 11;
}

InfoBox.prototype.initialize = function(map) {
    var div = document.createElement("div");
    div.style.position = "absolute";
	div.style.height = this.divHeight + "px";
    div.style.left 	= (map.fromLatLngToDivPixel(this.marker.getPoint()).x - this.anchorOffsetX)+ 'px';
    div.style.top 	= (map.fromLatLngToDivPixel(this.marker.getPoint()).y - this.divHeight - this.anchorOffsetY) + 'px';

	div.style.background = "url('" + this.marker.getIcon().image.replace(".png", "_extended.png") + "') no-repeat top right";
	div.style.padding = "1px 16px 0px 4px";
	div.style.fontFamily = "Verdana, sans-serif;";
	div.style.fontSize = "10px";
	div.style.color = "#FFFFFF";
	div.style.whiteSpace = "nowrap";

    div.innerHTML = this.divHtml
	Project.gmap.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    this.map_ = Project.gmap;
    this.div_ = div;
}

InfoBox.prototype.remove = function() {
    this.div_.parentNode.removeChild(this.div_);
}

InfoBox.prototype.redraw = function(force) {
    this.div_.style.left 	= (this.map_.fromLatLngToDivPixel(this.marker.getPoint()).x - this.anchorOffsetX)+ 'px';
    this.div_.style.top = (this.map_.fromLatLngToDivPixel(this.marker.getPoint()).y - this.divHeight - this.anchorOffsetY) + 'px';
}
/*** INFOBOX - END ***/
