var iconOne = new GIcon(); 
iconOne.image = 'fileadmin/content/immo/files/flag01.png';
iconOne.iconSize = new GSize(24, 24);
iconOne.iconAnchor = new GPoint(0, 24);
iconOne.infoWindowAnchor = new GPoint(0, 24);

var iconTwo = new GIcon(); 
iconTwo.image = 'fileadmin/content/immo/files/flag02.png';
iconTwo.iconSize = new GSize(24, 24);
iconTwo.iconAnchor = new GPoint(0, 24);
iconTwo.infoWindowAnchor = new GPoint(0, 24);

var iconThree = new GIcon(); 
iconThree.image = 'fileadmin/content/immo/files/flag03.png';
iconThree.iconSize = new GSize(24, 24);
iconThree.iconAnchor = new GPoint(0, 24);
iconThree.infoWindowAnchor = new GPoint(0, 24);

var iconFour = new GIcon(); 
iconFour.image = 'fileadmin/content/immo/files/flag04.png';
iconFour.iconSize = new GSize(24, 24);
iconFour.iconAnchor = new GPoint(0, 24);
iconFour.infoWindowAnchor = new GPoint(0, 24);

var customIcons = [];
customIcons["AppartmentRent"] = iconOne;
customIcons["AppartmentBuy"] = iconTwo;
customIcons["HouseRent"] = iconThree;
customIcons["HouseBuy"] = iconFour;
customIcons["Office"] = iconTwo;
customIcons["Gastronomy"] = iconTwo;
customIcons["Industry"] = iconTwo;
customIcons["Store"] = iconTwo;
customIcons["Misc"] = iconTwo;
customIcons["Investment"] = iconTwo;
customIcons["HouseType"] = iconTwo;
customIcons["WAZ"] = iconTwo;
customIcons["SiteTrade"] = iconTwo;
customIcons["SiteLivingRent"] = iconTwo;
customIcons["SiteLivingBuy"] = iconTwo;
customIcons["GarageRent"] = iconTwo;
customIcons["GarageBuy"] = iconTwo;
customIcons["FlatShareRoom"] = iconTwo;
customIcons["AssistedLiving"] = iconTwo;
customIcons["SeniorCare"] = iconTwo;

var markerGroups = {
	"AppartmentRent": [],
	"AppartmentBuy": [],
	"HouseRent": [],
	"HouseBuy": [],
	"Office": [],
	"Gastronomy": [],
	"Industry": [],
	"Store": [],
	"Misc": [],
	"Investment": [],
	"HouseType": [],
	"WAZ": [],
	"SiteTrade": [],
	"SiteLivingRent": [],
	"SiteLivingBuy": [],
	"GarageRent": [],
	"GarageBuy": [],
	"FlatShareRoom": [],
	"AssistedLiving": [],
	"SeniorCare": []
};

function initialize() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(48.658,10.17), 11);

	var loading = new GScreenOverlay('fileadmin/content/immo/files/loading.png',
        new GScreenPoint(0.5, 0.5, 'fraction', 'fraction'),  // screenXY
        new GScreenPoint(0.5, 0.5, 'fraction', 'fraction'),  // overlayXY        
        new GScreenSize(135, 79)    // size on screen
    );
	
    map.addOverlay(loading);
    
    GDownloadUrl('fileadmin/content/immo/immoapi/HzGetData.php', function(data) {
          var xml = GXml.parse(data);
          var markers = xml.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
				
				var name = markers[i].getAttribute("name");
				var type = markers[i].getAttribute("type");
				var city = markers[i].getAttribute("city");
				var zip = markers[i].getAttribute("zip");
				var street = markers[i].getAttribute("street");
				var houseno = markers[i].getAttribute("houseno");
				var netArea = markers[i].getAttribute("netArea");
				var noRooms = markers[i].getAttribute("noRooms");
				var price = markers[i].getAttribute("price");
				var picURL = markers[i].getAttribute("picURL");
				var contactHomePage = markers[i].getAttribute("contactHomePage");
				var fKey = markers[i].getAttribute("fKey");
				var uuid = markers[i].getAttribute("uuid");
				var geoloc = true;
				
				if( markers[i].getAttribute("lat") == "0.0" && markers[i].getAttribute("lng") == "0.0" ){
					geoloc = false;
				}
				
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
				
				// Display only Markers of suitable types
				if(type == 'AppartmentBuy' || type == 'AppartmentRent' || type == 'HouseBuy' || type == 'HouseRent'){
					var marker = createMarker(point, name, type, zip, city, street, houseno, netArea, noRooms, price, picURL, contactHomePage, fKey, uuid, geoloc);
					// Display Marker only if geo data available
					if( geoloc ){
						//$('#immotable').append('<div style="visibility: hidden;">'+'adding marker: '+type+'</div>');
						map.addOverlay(marker);
					}
				}
          }
          map.removeOverlay(loading);
		  
//// Paging with pajination jquery plugin
				$('#pager_container').pajinate({
					items_per_page : 10,
					item_container_id : '.pager_content',
					nav_panel_id : '.pager_navigation',
					nav_label_first : '<< Erste ',
					nav_label_last : ' Letzte >>' ,
					nav_label_prev : ' < zurück ',
					nav_label_next : ' vor > ' 
				});
////	
		});
  } 
}

