// JavaScript Document
//salesSearch()
function salesSearch(){
pricedrop = '<select name="price" style="width: 145px;" id="price"><option value="0" selected="">Any</option><option value="1">Under &euro;100,000</option><option value="2">&euro;100,000 - &euro;200,000</option><option value="3">&euro;200,000 - &euro;300,000</option><option value="4">&euro;300,000 - &euro;400,000</option><option value="5">&euro;400,000 - &euro;500,000</option><option value="6">Over &euro;500,000</option></select>'
	
	//change the price dropdown to sales prices
		if(pricedropdownstatus != 0){
		document.getElementById("salesrentalprices").innerHTML = pricedrop
		pricedropdownstatus = 0
		}

url = "mapframe.php?item_per_page=5&sort=1&location=Any&show=0&keywords=&price=0&price1=0&bedroom=Any&swimming=Any&type=Any&area=&searchopt=&propertyrefno=&advsearch="
document.getElementById("frame1").src=url
map.clearOverlays()
	for(i in latlong){
		if(latlong[i]['sales'] != undefined){
		renderMarker(i)
		}
	
	}



}
//============


//rentalSearch
function rentalSearch(){
		pricedrop = '<select name="price" style="width: 140px;" id="price"><option value="0" selected="">Any</option><option value="7">Under &euro;500</option><option value="8">&euro;500 - &euro;1,000</option><option value="9">Over &euro;1,000</option></select>'
		//change the price dropdown to rental prices
		
			if(pricedropdownstatus  == 0){
			document.getElementById("salesrentalprices").innerHTML = pricedrop
			pricedropdownstatus = 1
			}

url = "mapframe.php?item_per_page=5&sort=4&location=Any&show=1&keywords=&price=0&price1=0&bedroom=Any&swimming=Any&type=Any&area=&searchopt=&propertyrefno=&advsearch="
document.getElementById("frame1").src=url
map.clearOverlays()
	for(i in latlong){
		if(latlong[i]['rentals'] != undefined){
		renderMarker(i)
		}
	
	}


}

//=================
pricedropdownstatus = 0;
function altitude(){
var salerental;
if(document.getElementById("sales").checked == true){
	
	show = 0;
	salerental = 'sales';
	
	
	
}else{
		
		show = 1
		salerental = 'rentals';
		
}
cont = 1;
map.panTo(new GLatLng(38.847831, -0.149926));

city = document.getElementById("city").value
type = document.getElementById("type").value
price = document.getElementById("price").value
sorting = window.frames['frame1'].document.getElementById('sort').value
url = "mapframe.php?item_per_page=5&sort="+sorting+"&location="+city+"&keywords=&price="+price+"&price1=Any&bedroom=Any&swimming=Any&type="+type+"&area=&searchopt=&propertyrefno=&advsearch=&show="+show
var typos = 'restaurant';
var attributes = '';


//if nothing selected
if(city == "Any" && type == "Any" && price ==  "0"){


for(var i in latlong){
cont++
		if(latlong[i][salerental] != undefined){
		renderMarker(i)
		}
}
		
		
		//return false;
}

document.getElementById("frame1").src=url







//if city not selected from form
if(city == "Any"){
//if type and price selected
	if(type != "Any" && price != "0"){
	map.clearOverlays()
	
	for(i in typePrice){
	
	if(typePrice[i][type]){
		if(typePrice[i][type][price]){
		cont++
			if(latlong[i][salerental] != undefined){
			renderMarker(i)
			}

		}
	}
	
	}
	
	
		
		

	}else if(type != "Any"){
	map.clearOverlays()
		try{
	
		for(i in typePrice){
			if(typePrice[i][type]){
			cont++
				if(latlong[i][salerental] != undefined){
				renderMarker(i)
				}

			
			}
		
		}
		}catch (e){
					
					}
		
		
		
	}else if(price != "0"){
	map.clearOverlays()
		for(i in typePrice){
			for(t in typePrice[i]){
				if(typePrice[i][t][price]){
				cont++
				try{
					
					if(latlong[i][salerental] != undefined){
					renderMarker(i)
					}

				}catch (e){
					
					}
				}
			}
			
		}
	
	}

}else{
try{
/*map.panTo(new GLatLng(latlong[city]['latitude'],latlong[city]['longitude']));
map.setZoom(11);*/
map.setCenter(new GLatLng(latlong[city]['latitude'],latlong[city]['longitude']), 13);
}catch (e){
					
					}
map.clearOverlays()
var control;
control =1;

//if type and price not selected from form
if(type == "Any" && price == 0){
}else if(type > 0 && price > 0){
//if type and price selected
	if(typePrice[city][type]){	
		if(typePrice[city][type][price]){
		}else{
		control =0;
		}	
	}else{
	control =0;
	}
}else if(type > 0){
if(typePrice[city][type]){	

}else{
control =0;
}
}else if(price > 0){
//if only type selected
control =0;
	for (i in typePrice[city]){
		for(k in typePrice[city][i]){
			if(k == price){
			control =1;
			}
		}
	}
	
}

if(control == 1){


renderMarker(city)
}


}
	if(cont > 5 && cont < 15){
	map.setZoom(9);
	
	}else if(cont >=15){
	map.setZoom(7);
	}else{
	map.setZoom(11);
	}
return false
}

