//map

DEFAULTMAXZOOMIN = 8;
DEFAULTCENTERMAP = new google.maps.LatLng(51, 4.3);

ROADWORKCOLOR = '#C11B17';
DEVIATIONCOLOR = '#FFFF00';

ROADWORKCOLOROLDPHASE = '#a11B17';
DEVIATIONCOLOROLDPHASE = '#00FF00';

ROADWORKWEIGHT = 7;
ROADWORKOPACITY = 0.5;


google.maps.Map.prototype.selectedObject = null;
google.maps.Map.prototype.marker = null;

google.maps.Map.prototype.addControlsToMap = function (controlID, position) {
    var controlid = document.getElementById(controlID);
    this.controls[position].push(controlid);
}

google.maps.Map.prototype.addListener = function (eventName, fctCallBack) {
    return google.maps.event.addListener(this,eventName, fctCallBack);
}

google.maps.Map.prototype.focusAddress = function (address) {
    var map = this;
    address += ',Belgium';
    var geocoder = new google.maps.Geocoder();
    if (geocoder) {
        var opt = {
        	address: address
	    };
        
        geocoder.geocode(opt, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
               map.setCenter(results[0].geometry.location);

               var isStreet = true;
               //some location don't have bounds 
               try{
                  var addressComponent = results[0].address_components[0];
                  var types = addressComponent.types;
                  for(var i = 0; i<types.length;i++){
                      if(types[i] == 'route'){
                          isStreet = true;
                          break;
                      }else{
                          isStreet = false;
                      }
                  }
            	  var bounds = results[0].geometry.bounds;
            	  map.fitBounds(results[0].geometry.bounds);
               }
               catch (e) {
                   if(isStreet){
                      map.setOptions({
                            zoom: 18
                      });
                   }
                   else{
                       map.setOptions({
                            zoom: 12
                      });
                   }
               }
            }
        });
    }
};


/* opt:
 * provinceName
 * localityName
 * idProvince
 * idLocality
*/
google.maps.Map.prototype.focusAddressPublic = function (options) {
    var me = this;
    options = options == null ? {} : options;
    var opt = {
        functionName: 'getKaartGeometryBounds',
        idLocality: options.idLocality,
        idProvince: options.idProvince,
        address: options.address
    }

    opt.address +=  ',Belgium';
    GetJSONObjectFromDB(opt, function(data){
        try{
            var json = jQuery.parseJSON(data);
            if(json == -1){
                throw new Exception();
              }else{
                  var bounds = json.bounds;
                  var mapBounds = new google.maps.LatLngBounds();
                  for(var i=0, point; point = bounds[i]; i++){
                      var p = new google.maps.LatLng(point.lat, point.lng);
                      mapBounds.extend(p);
                  }
                  me.fitBounds(mapBounds);
                  return;
              }
        }
        catch(ex){
            alert('The address could not be found!');
        }
    })
};

google.maps.Map.prototype.refreshMap = function(){
	this.setOptions({
		zoom: DEFAULTMAXZOOMIN,
		center: DEFAULTCENTERMAP
	});
}


google.maps.Map.prototype.focusLocation = function (location) {
    this.setCenter(location);
    this.setZoom(16);
}











//marker

var rwStatusIcons = {
	InUitvoering: BASEURL+"fw_images/werken_inuitvoering_schaduw_300dpi.gif",
	KorteTermijn: BASEURL+"fw_images/werken_kortetermijn_schaduw_300dpi.gif",
	LangeTermijn: BASEURL+"fw_images/werken_langetermijn_schaduw_300dpi.gif"
}


var SignPointIcon = {
	DEFAULT: BASEURL + 'fw_images/default.gif'
};

google.maps.Marker.prototype.ID = null;
google.maps.Marker.prototype.infoWindow = null;

google.maps.Marker.prototype.setDefaultRoadworkIcon = function(){
	this.customizationIcon(SignPointIcon.DEFAULT);
};

google.maps.Marker.prototype.setDeviationIcon = function(number){
    var size = new google.maps.Size(24, 24);
    var origin = new google.maps.Point(1, 1);
    var anchor = new google.maps.Point(12, 12);
    var scaledSize = new google.maps.Size(24, 24);
    var url = "http://www.google.com/intl/en_ALL/mapfiles/dir_" + number + ".png"
    var markerImage = new google.maps.MarkerImage(url, size, origin, anchor, scaledSize);
    
    this.setIcon(markerImage);
}

google.maps.Marker.prototype.setInfoWindow = function (content) {
    if (this.infoWindow == null)
        this.infoWindow = new google.maps.InfoWindow();
    this.infoWindow.setContent(content);

    var infoWindow = this.infoWindow;
    var marker = this;    
    
    this.addListener('click', function () {
        infoWindow.open(marker.getMap(), marker);
    });

    var map = this.getMap();
    if(map != null){
    		map.addListener('click', function(){
    			infoWindow.close();
    		});
    }

}

google.maps.Marker.prototype.clear = function () {
    this.setMap(null);
}

google.maps.Marker.prototype.Init = function () {
    var opt = {
        draggable: true
    }
    this.setOptions(opt);
}

google.maps.Marker.prototype.customizationIcon = function (fileName) {
    var size = new google.maps.Size(32, 32);
    var origin = new google.maps.Point(1, 1);
    var anchor = new google.maps.Point(16, 16);
    var scaledSize = new google.maps.Size(32, 32);
    var markerImage = new google.maps.MarkerImage(fileName, size, origin, anchor, scaledSize);
    this.setIcon(markerImage);
}

