
// ------  Browser Detection 

IE = false; NN = false; IE4 = false; IE6 = false; NN4 = false; DOM = false; MAC = false;
if (document.all) IE = true;
if (navigator.appVersion.toLowerCase().indexOf('msie 4') != -1) IE4 = true;
if (navigator.appVersion.toLowerCase().indexOf('msie 6') != -1) IE6 = true;
if (navigator.appName.toLowerCase().indexOf('netscape') != -1) {
	NN = true;
	if (parseInt(navigator.appVersion) == 4) NN4 = true;
	if (parseInt(navigator.appVersion) == 5) DOM = true;
}
(NN && DOM) ? NN6 = true : NN6 = false;
if (IE6) DOM = true;
if (navigator.platform.toLowerCase().indexOf('mac') != -1) MAC = true;

// ------  Environment Detection 

var windowWidth; var windowHeight; var windowScrollX; var windowScrollY;
function detectEnvironmentProperties() {
	windowWidth = (NN) ? window.innerWidth : document.body.clientWidth;
	windowHeight = (NN) ? window.innerHeight : document.body.clientHeight;
	windowScrollX = (NN) ? window.pageXOffset : document.body.scrollLeft;
	windowScrollY = (NN) ? window.pageYOffset : document.body.scrollTop;
}

// ------  Bookmark functions

function addBookmark(url, title) {
 	if (IE4 || DOM) {
		window.external.AddFavorite(url,title);
		writeCookie('bookmark', 'true');
	} 
}

// ------  Url functions

function parseValueFromUrl(name) {
	var url = document.location.href;
	var value = null;
	if(url.indexOf('?') != -1) {
		parameters = url.substring(url.indexOf('?') + 1, url.length);
		if(parameters.lastIndexOf('#') != -1) parameters = parameters.substring(0, parameters.lastIndexOf('#'));
		parameters = parameters.split('&');
		for (i=0; i<parameters.length; i++) {
			equalsloc = parameters[i].indexOf('=');
			parameterName = parameters[i].substring(0, equalsloc);
			parameterValue = unescape(parameters[i].substring(equalsloc + 1, parameters[i].length));
			if(name==parameterName) value = parameterValue;
		}
	}
	return value;
}
	
// ------  Window functions
	
function openWindow(url, name, w, h, menubar, scrollbars) {

	var windowLeft = getCenterY() - parseInt(h/2);
	var windowTop = getCenterY() - parseInt(h/2);

	var parameters  = 'toolbar=0, location=0, menubar=' + menubar + ', scrollbars=' + scrollbars + ', width=' + w + ', height=' + h + ', top=' + windowTop + ', left=' + windowLeft + ', screenY=' + windowTop + ', screenX=' + windowLeft;

	window.open(url, name, parameters);
}

// ------ Image functions


function getImageLeft(img, ly) {
	(ly != null) ? layerloc = getLayerLeft(ly) : layerloc = 0;
	loc = element(img).offsetLeft;
	if (!NN6) loc += layerloc;
	return parseInt(loc);
}

function getImageTop(img, ly) {
	(ly != null) ? layerloc = getLayerTop(ly) : layerloc = 0;
	loc = element(img).offsetTop;
	if (!NN6) loc += layerloc;
	return parseInt(loc);
}

function getImageSource(img, ly) {
	return element(img).src;
}

function cacheImages(imgList, path) {
	if (path == null) path = '';
	var images = new Array();
	for (var i=0; i<imgList.length; i++) {
		images[i] = new Image();
		images[i].src = path + imgList[i];
	}
}	

// ------ Layer functions

function writeToLayer(ly, str) {
	element(ly).innerHTML = str + '\n';
}

function getLayerContent(ly) {
	return element(ly).innerHTML;
}

function getLayerLeft(ly) {
	return parseInt(element(ly).offsetLeft);
}

function getLayerTop(ly) {
	return parseInt(element(ly).offsetTop);
}

function getLayerClipWidth(ly) {
	return parseInt(element(ly).style.width);
}

function getLayerClipHeight(ly) {
	return parseInt(element(ly).style.height);
}

