function browser(request){
  var browsers = new Object();
  browsers["ns6+"] = (document.getElementById && !document.all);
  browsers["ie4+"] = (document.all);
  browsers["ie5"] = (document.all && !document.fireEvent && !window.opera);
  browsers["ie55"] = (document.all && document.fireEvent && !document.createComment);
  browsers["ie6"] = (document.all && document.fireEvent && document.createComment);
  
  if (browsers[request])
    {
    return true;
    }
  else
    {
    return false;
    }
}

function show_addtofavorites_links(){
  var i, obj;
  var container = document.getElementById('search_container');
  var num = container.getElementsByTagName('a').length;
  for (i = 0; i < num; i++)
    {
    obj = container.getElementsByTagName('a')[i];
    if (obj.className === "favlink")
      {
      if (browser("ie4+"))
        {
        obj.onmouseover = new Function("self.status = 'Add this page to your favorites'; return true;");
        obj.onmouseout = new Function("self.status = ''; return true;");
        obj.href = "javascript:window.external.AddFavorite('"+window.location.href+"', '"+document.title+"');";
        obj.style.display = "inline";
        }
      }
    }
}

function element_to_imglink(element, href){
  // Initial element must have "id", "src", "name" and "value" attributes
  if ((!element.id) || (!element.src) || (!element.name) || (!element.value))
    {
    return false;
    }
  
  var oldid = element.id;
  var newid = "new_"+oldid;
  
  obj = document.createElement("a");
  obj.setAttribute("id", newid);
  obj.setAttribute("name", element.name);
  obj.setAttribute("href", href);
  document.body.appendChild(obj);
  
  var newelement = document.getElementById(newid);
  
  obj = document.createElement("img");
  obj.setAttribute("border", "0");
  obj.setAttribute("src", element.src);
  obj.setAttribute("alt", element.value);
  newelement.appendChild(obj);
  
  element.parentNode.replaceChild(newelement, element);
  
  newelement.id = oldid;
  
  return true;
}

function createformelement(elementid){
  var element = document.getElementById(elementid);
  
  // Find form element (or stop at body)
  var parent = element.parentNode;
  while (parent.tagName != "FORM")
    {
    parent = parent.parentNode;
    if (parent.tagName == "BODY")
      {
      return false;
      }
    }
  
  var obj;
  var form = parent;
  var containerid = "jsformelements";
  var container = document.getElementById(containerid);
  
  // Create container
  if (!container)
    {
    obj = document.createElement("span");
    obj.setAttribute("id", containerid);
    form.appendChild(obj);
    
    // Redeclare container variable
    var container = document.getElementById(containerid);
    }
    
  // Destroy child elements
  while (container.hasChildNodes())
    {
    container.removeChild(container.firstChild);
    }
  
  obj = document.createElement("input");
  obj.setAttribute("type", "hidden");
  obj.setAttribute("name", element.name);
  obj.setAttribute("value", "1");
  container.appendChild(obj);
}

function imagepopup(url, width, height){
  // Creates a new image popup
  
  var new_window = window.open('', 'name', 'height='+height+', width='+width+', status=no, left=20, top=20');
  
	var tmp = new_window.document;
  
	tmp.write('<html><head><title>Photo</title></head>'+
            '<body style="margin:0;padding:0">'+
            '<img style="width:'+width+';height:'+height+';border:0" src="'+url+'" alt="" />'+
            '</body>'+
            '</html>');
  
	tmp.close();
  
  if (window.focus) 
    {
    new_window.focus();
    }
}

function togglechecks(status, containerid){
  // Check/Uncheck checkboxes inside of an element
  
  var container, element, i, num;
  
  container = document.getElementById(containerid);
  num = container.getElementsByTagName('input').length;
  
  i = 0;
  while (i < num)
    {
    element = container.getElementsByTagName('input')[i];
    if (element.type == "checkbox")
      {
      element.checked = status;
      }
    
    i++;
    }
}

