/**
 * Open a popup window and move it to the center.
 *
 * @param winUrl        popup window url
 * @param winName       popup window name
 * @param features      popup window specifications
 */

function imagePopup(path) {
  var win=window.open("/templates/jsp/mjm/popup.jsp?path="+path, "imagePopupWindow", "scrollbars=yes,resizable=yes,width=420,height=620");
  // Maybe the user has a popup blocker.
  if (! win)
    alert("Sie m??ssen Ihren Popup-Blocker deaktivieren, damit Sie das Bild sehen k??nnen!");
}

/**
 * Resize an image popup to fit the image.
 */

function resizeImagePopup() {
  var image = document.getElementById("popupImage");
  var imageWidth = image.width;
  var imageHeight = image.height;
  var resizeWidth = imageWidth;
  var resizeHeight = imageHeight;
  // Check if image is wider than screen.
  if (imageWidth >= (screen.width - 150))
    resizeWidth = screen.width - 150;
  // Check if image is higher than screen.
  if (imageHeight >= (screen.height - 150))
    resizeHeight = screen.height - 150;
  // ie needs more space because he always shows the vertical scrollbar
  if (document.all)
    window.resizeTo(resizeWidth + 50, resizeHeight + 120);
  else
    window.resizeTo(resizeWidth + 20, resizeHeight + 100);

  centerImagePopup();
}

/**
 * Center an image popup to the visible screen.
 */

function centerImagePopup() {
  var pixelX=document.all? window.document.body.clientWidth : window.innerWidth;
  var pixelY=document.all? window.document.body.clientHeight : window.innerHeight;
  var left = parseInt(screen.width/2-pixelX/2);
  var top = parseInt(screen.height/2-pixelY/1.3);
  // Check for negative values.
  if (left < 0)
    left = 0;
  if (top < 0)
    top = 0;
  window.moveTo(left, top);
}


/**
 * Return a form by it's id.
 *
 * @param id    form's id
 */

function getForm(id) {
		if (checkMandatories(id, 'Please fill in the * '))
			return document.getElementById(id);
}


/**
 * Set an active stylesheet.
 *
 * @param title		the title attribute of the stylesheet
 */

function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

/**
 * Get the name of the active stylesheet.
 *
 * @returns title	the name of the active stylesheet or null if not selected
 */

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
      return a.getAttribute("title");
  }
  return null;
}

/**
 * Get the name of the preferred stylesheet. Preferred stylesheets
 * are of relation stylesheet and have a title attribute set.
 *
 * @returns title	the title of the preferred stylesheet or null if not available
 */

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title"))
      return a.getAttribute("title");
  }
  return null;
}

/**
 * Create a cookie. 
 *
 * @param name		name of the cookie
 * @param value		value to save inside
 * @param days		number of days until cookie is erased
 */

function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else
    var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

/**
 * Read a cookie. 
 *
 * @param name		name of the cookie
 */

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
  return null;
}

/**
 * Erase a cookie by setting it's expiry date to the day before.
 *
 * @param name		name of the cookie
 */

function eraseCookie(name) {
  createCookie(name,"",-1);
}

/**
 * onload() event handler.
 */

init = function () {
  //fontSize('init');
  // Set the font size either from the cookie or the default stylesheet.
  var cookie = readCookie("fontSize");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  // Start the menu in internet explorer.
  if (document.all)
    startList();
  // In project preview swap the first image
  if (document.getElementById("image1")) {
    swap('image1');
  }
}
window.onload = init;

/**
 * onunload() event handler. 
 */
 
exit = function() {
  // Save the active stylesheet in a cookie.
  var title = getActiveStyleSheet();
  createCookie("fontSize", title, 365);  
}
window.onunload = exit;

