var oXMLHttpFeature;

function GetFeatureXMLHttpObject()
{
     oXMLHttpFeature = null;

      try
      {
            oXMLHttpFeature = new ActiveXObject("MSXML2.XMLHTTP");
      }
      catch (E)
      {
            try
            {
                  oXMLHttpFeature = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E)
            {
              oXMLHttpFeature = null;
            }
    }

      if ((oXMLHttpFeature == null) && (typeof(XMLHttpRequest) != 'undefined'))
      {
            oXMLHttpFeature = new XMLHttpRequest();
      }

      return oXMLHttpFeature;
}


var featObj = new Object;

function loadFeatures()
{

	if(oXMLHttpFeature)
	{
		oXMLHttpFeature.abort();
	}

	oXMLHttpFeature = GetFeatureXMLHttpObject();

	var url = "php/features.php";

	oXMLHttpFeature.open("POST", url, true);

	oXMLHttpFeature.onreadystatechange = function()
	    {
		  if (oXMLHttpFeature.readyState == 4)
		  {
			var JSONObject = new Object;

			var response = oXMLHttpFeature.responseText;
			
			JSONObject = JSON.parse(response);

			featObj = JSONObject.Features;

			loadFeature(0);
		  }
	    }

	oXMLHttpFeature.send(null);

}

function printImage(imageUrl, width, height, max) {

	if(width > height)
	{
		imageWidth = parseInt(width * (max/height));
		imageHeight = parseInt(height * (max/height));

	} else {

		imageWidth = parseInt(width * (max/width));
		imageHeight = parseInt(height * (max/width));
	}
	
	leftDif = parseInt((max - imageWidth) / 2);
	topDif = parseInt((max - imageHeight) / 2);

	image = "<img src=\""+imageUrl+"\" width=\""+imageWidth+"\" height=\""+imageHeight+"\" border=\"0\" align=\"top\" style=\"margin: 0px 5px 0px 0px; float: left; display: inline\">"; 
	
	return image;
}

var featCount = 0;

function loadFeature(count)
{
	
	var obj = document.getElementById('featDiv');
	
	var featText = "";
	
	featText = "<tr valign=\"top\"><td style=\"padding: 10px 10px 10px 10px; background-image: url(http://www.benjydavisproject.com/images/tableBackground.png)\"><table width=\"380\" height=\"160\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"top\">";
	
	subtableWidth ="130";
	
	if (featObj[featCount].imageUrl != "")
	{
		featText += "<td width=\"130\" style=\"padding: 0px 10px 0px 0px\">";

		subtableWidth = 130;

		url = featObj[featCount].imageUrl;

		width = featObj[featCount].imageWidth;

		height = featObj[featCount].imageHeight;

		photoInfo ="" + printImage(url, width, height, subtableWidth) +"";

		featText += "<span id=\"content\">"+ photoInfo  +" </span>";

		featText += "</td>";

	}

	featText += "<td><span id=\"headline\">"+ featObj[featCount].title +"</span><br/>";

	featText += "<span id=\"content\">"+ unescape(featObj[featCount].body) +"</span></td>";
	
	featText += "</tr></table></td></tr>";


	if (obj.firstChild)
	obj.removeChild(obj.firstChild);

	var t = document.createElement('div');
	t.innerHTML = featText;
	obj.appendChild(t);
	
	var totalFeatures = featObj.length;

	if(featCount >= totalFeatures)
	{
		featCount = 0;
	}
	
	fadeInOut('in', featCount);

}

var c=0;
var dir2 = "out"

function fadeInOut(dir, count){

	var speed=2;

	var obj = document.getElementById('featDiv');

	if(obj.filters) {
		obj.style.filter='alpha(opacity='+c+')';
	} else {
		obj.style.opacity=c/100;
	}

	if(dir=='in') {
		dir1='in'
		c++;
	} else {
		dir1='out';
		c--;
	}

	if(c>100){
		c=100;
		setTimeout('fadeInOut(dir2, featCount)',5000);
		return;
	}

	if(c<0){
		c=0;
		featCount++;
		loadFeature(featCount);
		return;
	}

	setTimeout('fadeInOut(dir1, featCount)',10)
	
}