/*
	Website scripts
	(c) Kerve Creative
	Map Scripts
*/

/* Initialise map settings, then load all functions into initialise */
function initialize(search_filter_data,rand,map_centre_data) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("mapholder22"));
		map.addControl(new YSliderControl());
		
		importData(map,search_filter_data,rand,map_centre_data);
	}
}

/* Import main properties data as XML. Use rand variable so IE browsers don't cache the xml as this will bomb the system */
function importData(map,search_filter_data,rand,map_centre_data) {
	GDownloadUrl("/includes/propertyFunctions.php?section=propertyMarkers"+search_filter_data+"&rand="+rand, function(data) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
				
		for (var i = 0; i < markers.length; i++) {
			var name = markers[i].getAttribute("name");
			var address = markers[i].getAttribute("address");
			var type = markers[i].getAttribute("type");
			var id = markers[i].getAttribute("id");
			var ref = markers[i].getAttribute("ref");
			var image = markers[i].getAttribute("image");
			var price = markers[i].getAttribute("price");
			var imagesize = markers[i].getAttribute("imagesize");
			var external_link = markers[i].getAttribute("external_link");
			var availability = markers[i].getAttribute("availability");
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
			var marker = createMarker(point,name,address,type,id,ref,image,availability,price,imagesize,external_link);
			map.addOverlay(marker);
			if(type != "property") {
				marker.hide();
			}
		}
	});
	
	/* centre map view */
	map.setCenter(new GLatLng(map_centre_data[0], map_centre_data[1]), map_centre_data[2]);
}
		
/*Create property icon entries on the map via imported xml*/
function createMarker(point,name,address,type,id,ref,image,availability,price,imagesize,external_link) {
	//Incoming Variables - data is produced via the XML
	//POINT = map marker lon/lat co-ordinates
	//NAME = Property/filter(sponsor) title/name eg. 2 Bed Property
	//ADDRESS = Property/filter(sponsor) address eg. Livingston Road, Olfield Park
	//TYPE = marker type (property/club/restaurant etc)
	//ID = Property/filter(sponsor) database ID
	//REF = Property/filter(sponsor) Best Dig's internal reference number (used in propert details url)
	//IMAGE = Property/filter(sponsor) image, this is built in the XML
	//AVAILABILITY: created in xml, coming soon, leased or available (inc. date if so) eg. Available beginning October 2008
	//EXTERNAL LINK: used on filter/sponsors, a link to the sponsors website.
	
	//Create PopUp Box HTML Wrapper
	if(type == 'property') {	
		var html_popup = '';
		html_popup += '<div id="tooltipoverlay" class="propertyDetails">&nbsp;</div>';
		html_popup += '<div id="tooltipposition">';
		if(image != '') {
			var sizeSplit = imagesize.split(":");
			html_popup += '	<div class="detailthumb">';
			html_popup += '		<a href="/properties/detail/' + ref + '/index.html" title="Click to View"><img src="' + image + '" alt="' + name + '" width="' + sizeSplit[0] + '" height="' + sizeSplit[1] + '" /></a>';
			html_popup += '	</div>';
		}
		html_popup += '	<div class="mapdetailscopy">';
		html_popup += '		<h2>&pound;' + price + '</h2>';
		html_popup += '		<p>' + name + '</p>';
		html_popup += '		<p><a class="orangelink" href="/properties/detail/' + ref + '/index.html" title="Click to View">' + address + '</a></p>';
		html_popup += '		<p><br /><strong>' + availability + '</strong></p>';
		html_popup += '	</div>';
		html_popup += '	<div class="detailslink2">';
		html_popup += '		<a href="/properties/detail/' + ref + '/index.html" title="Click to View"><img class="morefix" src="/images/more_info_details.png" alt="Click to View" /></a>';
		html_popup += '	</div>';
		html_popup += '</div>';
	} else {
		var html_popup = '';
		html_popup += '<div id="tooltipoverlay" class="poiDetails">&nbsp;</div>';
		html_popup += '<div id="tooltipposition">';
		if(image != '') {
			html_popup += '	<div class="detailthumb">';
			html_popup += '		<img src="' + image + '" alt="' + name + '" />';
			html_popup += '	</div>';
		}
		html_popup += '	<div class="mapdetailscopy">';
		html_popup += '		<p><strong>' + name + '</strong></p>';
		html_popup += '		<p>' + address + '</p>';
		html_popup += '	</div>';
		
		if(external_link != '') {
			html_popup += '	<div class="detailslink2">';
			html_popup += '		<a href="' + external_link + '" title="Click to View"><img class="morefix" src="/images/more_info_details.png" alt="Click to View" /></a>';
			html_popup += '	</div>';
		}
		
		html_popup += '</div>';
	}
	//#############################

	//Create Rollover Box HTML Wrapper
	var html_rollover = '';
	html_rollover += '<div class="map_rollover">';
	html_rollover += '	<p>' + name + '</p>';
	html_rollover += '</div>';
	
	//Create marker
	marker = new PdMarker(point, customIcons[type]);
	markerGroups[type].push(marker);
	marker.setOpacity(100);
	marker.setMouseOutEnabled(true);
	marker.allowLeftTooltips(true);
	
	//Run some extra variables on the PDMarker API. Built by Ben. Order of variables:
	//(detail_window_left (if no external div is used),detail_window_top,detail_window_classname,tooltip_window_classname,detail_window_close_image,detail_window_close_image_width,detail_window_close_image_height,detail_window_close_classname,div_for_details_content)
	marker.setToolTipVariables(40,10,'markerDetail','markerTooltip','http://www.google.com/mapfiles/close.gif','14','13','markerDetailClose','tooltipoverlay_wrapper');
	
	//Create marker rollover and detail window
	marker.setTooltip(html_rollover);
	marker.setDetailWinHTML(html_popup);
	
	return marker;
}
/*###########################*/

/*Toggle filter options on/off*/
function toggleGroup(type) {
	for (var i = 0; i < markerGroups[type].length; i++) {
		var filtermarker = markerGroups[type][i];
		if (filtermarker.isHidden()) {
			filtermarker.show();
		} else {
			filtermarker.hide();
		}
	}
}
/*###########################*/


/* Marker creation script for Contact Map */
function createContactMarker(point, name, address) {
    var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		var html = '';
		
		html += '<h2>' + name + '</h2>';
		html += '<p>' + address + '</p>';
		
		marker.openInfoWindowHtml(html);
	});

    return marker;
}