function getLayerWidth(ly) {
	return parseInt(element(ly).offsetWidth);
}

function getLayerHeight(ly) {
	return parseInt(element(ly).offsetHeight);
}

function moveLayer (ly, x, y) {
	element(ly).style.left = x + 'px';
	element(ly).style.top = y + 'px';
}	

function clipLayer(ly, l, r, t, b) {
	element(ly).style.clip = 'rect(' + t + 'px ' + r + 'px ' + b + 'px ' + l + 'px)';
} 

function simpleClipLayer(ly, w, h) {
	element(ly).style.clip = 'rect(0px ' + w + 'px ' + h + 'px 0px)';
} 

function scaleLayer(ly, w, h) {
	element(ly).style.width = w + 'px';
	element(ly).style.height = h + 'px';
}

function showLayer(ly) {
	element(ly).style.visibility = 'visible';
}

function hideLayer(ly) {
	element(ly).style.visibility = 'hidden';
}

function isVisible(ly) {
	return (element(ly).style.visibility == 'visible');
}

// ------ String functions

function replaceChars(str, oldChar, newChar) {
	var newStr = '';
	for (var i=0; i < str.length; i++) {
		(str.charAt(i) == oldChar) ? newStr += newChar : newStr += str.charAt(i);
	}
	return newStr;
}

function toId(str) {		// filter 'bad' characters and make a neat id
	var str = str.toLowerCase();
	var badChars = '\'.-+=\\\/" &èëéêìíîïâãàáäöõôóòùúûü';
	var newStr = '';
	for(var i=0; i<str.length; i++) {
		if (badChars.indexOf(str.charAt(i)) == -1) newStr += str.charAt(i);
	}
	return newStr;
}

function buildPath(level) {
	var path = '';
	for(var i=0; i<level; i++) {
		path += '../';
	}
	return path;
}

// ------ Layer Object

function Layer(id) {
	this.id = id;
	this.pos = new Vector();
	this.size = new Vector();
	
	this.init = Layer_init;
	this.hide = Layer_hide;
	this.show = Layer_show;
	this.move = Layer_move;
	this.clip = Layer_clip;
}

function Layer_init() {
	this.pos.set(getLayerLeft(this.id), getLayerTop(this.id));
	this.size.set(getLayerWidth(this.id), getLayerHeight(this.id));
}

function Layer_hide() {
	hideLayer(this.id);
}

function Layer_show() {
	showLayer(this.id);
}

function Layer_move(x, y) {
	moveLayer(this.id, x, y);
}

function Layer_clip(l, r, t, b) {
	clipLayer(this.id, l, r, t, b);
}

// ------ Vector Object (or point)

function Vector() {
	this.set = Vector_set;
	this.absDistance = Vector_absDistance;
}

function Vector_set(x, y) {
	this.x = x;
	this.y = y;
}

function Vector_absDistance() {
	return Math.sqrt((this.x * this.x) + (this.y * this.y));;
}

function getVectorDistance(v1, v2) {
	var hDist = v1.x - v2.x;
	var vDist = v1.y - v2.y;
	var distance = Math.sqrt((hDist * hDist) + (vDist * vDist));
	return distance;
}

// ------ HyperLink Object

function HyperLink(name, url, target) {
	this.name = name;
	this.url = url;
	this.target = target;
}

// ------ Other functions

function getCenterX() {												// get horizontal center of screen
	return parseInt(screen.width / 2);
}

function getCenterY() {												// get vertical center of screen
	return parseInt(screen.height / 2);
}

function element(id) {
	var e;
	IE4  ? e = document.all[id] : e = document.getElementById(id);
	return e; 
}

function pass(func) {
	eval(func);
}

// ------ Netscape Resize BugFix 

function reloadPage(init) {
 	if (init==true) with (navigator) {
		if (NN4) {pgW=innerWidth; pgH=innerHeight; onresize=reloadPage;}
	} else if (innerWidth!=pgW || innerHeight!=pgH) {
		location.reload();
	}
}
reloadPage(true);





