/*=========================================================================*\
|       window_class.js - Author : Norman Timmler        neteye°° GmbH      |
|                                                                           |
|                                                                           |
|       12/2000                                                             |
|                                                                           |
\*=========================================================================*/
// 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 windowCollection = new Array();
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 windowObj() {
	// window object
	this.obj = null;

	// methods mapping
	this.open = windowObj_open;
	this.close = windowObj_close;
	this.kill = windowObj_kill;
	this.focus = windowObj_focus;

	// properties
	this.opened = false;
	this.closed = false;

	this.moveurl = "about:blank";
	this.url = "about:blank";

	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;

	this.id = getID();
	this.name = getName();

	// object property functions (could only be read from new object)
	this.length = countID;
}

// methods
// --------------------------------------------------------------------------------------------------
function windowObj_open(id) {
	check = false;

	if (this.opened) {
		if (typeof this.obj == "object") {
			if (this.obj.closed) check = true;
		}
	} else {
		check = true;
	}

	if (check) {

		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.moveurl, this.name, parameters);	// generate window
		this.obj.moveTo(this.cornerX, this.cornerY);					// place window

		if (typeof this.obj == "object") {
			this.opened = true;
			this.closed = false;

			this.obj.document.location.href = this.url;
			this.obj.focus();
		} else {
			alert("Error while opening window!");
		}

	} 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.opened && !this.obj.closed) {
		this.obj.close();
		this.closed = true;
	}
	this.opened = false;
}

function windowObj_kill () {
	if (!this.opened) {
		windowCollection[this.id] = false;
		this.obj = null;
	} else {
		alert("Error, cannot kill window. Window is still open!");
	}
}

function windowObj_focus() {
	this.obj.focus();
}
// internal class functions
// --------------------------------------------------------------------------------------------------
function getID() {
	i = null;
	for (i=0; i <= windowCollection.length; i++) {
		if (!windowCollection[i]) {
			windowCollection[i] = true;
			return i;
		}
	}
	return i;
}

function getName() {
	var key = "";

	for (i = 0; i < 9; i++) {
		key = key + characters[getRandom(61)];
	}

	return key;
}

function countID() {
	var counter = 0;
	for (i=0; i <= windowCollection.length; i++) {
		if (windowCollection[i]) {
			counter++;
		}
	}
	return counter;
}

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";
}

