var scenario;
var currentUrl;
var activeImage =0;

function thumbsWidthDetails()
{
  setObjectsThumbsWidth('scrollerThumbs','thumbnail');
}

function setObjectsThumbsWidth(div,classe)
{
  if(holder=document.getElementById(div))
  {
    var thumbsWidth = 0;
    count = 0;
    if(nav=document.getElementsByTagName('div'))
    {
      for(i=0;i<nav.length;i++)
      {
        if(nav[i].className.indexOf(classe)!=-1)
        {
          thumbsWidth += nav[i].offsetWidth;
          count++;
        }
      }
    }
    holder.style.width = (thumbsWidth + 4) + 'px';
  }
}

function toggleDisplayBlockById($id, $mode, $displayText, $location) {
   if ($id != '') {
      $element = document.getElementById($id);
      
      if ($element != null) {
        switch ($mode) {
          case "on": {
              if ($displayText != null) {
                  if ($displayText.length > 0) {
                    $element.innerHTML = $displayText;
                  }
              }
              
              if($location != null) {
                  if($location.length > 0) {
                    $element.href = $location;
                  }
              }
              
              $element.style.display = 'block';
              break;
          }
          case "off": {
              $element.style.display = 'off';
              break;
          }
          case "auto": {
              if ($element.style.display == 'block') {
                  $element.style.display = 'none';
              }
              else {
                  if ($displayText != null) {
                      if ($displayText.length > 0) {
                        $element.innerHTML = $displayText;
                      }
                  }
                  
                  if($location != null) {
                      if($location.length > 0) {
                        $element.href = $location;
                      }
                  }
                  $element.style.display = 'block';
              }
              break;
          }
          default: {
              alert('There has not been provided a valid mode, valid modes are on / off / auto.');
              break;
          }
        }
      }
   }
}

function activateObjectTab(selectedTab) {
  sourceId = selectedTab.id; 
  selectedTab = 'tab_' + selectedTab.id;
  tabControl = document.getElementById('tabsholder');
  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 (selectedTab == 'tab_location') {
          node.style.width = '824px';
          node.style.height = '450px';
        }
        node.className = 'tabSelected';
      }
      else {
        node.style.display = 'none';
        node.style.visibility = 'hidden';
        node.className = 'tabDisabled';
      }
    }
  }
  activateObjectTabLink(sourceId);
  return false;
}

function activateObjectTabLink(selectedTab) {
  tabs = document.getElementById('pagenavholder').childNodes;

  for (i = 0; i < tabs.length; i++) {
    if (tabs[i].nodeName == 'A')
    {
      tabs[i].className = 'normal';
      
      if (tabs[i].id == selectedTab) {
        tabs[i].className = 'active';
      }
    }
  }
}

function setCompareAfterAdd (status)
{
  if (status == "SUCCES")
  {
    setText("Verwijder uit vergelijking");
    if (current_mode != "compare_table")
    {
      updateCompareLinks();
    }
  }

  setCompareCount('projectCount', 'projectCountBB', 'projectCountBOG');
  setCompareMessage("compare_message");
}

function setCompareAfterRemove (status)
{
  if (status == "SUCCES")
  {
    setText("Toevoegen aan vergelijking");
    if (current_mode == "compare_table")
    {
      hideCompareColumn(current_column);
    } 
    else
    {
      updateCompareLinks();
    }
  }
  
  setCompareCount('projectCount', 'projectCountBB', 'projectCountBOG');
  setCompareMessage("compare_message");
}

function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function showCompareMenu()
{
  var holder;
  var compareMenu;
  
  if ((holder = document.getElementById('compare-menu-holder')) && (compareMenu = document.getElementById('compare-menu')))
  {
    compareMenu.style.left    = findPosX(holder) + 'px';
    compareMenu.style.display = 'block';
  }                                 
}

function hideCompareMenu()
{
  var compareMenu;
  if (compareMenu = document.getElementById('compare-menu'))
  {
    compareMenu.style.display = 'none';
  }
}

function animateSearchForm (parentId,mode)
{
  var parent = document.getElementById(parentId);
  if (typeof(mode) == "undefined")
    mode = "auto";

  switch (mode)
  {
    case "open":
    {
      openSearchForm();
      break;
    }
    case "close":
    {
      closeSearchForm();
      break;
    }
    case "auto":
    {
      if (parent.style.display == "block")
        closeSearchForm();
      else
        openSearchForm();
      break;
    }
  }
}

function openSearchForm()
{
  var parent = document.getElementById("searchboxholder");
  var child1 = document.getElementById("searchwindow");
  var child2 = document.getElementById("searchholder");

  parent.style.display = "block";

  if (!child1.style.height || !child2.style.height)
  {
    child1.style.height = 1 + "px";
    child2.style.height = 1 + "px";
  }

  childHeight1 = parseInt(child1.style.height) + 30;
  childHeight2 = (childHeight1 > 23 ? childHeight1 - 23 : 0);

  if (childHeight1 < 243 || childHeight2 < 220)
  {
    setTimeout("openSearchForm()",1);
  }
  else
  {
    childHeight1 = 243;
    childHeight2 = 220;
  }

  child1.style.height = childHeight1 + "px";
  child2.style.height = childHeight2 + "px";
}

