function speedTest() {
    var duration = (new Date()).getTime() - start;

		if ( connection > THRESHOLD )
		{
			//slow connection
			connection = duration * 2;
		}
		else 
		{
			//fast connection
			connection = duration ;
		}
}


  function getGallery(gallID, desID) {

   var params = new Array();
   params['galleryID'] = gallID;
   params['designID'] = desID;
   var bindArgs = {
    url: "/portfolio/index.php",
    error: function(type, data, evt){
           },
    load: function(type, data, evt) {},
    mimetype: "text/plain",
    content: params
   };

   var req = dojo.io.bind(bindArgs);
   dojo.event.connect(req, "load", this, "populateData");
  }

  function populateData(type, data, evt) {
   var designIndex = document.getElementById("designIndex");  
   var pictureIndex = document.getElementById("pictureIndex");
   var galleryIndex = document.getElementById("galleryIndex");
  
   data = eval('(' + data + ')')


   if (data) {
     floorLIMIT = 0;
     upperLIMIT = data.images;
     floorGAL_LIMIT = 0;
     upperGAL_LIMIT = data.galleries;
		 floorDES_LIMIT = 0;
     upperDES_LIMIT = data.design.length;
		
		 design_string = '';		
	
		 for (count = 0; count < upperDES_LIMIT ; count++)
			{
				if( count == designID )
				{
         design_string = design_string + '<a href="javascript:;" onclick="getDesign(' + count + ')" class="rhselected" style="background: #6d2d15;color:#ffffff;">&#160;' + data.design[count] + '&#160;</a> &#160;';
				}
				else
				{
         design_string = design_string + '<a href="javascript:;" onclick="getDesign(' + count + ')" class="rh">&#160;' + data.design[count] + '&#160;</a> &#160;';
				}

			}
		
		 designIndex.innerHTML = design_string;

     if ( upperLIMIT > 0 ) {
	     index_string = '<a href="javascript:;" class="white" style="background: #f2f2f2;color:#5f5d5d" onclick="onclick_previousImage()" id="">&#160;&lt;&#160;</a>';
	     index_string = index_string + '<a href="javascript:;" class="white" style="background: #6d2d15;color:#ffffff;" onclick="selectItem(0)" id="_selected_0">&#160;1&#160;</a>';;
	     for(i=1; i<=upperLIMIT ; i++) {
		index_string = index_string + '<a href="javascript:;" class="white" style="background: #f2f2f2;color:#5f5d5d;" onclick="selectItem(' + i + ')" id="_selected_' + i + '">&#160;' + (i+1) + '&#160;</a>';
	     }
	     index_string = index_string + '<a href="javascript:;" class="white" style="background: #f2f2f2;color:#5f5d5d" onclick="onclick_nextImage()" id="">&#160;&gt;&#160;</a>';
     } else { index_string = '' };
  

     pictureIndex.innerHTML = index_string;


     gallery_string = '<a href="javascript:;" onclick="onclick_previousGallery()" class="rh">&#160; PREVIOUS </a> &#160;<a href="../html/ourWork.html">&#160;OUR WORK  &#160;</a>&#160;<a href="javascript:;" onclick="onclick_nextGallery()">&#160;NEXT&#160;</a>';

     galleryIndex.innerHTML = gallery_string;

   }
  }

  function fadeIn(id, startfade, endfade)
	{
	timer = 20;
	if (startfade < endfade)
		{
		for (i = startfade; i <= endfade; i++)
			{
			setTimeout("setOpacity(" + i + ",'" + id + "')", (timer * 4));
			timer = timer + 1;
			}
		}
	}


  function fadeOut(id, startfade, endfade)
	{
	timer = 0;
	if (startfade < endfade)
		{
		for (i = endfade; i >= startfade; i = i - 1)
			{
			setTimeout("setOpacity(" + i + ",'" + id + "')", (timer));
			timer++;
			}
		}
	}


  function setOpacity(opacity, id)
	{
	var el = document.getElementById(id).style;
	el.opacity = (opacity / 100);
	el.MozOpacity = (opacity / 100);
	el.KhtmlOpacity = (opacity / 100);
	el.filter = "alpha(opacity=" + opacity + ")";
	}


 function loadFirstImage() {
  currentID = 0;
  getItem(currentID,galleryID,designID);
 }

 function onclick_previousGallery() {
 fadeOut('itemImage', 0, '99.99');
  designID = 0;
  if(galleryID > floorGAL_LIMIT ) { galleryID--;  getGallery(galleryID,designID); }
  else { galleryID = upperGAL_LIMIT; getGallery(galleryID,designID); }
  loadFirstImage();
 }

 function onclick_nextGallery() { 
 fadeOut('itemImage', 0, '99.99');
  designID = 0;
  if(galleryID < upperGAL_LIMIT ) { galleryID++;  getGallery(galleryID,designID); }
  else { galleryID = floorGAL_LIMIT; getGallery(galleryID,designID); }
  loadFirstImage();
 }


 
 function onclick_previousImage() {
 fadeOut('itemImage', 0, '99.99');
  if(currentID > floorLIMIT ) { currentID--;  getItem(currentID,galleryID,designID); }
  else { currentID = upperLIMIT; getItem(currentID,galleryID,designID); }
 }

 function onclick_nextImage() { 
 fadeOut('itemImage', 0, '99.99');
  if(currentID < upperLIMIT ) { currentID++; getItem(currentID,galleryID,designID); }
  else { currentID = floorLIMIT; getItem(currentID,galleryID,designID); }
 }

 function selectItem(itemID) {
  fadeOut('itemImage', 0, '99.99');
  currentID = itemID;
  getItem(currentID,galleryID,designID);
 } 

 function getDesign(desID) {
  fadeOut('itemImage', 0, '99.99');
	designID = desID;
	getGallery(galleryID,designID);
  loadFirstImage();
 }

