var neoBox = null;

window.onload = function() {
  var loc = String(document.location);
  if (loc.indexOf('index') > -1 || loc.charAt(loc.length - 1) == '/') { setupSlider(); }

  var node = document.getElementById('maritime');
  var size = Math.max(node.offsetHeight, node.scrollHeight) - 2;

  var code = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="183" height="'+ size +'">'
  +'<param name="movie" value="maritime.swf"/>'
  +'<param name="allowScriptAccess" value="always"/>'
  +'<param name="bgcolor" value="#17181d"/>'
  +'<param name="quality" value="high"/>'
  +'<param name="wmode" value="transparent"/>'
  +'<param name="FlashVars" value="height='+ size +'"/>'
  +'<embed src="maritime.swf" width="183" height="'+ size +'" allowScriptAccess="always" bgcolor="#17181d" quality="high" wmode="transparent" FlashVars="height='+ size +'" type="application/x-shockwave-flash"/>'
  +'</object>';

  node = document.getElementById('sidebar');
  node.style.height = size +'px';
  node.innerHTML = code;

  node = document.getElementById('submit');
  if (node) {
    document.forms[0][0].focus();
    node.onmousedown = clickButton;
    node.onmouseup = clickButton;
  } else if (loc.indexOf('details') > -1) {
    setupDetails();
  }
}

function setupSlider() {
  var node = document.getElementById('slider');
/*
  var code = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="672" height="282">'
  +'<param name="movie" value="slider.swf"/>'
  +'<param name="bgcolor" value="#000000"/>'
  +'<param name="quality" value="high"/>'
  +'<embed src="slider.swf" width="672" height="282" bgcolor="#000000" quality="high" type="application/x-shockwave-flash"/>'
  +'</object>';
*/
  var code = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="672" height="138">'
  +'<param name="movie" value="frontPagePanel.swf"/>'
  +'<param name="bgcolor" value="#000000"/>'
  +'<param name="quality" value="high"/>'
  +'<embed src="frontPagePanel.swf" width="672" height="138" bgcolor="#000000" quality="high" type="application/x-shockwave-flash"/>'
  +'</object>';

  node.innerHTML = code;
}

function setupDetails() {
  var i, len, node, nodes = document.getElementsByTagName('img');

  for (i = 0, len = nodes.length; i < len; i++) {
    node = nodes[i];
    if (node.id) { continue; }
    node.onclick = changePhoto;
  }

  var body = document.getElementsByTagName('body')[0];
  neoBox = document.createElement('div');
  neoBox.className = 'neoBox';
  body.appendChild(neoBox);
  neoBox.onclick = hideBox;

  node = document.getElementById('enlarge');
  node.onmousedown = clickButton;
  node.onmouseup = clickButton;

  node = document.getElementById('inquire');
  node.onmousedown = clickButton;
  node.onmouseup = clickButton;
}

function changePhoto() {
  var file, node = document.getElementById('preview');

  file = this.src.replace('thumbs', 'photos');
  node.src = file;
  node.scrollIntoView();
  return false;
}

function clickButton() {
  var W = this.offsetWidth;

  if (this.style.paddingTop === '1px') {
    this.style.height = '26px';
    this.style.paddingTop = 0;
    this.style.paddingLeft = 0;
    this.style.width = W +'px';

    if (this.id == 'enlarge') {
      this.style.paddingLeft = '12px';
      this.style.width = (W - 12) +'px';
      showBox();
    } else if (this.innerHTML == 'Search') {
      validateSearch();
    } else if (this.innerHTML == 'Submit') {
      validateInquire();
    } else {
      document.location = 'inquire.php'+ document.location.search;
    }
  } else {
    this.style.height = '25px';
    this.style.paddingTop = '1px';
    this.style.paddingLeft = '2px';
    this.style.width = (W - 2) +'px';

    if (this.id == 'enlarge') {
      this.style.paddingLeft = '13px';
      this.style.width = (W - 13) +'px';
    }
  }
}

function showBox() {
  var node = document.getElementById('preview');
  var file = node.src.replace('photos', 'big');
  var load = new Image();
  node = document.createElement('span');
  neoBox.appendChild(node);

  load.onload = function() {
    var text = 'Click anywhere to close this window.';
    neoBox.innerHTML = "";

    var ele = document.createElement('img');
    ele.alt = ele.title = text;
    ele.src = this.src;
    ele.style.height = this.height;
    ele.style.width = this.width;
    neoBox.appendChild(ele);

    ele = document.createElement('div');
    ele.innerHTML = text;
    neoBox.appendChild(ele);
  };

  load.src = file;
  neoBox.style.display = 'block';
  neoBox.scrollIntoView();
  setDimensions();
}

function hideBox() {
  neoBox.innerHTML = "";
  setDimensions();
  neoBox.style.display = 'none';
}

function setDimensions() {
  var doc = document.documentElement;
  if (doc == null) { return; }

  if (typeof neoBox.style.minHeight === 'string') {
    neoBox.style.minHeight = (doc.scrollHeight >= doc.clientHeight) ? doc.scrollHeight +'px' : 0;
    neoBox.style.minWidth = (doc.scrollWidth >= doc.clientWidth) ? doc.scrollWidth +'px' : 0;
  } else {
    neoBox.style.height = Math.max(doc.scrollHeight, doc.clientHeight) +'px';
    neoBox.style.width = Math.max(doc.scrollWidth, doc.clientWidth) +'px';
  }
}

function validateSearch() {
  var form = document.forms[0];

  if (!form.parm.value.trim()) {
    alert("Please enter the name of the ship You're looking for.");
    form.parm.focus();
    return false;
  }
  form.submit();
}

function validateInquire() {
  var form = document.forms[0];

  if (!form.fullname.value.trim()) {
    alert('Please enter Your name.');
    form.fullname.focus();
    return false;
  }
  if (!validateAddress(form.email)) { return false; }
  form.submit();
}

function validateContact() {
  var form = document.forms[0];

  if (!form.fullname.value.trim()) {
    alert('Please enter Your name.');
    form.fullname.focus();
    return false;
  }
  if (!validateAddress(form.email)) { return false; }

  if (!form.comments.value.trim()) {
    alert('Please enter Your comment/question.');
    form.comments.focus();
    return false;
  }
  form.submit();
}

function validateAddress(node) {
  if (!node.value.trim()) {
    alert('Please enter Your E-Mail address.');
    node.focus();
    return false;
  }
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  if (filter.test(node.value)) return true;

  node.select();
  alert('This is not a valid E-Mail address.');
  return false;
}

String.prototype.trim = function() { return this.replace(/^\s+/,"").replace(/\s+$/,"").replace(/  +/g,"") }