function hide_elements_between_siblings(after_node, before_node) {
  // Hides all elements between two sibling nodes
  
  // @param  object  after_node   node to start hiding elements after
  // @param  object  before_node  node to stop hiding elements before
  
  // @return  boolean  success or failure
  
  var next_node;
  
  if (after_node.parentNode != before_node.parentNode)
    {
    return false;
    }
  else
    {
    if (after_node.nextSibling)
      {
      next_node = after_node.nextSibling;
      
      while (next_node != before_node)
        {
        // Is this an element node?
        if (next_node.nodeType == 1)
          {
          next_node.style.display = 'none';
          }
        
        if (next_node.nextSibling)
          {
          next_node = next_node.nextSibling;
          }
        else
          {
          break;
          }
        }
      }
    
    return true;
    }
}

function hide_apartment_virtual_tour() {
  // Hides the virtual tour on the apartment photos page
  
  // @return  VOID
  
  document.getElementById('virtual_tour').style.display = 'none';
}

function show_apartment_virtual_tour() {
  // Shows the virtual tour on the apartment photos page
  
  // @return  VOID
  
  document.getElementById('virtual_tour').style.display = 'block';
}

function hide_apartment_photos() {
  // Hides the photos on the apartment photos page
  
  // @return  VOID
  
  document.getElementById('largephotobox').style.display = 'none';
  document.getElementById('photos').style.display = 'none';
}

function show_apartment_photos() {
  // Shows the photos on the apartment photos page
  
  // @return  VOID
  
  document.getElementById('largephotobox').style.display = 'block';
  document.getElementById('photos').style.display = 'block';
}

function create_apartment_media_link(media) {
  // Changes specific text on the apartment photos page into a link
  
  // @param  string  media  media type (possible values: 
  //                        'virtual_tour', 'photos')
  
  // @return  VOID
  
  var child, children, link;
  
  children = document.getElementById('details_photoslinks')
               .getElementsByTagName('span');
  
  for (child in children)
    {
    if (children[child].className == media)
      {
      link = document.createElement('a');
      link.className = media;
      link.setAttribute('href', 
             add_hash_to_uri(String(window.location), 
               'details_photoslinks'));
      link.onclick = new Function(
        'show_hide_apartment_media("' + media + '");');
      document.getElementById('details_photoslinks')
        .insertBefore(link, children[child]);
      
      link.appendChild(children[child].firstChild);
      document.getElementById('details_photoslinks')
        .removeChild(children[child]);
      
      break;
      }
    }
}

function remove_apartment_media_link(media) {
  // Removes an apartment media link on the apartment photos page
  // but leaves descendant nodes behind
  
  // @param  string  media  media type (possible values: 
  //                        'virtual_tour', 'photos')
  
  // @return  VOID
  
  var descendant, descendants, i, link, links, span;
  
  links = document.getElementById('details_photoslinks')
            .getElementsByTagName('a');
  
  for (link in links)
    {
    if (links[link].className == media)
      {
      span = document.createElement('span');
      span.className = media;
      document.getElementById('details_photoslinks').insertBefore(
        span, links[link]);
      
      descendants = links[link].childNodes;
      
      i = 0;
      while (descendants.length > 0)
        {
        span.appendChild(descendants[i++]);
        }
      
      document.getElementById('details_photoslinks').removeChild(
        links[link]);
      
      break;
      }
    }  
}

function show_hide_apartment_media(media) {
  // Performs various tasks to show and hide various media
  
  // @param  string  media  media type to show (possible values: 
  //                        'virtual_tour', 'photos')
  
  // @return  VOID
  
  switch (media)
    {
    case 'virtual_tour':
      hide_all_apartment_media();
      show_apartment_virtual_tour();
      remove_apartment_media_link(media);
      create_apartment_media_link('photos');
      break;
    case 'photos':
      hide_all_apartment_media();
      show_apartment_photos();
      remove_apartment_media_link(media);
      create_apartment_media_link('virtual_tour');
      break;
    }
}

function hide_all_apartment_media() {
  // Hides all apartment media on the apartment photos page
  
  // @return  VOID
  
  hide_elements_between_siblings(
    document.getElementById('details_photoslinks'),
    document.getElementById('details_body')
      .getElementsByTagName('script')[0]);
}

function add_hash_to_uri(uri, hash) {
  // Adds a hash string to a URI
  
  // @param  string  uri   URI to append hash to
  // @param  string  hash  hash string (without '#')
  
  // @return  string  new URI
  
  var new_uri;
  
  hash_start = uri.indexOf('#');
  
  new_uri = (hash_start == -1) ? uri : uri.substring(0, hash_start);
  new_uri += '#' + hash;
  
  return new_uri;
}