
PageGMaps = {};
Import.clone(GMaps, PageGMaps);

PageGMaps.pointsXmlFile = '';
PageGMaps.linesXmlFile 	= '';
PageGMaps.iconsPath 	= '';
PageGMaps.iconWidth	    = 36;
PageGMaps.iconHeight    = 42;
PageGMaps.layersCount 	= 0;
PageGMaps.layersAndLinesCount 	= 0;
PageGMaps.selectedLayerId 		= 0;
PageGMaps.checkboxLayerPrefix 	= 'layer';
PageGMaps.linkToInstitution 	= '?str={str}&id={id}';
PageGMaps.map 			  = null;
PageGMaps.geo 			  = null;
PageGMaps.isLinesLoading  = null;
PageGMaps.markers 		= {};
PageGMaps.centerX = GMaps.position.X;
PageGMaps.centerY = GMaps.position.Y;
PageGMaps.labels = {
	  'more' 	: ''
	, 'address' : ''
	, 'phone'	: ''
	, 'fax'		: ''
	, 'email'	: ''
	, 'www'		: ''
};

//Initialization
PageGMaps.init = function(){
    if(GBrowserIsCompatible()){
        PageGMaps.map = new GMap2(document.getElementById('mapBox'));
        PageGMaps.setMapTypes();
        PageGMaps.map.addControl(new GMapTypeControl());
        PageGMaps.map.addControl(new GLargeMapControl3D());
        PageGMaps.map.setCenter(new GLatLng(PageGMaps.centerX, PageGMaps.centerY), PageGMaps.ZOOM);
        PageGMaps.map.enableDoubleClickZoom();
        PageGMaps.map.enableScrollWheelZoom();
        PageGMaps.map.enableContinuousZoom();
        PageGMaps.loadMarkersFromXML();
    }
};

//Loading markers from XML
PageGMaps.loadMarkersFromXML = function(){
    GDownloadUrl(PageGMaps.pointsXmlFile, function(xmlContent, callbackCode){
		var xml = GXml.parse(xmlContent);
        var markers = xml.documentElement.getElementsByTagName('point');
		var attributes = ['id', 'name', 'description', 'address', 'phone', 'fax', 'email', 'link'
		, 'icon', 'x', 'y', 'layerId', 'pageId', 'institutionId'];
		var v = {};
        for(var i = 0; i < markers.length; i++){
        	for(var j = 0; j < attributes.length; j++){
        		v[attributes[j]] = markers[i].getAttribute(attributes[j]).replace(/&quot;/g,'"');
        	}
			var id = markers[i].getAttribute('id');
			var layerId = markers[i].getAttribute('layerId');
			if((PageGMaps.selectedLayerId==0) || (PageGMaps.selectedLayerId==layerId)) {
	            PageGMaps.markers[id] = {
	            	  'layerId'	: layerId
            		, 'marker'	: PageGMaps.addMarker(v)
            		};
			}
        }
        PageGMaps.addMarkersToOverlay();
    });
};

