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);
}

function showImage(text)
{
	var imageContainer = document.getElementById("imageContainer");
	imageContainer.innerHTML = text;
}

function loadListingImage(id, image)
{
	makeHttpRequest('/includes/get_property_image.php?id='.concat(id, '&image=', image), showImage, false);
}

function displayLargeImage(id, image)
{
	makeHttpRequest('/includes/property_image_viewer.php?id='.concat(id, '&image=', image), showLargeImagesPopup, false);
}

var thumbStrip;
var thumbScroller;
//var position;
var thumbStripWidth;
var selectedImage;
var thumbScrollerRow;
var offsets;

function showLargeImagesPopup(text)
{
	var fullSizeImageContainer = document.getElementById("fullSizeImageContainer");
	//var propertyPopupContents = document.getElementById("propertyPopupContents");
	fullSizeImageContainer.innerHTML = text;
	fullSizeImageContainer.style.display = 'block';
	if(window.scrollY)
		fullSizeImageContainer.style.top = (window.scrollY - 100) + 'px';
	else
		fullSizeImageContainer.style.top = '-100px';
	fullSizeImageContainer.style.left = '90px';
	// 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;
	thumbStrip = document.getElementById("thumbStrip");
	thumbScroller = document.getElementById("thumbScroller");
	thumbStripWidth = thumbStrip.clientWidth;
	thumbScrollerRow = document.getElementById('thumbScrollerRow');
	//position = 0;
	for(var i=0; i<thumbScrollerRow.cells.length; i++)
	{
		if(thumbScrollerRow.cells[i].className == 'selected')
			selectedImage = i;
	}
	setTimeout("adjustOffset()",100);
	setTimeout("adjustOffset()",300);
	setTimeout("adjustOffset()",500);
}

function hideLargeImagesPopup()
{
	var fullSizeImageContainer = document.getElementById("fullSizeImageContainer");
	fullSizeImageContainer.style.display = 'none';
	//var agentsContainer = document.getElementById("agentsContainer");
	//if(agentsContainer)
	//	agentsContainer.style.zIndex = 0;
}

function switchImage(id, image)
{
	selectedImage = image;
	makeHttpRequest('/includes/property_image_viewer.php?id='.concat(id, '&single_image=', image), switchLargeImage, false);
	
	//document.getElementById("image_" + image).className = 'selected';
	
}

function switchLargeImage(text)
{
	var mainImage = document.getElementById("mainImage");
	mainImage.innerHTML = text;
	for(var i=0; i<thumbScrollerRow.cells.length; i++)
	{
		if(i == selectedImage)
			thumbScrollerRow.cells[i].className = 'selected';
		else
			thumbScrollerRow.cells[i].className = '';
	}
	adjustOffset();
}
function scrollStripRight(id)
{
	selectedImage++;
	if(selectedImage == thumbScrollerRow.cells.length)
		selectedImage = 0;
	switchImage(id, selectedImage);
	/*
	position -= 100;
	if((thumbScroller.clientWidth + position) < thumbStrip.clientWidth)
	{
		position = thumbStrip.clientWidth - thumbScroller.clientWidth;
	}
	thumbScroller.style.left = position + 'px';*/
}
function scrollStripLeft(id)
{
	selectedImage--;
	if(selectedImage < 0)
		selectedImage = thumbScrollerRow.cells.length - 1;
	switchImage(id, selectedImage);
	/*position += 100;
	if(position > 0)
		position = 0;
	thumbScroller.style.left = position + 'px';*/
}
function adjustOffset()
{
	//alert(selectedImage);
	var offset = thumbStripWidth / 2;
	for(var i=0; i<selectedImage; i++)
	{
		var t = thumbScrollerRow.cells[i].clientWidth;
		if( thumbScrollerRow.cells[i].clientWidth > 5)
			offset -= (thumbScrollerRow.cells[i].clientWidth + 10 );
		else
			offset -= 185;
	}
	offset -= (thumbScrollerRow.cells[i].clientWidth / 2 + 5 );
	thumbScroller.style.left = offset + 'px';
}