var newWindow;

// returns the coordinates of an element by adding the offsets
function getCoordinates(element) {
  var coords = new Array(2);
  coords[0] = 0;
  coords[1] = 0;
  while(1==1) {
    coords[0] += element.offsetLeft;
    coords[1] += element.offsetTop;
    if(element.offsetParent) {
      element = element.offsetParent;
    } else {
      break;
    }
  }
  return coords;
}
// returns the absolute y coordinate of an element
function getCoordinateY(element) {
  var coords = getCoordinates(element);
  return coords[1];
}

// resizes the zoom window by placing the rightCorner element to right bottom
function resizeZoomWindow(deltaX, deltaY) {
  var elementImg = getElement('rightCorner');
  var coords = getCoordinates(elementImg);
  var rightCornerX = coords[0] + elementImg.width + deltaX;
  var rightCornerY = coords[1] + elementImg.height + deltaY;
  var width;
  var height;
  if (navigator.appName.indexOf("Microsoft") != -1) {
    width = document.body.clientWidth;
    height = document.body.clientHeight;
  } else {
    width = window.innerWidth;
    height = window.innerHeight;
  }
  var deltaX = rightCornerX - width;
  var deltaY = rightCornerY - height;
  window.resizeBy(deltaX, deltaY);
}
// sets the height of the first element based on the heights of the next elements
function resizeMaximum(elementId, heightOthers) {
  var height;
  /* IE has another way of providing the width and height */
  var appName = navigator.appName;
  if(appName.indexOf('Microsoft') != -1) {
    height = document.body.clientHeight;
  } else {
    height = window.innerHeight;
  }
  var scrollWidth = 15; /* maximum scroll width possible */
  height -= scrollWidth;
  var newHeight = height - heightOthers;
  var element = getElement(elementId);
  element.height = newHeight - getCoordinateY(element);
  window.scroll(0,0);
}

// sets the height of the first element based on the heights of the next elements
function resizeTo(elementId, elementIdTo) {
  var heightOthers = 0;
  var widthMax = 0;
  for(var indexElement = 2; indexElement < resizeTo.arguments.length; indexElement++) {
    var argument = resizeTo.arguments[indexElement];
    if('string' == typeof(argument)) {
      var element = getElement(argument);
      heightOthers += element.height;
      if(element.width > widthMax) {
        widthMax = element.width;
      }
    }
    else {
      heightOthers += argument;
    }
  }

  var element = getElement(elementId);
  var elementTo = getElement(elementIdTo);
  element.height = getCoordinateY(elementTo) - getCoordinateY(element) - heightOthers;
  window.scroll(0,0);
}

// sets the height of the first element based on the heights of the next elements
function resize(elementId, minHeight) {
  /* first get the total height of all the elements other than the first one*/
  var heightOthers = 0;
  var widthMax = 0;
  for(var indexElement = 2; indexElement < resize.arguments.length; indexElement++) {
    var argument = resize.arguments[indexElement];
    if('string' == typeof(argument)) {
      var element = getElement(argument);
      heightOthers += element.height;
      if(element.width > widthMax) {
        widthMax = element.width;
      }
    }
    else {
      heightOthers += argument;
    }
  }

  var width;
  var height;
  /* IE has another way of providing the width and height */
  var appName = navigator.appName;
  if(appName.indexOf('Microsoft') != -1) {
    width = document.body.clientWidth;
    height= document.body.clientHeight;
  } else {
    width = window.innerWidth;
    height = window.innerHeight;
  }
  var scrollWidth = 15; /* maximum scroll width possible */
  width -= scrollWidth;
  height -= scrollWidth;
  /* if the window width is less than the maximum width, then a horizontal scrollbar can appear and hence the height has to be further reduced */
  if(width < widthMax) {
    height -= scrollWidth;
  }
  if(height < minHeight) {
    height = minHeight;
  }
  var newHeight = height - heightOthers;
  document.getElementById(elementId).height = newHeight;
  window.scroll(0,0);
}

// sets the opacity for an element
function setOpacity(element, opacity) {
  if(opacity > 0) {
    element.style['visibility'] = 'visible';
    element.style['display'] = 'block';
  } else {
    element.style['visibility'] = 'hidden';
    element.style['display'] = 'none';
  }
  element.style['opacity'] = opacity / 100;
  element.style['MozOpacity'] = opacity / 100;
  element.style['filter'] = 'alpha(opacity='+opacity+')';
}

// sets the active text class adds Selected at the end
function activateA() {
  for(var indexA = 0; indexA < activateA.arguments.length; indexA++) {
    var a = getElement(activateA.arguments[indexA]);
    a.className += "Selected";
  }
}

// resets the active text class removes Selected from the end
function deactivateA() {
  for(var indexA = 0; indexA < deactivateA.arguments.length; indexA++) {
    var a = getElement(deactivateA.arguments[indexA]);
    a.className = a.className.replace( /Selected/g, "" );;
  }
}

// sets the active image adds .active before the extension
function activateImgs() {
  for(var indexImg = 0; indexImg < activateImgs.arguments.length; indexImg++) {
    var img = getElement(activateImgs.arguments[indexImg]);
    var stringArray = img.src.split(".");
    stringArray.splice(stringArray.length-1,0,'active');
    img.src = stringArray.join(".");
  }
}

// resets the active image removes .active before the extension
function deactivateImgs() {
  for(var indexImg = 0; indexImg < deactivateImgs.arguments.length; indexImg++) {
    var img = getElement(deactivateImgs.arguments[indexImg]);
    var stringArray = img.src.split(".");
    stringArray.splice(stringArray.length-2,1);
    img.src = stringArray.join(".");
  }
}

// preloads a list of images
function preloadImages() {
  for(var index = 0; index < preloadImages.arguments.length; index++ ) {
    var img = new Image();
    img.src = preloadImages.arguments[index];
  }
}

// find an element from the document
function getElement(idElement) {
  if( document.getElementById ) {
    return document.getElementById(idElement);
  }
}

// get a quoted string from string array
function quoteStringArray(arrayString) {
  var returnString = arrayString.toString();
  returnString = returnString.replace( /,/g, "', '" );
  returnString = "'" + returnString + "'";
  return returnString;
}

// returns a random index based on the current time
function getRandomIndex(noOfElements, duration) {
  var d = new Date();
  var time = (d.getMinutes() * 60 + d.getSeconds()) / duration
  return Math.round(time % noOfElements);
}

function openNewWindow(url) {
  var id = "popup";
  var width = 400;
  var height= 400;
  if(openNewWindow.arguments.length > 1) {
    id = openNewWindow.arguments[1];
    if(openNewWindow.arguments.length > 2) {
      width = openNewWindow.arguments[2];
      if(openNewWindow.arguments.length > 3) {
        height = openNewWindow.arguments[3];
      }
    }
  }
  var specs = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, titlebar=no, width=" + width + ", height=" + height;
  var newWindow = window.open(url, id, specs);
  newWindow.focus();
  return newWindow;
}

function printMainPage() {
  newWindow = window.open(getElement("mainPage").src,"print");
  newWindow.focus();
  setTimeout("newWindow.print()", 1000);
}

function setAnchor() {
  var indexAnchor;
  if((indexAnchor = location.href.indexOf('#')) != -1) {
    getElement("mainPage").src += location.href.substring(indexAnchor);
  }

}