/**
 * @author Thomas Müller
 * modified by Denis Krüger
 */
window.addEvent('domready',function(){load();});

function load()
{
	if (!GBrowserIsCompatible()) {
		return null;
	}
	
	// initialize the gMap
	var map = new GMap2(document.getElementById("gmap"));
	map.setCenter(new GLatLng(51.04407057266856, 13.725013732910156), 13);
	map.addControl(new GLargeMapControl());
	map.enableScrollWheelZoom();
	var icons = [];
	
	// get the markers for the lg_objects 
	var markers = getMarkers();
  
	var bounds = new GLatLngBounds();
	var atLeastOneMarker=false;
	markers.each(function(m)
	{
		map.addOverlay(m);
		bounds.extend(m.getLatLng());
		atLeastOneMarker=true;	
	});
  
  	if(atLeastOneMarker==true)
  	{
	// rezentrieren der map und anpassen des Kartenausschnitts, 
	// so dass alle Marker eingeblendet werden können
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

	// events für die Listview
	initListView();
	
	if(Browser.Engine.trident && Browser.Engine.version == 4)
		updateFooter.delay(1000);
	}
}

/**
 * erstellen der marker für die map, inkl. infoWindowsHtml
 * @return {Hash} markers
 */
function getMarkers()
{
	var markers = new Hash();
	
	
	var icons = new Hash();
	var i = 1;
	locations.each(function(loc)
	{
		var ic =new GIcon(G_DEFAULT_ICON,window.location.protocol+'//'+window.location.host+'/php/gmap_marker.php?char='+loc.identifier);
		ic.iconSize = new GSize(18, 32);
		ic.shadowSize = new GSize(39,32);
		icons.set(loc.identifier,ic);
		i = i+1;
	});
	
	locations.each(function(loc)
	{
		var marker = new GMarker(new GLatLng(loc.lat, loc.lon),{title: loc.title, icon : icons.get(loc.identifier)});
		
		var infoWindow = new Element('div',{'class':'locInfoWindow'})
			.adopt(new Element('h2').adopt(infoWindowLink(loc.identifier+") "+loc.title,loc)))
			.adopt(new Element('p').adopt(infoWindowLink('<br>'+(loc.img!=""?'<img class="lg_prevImg" src="'+loc.img+'">':""),loc))
		);

		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(infoWindow,{pixelOffset:new GSize(-10,-10)});
        });
		markers.set(loc.id, marker);
	});
	return markers;
}


function infoWindowLink(con, loc){
	return new Element('a',{'href':window.location.protocol+'//'+window.location.host+'/de/drewag/liegenschaften/dg_liegenschaften_'+loc.typ_lg+'_detail.php?exposee='+loc.unique}).set('html', con);
}

/**
 * ein- und ausblenden von Markern einer Kategorie
 * @param {Object} el
 * @param {Object} markers
 */
function toggleMarkers(el,markers){
	ctm = types.get(el.getElement('input').get('value'));		// currentTypeMarkers
	if(el.getElement('input').getProperty('checked') == false){
		ctm.each(function(cm){
			markers[cm].hide();
			el.getElement('input').setProperty('checked',false);
		});
	}else{
		ctm.each(function(cm){
			markers[cm].show();
			el.getElement('input').setProperty('checked',true);
		});
	}
}


/**
 * die click-events für die Listenansicht erstellen
 */
function initListView(){
	//var acc = new Accordion($$('.accTitle'),$$('.accContent'));
	var conSizes = new Hash();
	$$('.accTitle').each(function(el,i){
		var con = el.getNext();
		conSizes.set(i, con.getSize().y);

		if (i > 0) {
			con.setStyles({
				'height':'1px',
				'opacity': '0'
			});
		}
		con.setStyles({
			'overflow': 'hidden'
		});
		
		el.addEvent('click',function(ev){
			new Event(ev).stop();
			var h = 1;
			var o = 0;
			if(ev.target.getNext().getSize().y == 1){
				h = conSizes.get($$('.accContent').indexOf(ev.target.getNext()));
				o = 1;
			}
			ev.target.getNext().morph({'height': h+'px', 'opacity': o});
			
			if(Browser.Engine.trident && Browser.Engine.version == 4){
				updateFooter.delay(1000);
			}
		});
	});
}


/**
 * für den lieben ie6 eine funktion die den Footer an den unteren Rand der Seite setzt
 */
function updateFooter(){
	var f = $('footer');
	var winH = ( parseInt($('content').getSize().y) + 157);
	f.setStyles({
		'position' : 'absolute',
		'top': winH.toString()+'px'
	});
}
