// JavaScript Document

//alert box

// verify browser language
function getLang(type) {
    var lang;
    if (typeof navigator.userLanguage != "undefined") {
        lang = navigator.userLanguage.toLowerCase();
    } else if (typeof navigator.language != "undefined") {
        lang = navigator.language.toLowerCase();
    }
    return (lang && lang.indexOf(type.toLowerCase()) == 0)
}
//
function switchLang(data_lang) {
	localStorage.setItem('lang',data_lang);
	window.location=i_start.htm;
}

function getData(ID_query) {
	localStorage.setItem('data_id', ID_query);
	var details = "";
	$.get(localStorage.getItem('xmlFile'), function(theXML){
		$('immo',theXML).each(function(i){
			var id = $(this).find("id").text();
			if (localStorage.getItem('data_id') == id) {
				var localite = $(this).find("localite").text();
				var objet = $(this).find("objet").text();
				var nature = $(this).find("nature").text();
				var commentaire = $(this).find("commentaire").text();
				var photo = $(this).find("photo").text();
				var prixeuro = $(this).find("prixeuro").text();
				var charges = $(this).find("charges").text();
				var latitude = $(this).find("latitude").text();
				var longitude = $(this).find("longitude").text();
				var rue = $(this).find("rue").text();

				localStorage.setItem('latitude', latitude);
				localStorage.setItem('longitude', longitude);

				if (localStorage.getItem('lang') == "fr") {
					if (nature == "v") {
						var text0 = "Vente";
						var text1 = "Prix de vente: ";
						var text2 = "Charges: ";
					}
					if (nature == "l") {
						var text0 = "Location";
						var text1 = "Prix de location: ";
						var text2 = "Charges: ";
					}
					if (nature == "p") {
						var text0 = "Promotion";
						var text1 = "Prix: ";
						var text2 = "Charges: ";
					}
					if (nature == "vp") {
						var text0 = "Vente/Promotion";
						var text1 = "Prix: ";
						var text2 = "Charges: ";
					}
					var texte3 = new Array("Impression","Afficher carte","à","Année de construction","Etage","Surface","Surface d'habitation","Cuisine","Salle à manger","Living","Chambre à coucher","Salle de bains/WC","Bureau/Débarras","Terrasse/Balcon","Garage","Cave","Grenier","Chauffage","Etat","Divers","Retour à la liste","Carte interactive");
				}
				if (localStorage.getItem('lang') == "de") {
					if (nature == "v") {
						var text0 = "Verkauf";
						var text1 = "Verkaufspreis: ";
						var text2 = "Kosten: ";
					}
					if (nature == "l") {
						var text0 = "Vermietung";
						var text1 = "Mietpreis: ";
						var text2 = "Kosten: ";
					}
					if (nature == "p") {
						var text0 = "Promotion";
						var text1 = "Preis: ";
						var text2 = "Kosten: ";
					}
					if (nature == "vp") {
						var text0 = "Verkauf/Promotion";
						var text1 = "Preis: ";
						var text2 = "Kosten: ";
					}
					var texte3 = new Array("Druckversion","Karte ansehen","gelegen in","Baujahr","Stockwerke","Grundstück","Wohnfläche","Küche","Esszimmer","Salon","Schlafzimmer","Badezimmer/WC","Büro/Abstellraum","Terrasse/Balkon","Garage","Keller","Dachboden","Heizung","Allg. Zustand","Sonstiges","","Interaktive Karte");
				}
				if (localStorage.getItem('lang') == "en") {
					if (nature == "v") {
						var text0 = "Sale";
						var text1 = "Price: ";
						var text2 = "Charges: ";
					}
					if (nature == "l") {
						var text0 = "Rental";
						var text1 = "Price: ";
						var text2 = "Charges: ";
					}
					if (nature == "p") {
						var text0 = "Promotion";
						var text1 = "Price: ";
						var text2 = "Charges: ";
					}
					if (nature == "vp") {
						var text0 = "Sale/Promotion";
						var text1 = "Price: ";
						var text2 = "Charges: ";
					}
					var texte3 = new Array("Print","View map","in","Year of construction","Floor","Surface of the building ground","Living space","Kitchen","Dining room","Living","Bedroom","Bathroom/Lavatories","Drawing room","Terasse/Balcony","Garage","Basement","Loft","Heating","Overall condition","Various","","Interactif map");
				}
				details = "<h2>" + text0 + ": " + objet + " " + texte3[2] + " " + localite + "</h2><br />";
				details += "<p>" + commentaire + "</h2><br /><p>";
				if ($(this).find("anneeconstr").text() != "") {details +=  texte3[3] + ": " +  $(this).find("anneeconstr").text() + "<br />";}
				if ($(this).find("etage").text() != "") {details +=  texte3[4] + ": " +  $(this).find("etage").text() + "<br />";}
			    if ($(this).find("terrain").text() != "") {details +=  texte3[5] + ": " +  $(this).find("terrain").text() + "<br />";}
				if ($(this).find("surfacehabit").text() != "") {details +=  texte3[6] + ": " +  $(this).find("surfacehabit").text() + "<br />";}
				if ($(this).find("cuisine").text() != "") {details +=  texte3[7] + ": " +  $(this).find("cuisine").text() + "<br />";}
			    if ($(this).find("sallemanger").text() != "") {details +=  texte3[8] + ": " +  $(this).find("sallemanger").text() + "<br />";}
			    if ($(this).find("living").text() != "") {details +=  texte3[9] + ": " +  $(this).find("living").text() + "<br />";}
			    if ($(this).find("chambrecouch").text() != "") {details +=  texte3[10] + ": " +  $(this).find("chambrecouch").text() + "<br />";}
			    if ($(this).find("sdebainwc").text() != "") {details +=  texte3[11] + ": " +  $(this).find("sdebainwc").text() + "<br />";}
			    if ($(this).find("salon").text() != "") {details +=  texte3[12] + ": " +  $(this).find("salon").text() + "<br />";}
			    if ($(this).find("terrasse").text() != "") {details +=  texte3[13] + ": " +  $(this).find("terrasse").text() + "<br />";}
			    if ($(this).find("garage").text() != "") {details +=  texte3[14] + ": " +  $(this).find("garage").text() + "<br />";}
			    if ($(this).find("cave").text() != "") {details +=  texte3[15] + ": " +  $(this).find("cave").text() + "<br />";}
			    if ($(this).find("grenier").text() != "") {details +=  texte3[16] + ": " +  $(this).find("grenier").text() + "<br />";}
			    if ($(this).find("chauffage").text() != "") {details +=  texte3[17] + ": " +  $(this).find("chauffage").text() + "<br />";}
			    if ($(this).find("etat").text() != "") {details +=  texte3[18] + ": " +  $(this).find("etat").text() + "<br />";}
				if ($(this).find("divers").text() != "") {details +=  texte3[19] + ": " +  $(this).find("divers").text() + "<br />";}
				details += "</p>";
				if (photo != "") {
					details += "<p><img src='http://www.immo-biewer.lu/pictures/" + photo + "' border='0' alt='Photo'></p>";
				}
				details += "<p class='info'>" + text1 + prixeuro + " €</p>";
				if (nature == "l") {
					details += "<p class='info'>" + text2 + charges + " €</p>";	
				}
				if (rue != "") {
					details += "<img src='http://maps.google.com/staticmap?center=" + latitude + "," + longitude;
					details += "&zoom=14&size=320x320&maptype=mobile&markers=" + latitude + "," + longitude;
					details += ",&key=ABQIAAAAMWN3R1OsPpThecPLd8hedhTav9-bfr8EDTq43RntO3IA5wCuphThXUKwIry-JCke0WrSzLd5tEzHGw";
					details += "&sensor=false' /><br />";
					details += "<ul class='edgetoedge'><li class='arrow'><a href='showmap.htm' onclick='getMap();return false' target='_blank'><img src='map.gif' width='30' height='30' alt='MAP' />&nbsp;" + texte3[21] + "</a></li></ul>";
				}
				$("#detail_data").html(details);
				jQT.goTo($('#immodetail'),'fade');
				//return false;
			}			
		});	
	});
}


