// global variables //
var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'content';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,message,type,autohide) {
  if(!type) {
	type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
	dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
	dialogheader.appendChild(dialogtitle);
	dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
	dialogclose.setAttribute('onclick','hideDialog()');
	dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
	dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = "AAPQIS is a project of the Aquaculture Management and Conservation Service (FIMA) of the Food and Agriculture Organization of the United Nation (FAO), Fisheries and Aquaculture Department. AAPQIS is designed to provide information on aquaculture development and aquatic animal biosecurity issues of relevance to national policy-makers concerned with aquaculture and aquatic biosecurity, aquatic animal heath experts (including national competent authorities) and the general aquaculture community.<BR><BR>In the face of declining or stagnant capture fisheries, aquaculture continues to expand globally, fueled by an increasing world population and the need for high protein food commodities.  Modern aquaculture is a relatively young and very dynamic sector, and is characterized by a great diversity of species cultured (fish, shellfish, molluscs and seaweeds), culture systems (extensive, semi-intensive, intensive; fresh, brackish and marine) and players (government, private sector, community). <BR><BR>The recent developments towards less restrictive trade, particularly through memberships in the World Trade Organization (WTO), have expanded international trade in live aquatic animals and their products. At the same time, this rapidly evolving situation has left policy-makers in many countries with the dilemma of how to reap the economic and social benefits of free trade while at the same time protecting national biodiversity; existing capture fisheries and aquaculture; human, animal and plant health and national and local economies and social systems from the sometimes devastating impacts caused by exotic diseases and pathogens that are spread by poorly regulated trade in live aquatic animals. AAPQIS was thus conceived and dedicated to providing an easily accessible, Web-based, entry point for access to information on aquatic animal biosecurity. <BR><BR>The information and links to resources provided by AAPQIS will assist policy-makers and other agencies and individuals in developing and implementing national policy and plans for aquatic animal health management.  It will also be a valuable resource to users with interests ranging from international and regional aquatic animal health issues, to aquaculturists and individual farm managers wanting to understand the international situation and improve on-farm biosecurity and management.";
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
	dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
	dialogclose.style.visibility = "visible";
  }
}

// hide the dialog box //
function hideDialog() {
  var dialog = document.getElementById('dialog');
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
	flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
	clearInterval(dialog.timer);
  }
}