
function immoSearch1()
{
	GUnload();
	if ($('searchspinner')) $('searchspinner').style.display = 'inline';
	var infoHTML = $('city').selectedIndex > -1 ? $('city')[$('city').selectedIndex].value : '/6.0';
	var immoXML = '/51.0/search/bestreturn';
	var city = $('city').selectedIndex > 0 ? $('city')[$('city').selectedIndex].text : '';
	var roomCountComplete = $('roomCountComplete').selectedIndex > 0 ? $('roomCountComplete')[$('roomCountComplete').selectedIndex].text : '';
	var roomCountFrom = $F('roomCountFrom');
	var roomCountTo = $F('roomCountTo');
	var purchasePriceFrom = $F('purchasePriceFrom');
	var purchasePriceTo = $F('purchasePriceTo');
	roomCountFrom = roomCountFrom == '' ? 0 : roomCountFrom;
	roomCountTo = roomCountTo == '' ? 999 : roomCountTo;
	purchasePriceFrom = purchasePriceFrom == '' ? 0 : purchasePriceFrom;
	purchasePriceTo = purchasePriceTo == '' ? 999999999 : purchasePriceTo;
	new Ajax.Request(infoHTML, { method: 'get',  parameters:{'options[city]': city, 'options[roomCountComplete]': roomCountComplete}, onComplete: composeImmoContent });
	new Ajax.Request(immoXML, { method: 'get', parameters: {'options[city]': city, 'options[room_count][from]': roomCountFrom, 'options[room_count][to]': roomCountTo, 'options[purchase_price][from]': purchasePriceFrom, 'options[purchase_price][to]': purchasePriceTo}, onComplete: composeSearchResult1 });
}

function immoSearch2()
{
	if($('city').selectedIndex == -1) {
		return false;
	}
	GUnload();
	if ($('searchspinner')) $('searchspinner').style.display = 'inline';
	var infoHTML = $('city').selectedIndex > -1 ? $('city')[$('city').selectedIndex].value : '/7.0';
	//var infoHTML = $('city').selectedIndex > -1 ? '/27.2' : '/7.0';
	var immoXML = '/7.0/search/all';
	var city = $('city').selectedIndex >= 0 ? $('city')[$('city').selectedIndex].text : '';
	var roomCountFrom = $F('roomCountFrom');
	var roomCountTo = $F('roomCountTo');
	var purchasePriceFrom = $F('purchasePriceFrom');
	var purchasePriceTo = $F('purchasePriceTo');
	roomCountFrom = roomCountFrom == '' ? 0 : roomCountFrom;
	roomCountTo = roomCountTo == '' ? 999 : roomCountTo;
	purchasePriceFrom = purchasePriceFrom == '' ? 0 : purchasePriceFrom;
	purchasePriceTo = purchasePriceTo == '' ? 999999999 : purchasePriceTo;
	new Ajax.Request(infoHTML, { method: 'get', onComplete: composeImmoContent });
	new Ajax.Request(immoXML, { method: 'get', parameters: {'options[city]': city, 'options[room_count][from]': roomCountFrom, 'options[room_count][to]': roomCountTo, 'options[purchase_price][from]': purchasePriceFrom, 'options[purchase_price][to]': purchasePriceTo}, onComplete: composeSearchResult2 });
}

