// JavaScript Document

var mainLinkIds = ['homeLink', 'portLink', 'newsLink', 'aboutLink', 'contactLink'];
var stopEase = false;
var thumbnailSlidePos = 0;
var theBody;
var Browser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

  function easeProp(elementId, value, freq, movement, stopAt, accelerate, property, direction)
  {
    if(!stopEase)
    {
      var element = document.getElementById(elementId);
      if(element)
      {
        var nextValue = value + movement;
        if(direction == 1)
        {
          if(nextValue > stopAt)
          {
            nextValue = stopAt;
          }
        }
        else
        {
          if(nextValue < stopAt)
          {
            nextValue = stopAt;
          }
        }
        var nextMovement = movement + accelerate;

        if(direction == 1)
        {
          if(nextMovement <= 0)
          {
            nextMovement = 1;
          }
        }
        else
        {
          if(nextMovement >= 0)
          {
            nextMovement = -1;
          }
        }

        element.style[property] = nextValue + 'px';
        thumbnailSlidePos = nextValue;
        if(direction == 1)
        {
          if(nextValue < stopAt)
          {
            setTimeout('easeProp("' + elementId + '", ' + nextValue + ', ' + freq + ', ' + nextMovement + ',' + stopAt + ', ' + accelerate + ', "' + property + '", ' + direction + ')', freq);
          }
        }
        else
        {
          if(nextValue > stopAt)
          {
            setTimeout('easeProp("' + elementId + '", ' + nextValue + ', ' + freq + ', ' + nextMovement + ',' + stopAt + ', ' + accelerate + ', "' + property + '", ' + direction + ')', freq);
          }
        }
      }
    }

  }


function wireThumbnailControls()
{
  var moveLeft = document.getElementById('moveLeft');
  if(moveLeft)
  {
    var moveRight = document.getElementById('moveRight');
    moveRight.onmouseover = function()
    {
      stopEase = false;
      var stopAt = document.getElementById('thumbnailSlide').getAttribute('stylewidth');
      stopAt = stopAt * -1;
      stopAt = stopAt + 135;
      easeProp('thumbnailSlide', thumbnailSlidePos, 1, -1, stopAt, 0, 'left', -1);
    }
    moveLeft.onmouseout = function()
    {
      stopEase = true;
    }
    moveLeft.onmouseover = function()
    {
      stopEase = false;
      var stopAt = document.getElementById('thumbnailSlide').getAttribute('stylewidth');
      stopAt = stopAt * 1;
      stopAt = stopAt - 135;
      easeProp('thumbnailSlide', thumbnailSlidePos, 1, 1, 0, 0, 'left', 1);
    }
    moveRight.onmouseout = function()
    {
      stopEase = true;
    }
    var imgs = document.getElementById('thumbnailSlide').getElementsByTagName('img');
    for(i = 0; i < imgs.length; i++)
    {
      imgs[i].onmouseover = function()
      {
        document.getElementById('portDetImg').src = this.getAttribute('full');
        //theBody.innerHTML = theBody.innerHTML + '<p>' + document.getElementById('portDetImg').src + '</p>';
        var imgHeight = this.getAttribute('fullheight') * 1;
        //theBody.innerHTML = theBody.innerHTML + '<p>' + imgHeight + '</p>';
        var margin_top = 0;
        if(imgHeight < 440)
        {
          var dif = 440 - imgHeight;
          //theBody.innerHTML = theBody.innerHTML + '<p>' + dif + '</p>';
          margin_top = Math.ceil(dif/2);
          //theBody.innerHTML = theBody.innerHTML + '<p>' + margin_top + '</p>';
        }
        //theBody.innerHTML = theBody.innerHTML + '<p>' + document.getElementById('portDetImg').style.marginTop + '</p>';
        document.getElementById('portDetImg').style.marginTop = margin_top + 'px';
        //theBody.innerHTML = theBody.innerHTML + '<p>' + document.getElementById('portDetImg').style.marginTop + '</p>';
      }
    }
  }
}

