var map;
var rwMap;
var miniMap;

rwMaps.controllerResults.prototype.getURL = function(){
	// this loops through the displays properties to then request for map data
	var ids="";
	var properties = jQuery('#columna').find('div').filter('[class=PropertyList]');
	jQuery.each(properties, function(i,property){
		ids += property.id.replace(/p/g, "")+",";
	});
	// create the url
	var url = rwMaps.config.dataURL;
	url = url + "propertyIDs="+ids;

	return url;
}

function rwOpenMap(){
	if(typeof(rwMap) != "object"){
		// initilize the map
		jQuery('#mapDetailPlaceHolder').show();
		var mapWidth = jQuery('#images').width();
		var mapHeight = jQuery('#images').height();

		var container = document.createElement("div");
		container.id = "rwmap";
		container.setAttribute("style", "width:"+mapWidth+"px");
		container.setAttribute("width", mapWidth+"px");
		container.setAttribute("style", "height:"+mapHeight+"px");
		container.setAttribute("height", mapHeight+"px");

		jQuery('#mapDetailPlaceHolder').append(container);
		jQuery('#rwmap').css({"width" : mapWidth, "height" : mapHeight, "overflow":"hidden"});
		jQuery('#rwmap').width(mapWidth+"px");
		jQuery('#rwmap').height(mapHeight+"px");

		// hide the images
		jQuery('#images').hide();
		jQuery('#mapDetailPlaceHolder').show();

		// init map
		rwMaps.config.baseURL = '/wp-content/plugins/rwMaps/';
		rwMaps.config.init = 'jsapi';
		rwMaps.config.mode = 'property';
		// turn off some of the map options
		//rwMaps.config.typeControl = false;
		//rwMaps.config.zoomControl = false;
		rwMaps.config.overlayControl = false;
		//rwMaps.config.disableDragging = true;
		// don't open the property
		rwMaps.config.openProperty = false;

		//rwMaps.config.delay = false;
		rwMaps.config.dataURL = "/wp-content/plugins/rwMapsRaywhiterural/data/property.php?";
		rwMap = new rwMaps();
		rwMap.init();
		
	}else{
		jQuery('#images').hide();
		jQuery('#mapDetailPlaceHolder').show();
	}
	jQuery('#mapDetail').html("HIDE MAP");
	//map.checkResize();
}
function rwCloseMap(){
	jQuery('#images').show();
	jQuery('#mapDetailPlaceHolder').hide();
	jQuery('#mapDetail').html("MAP");
}

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("#mapDetails").length){
		function loadMiniMap(){
			rwMaps.config.baseURL = '/wp-content/plugins/rwMaps/';
			miniMap = new rwMiniMap("mapDetails");
		}
		google.load("maps", "2", {"callback" : loadMiniMap});
	}

	if(jQuery('#mapPlaceholder').length && (jQuery("#pagination").length || jQuery(".pagination").length )){
		// add the map to the page after the filter
		var container = document.createElement("div");
		container.id = "rwmap";
		
		jQuery('#mapPlaceholder').html("");
		jQuery('#mapPlaceholder').append(container);

		//jQuery('#pagination').prepend(container);
		
		var mapWidth = jQuery('#mapPlaceholder').width();
		var mapHeight = jQuery('#mapPlaceholder').height();
		//mapHeight = (mapHeight < mapWidth) ? mapWidth : mapHeight;
		jQuery('#rwmap').css({'width' : mapWidth+"px", height : mapHeight+"px", "overflow":"hidden"});

		
		rwMaps.config.baseURL = '/wp-content/plugins/rwMaps/';
		rwMaps.config.init = 'jsapi';
		//rwMaps.config.mode = 'suburb';
		rwMaps.config.mode = 'results';
		//rwMaps.config.delay = false;
		rwMaps.config.searchCriteria = true;
		
		rwMaps.config.dataURL = "/wp-content/plugins/rwMapsRaywhiterural/data/property.php?";
		rwMap = new rwMaps();
		rwMap.init();

	}else if(jQuery('#mapDetailPlaceHolder').length){
		if(rwMaps.config.directions){
			rwMaps.directions.prototype.addGetDirectionsDisplay();
		}
		
		// assign click events to the map place holder
		jQuery('#mapDetail').css({'cursor' : 'pointer'});
		jQuery('#mapDetail').attr("title", "View map");
		jQuery('#mapDetail').click(function () { 
			if(jQuery('#mapDetail').html() == "MAP"){
				rwOpenMap();
			}else{
				rwCloseMap();
			}
		});
		
	}else if(jQuery('#mapDetailsDISABLED').length){
		// MINI MAP (Current disabled)
		
		// this is the property page
		// add the map to the page after the filter
		var container = document.createElement("div");
		container.id = "rwmap";
		jQuery('#mapDetails').html("");
		jQuery('#mapDetails').append(container);
		
		var mapWidth = "100%";
		var mapHeight = "180px";
		jQuery('#rwmap').css({"width" : mapWidth, "height" : mapHeight, "overflow":"hidden"});
		
		rwMaps.config.baseURL = '/wp-content/plugins/rwMaps/';
		rwMaps.config.init = 'jsapi';
		rwMaps.config.mode = 'property';
		// turn off some of the map options
		rwMaps.config.typeControl = false;
		rwMaps.config.zoomControl = false;
		rwMaps.config.overlayControl = false;
		rwMaps.config.disableDragging = true;
		// don't open the property
		rwMaps.config.openProperty = false;
		
		//rwMaps.config.delay = false;
		rwMaps.config.dataURL = "/wp-content/plugins/rwMapsRaywhiterural/data/property.php?";
		rwMap = new rwMaps();
		rwMap.init();

	}else if(!jQuery("#noResult").length && jQuery('#mapPlaceholder').length){
		// hide the map text
		//jQuery("#mapPlaceholder").hide();
		// to do currently there is a big blank spot
	}
	

});