function composeSearchResult1(transport)
{
	var xml = transport.responseXML;
	var searchResultTpl = new Template(''
		+ '<div class="#{item_align}">'
		+   '<div class="searchResultHead"><a href="/immo/#{id}/#{rate_of_return};#{room_count}" onClick="immoShow(\'/expose/#{id}/#{rate_of_return};#{room_count}\'); setLastObjectRequest(\'/expose/#{id}/#{rate_of_return};#{room_count}\'); return false;"><h3>#{city}, #{street} #{house_number}</h3></a></div>'
		+   '<div class="searchResultImage"><a href="/immo/#{id}/#{rate_of_return};#{room_count}" onClick="immoShow(\'/expose/#{id}/#{rate_of_return};#{room_count}\'); setLastObjectRequest(\'/expose/#{id}/#{rate_of_return};#{room_count}\'); return false;"><img src="#{thumbnail}" title="#{title}" alt="#{title}" /></a></div>'
		+   '<div class="searchResultText">'
		+     'Kaufpreis: #{purchase_price}<br />'
		+     'Wohnfläche: #{comm_usable_area}<br />'
		+     'Anzahl Zimmer: #{room_count}<br />'
		+     'Nettokaltmiete: #{cold_rent}<br />'
		+     '<span class="rate_of_return">Rendite: #{rate_of_return}%</span>'
		+   '</div>'
		+ '</div>');

	var searchResultHtml = '';
	var items = xml.getElementsByTagName('item');
	if (items.length > 0) searchResultHtml = '<h2>Aktuelle Angebote' + ($('city').selectedIndex > 0 ? ' in ' + $('city')[$('city').selectedIndex].text : '') + '</h2>' + '<div class="separator"></div>';
	else searchResultHtml = '<h2>Ihre Suche ergab keine Treffer.</h2>';
	for (var i = 0; i < items.length; i++) {

		if(items[i].getElementsByTagName('purchase_price')[0].firstChild.nodeValue == '0,00&nbsp;&euro;') {
        	var priceUpdated = 'auf Anfrage';
        }
        else {
			var priceUpdated = items[i].getElementsByTagName('purchase_price')[0].firstChild.nodeValue;
        }
		
		if((i % 2) == 0) {
        	var itemAlign = 'searchResultItemLeft';
        }
        else {
			var itemAlign = 'searchResultItemRight';
        }

		searchResultHtml += searchResultTpl.evaluate({
			item_align:  itemAlign,
			id: items[i].getElementsByTagName('id')[0].firstChild.nodeValue,
			title: items[i].getElementsByTagName('title')[0].firstChild.nodeValue,
			city: items[i].getElementsByTagName('city')[0].firstChild.nodeValue,
			street: items[i].getElementsByTagName('street')[0].firstChild.nodeValue,
			house_number: items[i].getElementsByTagName('house_number')[0].firstChild.nodeValue,
			purchase_price: priceUpdated,
			comm_usable_area: items[i].getElementsByTagName('comm_usable_area')[0].firstChild.nodeValue,
			room_count: items[i].getElementsByTagName('room_count')[0].firstChild.nodeValue,
			cold_rent: items[i].getElementsByTagName('cold_rent')[0].firstChild.nodeValue,
			rate_of_return: items[i].getElementsByTagName('rate_of_return')[0].firstChild.nodeValue,
			thumbnail: items[i].getElementsByTagName('thumbnail')[0].firstChild.nodeValue		});
	}
	$('searchResult').update(searchResultHtml + '<div class="separator"></div>');
	if ($('searchspinner')) $('searchspinner').style.display = 'none';
}