function getMap() {
	$('#map_data').googleMaps({
		latitude: localStorage.getItem('latitude'),
		longitude: localStorage.getItem('longitude'),
		depth: 14,
		markers: [{latitude: $(this).find("latitude").text(),longitude: $(this).find("longitude").text(),icon: {image: 'marker.gif',shadow: 'marker_shadow.gif',iconSize: '30,23',shadowSize: '50,21'}}]
	});
	jQT.goTo($('#showmap'),'fade');
	return false;
}





	var lang = "";
	if (getLang("de")) {lang = "de";}
	if (getLang("en")) {lang = "en";}
	if (getLang("fr")) {lang = "fr";}
	if (localStorage.getItem('lang') == "de") {lang = "de";}
	if (localStorage.getItem('lang') == "fr") {lang = "fr";}
	if (localStorage.getItem('lang') == "en") {lang = "en";}
	if (lang == "") {lang = "fr"};
	if (lang == "fr") {
		xml_file = "immo_fr.xml";
		rss_file = "http://www.immo-biewer.lu/immobiewer.xml";
	}
	if (lang == "de") {
		xml_file = "immo_de.xml";
		rss_file = "http://www.immo-biewer.lu/immobiewer_de.xml";
	}
	if (lang == "en") {
		xml_file = "immo_en.xml";
		rss_file = "http://www.immo-biewer.lu/immobiewer_en.xml";
	}
	localStorage.setItem('lang', lang);
	localStorage.setItem('xmlFile',xml_file);
	localStorage.setItem('rssFile',rss_file);

	var datum = "";
	$.get(localStorage.getItem('xmlFile'), function(theXML){
		$('immobiewer',theXML).each(function(i){
			var id = $(this).find("dateup").text();
				var datum = $(this).find("dateup").text();
				localStorage.setItem('datum',datum);
			});
	});

	var jQT = $.jQTouch({
		icon: 'iimmo.png',
		statusBar: 'black',
		startupscreen: 'istartup.png'
	});

