var map;
var rwMap;

jQuery(function() {
// TO DO
// this code needs to mimic the search data forms,
// as currently there are 3 different ones and the map needs to know what to copy
// and send to its data service
	if(jQuery('#map').length && jQuery("#pagination").length){
		
		// add the map to the page after the filter
		var container = document.createElement("div");
		container.id = "rwmap";
		jQuery('#map').html("");
		jQuery('#map').append(container);
		
		var mapWidth = jQuery('#map').width();
		var mapHeight = jQuery('#post-3022').height();
		mapHeight = (mapHeight < mapWidth) ? mapWidth : mapHeight;
		jQuery('#rwmap').css({'width' : mapWidth+"px", height : mapHeight+"px"});
		
		rwMaps.config.baseURL = '/wp-content/plugins/rwMaps/';
		rwMaps.config.init = 'jsapi';
		//rwMaps.config.mode = 'suburb';
		rwMaps.config.mode = 'results';

		// display the search filters on trains etc..
		//rwMaps.config.loadLocalSearch =true;
		// displays the search filters like state => region etc.
		//rwMaps.config.loadFilter=true;
		// display the ray white colours / images ontop of the map
		//rwMaps.config.loadImageOverlay = true;

		jQuery(jQuery("#filter_COM").serializeArray()).each(function(){
			//console.log(this.name + ":" + this.value);
			var name = this.name;
			//name = name.replace(/BUS/g, "");
			rwMaps.config.searchCriteria += name + "=" + this.value + "&";
		});
		//rwMaps.config.searchCriteria += "&currPage=" + parseInt(jQuery('[name="page_number"]').val());

		//rwMaps.config.delay = false;
		rwMaps.config.dataURL = "/wp-content/plugins/rwMapsRaywhitecommercial/data/property.php?";
		rwMap = new rwMaps();
		rwMap.init();
	}else if(jQuery('#detailPage').length && jQuery('#map').length && jQuery('#noResult').length != 1){
		// this is the property page
		
		// add the map to the page after the filter
		var container = document.createElement("div");
		container.id = "rwmap";
		jQuery('#map').html("");
		jQuery('#map').append(container);
		
		var mapWidth = "100%";
		var mapHeight = "300px";
		jQuery('#rwmap').css({'width' : mapWidth, height : mapHeight});
		
		rwMaps.config.baseURL = '/wp-content/plugins/rwMaps/';
		rwMaps.config.init = 'jsapi';
		rwMaps.config.mode = 'property';

		//rwMaps.config.delay = false;
		rwMaps.config.dataURL = "/wp-content/plugins/rwMapsRaywhitecommercial/data/property.php?";
		rwMap = new rwMaps();
		rwMap.init();
	}else if(!jQuery("#pagination").length){
		// hide the map text
		jQuery("#containerfour").hide();
		jQuery("#map").hide();
	}
});