google.maps.Marker.prototype.addListener = function (eventName, fctCallBack) {
    google.maps.event.addListener(this, eventName, fctCallBack);
}

google.maps.Marker.prototype.setStatus = function(idStatus){
	switch(idStatus){
		case 1:
			this.customizationIcon(rwStatusIcons.InUitvoering);
		break;
		case 2:
			this.customizationIcon(rwStatusIcons.KorteTermijn);
		break;
		case 3:
			this.customizationIcon(rwStatusIcons.LangeTermijn);
		break;
	}
}








//polyline


google.maps.Polyline.prototype.colorRW = '#C11B17';
google.maps.Polyline.prototype.colorDeviation = '#FFFF00';

google.maps.Polyline.prototype.roadWorkStatusId = -1;
google.maps.Polyline.prototype.centerMarker = null;
google.maps.Polyline.prototype.markers = null;
google.maps.Polyline.prototype.isDeviation = false;

google.maps.Polyline.prototype.idRW = -1;
google.maps.Polyline.prototype.idGeometry = -1;
google.maps.Polyline.prototype.isDeleted = false;
google.maps.Polyline.prototype.deviationIcons = null;



if (!google.maps.Polyline.prototype.getBounds) {
    google.maps.Polyline.prototype.getBounds = function (latLng) {
        var bounds = new google.maps.LatLngBounds();
        var path = this.getPath();
        for (var i = 0; i < path.getLength(); i++) {
            bounds.extend(path.getAt(i));
        }
        return bounds;
    }
}
google.maps.Polyline.prototype.setMapRoadWork = function (value) {
    this.setMap(value);
    if (this.centerMarker == null)
        return;

    if (value != null) {
        if (!this.isDeviation)
            this.centerMarker.setMap(value);
    }
    else {
        this.centerMarker.setMap(null);
    }
}

google.maps.Polyline.prototype.setSize = function (size) {
    var opt = {
        strokeWeight: size
    }
    this.setOptions(opt);
}

google.maps.Polyline.prototype.setColor = function (color) {
    var opt = {
        strokeColor: color
    }
    this.setOptions(opt);
}


google.maps.Polyline.prototype.addMarker = function (marker) {
    if (this.markers == null)
        this.markers = new Array();
    this.markers.push(marker);
    this.getPath().push(marker.getPosition());
}

google.maps.Polyline.prototype.addPoint = function (point) {
    var marker = new google.maps.Marker();
    marker.setPosition(point);
    this.addMarker(marker);
}

google.maps.Polyline.prototype.clear = function () {
    var markers = this.markers;
    if (markers != null) {
        for (var i = 0; i < markers.length; i++) {
            markers[i].clear();
        }
    }
    if(this.deviationIcons != null){
    	this.deviationIcons.clear();
    }
    	
    
    this.setMap(null);
    this.markers = new Array();
    this.setPath(new Array());
}

google.maps.Polyline.prototype.animatePolyline = function () {
    google.maps.event.addListener(this, 'mouseover', function () {
        var opt = {
            strokeWeight: 10
        }
        this.setOptions(opt);
    });

    google.maps.event.addListener(this, 'mouseout', function () {
        var opt = {
            strokeWeight: 8
        }
        this.setOptions(opt);
    });

}

google.maps.Polyline.prototype.removeMarker = function (marker) {
    var markers = this.markers;
    var index = -1;
    for (var i = 0; i < markers.length; i++) {
        if (markers[i] == marker) {
            index = i;
            break;
        }
    }
    if (index != -1) {
        markers.splice(index, 1);
        this.getPath().removeAt(index);
        marker.clear();
    }
}

google.maps.Polyline.prototype.addArrayLatLng = function (array) {
    this.setPath(array);
}

google.maps.Polyline.prototype.addListener = function (eventName, fctCallBack) {
    google.maps.event.addListener(this, eventName, fctCallBack);
}

google.maps.Polyline.prototype.setType = function(idDeviation){
	switch(idDeviation.toString()){
		//rw
		case '0':
			this.setSize(7);
			this.setColor(ROADWORKCOLOR);
		break;
		
		//deviation
		default :
			this.setSize(7);
			this.setColor(DEVIATIONCOLOR);
		break;
	}
}


google.maps.Polyline.prototype.getGeometry = function () {
        var geometry = '';
        var path = this.getPath();
        for (var i = 0; i < path.getLength(); i++) {
            var latLng = path.getAt(i);
            geometry += latLng.lat() + ',' + latLng.lng() + ';';
        }
        geometry = geometry.substring(0, geometry.length - 1);
        return geometry;
    }

google.maps.Polyline.prototype.length = function(){
	return this.getPath().length;
}
google.maps.Polyline.prototype.GetDirectionPoints = function(){
	if(this.deviationIcons != null){
		return this.deviationIcons.getDeviationIcons();
	}
	return null;
}
//google.maps.Polyline.prototype.



//KML Layer
google.maps.KmlLayer.prototype.name = '';
google.maps.KmlLayer.prototype.zoom_min = 8;
google.maps.KmlLayer.prototype.zoom_max = 20;
google.maps.KmlLayer.prototype.roadWorkStatusId = -1;
google.maps.KmlLayer.prototype.isDeviation = false;