$(document).ready(function() {
	var vente = "";
	var location = "";
	var promotion = "";
	var counter_v = 0;
	var counter_l = 0;
	var counter_p = 0;
	$.get(localStorage.getItem('xmlFile'), function(theXML){
	$('immo',theXML).each(function(i){
			var id = $(this).find("id").text();
			var localite = $(this).find("localite").text();
			var objet = $(this).find("objet").text();
			var nature = $(this).find("nature").text();
			var etatvente = $(this).find("etatvente").text();
			if ((nature == "v") || (nature == "vp")) {
				if (etatvente != "") {
					vente += "<li class='arrow'>" + localite + ": " + objet +" (<font size='-1'>" + etatvente + "</font>)</li>";
				} else {
					vente += "<li class='arrow'><a href='#immodetail' onclick='getData("+id+");return false'>" + localite + ": " + objet +"</a></li>";
				}
				counter_v++;
			}
			if (nature == "l") {
				if (etatvente != "") {
					location += "<li class='arrow'>" + localite + ": " + objet +" (<font size='-1'>" + etatvente + "</font>)</li>";
				} else {
					location += "<li class='arrow'><a href='#immodetail' onclick='getData("+id+");return false'>" + localite + ": " + objet +"</a></li>";
				}
				counter_l++;
			}
			if ((nature == "p") || (nature == "vp")) {
				if (etatvente != "") {
					promotion += "<li class='arrow'>" + localite + ": " + objet +" (<font size='-1'>" + etatvente + "</font>)</li>";
				} else {
					promotion += "<li class='arrow'><a href='#immodetail' onclick='getData("+id+");return false'>" + localite + ": " + objet +"</a></li>";
				}
				counter_p++;
			}
		});
		if (localStorage.getItem('lang') == "fr") { var texta = "<h2>Pas de données!</h2>";}
		if (localStorage.getItem('lang') == "de") { var texta = "<h2>Keine Daten vorhanden!</h2>";}
		if (localStorage.getItem('lang') == "en") { var texta = "<h2>No data!</h2>";}
		if (counter_v > 0) {
			$("#vente_data").html("<ul class='edgetoedge'>"+vente+"</ul>");
		} else {
			$("#vente_data").html(texta);
		}
		if (counter_l > 0) {
			$("#location_data").html("<ul class='edgetoedge'>"+location+"</ul>");
		} else {
			$("#location_data").html(texta);
		}
		if (counter_p > 0) {
			$("#promotion_data").html("<ul class='edgetoedge'>"+promotion+"</ul>");
		} else {
			$("#promotion_data").html(texta);
		}
	});
})