//Create marker
PageGMaps.addMarker = function(v){
    if(v.icon == ''){
    	var icon = new GIcon(G_DEFAULT_ICON);
        icon.iconSize = new GSize(20, 34);
    }
    else{
    	var icon = new GIcon();
        icon.image = PageGMaps.iconsPath + v.icon;
        icon.iconSize = new GSize(PageGMaps.iconWidth, PageGMaps.iconHeight);
    }

    icon.iconAnchor = new GPoint(18, 42);
    icon.infoWindowAnchor = new GPoint(18, 0);
    var marker = new GMarker(
    	  new GLatLng(parseFloat(v.x), parseFloat(v.y))
    	, {'title': v.name, 'icon': icon}
    );
    var content = '<div class="marker_title"><b>' + v.name + '</b></div>';
	if(parseInt(v.institutionId) == 0){
		if(v.description != ''){
			content += '<div>' + v.description + '</div>';
		}
		if(v.link != ''){
			content += '<div><a href="' + v.link + '" target="_blank">' + v.link + '</a></div>';
		}
	}
	else{
		content += '<div>' + v.address + '</div>';
		if(v.phone != ''){
			content += '<div>tel.: ' + v.phone + '</div>';
		}
		if(v.fax != ''){
			content += '<div>fax: ' + v.fax + '</div>';
		}
		if(v.email != ''){
			content += '<div>e-mail: <a href="mailto:' + v.email + '">' + v.email + '</a></div>';
		}
		if(v.link != ''){
			content += '<div>www: <a href="' + v.link + '" target="_blank">' + v.link + '</a></div>';
		}
		if(v.pageId != '' && v.institutionId != ''){
			var href = PageGMaps.linkToInstitution;
			href = href.replace('{str}', v.pageId);
			href = href.replace('{id}', v.institutionId);
			content += '<div style="margin: 5px 0 0 0;"><a href="' + href + '" target="_blank">'
			+ PageGMaps.labels.more + '</a></div>';
		}
	}
	marker.info = '<div class="marker_window">' + content + '</div>';
    GEvent.addListener(marker, 'click', function(a, b){
        marker.openInfoWindowHtml(marker.info);
    });
    return marker;
};

//Add markers to map overlay
PageGMaps.addMarkersToOverlay = function() {
	for(var i in PageGMaps.markers){
		PageGMaps.map.addOverlay(PageGMaps.markers[i]['marker']);
	}
	if (PageGMaps.isLinesLoading != null && PageGMaps.isLinesLoading == true) {
		for(var i = 0; i <= PageGMaps.layersCount; i++){
			var checkboxName = PageGMaps.checkboxLayerPrefix + i;
			if (document.interfacePoints[checkboxName].checked) {
				PageGMaps.visibileLayer(document.interfacePoints[checkboxName].value,true);
			} else {
				PageGMaps.visibileLayer(document.interfacePoints[checkboxName].value,false);
			}
		}
	}
}

//Change layer visibility
PageGMaps.changeLayerVisibility = function(field, index){
	var form = field.form;
	if(field.type != 'checkbox'){
		field = form[PageGMaps.checkboxLayerPrefix + index];
		field.checked = !field.checked;
	}
	if(index > 0){
		checkboxName = PageGMaps.checkboxLayerPrefix + 0;
		form[checkboxName].checked = false;
		PageGMaps.visibileLayer(field.value, field.checked);
	}
	else {
		var checkboxName;
		for(var i = 0; i <= PageGMaps.layersCount; i++){
			checkboxName = PageGMaps.checkboxLayerPrefix + i;
			form[checkboxName].checked = field.checked;
			PageGMaps.visibileLayer(form[checkboxName].value, field.checked);
		}
	}
};

//Check layer visibility
PageGMaps.visibileLayer = function(layerId, isVisible){
	for(var i in PageGMaps.markers){
		if(PageGMaps.markers[i]['layerId'] == layerId){
			with(PageGMaps.markers[i]['marker']){
				isVisible ? show() : hide();
			}
		}
	}

};

//Open infoWindow
PageGMaps.openInfoWindow = function(pointId){
	for(var i in PageGMaps.markers){
		var layerId = PageGMaps.markers[i]['layerId'];
		var info    = PageGMaps.markers[i]['marker'].info;
		if(i == pointId){
		  with(PageGMaps.markers[i]['marker']){
			openInfoWindowHtml(info);
			PageGMaps.visibileLayer(layerId, true);
			for(var i = 0; i <= PageGMaps.layersCount; i++){
				var checkboxName = PageGMaps.checkboxLayerPrefix + i;
				if (document.interfacePoints[checkboxName].value == layerId) {
					document.interfacePoints[checkboxName].checked = true;
				}
			}
		  }
		}
	}
};

//Set map types
PageGMaps.setMapTypes = function(){
	var mapTypes = PageGMaps.map.getMapTypes();
	mapTypes[0].getName = function(){
	    return 'Mapa';
	}
	mapTypes[1].getName = function(){
	    return 'Satelita';
	}
	mapTypes[2].getName = function(){
	    return 'Hybryda';
	}
};