function classSwapping(a,o,c1,c2)
{
  switch (a){
    case 'swap':
      o.className =! classSwapping('check',o,c1)?o.className.replace(c2,c1):
      o.className.replace(c1,c2);
    break;
    case 'add':
      if(!classSwapping('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

function showSubs(obj)
{
  var subUL = document.getElementById(obj.id + 'SubUL');
  
  if(subUL)
  {
    var lis = subUL.getElementsByTagName('li');
    for(var i = 0; i < lis.length; i++)
    {
      //classSwapping('remove', lis[i], 'hidden');
    }
  }
}

function hideSubs(obj)
{
  var subUL = document.getElementById(obj.id + 'SubUL');

  if(subUL)
  {
    var lis = subUL.getElementsByTagName('li');
    for(var i = 0; i < lis.length; i++)
    {
      classSwapping('add', lis[i], 'hidden');
    }
  }
}

function slideShow(slide_num, x_pos)
{

  var next_slide_num_1 = slide_num + 1;
  if(next_slide_num_1 == 4)
  {
    next_slide_num_1 = 0;
  }
  var next_slide_num_2 = next_slide_num_1 + 1;
  if(next_slide_num_2 == 4)
  {
    next_slide_num_2 = 0;
  }
  var next_slide_num_3 = next_slide_num_2 + 1;
  if(next_slide_num_3 == 4)
  {
    next_slide_num_3 = 0;
  }
  var slide1 = document.getElementById('slide' + slide_num);
  var slide2 = document.getElementById('slide' + next_slide_num_1);
  var slide3 = document.getElementById('slide' + next_slide_num_2);
  var slide4 = document.getElementById('slide' + next_slide_num_3);
  
  if(slide1)
  {
    slide1.style.zIndex = 4;
    slide2.style.zIndex = 1;
    slide3.style.zIndex = 2;
    slide4.style.zIndex = 3;

    if(x_pos < 0)
    {
      x_pos = x_pos + 70;
      if(x_pos > 0)x_pos = 0;
      slide1.style.left = x_pos + 'px';
      setTimeout('slideShow(' + slide_num + ', ' + x_pos + ')', 1);
    }
    else
    {
      setTimeout('slideShow(' + next_slide_num_1 + ', -620)', 6000);
    }
  }

}


function switchToHoverImage(obj)
{
  obj.original_source = obj.src;
  var source = new String(obj.src);
  source = source.replace(/normal\.png/, 'hover.png');
  obj.src = source.replace(/on\.png/, 'hover.png');
}

function switchToOriginalImage(obj)
{
  obj.src = obj.original_source;
}

function setMainLinkMouseOvers()
{
  for(var i = 0; i < mainLinkIds.length; i++)
  {
    if(document.getElementById(mainLinkIds[i] + 'Img'))
    {
      document.getElementById(mainLinkIds[i] + 'Img').onmouseover = function()
      {
        switchToHoverImage(this);
      }
      document.getElementById(mainLinkIds[i]).onmouseover = function()
      {
        showSubs(this);
      }
      document.getElementById(mainLinkIds[i] + 'Img').onmouseout = function()
      {
        switchToOriginalImage(this);
      }
      document.getElementById(mainLinkIds[i]).onmouseout = function()
      {
        hideSubs(this);
      }
    }
    document.getElementById(mainLinkIds[i]).onmouseover = function()
    {
      var lis = this.getElementsByTagName('li');
      for(var i = 0; i < lis.length; i++)
      {
        classSwapping('remove', lis[i], 'hidden');
      }
    }
    document.getElementById(mainLinkIds[i]).onmouseout = function()
    {
      var lis = this.getElementsByTagName('li');
      for(var i = 0; i < lis.length; i++)
      {
        classSwapping('add', lis[i], 'hidden');
      }
    }
  }
}

window.onload = function()
{
  theBody = document.getElementsByTagName('body');
  theBody = theBody[0];
  setMainLinkMouseOvers();
  setTimeout('slideShow(0, 0)', 1);
  wireThumbnailControls();
  if(Browser.Version() > 6)
  {
    var slideshowBadge = document.getElementById('slideshowBadge');
    var navigation = document.getElementById('navigation');
    if(slideshowBadge)
    {
      slideshowBadge.src = slideshowBadge.src.replace('gif', 'png');
      slideshowBadge.style.marginTop = '-115px';
      
    }
    if(navigation)
    {
      classSwapping('add', navigation, 'special');
      
    }
  }
}
