function makeHttpRequest(url, callback_function, return_xml)
{
  var http_request, response, i;

  var activex_ids = [
    'MSXML2.XMLHTTP.3.0',
    'MSXML2.XMLHTTP',
    'Microsoft.XMLHTTP'
  ];

  if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE6 and older
    for (i = 0; i < activex_ids.length; i++) {
      try {
        http_request = new ActiveXObject(activex_ids[i]);
      } catch (e) {}
    }
  }

  if (!http_request) {
    alert('Unfortunatelly you browser doesn\'t support this feature.');
    return false;
  }

  http_request.onreadystatechange = function() {
    if (http_request.readyState !== 4) {
        // not ready yet
        return;
    }
    if (http_request.status !== 200) {
      // ready, but not OK
      alert('There was a problem with the request.(Code: ' + http_request.status + ')');
      return;
    }
    if (return_xml) {
      response = http_request.responseXML;
    } else {
      response = http_request.responseText;
    }
    // invoke the callback
    callback_function(response);
  };

  http_request.open('GET', url, true);
  http_request.send(null);
}

var propertyPopup;
var propertyPopupContents;
var propertyPopupContentsNext;
var propertyPopupContentsPrevious;
var position;
var propertyPopupWidth;
var currentProperty;
var scrollWidth = 644;
var leftOffset = 77;
var topOffset = 30;
var height = '393px';
var nextMLS
var previousMLS

function showProperties(text)
{
	propertyPopup = document.getElementById("propertyPopup");
	currentProperty = document.getElementById("currentProperty");
	
	if(propertyPopupContents == null)
	{
		propertyPopupContents = document.getElementById("propertyPopupContents");
		propertyPopupContents.style.width = '810px';
	}
	currentProperty.innerHTML = text;
	propertyPopup.style.display = 'block';

	var currentMLS = text.split('<div class="mls">')[1].split('</div>')[0];

	makeHttpRequest('/includes/fetch_property.php?id=' + currentMLS + '&direction=next', prepareNextProperty, false);
	makeHttpRequest('/includes/fetch_property.php?id=' + currentMLS + '&direction=previous', preparePreviousProperty, false);
	
	// agents contain displays over the top so we adjust the z index to move it down
	//var agentsContainer = document.getElementById("agentsContainer");
	//if(agentsContainer)
	//	agentsContainer.style.zIndex = -1;
}

function prepareNextProperty(text)
{
	propertyPopupWidth = propertyPopupContents.clientWidth;
	
	propertyPopupContentsNext = document.createElement('div');
	propertyPopupContentsNext.innerHTML = text;
	propertyPopupContentsNext.style.left = '810px';
	propertyPopupContentsNext.style.top = topOffset + 'px';
	propertyPopupContentsNext.style.width = scrollWidth + 'px';
	propertyPopupContentsNext.style.position = 'absolute';
	propertyPopupContentsNext.style.height = height;
	currentProperty.parentNode.appendChild(propertyPopupContentsNext);
	nextMLS = text.split('<div class="mls">')[1].split('</div>')[0];
}

function preparePreviousProperty(text)
{
	propertyPopupWidth = propertyPopupContents.clientWidth;
	
	propertyPopupContentsPrevious = document.createElement('div');
	propertyPopupContentsPrevious.innerHTML = text;
	propertyPopupContentsPrevious.style.left = '-' + scrollWidth + 'px';
	propertyPopupContentsPrevious.style.top = topOffset + 'px';
	propertyPopupContentsPrevious.style.width = scrollWidth + 'px';
	propertyPopupContentsPrevious.style.position = 'absolute';
	propertyPopupContentsPrevious.style.height = height;
	currentProperty.parentNode.appendChild(propertyPopupContentsPrevious);
	previousMLS = text.split('<div class="mls">')[1].split('</div>')[0];
}

function scrollLeft()
{
	position-=50;
	if(position + propertyPopupWidth > leftOffset)
	{
		currentProperty.style.left = position + 'px';
		propertyPopupContentsNext.style.left = (position + propertyPopupWidth) + 'px';
		setTimeout("scrollLeft()",30);
	}
	else
	{
		currentProperty.style.left = '-' + scrollWidth + 'px';;
		propertyPopupContentsNext.style.left =  leftOffset + 'px';
		propertyPopupContentsNext.style.top = topOffset + 'px';
		propertyPopupContentsPrevious.parentNode.removeChild(propertyPopupContentsPrevious);
		propertyPopupContentsPrevious = currentProperty;
		currentProperty = propertyPopupContentsNext;
		currentProperty.id = "currentProperty";
		
		makeHttpRequest('/includes/fetch_property.php?id=' + nextMLS + '&direction=next', prepareNextProperty, false);
	}
}

function scrollRight()
{
	position+=50;
	if( propertyPopupWidth - position > -leftOffset)
	{
		currentProperty.style.left = position + 'px';
		propertyPopupContentsPrevious.style.left = (position - propertyPopupWidth) + 'px';
		setTimeout("scrollRight()",30);
	}
	else
	{
		currentProperty.style.left = '810px';
		propertyPopupContentsPrevious.style.left =  leftOffset + 'px';
		propertyPopupContentsPrevious.style.top = topOffset + 'px';
		propertyPopupContentsNext.parentNode.removeChild(propertyPopupContentsNext);
		propertyPopupContentsNext = currentProperty;
		currentProperty = propertyPopupContentsPrevious;
		currentProperty.id = "currentProperty";

		makeHttpRequest('/includes/fetch_property.php?id=' + previousMLS + '&direction=previous', preparePreviousProperty, false);
	}
}


function hidePropertyPopup()
{
	var propertyPopup = document.getElementById("propertyPopup");
	propertyPopup.style.display = 'none';
	
	propertyPopupContentsNext.parentNode.removeChild(propertyPopupContentsNext);
	propertyPopupContentsPrevious.parentNode.removeChild(propertyPopupContentsPrevious);
	//var agentsContainer = document.getElementById("agentsContainer");
	//if(agentsContainer)
	//	agentsContainer.style.zIndex = 0;
}
function showProperty(mls)
{
	makeHttpRequest('/includes/fetch_property.php?id=' + mls, showProperties, false);
}
function showPropertyFromLeft(mls)
{
	position = 0;
	scrollLeft();
}
function showPropertyFromRight(mls)
{
	position = leftOffset;
	scrollRight();
}
function gotoLakeProperties()
{
	var lakeSelector = document.getElementById("viewLake");
	var lake = lakeSelector.options[lakeSelector.selectedIndex].text.replace(/ /g, '_').toLowerCase();
	if(lake != 'select_a_lake')
		window.location = '/lakeshore/' + lake + '/listings/';
}
function gotoCityProperties()
{
	var citySelector = document.getElementById("viewCity");
	var city = citySelector.options[citySelector.selectedIndex].text.replace(/ /g, '_').toLowerCase();
	if(city != 'select_a_city')
		window.location = '/city/' + city + '/listings/';
}