function createMarker(point, name, type, zip, city, street, houseno, netArea, noRooms, price, picURL, contactHomePage, fKey, uuid, geoloc) {
	var marker = new GMarker(point, customIcons[type]);
	markerGroups[type].push(marker);

	var typ = translateType(type);
	var volllink = '<a class="exposelink" href="fileadmin/content/immo/immoapi/HzGetExpose.php?id=' + uuid + '" target="_blank">Vollansicht</a>';
	var deeplink = '<a class="deeplink" href="http://immobilienscout24.de/' + uuid + '" target="">Details bei Immoscout24 aufrufen</a>';
	var immourl = 'http://immobilienscout24.de/'+uuid;
	var immolink = '<a class="immolink" href="javascript:opendetails(\''+ immourl +'\');">Details bei Immobilienscout24</a>';
	var kontaktlink = '<a class="kontaktlink" href="http://' + contactHomePage + '" target="_blank">'+ contactHomePage + '</a>';
	var divpic = '<div class="immo-pic"><img src="'+picURL+'" /></div>';
	
	var divtexton = '<div class="immo-marker-popup">';
	var info1 = noRooms + ' Zimmer<br />';
	var info2 = netArea + ' qm<br />';
	var info3 = price + ' Euro<br />';
	var info4 = street + ' ' + houseno + '<br />';
	var info5 = zip + ' ' + city + '<br />';
	var info6 = kontaktlink + '<br />';
	var info7 = immolink + '<br />';
	var divtextoff = '</div>';

	var divtext = divtexton + info1 + info2 + info3 +info4 +info5 + info6 + info7 + divtextoff;
	var head = '<div class="immo-marker-title">'+typ+'<br/><b>'+name+'</b><br/>';
	var foot = '<div class="immo-marker-footer">&nbsp;</div></div>';
	var body = divpic + divtext;
	var html = head+body+foot;

	GEvent.addListener(marker, 'click', function() {
	  marker.openInfoWindowHtml(html);
	});
	
	if( geoloc == false){
		appendTableRow(type, name, kontaktlink, divpic, immolink, netArea, price);
	}
	
	return marker;
}

function appendTableRow(type, name, kontaktlink, divpic, immolink, netArea, price){
	
	divleft = divpic;
	divmid = '<div class="immo-tablerow-mid">'+translateType(type)+'<br /><strong>'+name+'</strong>, '+netArea+' qm, '+price+' €'+'<br />'+kontaktlink+'</div>';
	divright = '<div class="immo-tablerow-right" >'+immolink+'</div>';
	divclear = '<div class="immo-tablerow-clear" />';
	
	$('.pager_content').append('<div class="'+type+'">'+divleft+divright+divmid+divclear+'</div>');
	//$('#immotable').append('<div class="">'+divleft+divright+divmid+divclear+'</div>');
	
	//$('#immotable').append("<tr class="+type+"><td>"+divpic+"</td><td>"+translateType(type)+"<br />"+name+"<br />"+kontaktlink+"</td><td>"+immolink+"</td></tr>");
}

function translateType(type) {
	var typ = '';
	switch (type) {
	  case "AppartmentRent": typ = 'Wohnung mieten';
	  	break;
	  case "AppartmentBuy": typ = 'Wohnung kaufen';
		break;
	  case "HouseRent": typ = 'Haus mieten';
	  	break;
	  case "HouseBuy": typ = 'Haus kaufen';
	  	break;
	  default: typ = type; 
	}
	return typ;
}

function toggleGroup(type) {

	//$("."+type).css("display","none");

	for (var i = 0; i < markerGroups[type].length; i++) {
		var marker = markerGroups[type][i];
		if (marker.isHidden()) {
			marker.show();
			$("."+type).show();
		} else {
			marker.hide();
			$("."+type).hide();
		}
	} 
}

function opendetails (url) {
	fenster = window.open(url, "_blank", "width=800,height=600,status=yes,scrollbars=yes, resizable=yes");
	fenster.focus();
}
