//Estate Agent IDs
// Venmore = 930
// William Gleave = 1147
// BFL = 223
var eaid = 930;

var map;
var Icon1 = [];
var Icon2 = [];
var Icon3 = [];
var Icon4 = [];
var icons = [];
var marker = [];
var newMarker = [];
var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;
var icontype;

function homesMap(){
//	alert('set map');
    if (GBrowserIsCompatible()) {

	function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }
	  
      map = new GMap2(document.getElementById("googleMap"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(aLat, aLong), zoom);
//	alert('map set');
loadMarkers();
} else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}


function loadMarkers(RentalPeriod, Town, GlobalPropType, Bedrooms, MinPrice, MaxPrice, GType){
	
	map.clearOverlays();
	
	

      // Create some custom icons

      // This icon uses the same shape as the default Google marker
      // So we can use its details for everything except the image
	  function makeIcons(i){
		  Icon1[i] = new GIcon();
		  Icon1[i].image = "images/marker_house.gif";
		  Icon1[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
		  Icon1[i].iconSize = new GSize(34, 46);
		  Icon1[i].shadowSize = new GSize(37, 34);
		  Icon1[i].iconAnchor = new GPoint(9, 34);
		  Icon1[i].infoWindowAnchor = new GPoint(9, 2);
		  Icon1[i].infoShadowAnchor = new GPoint(18, 25);
		  Icon1[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
		  Icon1[i].printImage = "images/marker_house.gif";
		  Icon1[i].mozPrintImage = "images/marker_house.gif";
		  Icon1[i].title = "Property Details";
	
		  Icon2[i] = new GIcon();
		  Icon2[i].image = "images/marker_plot.gif";
		  Icon2[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
		  Icon2[i].iconSize = new GSize(34, 46);
		  Icon2[i].shadowSize = new GSize(37, 34);
		  Icon2[i].iconAnchor = new GPoint(9, 34);
		  Icon2[i].infoWindowAnchor = new GPoint(9, 2);
		  Icon2[i].infoShadowAnchor = new GPoint(18, 25);
		  Icon2[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
		  Icon2[i].printImage = "images/marker_plot.gif";
		  Icon2[i].mozPrintImage = "images/marker_plot.gif";
		  Icon2[i].title = "Property Details";
		  
		  Icon3[i] = new GIcon();
		  Icon3[i].image = "images/marker_flat.gif";
		  Icon3[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
		  Icon3[i].iconSize = new GSize(34, 46);
		  Icon3[i].shadowSize = new GSize(37, 34);
		  Icon3[i].iconAnchor = new GPoint(9, 34);
		  Icon3[i].infoWindowAnchor = new GPoint(9, 2);
		  Icon3[i].infoShadowAnchor = new GPoint(18, 25);
		  Icon3[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
		  Icon3[i].printImage = "images/marker_flat.gif";
		  Icon3[i].mozPrintImage = "images/marker_flat.gif";
		  Icon3[i].title = "Property Details";
		  
		  Icon4[i] = new GIcon();
		  Icon4[i].image = "images/marker_business.gif";
		  Icon4[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
		  Icon4[i].iconSize = new GSize(34, 46);
		  Icon4[i].shadowSize = new GSize(37, 34);
		  Icon4[i].iconAnchor = new GPoint(9, 34);
		  Icon4[i].infoWindowAnchor = new GPoint(9, 2);
		  Icon4[i].infoShadowAnchor = new GPoint(18, 25);
		  Icon4[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
		  Icon4[i].printImage = "images/marker_business.gif";
		  Icon4[i].mozPrintImage = "images/marker_business.gif";
		  Icon4[i].title = "Property Details";
		
		// An array of GIcons, to make the selection easier 
		//icons[0][i] = Icon1[i];
	  	//icons[1][i] = Icon2[i];
		//alert('making Icon'+i);
	  }

      // the icon information is passed to this function
      function createMarker(point,name,html,icontype,i) {
		//alert('creating marker'+i);
       // marker = new GMarker(point,icons[icontype][i]);
	   marker[i] = new GMarker(point,icontype);
//		alert('marker');
        GEvent.addListener(marker[i], "click", function() {
          marker[i].openInfoWindowHtml(html);
        });
//		alert('click added');
        // save the info we need to use later for the side_bar
        gmarkers[i] = marker;
		gmarkers[i].title = "Property Details";
        htmls[i] = html;
        // add a line to the side_bar html
        side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
        
        return marker[i];
      }
	  
//	alert('load markers');
	// get variables for php xml

	if(Town!=""){
		var Town = document.getElementById('Town').value;
	}
//        Town = Town.toLowerCase();
//        if(Town == "enter+postcode+or+area" || Town == "enter postcode or area" || Town == "enter%20postcode%20or%20area"){
//            Town = "";
//        }
	if(refineTypeBuy!=""){
		var refineTypeBuy = document.getElementById('refineTypeBuy').checked;
	}
	if(GlobalPropType!=""){
		var GlobalPropType = document.getElementById('GlobalPropType').value;
	}
	if(Bedrooms!=""){
		var Bedrooms = document.getElementById('Bedrooms').value;
	}
	if(MinPrice!=""){
		var MinPrice = document.getElementById('MinPrice').value;
	}
	if(MaxPrice!=""){
		var MaxPrice = document.getElementById('MaxPrice').value;
	}
	
//	alert(Town);
//	alert(refineTypeBuy);
//	alert(GlobalPropType);
//	alert(Bedrooms);
//	alert(MinPrice);
//	alert(MaxPrice);
	
	
	var stringArgs = "";
	
	if(refineTypeBuy == true){
		var RentalPeriod = '1';
	}else{
		var RentalPeriod = '4';
	}
	
//	alert('set vars 1');
	stringArgs = "";
	
	if(Town){stringArgs = addArg(stringArgs, 'Town='+Town)}
	if(GlobalPropType || GlobalPropType=='0'){stringArgs = addArg(stringArgs, 'GlobalPropType='+GlobalPropType)}
	if(Bedrooms){stringArgs = addArg(stringArgs, 'Bedrooms='+Bedrooms)}
	if(MinPrice){stringArgs = addArg(stringArgs, 'MinPrice='+MinPrice)}
	if(MaxPrice){stringArgs = addArg(stringArgs, 'MaxPrice='+MaxPrice)}
	if(RentalPeriod){stringArgs = addArg(stringArgs, 'RentalPeriod='+RentalPeriod)}
	
	
	//alert(stringArgs);
	//prompt("","phpScripts/xml_property_map.php?args"+stringArgs);

        $.ajax({
            type: "GET",
            url: "phpScripts/xml_property_map.php",
            dataType: 'xml',
            cache: true,
            data: stringArgs,
            //data: {"Town" : "'"+Town+"'", "GlobalPropType" : "'"+GlobalPropType+"'", "Bedrooms" : "'"+Bedrooms+"'", "MinPrice" : "'"+MinPrice+"'", "MaxPrice" : "'"+MaxPrice+"'", "RentalPeriod" : "'"+RentalPeriod+"'"},
                success: function(xmlDoc){
                    var i = 0;
                    html        = new Array();
                    address     = new Array();
                    $(xmlDoc).find('marker').each(function(){
                        
                        var lat         = parseFloat($(this).attr("lat"));
                        var lng         = parseFloat($(this).attr("lng"));
                        var point       = new GLatLng(lat,lng);
			
                        var imageNo     = $(this).attr("imgNo");
			var groupName   = $(this).attr("groupName");
			var sa1         = $(this).attr("sa1");
			var sa2         = $(this).attr("sa2");
			var twn         = $(this).attr("twn");
			var pc          = $(this).attr("pc");
                        var price       = $(this).attr("price");
			var beds        = $(this).attr("beds");
			var propid      = $(this).attr("propid");
			var ebid        = $(this).attr("ebid");
			
                        var anAddress = "";
			if(sa1){anAddress += sa1+'<br />';}
			if(sa2){anAddress += sa2+'<br />';}
			if(twn){anAddress += twn+'<br />';}

                        address.push(anAddress)
			
			var aHTML = '<div style="width:200px; height:240px; overflow:auto; background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000; padding:5px;" >';
			if(imageNo==""){}else{
				aHTML += '<img src="http://data.dezrez.com/PictureResizer.ASP?PropertyID='+propid+'&PhotoID='+imageNo+'&AgentID='+eaid+'&BranchID='+ebid+'&width=175&rotation=0" width="175" style="margin:0 12px 5px 12px;; float:left;">';
			}
			aHTML += '<div style="width:200px; margin:5px 0 5px 0; float:left;">'+address[i]+'';
			aHTML += '<strong style="color:#FF0000;">'+beds+' Bedroom '+groupName+'<br />';
			//aHTML += ''+intro+'<br />';
			aHTML += '&pound;'+price+'</strong><br />';
			aHTML += '<br /><a href="property_details.php?id='+propid+'">More details &raquo;</a></div>';
			aHTML += '</div>';
                        html.push(aHTML);

                        var label = beds+' '+groupName;

                        // create the marker
			makeIcons(i);
			if(groupName=='House' || groupName=='Detached House' ||  groupName=='Bungalow' ||  groupName=='Terraced' ||  groupName=='Maisonette' ||  groupName=='Semi-Detached House' ||  groupName=='Studio'){
                            icontype = Icon1[i];
			}if(groupName=='Building Plot'){
				icontype = Icon2[i];
			}if(groupName=='Apartment' || groupName=='Flat Bedsit'){
				icontype = Icon3[i];
			}if(groupName=='Commercial' || groupName=='Business'){
				icontype = Icon4[i];
			}
//			alert('set icon'+i);
                        newMarker[i] = createMarker(point,label,html[i],icontype,i);
                        map.addOverlay(newMarker[i]);
                        i++;
                    });
                }
        });

      
    
}

function addArg(argumentString, args){
    if(argumentString != ""){
        argumentString += "&";
    }
    argumentString += args;
    return argumentString;
}


