var gProperties = new Array();				// Array to store list of property hashes
var gcurrentIndex = 0;						// Track what property we're currently on.
var gResponseID = 0;						// result cache from MLS db to form proper links.
var responseLink = "";						// Uses client type and MLS name to help form link
var gInterval = "";							// Reference to Interval

/* Hash (associative array) class */

function shuffle(o) {
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
}

function Hash()
{
		this.length = 0;
		this.items = new Array();
		for (var i = 0; i < arguments.length; i += 2) {
				if (typeof(arguments[i + 1]) != 'undefined') {
						this.items[arguments[i]] = arguments[i + 1];
						this.length++;
				}
		}

		this.removeItem = function(in_key)
		{
				var tmp_value;
				if (typeof(this.items[in_key]) != 'undefined') {
						this.length--;
						var tmp_value = this.items[in_key];
						delete this.items[in_key];
				}

				return tmp_value;
		}

		this.getItem = function(in_key) {
				return this.items[in_key];
		}
		

		this.setItem = function(in_key, in_value)
		{
				if (typeof(in_value) != 'undefined') {
						if (typeof(this.items[in_key]) == 'undefined') {
								this.length++;
						}

						this.items[in_key] = in_value;
				}

				return in_value;
		}

		this.hasItem = function(in_key)
		{
				return typeof(this.items[in_key]) != 'undefined';
		}
}


function featuredPlay() {
	/* Fire off XHR and parse returned XML into useable JS objects.  Call renderDisplay() to output data on interval. */
	var client_mls = document.getElementById("mls_name").value;
	var client_id = document.getElementById("client_id").value;
	var client_type = document.getElementById("client_type").value;
	
	/* Must be hooked with apache rewrite rule in order to avoid XSS security */
	var requestLine = "/xml/" + client_mls + "/" + client_type + "/" + client_id;
	
	var request = GXmlHttp.create();
	request.open("GET", "/getURL.php", true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			/* Get basic variables out of root of XML document */
			var foo = request.responseXML;
			var oRoot = foo.documentElement;
			var properties = oRoot.getElementsByTagName("property");
			gResponseID = oRoot.getAttribute("id");
			responseLink = oRoot.getAttribute("link");
		
			/* Parse through each property child in the XML.  Resulting dataset for all with be an array of hashes */
			for (var i = 0; i < properties.length; i++ ) {
				var prop = new Hash();
				var featured_id = properties[i].getAttribute("id");
				prop.setItem("id",featured_id);
				currentChild = properties[i].firstChild;
				do {
					if (currentChild.childNodes.length == 1) {
						/* Add quoting to avoid any character issues */
						prop.setItem(currentChild.childNodes[0].parentNode.nodeName, '"' + currentChild.childNodes[0].nodeValue + '"' );
					}
					
				} while (currentChild = currentChild.nextSibling);
				/* Push newly formed hash into array */
				gProperties.push(prop);
			}
			/* Fire off event once to start */
			renderDisplay('start','auto');
			/* Now rotate on given interval */
			var timer = document.getElementById("playTimer").value;
			gInterval = setInterval("renderDisplay('next','auto')",timer *1000);
		}
		
	};
	request.send(null);
}

/* */
function renderDisplay(action,userAction) {
	/* Use generated hash arrays to output data.  This is what should be customized on a per-client basis, if necessary.  
	   e.g. DIVs are named differently or the data set is too large to iterate through each hash key/value pair. */
	
	/* Helpers for automatic and user-launched behaviors.  Manipulates the current Index of the gProperties[] array */
	switch(action) {
		case "start":   gcurrentIndex = 0;break;
		case "next":    if (gcurrentIndex == gProperties.length-1) { gcurrentIndex = 0;  } else { gcurrentIndex++; }; break;
		case "prev":    if (gcurrentIndex == 0) { gcurrentIndex = gProperties.length-1; } else { gcurrentIndex--; }; break;
	}
	
	var property = gProperties[gcurrentIndex];
	var property_id = 0;
	/* Comma-separated list of fields we're using. */
	var includeList = document.getElementById("featuredOnly").value;

	/* Use this list to start writing out fields.  Fields must be named as featured_<xml node name> */
	for (var i in property.items) {
		if (includeList.indexOf(i) != -1) {
			switch(i) {			// do some special things, depending on the field.  Also a good place for customization.
				case "id": property_id = property.items[i]; break;
				case "picture": 	document.getElementById('featured_picture').onclick = function () {propFeaturesPage("http://mls.neteconomist.com" + responseLink + "/" + property_id + "/" + gResponseID + "/0")};
				               		document.getElementById('featured_picture').style.backgroundImage = "url(" + property.items[i] + ")";
									document.getElementById('featured_picture').style.cursor = "pointer";
									break;
				default: eval("document.getElementById('featured_" + i + "').innerHTML = " + property.items[i]);
			}
		}
		
	}
	
	/* If the user starts paging through the properties on their own, stop the timer */
	if (userAction == "clicked") {
		clearInterval(gInterval);
	}
	/* Keep interval going otherwise by returning false */
	return false;
}

function propFeaturesPage(uri) {
	/* Enables links for DIVs that have onClick behaviors */
	document.location.href = uri;
}
