
  Prototype.Browser.IE6=Prototype.Browser.IE &&
  parseInt(navigator.userAgent.substring
  (navigator.userAgent.indexOf("MSIE")+5))==6;
  Prototype.Browser.IE7=Prototype.Browser.IE && !Prototype.Browser.IE6;

  var ship = new function() {

    var tabPages = new Array();
    var tabs = new Array();
    var activeTabIndex = 0;

    var thumbTimeoutId = null;
    var thumbs = new Array();
    var activeThumb = 0;
    var thumbDelay = 3000;

    var overlay = null;

    this.init = function() {

      // Tabs
      tabPages = $$('.tabContentInner');
      $$('a.tabActive, a.tabInactive').each(function(elm) {
        var i = tabs.length;
        tabs[i] = elm;
        elm.observe('click', function() {
          handleTabClick(i);
        });
      });

      // Thumbs
      thumbs = $$('a.shipThumb');
      $A(thumbs).each(function(elm, i) {
        elm.observe('mouseout', handleThumbMouseOut);
        elm.observe('mouseover', function() {
          handleThumbMouseOver(i);
        });
      });
      thumbTimeoutId = window.setTimeout(handleThumbChange, thumbDelay);

      // Cabin Types
      var re = /\(([^)]+)\)$/;
      $$('.cabinType h3').each(function(elm) {

        var id = re.exec(elm.innerHTML)[1];

        var opt = new Element('OPTION');
        opt.value = id;
        opt.innerHTML = elm.innerHTML;
        $('cabinDropdown').appendChild(opt);

        elm.up('DIV').id = id;
      });

      // Offer grids
      $$('table.grid tr td a').each(function(elm) {
        elm.observe('click', handleEnquireLinkClick);
      });

      $$('.enquireLink').each(function(elm) {
        elm.observe('click', handleEnquireMainLinkClick);
      });

      $('cabinDropdown').observe('change', handleCabinTypeChange);
      $('findCruiseButton').observe('click', handleFindCruiseClick);
    }

    function handleCabinTypeChange() {
      var id = this.getValue();
      Effect.ScrollTo(id);
    }

    function handleThumbMouseOut() {
      thumbTimeoutId = window.setTimeout(handleThumbChange, thumbDelay);
    }

    function handleThumbMouseOver(thumbIndex) {
      window.clearTimeout(thumbTimeoutId);
      changeThumb(activeThumb, thumbIndex);
    }

    function handleFindCruiseClick() {

      if(overlay == null) {
        overlay = new Element('DIV');
        overlay.id = 'shipOverlay';
        overlay.setStyle({height: document.viewport.getHeight() + document.body.scrollHeight +  'px', display: 'none'});
        $$('body')[0].appendChild(overlay);
        overlay.observe('click', handleOverlayClick);
      }

      if(!Prototype.Browser.IE7 && !Prototype.Browser.IE6) Effect.Appear(overlay, {duration: 0.3, from: 0.0, to: 0.6});
      Effect.ScrollTo('header');
    }

    function handleOverlayClick() {
      Effect.Fade(overlay, {duration: 0.3});
    }

    function handleThumbChange() {

      if(activeThumb == thumbs.length-1) {
        changeThumb(activeThumb, 0);
      }
      else {
        changeThumb(activeThumb, activeThumb+1);
      }

      thumbTimeoutId = window.setTimeout(handleThumbChange, thumbDelay);
    }

    function changeThumb(prev, next) {
      thumbs[prev].removeClassName('shipThumbActive');

      thumbs[next].addClassName('shipThumbActive');
      $$('a.shipPreview img')[0].src = thumbs[next].href;
      $$('a.shipPreview')[0].href = thumbs[next].href;
      activeThumb = next;
    }

    function handleTabClick(tabIndex) {

      tabs[activeTabIndex].className = 'tabInactive';
      tabPages[activeTabIndex].hide();
      tabs[tabIndex].className = 'tabActive';
      tabPages[tabIndex].show();
      activeTabIndex = tabIndex;
      return false;
    }

    function handleEnquireMainLinkClick(e) {

      var ship = $$('#tabContent H1')[0].innerHTML;

      var queryString = 'ship=' + encodeURIComponent(ship);
      var url = "http://www2.lowcostcruising.com/enquiry/sendEnquiry.php?" + queryString;
      pageTracker._getLinkerUrl(url);
      var newwin = window.open(url, "newwin", "width=600,height=550,toolbar=false,locationbar=false,directories=false,status=false,menubar=false,scrollbars=true,resizable=true,copyhistory=false");
      newwin.focus();

      e.preventDefault();
      return false;
    }

    function handleEnquireLinkClick(e) {

      var ship = $$('#tabContent H1')[0].innerHTML;

      var sailDate = this.up('tr').cells[0].innerHTML;
      var sailNights = this.up('tr').cells[1].innerHTML;
      var destination = this.up('tr').cells[2].innerHTML;
      var insidePrice = this.up('tr').cells[3].innerHTML;
      var outsidePrice = this.up('tr').cells[4].innerHTML;
      var balconyPrice = this.up('tr').cells[5].innerHTML;
      var reference = this.up('tr').cells[6].innerHTML;

      var queryString = 'ship=' + encodeURIComponent(ship) + '&sailDate=' + encodeURIComponent(sailDate) + '&sailNights=' + encodeURIComponent(sailNights) + '&destination=' + encodeURIComponent(destination) + '&insidePrice=' + encodeURIComponent(insidePrice) + '&outsidePrice=' + encodeURIComponent(outsidePrice) + '&balconyPrice=' + encodeURIComponent(balconyPrice) + '&reference=' + encodeURIComponent(reference);
      var url = "http://www2.lowcostcruising.com/enquiry/sendEnquiry.php?" + queryString;
      pageTracker._getLinkerUrl(url);
      var newwin = window.open(url, "newwin", "width=600,height=550,toolbar=false,locationbar=false,directories=false,status=false,menubar=false,scrollbars=true,resizable=true,copyhistory=false");
      newwin.focus();

      e.preventDefault();
      return false;
    }
  }

  Event.observe(window, 'load', ship.init);
