var photosLoaded      = false;
var floorplansLoaded  = false;
var globalImageType   = 'Image';

function in_array($search_value, $array_to_search) {
  for ($i = 0; $i < $array_to_search.length; $i++) {
    if ($search_value == $array_to_search[$i]) {
      return true;
    }
  }
  return false;
}


function activateObjectTab(selectedTab)
{
  selectedTab = selectedTab.id.replace('tab_', '');
  
  if (tabControl = document.getElementById('tabscontentholder'))
  {
    tabControlChilds = tabControl.childNodes;
    for (i = 0; i < tabControlChilds.length; i++)
    {
      node = tabControlChilds[i];
      if (node.nodeName == "DIV")
      {
        // Checks if it is a tab
        if (node.id == selectedTab)
        {
          node.style.display = 'block';
          node.style.visibility = 'visible';
          if (node.className == 'locatie') {
            node.style.height = '391px';
          }
        }
        else
        {
          node.style.display = 'none';
        }
      }
    }
  }
  activateObjectTabLink(selectedTab);

  return false;
}

function activateObjectTabLink(selectedTab)
{
  var tabs        = document.getElementById('tabsholder').childNodes;
  var selectedTab = 'tab_' + selectedTab;
  
  for (i = 0; i < tabs.length; i++)
  {
    if (tabs[i].nodeName == 'A')
    {
      if (tabs[i].className == 'active' && tabs[i].id != selectedTab)
        tabs[i].className = 'normal';
      else if (tabs[i].id == selectedTab)
        tabs[i].className = 'active';
    }
  }
}

function retrieveThumbnails(uuid, buildingType, imageType)
{
  globalImageType         = (!imageType) ? 'Image' : imageType;
  var holderId            = (globalImageType == 'Floorplan') ? 'objectdetailfloorplanthumbs' : 'objectdetailthumbs';
  
  if (((globalImageType == 'Image' && photosLoaded === false) || (globalImageType == 'Floorplan' && floorplansLoaded === false)) && document.getElementById(holderId))
  {
    var url               = 'object_details_thumbnails.php';
    var params            = 'uuid=' + uuid + '&buildingType=' + buildingType + '&imageType=' + globalImageType;
    var responseFunction  = 'retrieveThumbnailsResponse';
    remoteRequest(url, params, responseFunction, 'POST');
  }
}

function retrieveThumbnailsResponse()
{
  if (http_request && http_request.readyState == 4)
  {
    var holderId                = (globalImageType == 'Floorplan') ? 'objectdetailfloorplanthumbs' : 'objectdetailthumbs';
    
    if (div = document.getElementById(holderId))
    {
      if (http_request.status == 200 && http_request.responseText!='')
      {
        div.innerHTML           = http_request.responseText;
      }

      div.style.backgroundImage = 'none';
      if (globalImageType == 'Floorplan')
        floorplansLoaded        = true;
      else
        photosLoaded            = true;
    }
  }
}

function showImage(thumbElem, link, targetId)
{
  var mainImage     = document.getElementById(targetId);
  var thumbsHolder  = thumbElem.parentNode;
  
  if (mainImage)
  {
    mainImage.src = link;
    
    if (thumbsHolder.childNodes && thumbsHolder.childNodes.length > 0)
    {
      for (i = 0; i < thumbsHolder.childNodes.length; i++)
      {
        if (thumbsHolder.childNodes[i].id == thumbElem.id)
          thumbsHolder.childNodes[i].className = 'active';
        else if (thumbsHolder.childNodes[i].className == 'active')
          thumbsHolder.childNodes[i].className = 'normal';

      }
    }
  }
}