function closeSearchForm()
{
  var parent = document.getElementById("searchboxholder");
  var child1 = document.getElementById("searchwindow");
  var child2 = document.getElementById("searchholder");

  if (!child1.style.height || !child2.style.height)
  {
    child1.style.height = 1 + "px";
    child2.style.height = 1 + "px";
  }

  childHeight1 = parseInt(child1.style.height) - 40;
  childHeight2 = (childHeight1 > 23 ? childHeight1 - 23 : 0);

  if (childHeight1 > 1)
  {
    setTimeout("closeSearchForm()",1);
  }
  else
  {
    parent.style.display = "none";
    childHeight1 = 1;
    childHeight2 = 1;
  }

  child1.style.height = childHeight1 + "px";
  child2.style.height = childHeight2 + "px";
}

function search_form_get_params()
{
  var params = 'uri=' + obtainURI();
  if(input = document.getElementsByTagName("input"))
  {
    for (i = 0; i < input.length; i++)
    {
      //params += "&" + input[i].name + "=" + input[i].value;
    }
  }
  return params;
}

function setCurrentUrl(url)
{
  var base_href = getBaseHref().href;
  if (base_href.indexOf('http://' + window.location.hostname + '/') != -1)
  {
    currentUrl = base_href;
  }
  else
  {
    var addition = '';
    var position = base_href.replace('http://', '').indexOf('/');
    if (position != -1)
    {
      addition = base_href.substring(position + 8, base_href.length);
    }
    currentUrl = 'http://' + window.location.hostname + '/' + addition;
  }
}

function toggleSearchForm (s, resultpage, urlAddition)
{
  var searchform = document.getElementById("searchboxholder");
  scenario = s;
  
  if(!urlAddition)
    urlAddition = '';

  if (searchform.className == scenario)
  {
    animateSearchForm("searchboxholder");
  }
  else
  {
    if(window.XMLHttpRequest || window.ActiveXObject)
    {
      setCurrentUrl();
      var params = search_form_get_params();

      params  = "scenario=" + scenario;
      params += "&resultid=" + resultpage;
      params += urlAddition;

      remoteRequest(currentUrl + 'frontend-g4/tools/search_form_dynamic.php',params,'searchform_request_after');
    }
  }
}

function searchform_request_after ()
{
  if (http_request && http_request.readyState == 4)
  {
    if (http_request.status == 200 && http_request.responseText && http_request.responseText.trim() != '')
    {
      include_js("frontend-g4/inc/js/fgw_project_search_form_dynamic.js");
      response = http_request.responseText;

      search_form_body   = document.getElementById("mainsearch");
      search_form_body.innerHTML = response;

      search_form_holder = document.getElementById("searchboxholder");
      search_form_holder.className = scenario;
      animateSearchForm("searchboxholder","open");

      dyn_srch_start();
    }
  }
}

function addThumbnails(src, width, height)
{
  var url           = 'scripts/resize_image.php';
  var params        = 'images=' + src + '&width=' + width + '&height=' + height;
  var onreadystate  = 'placeThumbnails';
  
  remoteRequest(url, params, onreadystate);
}

/**
* Place the thumbnails
* 
* @param void
* @return void
*/
function placeThumbnails()
{
  if (http_request && http_request.readyState == 4 && http_request.status == 200 && http_request.responseText!='')
  {
    if(container = document.getElementById('slider-container'))
    {
      if(http_request.responseText.indexOf('|')!==-1)
      {
        var images = http_request.responseText.split('|');
      }
      else
      {
        var images = new Array(http_request.responseText);
      }
      
      for(var i=0; i<images.length; i++)
      {
        image = images[i].split('<>');
        
        var img = document.createElement("img");
        container.appendChild(img);
        
        img.src     = 'img/cache/' + image[1];
        img.number  = i;
        img.id      = 'image' + i;
        img.org_img = image[0];
        img.onclick = function() {moveSpecificImage(this.number);};
      }
      activeImage = 0;
    }
  }
}

function changePhoto(src)
{
  var url           = 'scripts/resize_image.php';
  var params        = 'image=' + src + '&width=540&height=360';
  var onreadystate  = 'changePhotoCallback';
  remoteRequest(url, params, onreadystate);
}

function changePhotoCallback()
{
  if (http_request && http_request.readyState == 4 && http_request.status == 200 && http_request.responseText!='')
  {
    if(img = document.getElementById('bigImage'))
    {
      var image = http_request.responseText.split('<>');
      img.src = 'img/cache/' + image[1];
    }
  }
}

/**
* Move to the next image
*
* @param void
* @return void
*/
function moveNextImage()
{
  var imageNumber = activeImage + 1;
  moveSpecificImage(imageNumber);
}

/**
* Move to the previous image
*
* @param void
* @return void
*/
function movePrevImage()
{
  var imageNumber = activeImage - 1;
  moveSpecificImage(imageNumber);
}

/**
* Move to a specific image
*
* @param int imageNumber
* @return void
*/
function moveSpecificImage(imageNumber)
{
  if ((firstImage = document.getElementById('image0')) && (image = document.getElementById('image' + imageNumber)) && (holder = document.getElementById('imgslider')))
  {
    var pos = (image.offsetLeft - firstImage.offsetLeft) - (holder.offsetWidth  / 2) + (image.offsetWidth / 2);
    if (pos < 0)
      pos = 0;
    
    slideNavigationVertical(holder.id, pos);
    changePhoto(image.org_img);
    activeImage = imageNumber;
  }
}