// variables
var ver = 0;
var ie  = (document.all) ? true : false;
var mac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false;
if (ie) ver = parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf("MSIE") + 5, 5));

// characters for window id
var characters = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9");

var offsetWidth = 0;
var offsetHeight = 0;

// set browser offset
if (mac && ie) {
  if (ver < 4.6) {        //macintosh Internet Explorer 4.50 and previous
    offsetWidth = 16;
    offsetHeight = 14;
  }
}

// window class initialize function
// --------------------------------------------------------------------------------------------------
function Window(name) {
  // window object
  this.obj = null;

  // methods mapping
  this.open = windowObj_open;
  this.close = windowObj_close;
  this.focus = windowObj_focus;

  // properties
  this.moveurl = "/_images/spacer.gif";
  this.url = "/_images/spacer.gif";

  this.width = 500;
  this.height = 200;

  this.x = 0;
  this.y = 0;

  this.align = new align();

  this.copyhistory = false;
  this.directories = false;
  this.location = false;
  this.menubar = false;
  this.resizable = false;
  this.scrollbars = false;
  this.status = false;
  this.toolbar = false;


  if (typeof name != "undefined" || name != "") this.name = name;
  else this.name = getName();


}

// methods
// --------------------------------------------------------------------------------------------------
function windowObj_open(id) {
  check = false;

  if (this.obj != null) {
    if (this.obj.closed) check = true;
  } else {
    check = true;
  }

  if (true) {

    var parameters = "";
    parameters += "width=" + eval(this.width + offsetWidth) + ",";
    parameters += "height=" + eval(this.height + offsetHeight) +  ",";
    parameters += "copyhistory=" + boolToYN(this.copyhistory) + ",";
    parameters += "directories=" + boolToYN(this.directories) + ",";
    parameters += "location=" + boolToYN(this.location) + ",";
    parameters += "menubar=" + boolToYN(this.menubar) + ",";
    parameters += "resizable=" + boolToYN(this.resizable) + ",";
    parameters += "scrollbars=" + boolToYN(this.scrollbars) + ",";
    parameters += "status=" + boolToYN(this.status) + ",";
    parameters += "toolbar=" + boolToYN(this.toolbar);

    leftTopCorner(this);  // calculate left-top-corner of window

    this.obj = window.open(this.url, this.name, parameters);  // generate window
    this.obj.moveTo(this.cornerX, this.cornerY);          // place window
    //this.obj.document.location.href = this.url;

  } else {
    // set new content for window even it is open
    if (this.obj.document.location.href != this.url) this.obj.document.location.href = this.url;
    this.obj.focus();

  }
}

function windowObj_close () {
  if (this.obj) {
    this.obj.close();
  }
}

function windowObj_focus() {
  this.obj.focus();
}

// internal class functions
// --------------------------------------------------------------------------------------------------
function getName() {
  var key = "";

  for (i = 0; i < 9; i++) {
    key = key + characters[getRandom(61)];
  }

  return key;
}

function boolToYN(boolValue) {
  if (boolValue) {
    return "yes";
  } else {
    return "no";
  }
}

function leftTopCorner(windowObject) {
  windowObject.cornerX = 0;
  windowObject.cornerY = 0;

  // calculate x
  switch (windowObject.align.x) {
    case "center":
      windowObject.cornerX = Math.round(screen.availWidth / 2) - Math.round(windowObject.width / 2);
      break;
    case "right":
      windowObject.cornerX = screen.availWidth - windowObject.width - 10; // 10px min. caused by borders
      break;
  }
  windowObject.cornerX += windowObject.x;


  // calculate y
  switch (windowObject.align.y) {
    case "middle":
      windowObject.cornerY = Math.round(screen.availHeight / 2) - Math.round(windowObject.height / 2);
      break;
    case "bottom":
      windowObject.cornerY = screen.availHeight - windowObject.height - 30; // 30px min. caused by borders
      break;
  }
  windowObject.cornerY += windowObject.y;
}

function getRandom(base) {

  var ran1 = Math.round(Math.random() * base);
  var ran2Date = new Date();
  var ran2 = Math.round((ran2Date.getMilliseconds() / 1000) * base);
  var ran = ran1 + ran2;

  if (ran > base) ran = ran - base;

  return ran;
}
// sub classes
// --------------------------------------------------------------------------------------------------
function align() {
  this.x = "left";
  this.y = "top";
}