function composeSearchResult2(transport)
{
	var xml = transport.responseXML;
	var searchResultTpl = new Template(''
		+ '<div class="#{item_align}"">'
		+ '  <div class="searchResultHead"><a href="/immo/#{id}" onClick="immoShow(\'/expose/#{id}\'); return false;"><h3>#{street} #{house_number}</h3></a></div>'
		+ '  <div class="searchResultImage"><a href="/immo/#{id}" onClick="immoShow(\'/expose/#{id}\'); return false;"><img src="#{thumbnail}" title="#{title}" alt="#{title}" /></a></div>'
		+ '  <div class="searchResultText">'
		+ '    Kaufpreis: #{purchase_price}<br />'
		+ '    Wohnfläche: #{living_area}<br />'
		+ '    Anzahl Zimmer: #{room_count}<br />'
		+ '    Frei ab: #{available_date}<br />'
		+ '  </div>'
		+ '</div>'
		+ '#{separator}');

	var searchResultHtml = '';
	var items = xml.getElementsByTagName('item');
	if (items.length > 0) searchResultHtml = '<h2>Aktuelle Angebote' + ($('city').selectedIndex > 0 ? ' in ' + $('city')[$('city').selectedIndex].text : '') + '</h2>' + '<div class="separator"></div>';
	else {
		if ($('city').selectedIndex > 0) {
		
			searchResultHtml = '<h3>Im Moment steht in ' + $('city')[$('city').selectedIndex].text + ' keine Wohnung zur sofortigen Selbstnutzung zur Verfügung. Wenn Sie dennoch Kontakt mit uns aufnehmen merken wir Sie gerne vor. Klicken Sie dazu auf "Kontakt".</h3>';
		}
		else {
		
			searchResultHtml = '<h3>Im Moment steht an diesem Standort keine Wohnung zur sofortigen Selbstnutzung zur Verfügung. Wenn Sie dennoch Kontakt mit uns aufnehmen merken wir Sie gerne vor. Klicken Sie dazu auf "Kontakt".</h3>';
		}
	}
	for (var i = 0; i < items.length; i++) {

		if(items[i].getElementsByTagName('purchase_price')[0].firstChild.nodeValue == '0,00&nbsp;&euro;') {
            var priceUpdated = 'auf Anfrage';
        }
        else {
            var priceUpdated = items[i].getElementsByTagName('purchase_price')[0].firstChild.nodeValue;
        }
		
		if((i % 2) == 0) {
        	var itemAlign = 'searchResultItemLeft';
        }
        else {
			var itemAlign = 'searchResultItemRight';
        }

		searchResultHtml += searchResultTpl.evaluate({
			item_align:  itemAlign,
			id: items[i].getElementsByTagName('id')[0].firstChild.nodeValue,
			title: items[i].getElementsByTagName('title')[0].firstChild.nodeValue,
			street: items[i].getElementsByTagName('street')[0].firstChild.nodeValue,
			house_number: items[i].getElementsByTagName('house_number')[0].firstChild.nodeValue,
			purchase_price: priceUpdated,
			living_area: items[i].getElementsByTagName('living_area')[0].firstChild.nodeValue,
			room_count: items[i].getElementsByTagName('room_count')[0].firstChild.nodeValue,
			available_date: items[i].getElementsByTagName('available_date')[0].firstChild.nodeValue,
			thumbnail: items[i].getElementsByTagName('thumbnail')[0].firstChild.nodeValue,
			separator: i % 2 ? '<div class="separator"></div>' : ''
		});
	}
	$('searchResult').update(searchResultHtml);
	if ($('searchspinner')) $('searchspinner').style.display = 'none';
}

function setLastObjectRequest(request) {
	$('lastObjectRequest').value = request;
}

function getLastObjectRequest() {
	immoShow($('lastObjectRequest').value);
}

function immoShow(immoHTML)
{
	GUnload();
	if ($('searchspinner')) $('searchspinner').style.display = 'inline';
	new Ajax.Request(immoHTML, { method: 'get', onComplete: composeImmoContent });
}

function composeImmoContent(transport)
{
	$('immoContent').update(transport.responseText);
	border.render('shadowedBox1');
	if ($('searchspinner')) $('searchspinner').style.display = 'none';
	
	$('citySelect').selectedIndex = $('city').selectedIndex;
	$('roomSelect').selectedIndex = $('roomCountComplete').value;
	$('ObjektLink').href = $('lastObjectRequest').value;
}

function toggleDisplay(object)
{
	$(object).style.display = $(object).style.display == 'none' || $(object).style.display == '' ? 'block' : 'none';
}

function toggleNav(anchor) {
	$$('.'+anchor).style.color = '#c00';
	alert(anchor);
}

function showGMap(x,y,z) {

	if (x == '' || y == '') {
		/*jQuery.facebox(function() {
			jQuery.get('http://suedwert.de/54', function(data) {
				jQuery.facebox(data);
			})
		});*/
		jQuery.facebox({image: '/static/images/missing_geo.png'});
		return;
	}
	if (GBrowserIsCompatible()) {
		var targetContainer = $('immoDetail');
		var map = new GMap2(targetContainer);
		map.setCenter(new GLatLng(x,y), 13);
		map.addOverlay(new GMarker(map.getCenter()));
		map.setUIToDefault();
	}
}

function toggleNavColor(target) {
	$(target).style.color = '#6FB41D';
	$('ObjektLink').style.color = '#4D4D4D';
	$('GalerieLink').style.color = '#4D4D4D';
	return;
}

function setPriceOnDemand() {
	var priceValue = $('priceColumn').innerHTML;
	
	if(priceValue == '&nbsp;') {
		$('priceColumn').update('auf Anfrage');
		$('priceColumnTag').hide();
	}
	return;
}

function loadingDoneScrollToTop() {
	
	$('topLocator').scrollTo();
	return;
}
