var Departments = {
	markers: new List(),
	lookup: new List(),
	map: null,
	geocoder: null,
	icon: null,
	show: null,
	
	setMap: function(mapObj){
		Departments.geocoder = new GClientGeocoder();
		var map = new GMap2(mapObj);
		if (GBrowserIsCompatible()) {
			Departments.map = new GMap2(document.getElementById("map"));
			Departments.map.addControl(new GSmallMapControl());
			
			Departments.icon = new GIcon(G_DEFAULT_ICON);
			Departments.icon.image = Toolbox.makeURL('share/web/images/marker_old.png');
			Departments.icon.shadow = Toolbox.makeURL('share/web/images/spacer.png');
			Departments.icon.iconSize = new GSize(24, 16);
			Departments.icon.shadowSize  = new GSize(1, 1);
			Departments.icon.iconAnchor = new GPoint(12, 8);
			
			if(this.show == null){
				Departments.map.setCenter(new GLatLng(56.17, 10.8), 7, G_NORMAL_MAP);
			}else{
				Departments.map.setCenter(this.show, 15, G_NORMAL_MAP);
				Departments.icon.iconSize = new GSize(24, 16);
			}
		}

		while(marker = Departments.markers.each()){
			Departments.showAddress(marker[0], marker[1]);
		}
	},
	
	showAddress: function(id, address) {
		if(typeof map != 'null'){
			if(typeof address != 'string'){
				var marker = new GMarker(new GLatLng(address[1], address[2]), { icon: Departments.icon });
				GEvent.addListener(marker, "click", Departments.getDepartmentContent.bind(this, id, marker));
		        Departments.map.addOverlay(marker);
			} else {
				Departments.geocoder.getLatLng(address, function(point) {
					if (point) {
						new Ajax.Request(Toolbox.makeURL('rpc/departments/'+id+'/?latitude='+point.lat()+'&longitude='+point.lng()), { method:'get' });
						var marker = new GMarker(point, { icon: Departments.icon });
						GEvent.addListener(marker, "click", Departments.getDepartmentContent.bind(this, id, marker));
				        Departments.map.addOverlay(marker);
					}
				});
			}
		}
	},
	
	getDepartmentContent: function(department, marker){
		new Ajax.Request(Toolbox.makeURL('rpc/departments/'+department+'/'), {
			method:'get',
			onSuccess: Departments.showDepartmentContent.bind(this, marker)
		});
	},
	
	showDepartmentContent: function(marker, transport){
		department = transport.responseXML.getElementsByTagName('department').item(0);
		div = document.createElement('div');
		div.setAttribute('class', 'marker');

		h2 = div.appendChild(document.createElement('h2'));
		h2.appendChild(document.createTextNode(department.getElementsByTagName('name').item(0).firstChild.nodeValue));

		p = div.appendChild(document.createElement('p'));
		p.appendChild(document.createTextNode(department.getElementsByTagName('address').item(0).firstChild.nodeValue));
		p.appendChild(document.createElement('br'));
		p.appendChild(document.createTextNode(department.getElementsByTagName('zipcode').item(0).firstChild.nodeValue));
		p.appendChild(document.createTextNode(' '));
		p.appendChild(document.createTextNode(department.getElementsByTagName('city').item(0).firstChild.nodeValue));
		p.appendChild(document.createElement('br'));		
		if(department.getElementsByTagName('phone').length > 0){
			p.appendChild(document.createTextNode('Tlf.: '+department.getElementsByTagName('phone').item(0).firstChild.nodeValue));
			p.appendChild(document.createElement('br'));
		}
		if(department.getElementsByTagName('fax').length > 0){
			p.appendChild(document.createTextNode('Fax: '+department.getElementsByTagName('fax').item(0).firstChild.nodeValue));
			p.appendChild(document.createElement('br'));
		}
		
		p.appendChild(document.createElement('br'));
		a = p.appendChild(document.createElement('a'));
		a.setAttribute('href', 'afdelinger/'+department.getAttribute('id')+'/');
		a.appendChild(document.createTextNode('Gå til afdelingen'));
		p.appendChild(document.createElement('br'));
		
		marker.openInfoWindowHtml(div);
	}
		
		
}

Event.observe(window, 'load', function(){
	if($('map')){
		Departments.setMap($('map'));
	}
});