function getItem(itemID,gallID, desID) {
 var params = new Array();
 params['itemID'] = itemID;
 params['gallID'] = gallID;
 params['desID'] = desID;
 var bindArgs = {
  url: "/portfolio/index.php",
  error: function(type, data, evt){
         },
  load: function(type, data, evt) {},
 mimetype: "text/plain",
  content: params
 };
 var req = dojo.io.bind(bindArgs);
 dojo.event.connect(req, "load", this, "populateDiv");
}

 function loadImage( image, ID )
 {
   ID.src = image;
 }

function populateDiv(type, data, evt) {
 var itemTitle = document.getElementById("itemTitle");
 var itemDescEmp = document.getElementById("itemDescriptionEmphasise");
 var itemDesc = document.getElementById("itemDescription");
 var itemImage = document.getElementById("itemImage");
 var itemQuote = document.getElementById("quoteSpace");
 var itemQuoteAuthor = document.getElementById("quoteAuthor");
 var itemLink = document.getElementById("externalLink");
 var itemImageLink = document.getElementById("itemImageLink");
 var quoteTable = document.getElementById("quoteTable");

 data = eval('(' + data + ')')

 if (data) {

 var selectedNode = document.getElementById("_selected_" + data.currentID); 

   
  itemTitle.innerHTML = data.title;
  itemDesc.innerHTML = data.description;
  itemDescEmp.innerHTML = data.emphasis;
  itemImage.height = data.imageX;
  itemImage.width = data.imageY;
  itemImage.src = '/images/trans.gif';
  loadImage( data.image, itemImage );
//  setTimeout('loadImage("' + data.image + '",' + itemImage + ')', connection );
  itemLink.innerHTML = data.extLinkTitle;
  itemLink.href = data.extLink;
  itemImageLink.href = data.extLink;

  quoteTable.style.display = "none";

  if (data.quote != '') 
  {
   itemQuote.innerHTML = "“" + data.quote + "“"; 
   quoteTable.style.display = "";
  } 
  else 
  {
   itemQuote.innerHTML = ""; 
  }
  itemQuoteAuthor.innerHTML = data.quoteAuthor;


  setTimeout("fadeIn('itemImage', 0, '99.99')", connection * 2);


  for ( i=floorLIMIT; i <= upperLIMIT; i++)
  {
    document.getElementById("_selected_" + i).style.background = '#f2f2f2';
    document.getElementById("_selected_" + i).style.color = '#5F5D5D';
    document.getElementById("_selected_" + i).innerHTML = "&nbsp;" + (i+1) + "&nbsp;";
  }

  selectedNode.innerHTML = "&nbsp;" + (data.currentID+1) + "&nbsp;";
  selectedNode.style.background = '#6d2d15';
  selectedNode.style.color = '#ffffff';

 }


}


 function indexRollOver( indexNo )
 {
  if (indexNo == currentID )
  { 
    document.getElementById("_selected_" + indexNo).style.background = '#f2f2f2';
    document.getElementById("_selected_" + indexNo).style.color = '#5F5D5D';
  }
  else
  { 
    document.getElementById("_selected_" + indexNo).style.background = '#6d2d15';
    document.getElementById("_selected_" + indexNo).style.color = '#ffffff';
  }

 }

 function indexRollOut( indexNo )
 {
  if (indexNo == currentID )
  { 
    document.getElementById("_selected_" + indexNo).style.background = '#6d2d15';
    document.getElementById("_selected_" + indexNo).style.color = '#ffffff';
  }
  else
  { 
    document.getElementById("_selected_" + indexNo).style.background = '#f2f2f2';
    document.getElementById("_selected_" + indexNo).style.color = '#5F5D5D';
  }

 }


