var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isOpera5 = (navigator.appVersion.indexOf("MSIE 5") != -1 && navigator.userAgent.indexOf("Opera 5") != -1) ? true : false;
var isOpera6 = (navigator.appVersion.indexOf("MSIE 5") != -1 && navigator.userAgent.indexOf("Opera 6") != -1) ? true : false;
var isN6 = (navigator.userAgent.indexOf("Gecko") != -1);
var isN4 = (document.layers) ? true : false;
var isMac = (navigator.userAgent.indexOf("Mac") != -1);
var isIE = (document.all && !isOpera && (!isMac || navigator.appVersion.indexOf("MSIE 4") == -1)) ? true : false;
// child window section
var oWin = null;
var winX = 0, winY = 0;
var winWidth = 690, winHeight = 532, winOffsetX = 0, winOffsetY = 0;
var defaultURL = "/wait.html";
var winTarget = null;
var winURL = defaultURL;

function open_child_win(sURL)
{
  var hasWin = false;
  var oldURL = winURL;
  if (sURL != "") {
    winURL = sURL;
  } else {
    winURL = defaultURL;
  }
  if (oWin != null && !oWin.closed) {
    hasWin = true;
    oWin.moveTo(winX + winOffsetX, winY + winOffsetY);
    if (oldURL != sURL) {
      oWin.document.location.href = winURL;
    }
  }
  if (!hasWin) {
    if (isIE || isOpera6) {
      oWin = window.open(winURL, "pf_child_win", "toolbar=0,scrollbars=yes,status=no,resizable=1,width=" + winWidth + ",height=" + winHeight + ",left=" + (winX + winOffsetX) + ",top=" + (winY + winOffsetY));
    } else {
      oWin = window.open(winURL, "pf_child_win", "toolbar=0,scrollbars=yes,status=no,resizable=1,width=" + winWidth + ",height=" + winHeight + ",screenx=" + (winX + winOffsetX) + ",screeny=" + (winY + winOffsetY));
    }
  }
  oWin.focus();
}

function fnGalleryOver(oTd)
{
  oTd.className = 'clsGalleryOver';
}

function fnGalleryOut(oTd)
{
  oTd.className = 'clsGalleryOut';
}

function fnZoomThumbnail(o)
{
  if (null != o) {
    sThumbnailerURL = o.src;
    sMainURL = sThumbnailerURL.replace(".tn.", ".");
    open_child_win(sMainURL);
  }
}

function fnZoomThumbnailer(o)
{
  if (null != o) {
    sThumbnailerURL = o.src;
    sMainURL = sThumbnailerURL.replace(".tn.", ".");
    sZoomURL = "/zoom.php?img=" + sMainURL;
    open_child_win(sZoomURL);
  }
}

function fnZoomThumbnailerWithText(o, sText)
{
  if (null != o) {
    sThumbnailerURL = o.src;
    sMainURL = sThumbnailerURL.replace(".tn.", ".");
    sZoomURL = "/zoom.php?img=" + sMainURL;
    if (sText != "") {
      var nTextLength = sText.length;
      for(i = 0; i <= nTextLength; i++){
        if(sText.substring(i, i + 1) == " "){
          sText = sText.substring(0, i) + "+" + sText.substring(i + 1, nTextLength);
        }
      }
      sZoomURL = sZoomURL + "&alt=" + sText;
    }
    open_child_win(sZoomURL);
  }
}

// common section
function mailto(domain, user)
{ 
  document.location.href = "mailto:" + user + "@" + domain;
}

// section for use in gallery popups
var message = "The entire content on this site is protected by copyright, trademark rights and database rights.\nNo reproduction without the consent of the relevant owner.";

function clickIE4()
{
  if (event.button == 2){
    return false;
  }
}

function clickNS4(e)
{
  if (document.layers || document.getElementById && !document.all){
    if (e.which == 2 || e.which == 3){
      alert(message);
    }
  }
}

function openImageWindow(imageSrc)
{
  sContent = "";
  sContent += "<html><head><title>PicViewer</title>\n";
  sContent += "<BASE HREF='http://www.diving.ee/'/>\n";
//  sContent += "<script>function fnResizeWindow(){window.resizeTo(document.images[0].width + 22,document.images[0].height + 48);}</script></head>\n";
  sContent += "<script>function fnResizeWindow(){window.resizeTo(document.images[0].width + 22,document.images[0].height + 72);}</script></head>\n";
  sContent += "<body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>\n";
  sContent += "<script>\n";
  sContent += "document.write('<div align=center>');\n";
  sContent += "document.write('<table cellpadding=5 cellspacing=0 border=0>');\n";
  sContent += "document.write('<tr><td><img id=\\'popup_img\\' src=\\'" + imageSrc + "\\' onLoad=\\'fnResizeWindow();\\' galleryimg=\\'no\\'></td></tr>');\n";
  sContent += "document.write('</table>');\n";
  sContent += "document.write('</div>');\n";
  sContent += "</script></body></html>\n";
  var _parms = 'directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=yes';
  var _windowName = "PicViewer";
  winPopup = window.open("", _windowName, _parms);
  winPopup.document.open();
  winPopup.document.write(sContent);
  if (winPopup.document.layers){
    winPopup.document.captureEvents(Event.MOUSEDOWN);
    winPopup.document.onmousedown = clickNS4;
  } else if (winPopup.document.all && !winPopup.document.getElementById){
    winPopup.document.onmousedown = clickIE4;
  }
  winPopup.document.oncontextmenu = new Function("return false");
  winPopup.document.close();
  winPopup.focus();
}

