// JavaScript Document
(function() {

var currentWidth = 0;
var animating = false;
var search = false;

addEventListener("load", function(event)
{
  setTimeout(preloadImages, 0);
  setTimeout(checkOrientAndLocation, 0);
  setInterval(checkOrientAndLocation, 300);
  toggle('search');
  toggle('overlay');

}
, false);

addEventListener("unload", function(event)
{
  var atags = document.getElementsByTagName('A');
  for( var i=0; i<atags.length; i++ )
  {
    if(atags[i].hasAttribute('selected'))
    {
      atags[i].removeAttribute('selected');
    }
  }
}
, false);

addEventListener("change", function(event)
{
  if(event.target.nodeName == 'SELECT')
    //window.location = 'http://' + location.host + event.target.value;
	window.location = 'http://' + event.target.value;
	//window.location = event.target.value;
  else
    submitForm(findParent(event.target, "form"));
}
, true);

addEventListener("click", function(event)
{
  var link = findParent(event.target, "a");
  if (link && link.href.indexOf("mailto:") < 0 && link.href.charAt(0) != '#')
  {
    function unselect() { link.removeAttribute("selected"); }

    if (link == $("overlay"))
    {
      if(search)
      {
        search = false;
        toggle('search');
      }
      toggle('overlay');
    }
    else if (link == $("searchButton"))
    {
      search = search ? false : true;
      toggle('search');
      toggle('overlay');
    }
    else if (link.getAttribute("type") == "external")
      window.open(link.href);
    else if (link.getAttribute("type") == "submit")
      submitForm(findParent(link, "form"));
    else if (link.getAttribute("type") == "cancel")
      cancelDialog(findParent(link, "form"));
    else if (!link.target)
    {
      link.setAttribute("selected", "progress");
      window.location = link.href;
    }
    else
      return;
    event.preventDefault(); 
  }
}
, true);

function checkOrientAndLocation()
{
  if (animating)
    return;

  if (window.innerWidth != currentWidth)
  {   
    currentWidth = window.innerWidth;
    var orient = currentWidth == 320 ? "profile" : "landscape";
    document.body.setAttribute("orient", orient);
    setTimeout(scrollTo, 100, 0, 1);
  }
}

function preloadImages()
{
  var preloader = document.createElement("div");
  preloader.id = "preloader";
  document.body.appendChild(preloader);
}

function submitForm(form)
{
	window.location = form.action + '?' + encodeForm(form).join('&');
	//window.location = form.action + '?' + encodeForm(form).join('&');
  //window.location = 'http://www.med.yale.edu/library/search/federated-pubmed.php' + '?' + 'words=' + encodeForm(form).join('&');
}

function encodeForm(form)
{
  function encode(inputs)
  {
    for (var i = 0; i < inputs.length; ++i)
    {
      if (inputs[i].name)
        args.push(inputs[i].name + "=" + escape(inputs[i].value));
    }
  }
  var args = [];
  encode(form.getElementsByTagName("input"));
  encode(form.getElementsByTagName("select"));
  return args;    
}

function findParent(node, localName)
{
  while (node && (node.nodeType != 1 || node.localName.toLowerCase() != localName))
    node = node.parentNode;
	//node='http://www.med.yale.edu/library';
  return node;
}

function $(id) { return document.getElementById(id); }

function toggle(p)
{
  var s = $(p);
  var c = $('screen');
  var d = s.style.display == 'none' ? 'block' : 'none';
  s.style.display = d;
  c.style.display = d;
  return d;
}

function overlayBackground(i)
{
  setTimeout(function()
  {
    scrollTo(0, 1);
  }, 0);
  var o = $('overlay');
  o.style.backgroundPosition = 'center center, center center';
  o.style.backgroundRepeat = 'no-repeat, no-repeat';
  $('overlay').style.backgroundImage = 'url(' + i + '),url(../i/loading_black.gif)';
}

})();

function showAlert(t)
{
  alert('Click "OK" below to play your ' + t + '.\n* This may take several seconds to load.\n* Once the ' + t + ' has finished, press the back arrow button to return.');
}

function videoPlayer(filename, a, b, c, d, e, f, g)
{
  showAlert('video');
  document.location = 'http://' + location.host + '/media/' + filename + '_ref.mov';
} 