var slideshow = new Spry.Widget.SlidingPanels("slideshow");
var params = Spry.Utils.getLocationParamsAsObject();
var observer = new Object;

var propertyCount = 0;
var currentPanel = 1;
var direction = 1;

var rows;

function nextPanel() {
	currentPanel = currentPanel + direction;
	if (direction == 1) {
		if (currentPanel > propertyCount) {
			direction = -1;
			currentPanel = propertyCount - 1;
		}
	}
	else {
		if (currentPanel == 0) {
			direction = 1;
			currentPanel = 2
		}
	}
	slideshow.showPanel(currentPanel - 1);
	document.getElementById("titleband").innerHTML = rows[currentPanel - 1].address + ", " + rows[currentPanel - 1].city;
}

observer.onPostLoad = function() {
	if (params) {
		if (params.id != undefined) {
			dsProperties.setXPath("properties/property[@id = '" + params.id + "']");
		}
		if (params.msg != undefined) {
			var message = document.getElementById('message');
			message.innerHTML = decodeURI(params.msg);
			message.className = "box";
		}
	}

	rows = dsProperties.getData();
	propertyCount = rows.length;
	if (propertyCount > 0) {
		document.getElementById("titleband").innerHTML = rows[currentPanel - 1].address + ", " + rows[currentPanel - 1].city;
		if (propertyCount > 1) {
			timer = setInterval("nextPanel()", 5000);
		}
	}
	else {
		if (params && params.id != undefined) {
			window.location = "index.html?msg=" + encodeURI("The property you're looking for could not be found.");
		}
	}
}

dsProperties.addObserver(observer);