//function will be called from tooltip
function cityResult(city,show){
url = "mapframe.php?item_per_page=5&sort=1&location="+city+"&show="+show+"&keywords=&price=0&price1=Any&bedroom=Any&swimming=Any&type=Any&area=&searchopt=&propertyrefno=&advsearch="
document.getElementById("frame1").src=url
}


//function 
function renderMarker(i,openwindow){

try{
		var point = new GLatLng(latlong[i]['latitude'],latlong[i]['longitude']);
		
		typo ="restaurant"
		label=i
		attributesrent = ''
		
		if(latlong[i]['sales'] != undefined){
			if(latlong[i]['sales'] > 1){	
			attributessale = latlong[i]['sales'] + " properties for Sale"
			}else{
				attributessale = latlong[i]['sales'] + " property for Sale"
				}
		}
		
		if(latlong[i]['rentals'] != undefined){
			
			
		if(latlong[i]['rentals'] > 1){	
		attributesrent = '<li><a href=\'javascript:cityResult("'+i+'",1)\'>'+latlong[i]['rentals'] + " properties for Rent"
		}else{
			attributesrent = '<li><a href=\'javascript:cityResult("'+i+'",1)\'>'+latlong[i]['rentals'] + " property for Rent"
			}
		
		}
		
	
	html = '<table width="240" border="0" align="left"><tbody><tr><td><img height="53" width="90" src="images/logo.jpg"/></td><td style="color: rgb(60, 95, 126); font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 3px;"><div style="padding: 1px 0pt 0pt;"><b><a style="text-decoration: none;color: rgb(0, 0, 0);" href=\'javascript:cityResult("'+i+'")\'>'+i+'</a></b><li><a href=\'javascript:cityResult("'+i+'",0)\'>'+attributessale+'</a></li>'+attributesrent+'</div></td></tr></tbody></table>'
		var marker = createMarker2(point,label,typo,html);
		map.addOverlay(marker);
		
		if(openwindow){
		marker.openInfoWindowHtml(html);
		}
		

}catch (e){
					
					}
		
		
		

}



//========================================================================================
   // A function to create the marker and set up the event window
      function createMarker2(point,name,type,html) {
        var marker = new GMarker(point,customIcons[type]);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the side_bar
        gmarkers[i] = marker;
        // add a line to the side_bar html
        i++;
        return marker;
      }
	  
	
	
	 
    function initialize(lat,long) {
	
      if (GBrowserIsCompatible()) {
       	   if(lat != undefined)
		   {
        map.setCenter(new GLatLng(lat,long), 13);
		   }
		else
		{
		map.setCenter(new GLatLng(38.538689,-0.508917), 13);
		map.panTo(new GLatLng(38.847831, -0.149926));
		}
		
		
		if(isfromlisting){
			
		for(var i in fromlisting){	
		
		renderMarker(i)
		}
		

			}else{
		for(var i in latlong){		
		renderMarker(i)
		}
			}
		
		//Initially set Zoom level
		map.setZoom(7);
      }
    }