function openGalleryWindow(o)
{
  var imageSrc = "/blob.php?" + o.id.substring(3);
  var image_text = o.title;
  var gallery_URL = new Array();
  var gallery_text = new Array();
  var current_index = 0;
  sName = o.name;
  if (sName.substring(0, 7) == "GALLERY") {
    collection = document.getElementsByName(sName);
    if (collection) {
      for (i = 0; i < collection.length; i++) {
        oItem = collection[i];
        gallery_URL[i] = "/blob.php?" + oItem.id.substring(3);
        gallery_text[i] = oItem.title;
        if (o == oItem) current_index = i;
      }
    }
  }
  sContent = "";
  sContent += "<html><head><title>PicViewer</title>\n";
  sContent += "<BASE HREF='http://www.diving.ee/'/>\n";
  sContent += "<style>.popup_text {color: #9c9c9c; font-family: Arial, Helvetica, sans-serif; font-size: 9px; font-weight: normal; text-decoration: none}</style>\n";
  sContent += "<script>\n";
  sContent += "var source_URL = new Array();\n";
  sContent += "var source_text = new Array();\n";
  for (i = 0; i < gallery_URL.length; i++) {
    sContent += "source_URL[" + i + "] = '" + gallery_URL[i] + "';\n";
    sContent += "source_text[" + i + "] = '" + gallery_text[i] + "';\n";
  }
  sContent += "var current_index = " + current_index + ";\n";
  sContent += "function fnNextImage(){new_index = current_index + 1; if(new_index >= source_URL.length) new_index = 0; return fnSelectImage(new_index);}\n";
  sContent += "function fnPreviousImage(){new_index = current_index - 1; if(new_index < 0) new_index = source_URL.length - 1; return fnSelectImage(new_index);}\n";
  sContent += "function fnSelectImage(this_index){oImage = document.getElementById('popup_img'); if(oImage) oImage.src = source_URL[this_index]; oCaption = document.getElementById('popup_text'); if(oCaption) oCaption.innerHTML = source_text[this_index]; current_index = this_index; return false;}\n";
  sContent += "function fnResizeWindow(){window.resizeTo(document.images[0].width + 22,document.images[0].height + 122);}</script></head>\n";
  sContent += "<body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0>\n";
  sContent += "<script>\n";
  sContent += "document.write('<div align=center>');\n";
  sContent += "document.write('<table cellpadding=0 cellspacing=0 border=0>');\n";
  sContent += "document.write('<tr height=5><td colspan=3></td></tr>');\n";
  sContent += "document.write('<tr><td colspan=3><img id=\\'popup_img\\' src=\\'" + imageSrc + "\\' onLoad=\\'fnResizeWindow();\\' galleryimg=\\'no\\'></td></tr>');\n";
  sContent += "document.write('<tr height=2><td colspan=3></td></tr>');\n";
  sContent += "document.write('<tr><td colspan=3 align=center id=\\'popup_text\\' class=\\'popup_text\\'>" + image_text + "</td></tr>');\n";
  if (gallery_URL.length > 1) {
    sContent += "document.write('<tr height=33><td align=right><a href=\\'javascript:;\\' onClick=\\'return fnPreviousImage();\\'><img src=\\'/images/theme/prev.gif\\' width=32 height=32 border=0 alt=\\'previous image\\'></a></td>');\n";
    sContent += "document.write('<td align=center><a href=\\'javascript:;\\' onclick=\\'window.close()\\'><img src=\\'/images/theme/close.gif\\' width=32 height=32 border=0 alt=\\'close this window\\'></a></td>');\n";
    sContent += "document.write('<td align=left><a href=\\'javascript:;\\' onclick=\\'return fnNextImage();\\'><img src=\\'/images/theme/next.gif\\' width=32 height=32 border=0 alt=\\'next image\\'></a></td></tr>');\n";
  }
  sContent += "document.write('<tr height=3><td colspan=3></td></tr>');\n";
  sContent += "document.write('</table>');\n";
  sContent += "document.write('</div>');\n";
  sContent += "</script></body></html>\n";
  var _parms = 'directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=yes';
  var _windowName = "PicViewer";
  winPopup = window.open("", _windowName, _parms);
  winPopup.document.open();
  winPopup.document.write(sContent);
  if (winPopup.document.layers){
    winPopup.document.captureEvents(Event.MOUSEDOWN);
    winPopup.document.onmousedown = clickNS4;
  } else if (winPopup.document.all && !winPopup.document.getElementById){
    winPopup.document.onmousedown = clickIE4;
  }
  winPopup.document.oncontextmenu = new Function("return false");
  winPopup.document.close();
  winPopup.focus();
}

