//This work by Jordan Brailean is licensed under
//a Creative Commons Attribution-Noncommercial 2.5 Canada License.

var obXHR = getXMLRefObject();
    
if (!obXHR)
{
    alert ("ERROR: Unable to create XML Object");
}

/******************************************************************************
this file contains a framework that we can use to make our life easier
with respect to working with the XMLHTTPRequest Object. We are going to make
the process of connecting to a back end URL as transparant as possible.
The Framework will be composed of 3 functions.
1. getXMLRefObject
2. getXHRText - for getting text back
3. getPostXHRText - for getting back off of a post
******************************************************************************/

/*************************************
 *Function getXMLRefObject
 *Purpose This routine will just proceed to get a reference (if it can)
 *          to the XMLHTTPResponse Object
 *************************************/
function getXMLRefObject()
{
    var obXHR = null
    
    if ( window.XMLHttpRequest )
    {
            obXHR = new window.XMLHttpRequest();
    }
    else
    {
            //IE stuff
            if(window.ActiveXObject)
            {
                    obXHR = new ActiveXObject("MicrosoftXMLHTTP");
            }
    }
    return obXHR;
}

/**************************************
Function get XML HTTP Request (get XHRTText)
Purpose this reoutine will be used to retrieve some text from a specified
	URL using the XMLHTTPRequest object and the Get protocol.
Params	sURL = the URL string we are trying to contact
	callBack = this is a call back function to be called when we
	actually are finished retrieving our text.
**************************************/
function getXHRText(sURL, callBack)
{
	if ( ( obXHR = getXMLRefObject() ) != null )
	{
		obXHR.open("GET", sURL);

		obXHR.onreadystatechange = function()
		{
			if (obXHR.readyState==4 && obXHR.status==200)
			{
				callBack(obXHR.responseText);
				delete obXHR;
			}
		}

		obXHR.send(null);
	}
}

/**************************************
Function get Post XML HTTP Request (get XHRTText)
Purpose this reoutine will be used to retrieve some text from a specified
	URL using the XMLHTTPRequest object and the POST protocol.
Params	sURL = the URL string we are trying to contact
	sData - the data we want to send accross
	callBack = this is a call back function to be called when we
	actually are finished retrieving our text.
**************************************/
function getPostXHRText(sURL, sData, callBack)
{
	if ( ( obXHR = getXMLRefObject() ) != null )
	{
		obXHR.open("POST", sURL);
		obXHR.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");

		obXHR.onreadystatechange = function()
		{
			if (obXHR.readyState==4 && obXHR.status==200)
			{
				callBack(obXHR.responseText);
				delete obXHR;
			}
		}
		obXHR.send(sData);
	}
}


// ---------------------------------------------------------------------------------
// --------------------------------google maps integration--------------------------
// ---------------------------------------------------------------------------------

//set up some globals because of issues with variable scope.
var map_address = "temp";
var map_available = "0";
var map_propertyid = "0";			
var map_infoHTML = "temp";
var splitdata = "temp";

function res_loadmap()
{
	//use the getXHRText to return a string into the
	//displayresults method
	initialize();
	getXHRText("/ajax_loadmap.php", initializemap);
}

var map = null;
var geocoder = null;
var baseIcon = null;

function initialize()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(50.4500, -104.6090), 12);
		geocoder = new GClientGeocoder();

		map.removeMapType(G_SATELLITE_MAP);
        	var mapControl = new GMapTypeControl();
        	map.addControl(mapControl);
        	map.addControl(new GSmallMapControl());
		map.enableScrollWheelZoom();

		//base icon - sets size and shadow etc.
		//this icon will be red (unavailable) by default
		//because most properties should be rented out
        	baseIcon = new GIcon();
        	baseIcon.image = "utilities/images/redmarker.png";
        	baseIcon.shadow = "utilities/images/markershadow.png";
        	baseIcon.iconSize = new GSize(12, 20);
        	baseIcon.shadowSize = new GSize(22, 20);
        	baseIcon.iconAnchor = new GPoint(6, 20);
        	baseIcon.infoWindowAnchor = new GPoint(5, 1);
        	// Set up our GMarkerOptions object literal
        	markerOptions = { icon:baseIcon };
      	}
}


function initializemap(mapdata)
{
	var splitdata = mapdata.split("<hr>");

	for(i = 0; i < splitdata.length - 1; i = i + 4)
	{
		map_lat = splitdata[i];
		map_lng = splitdata[i + 1];
		map_avail = splitdata[i + 2];
		map_infoHTML = splitdata[i + 3];
//alert(map_lat + ", " + map_lng + ", " + map_avail + ", " + map_infoHTML);
		var point = new GLatLng(map_lat, map_lng);

		if (map_avail == "1")
		{
			var availIcon = new GIcon(baseIcon);  
			availIcon.image = "utilities/images/greenmarker.png";  
			//use the temp icon for available places instead of baseIcon
			createMarker(point, map_infoHTML, availIcon);
		}
		else
		{
			createMarker(point, map_infoHTML, markerOptions);
		}
	}

	function createMarker(location, html, iconType)
	{
		var marker = new GMarker(location, iconType);
		GEvent.addListener(marker, "click", function()
		{
			map.openInfoWindowHtml(location, html);
		});
		map.addOverlay(marker);
	}
}

function galleryswap(filename)
{
	var image = document.getElementById("gallerypic");
	image.src = '/pictures/' + filename;
}

function opengallery(galleryid)
{
	window.open("gallery.php?g=" + galleryid + "\"", "Gallery","toolbar=0,scrollbars=1,resizable=0,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=600");
}














