
// ------  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) {
	var windowLeft = getCenterX() - parseInt(w/2);
	var windowTop = getCenterY() - parseInt(h/2);
	
	var parameters  = 'toolbar=0, location=0,menubar=' + menubar + ',width=' + w + ',height=' + h + ',top=' + windowTop + ',left=' + windowLeft;
	
	window.open(url, name, parameters);
}

// ------ Image functions

function changeImage(img, url, ly) {
	if (NN4) {
		(ly == null) ? document.images[img].src = url :	document.layers[ly].document.images[img].src = url;
	} else {
		element(img).src = url;
	}
}

function getImageLeft(img, ly) {
	(ly != null) ? layerloc = getLayerLeft(ly) : layerloc = 0;
	if (NN4) {
		(ly == null) ? loc = document.images[img].x :	loc = layerloc + document.layers[ly].document.images[img].x;
	} else {
		loc = element(img).offsetLeft;
		if (!NN6) loc += layerloc;
	}
	return parseInt(loc);
}

function getImageTop(img, ly) {
	(ly != null) ? layerloc = getLayerTop(ly) : layerloc = 0;
	if (NN4) {
		(ly == null) ? loc = document.images[img].y : loc = layerloc + document.layers[ly].document.images[img].y;
	} else {
		loc = element(img).offsetTop;
		if (!NN6) loc += layerloc;
	}
	return parseInt(loc);
}

function getImageSource(img, ly) {
	var src;
	if (NN4) {
		(ly == null) ? src = document.images[img].src : src = document.layers[ly].document.images[img].src;
	} else {
		src = element(img).src;
	}
	return src;
}

function chacheImages(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) {
	if (NN4) {
		document.layers[ly].document.open();
		document.layers[ly].document.write(str);
		document.layers[ly].document.close();
	} else {
		element(ly).innerHTML = str + '\n';
	}
}

function getLayerContent(ly) {
	var content = '';
	if (!NN4) content = element(ly).innerHTML;
	return content;
}

function getLayerLeft(ly, mask) {
	var loc = 0;
	if (NN4) {
		(mask == null) ? loc = document.layers[ly].x : loc = document.layers[mask].document.layers[ly].x;
	} else {
		loc = element(ly).offsetLeft;
	}
	return parseInt(loc);
}

function getLayerTop(ly, mask) {
	var loc = 0;
	if (NN4) {
		(mask == null) ? loc = document.layers[ly].y : loc = document.layers[mask].document.layers[ly].y;
	} else {
		loc = element(ly).offsetTop;
	}
	return parseInt(loc);
}

function getLayerWidth(ly, mask) {
	var w = 0
	if (NN4) {
		(mask == null) ? w = document.layers[ly].clip.width : w = document.layers[mask].document.layers[ly].clip.width;
	} else {
		w = element(ly).style.width;
	}
	return parseInt(w);
}

function getLayerHeight(ly, mask) {
	var h = 0
	if (NN4) {
		(mask == null) ? h = document.layers[ly].clip.height : h = document.layers[mask].document.layers[ly].clip.height;
	} else {
		h = element(ly).style.height;
	}
	return parseInt(h);
}

function moveLayer (ly, x, y, mask) {
	if (NN4) {
		if (mask == null) {
			document.layers[ly].left = x;
			document.layers[ly].top = y;
		} else {
			document.layers[mask].document.layers[ly].left = x;
			document.layers[mask].document.layers[ly].top = y;
		}
	} else {
		element(ly).style.left = x + 'px';
		element(ly).style.top = y + 'px';
	}
}	

function clipLayer(ly, l, r, t, b) {
	if (NN4) {
		document.layers[ly].clip.left = l;
		document.layers[ly].clip.right = r;
		document.layers[ly].clip.top = t;
		document.layers[ly].clip.bottom = b;
	} else {
		element(ly).style.clip = 'rect(' + t + 'px ' + r + 'px ' + b + 'px ' + l + 'px)';
	}
} 

function scaleLayer(ly, w, h) {
	if (NN4) {
		document.layers[ly].width = w;
		document.layers[ly].height = h;
	} else {
		element(ly).style.width = w + 'px';
		element(ly).style.height = h + 'px';
	}
}

function showLayer(ly) {
	NN4 ? document.layers[ly].visibility = true : element(ly).style.visibility = 'visible';
}

function hideLayer(ly) {
	NN4 ? document.layers[ly].visibility = false : element(ly).style.visibility = 'hidden';
}

function isVisible(ly) {
	var visibility = false;
	if (NN4) {
		if (document.layers[ly].visibility == 'show') visibility = true;
	} else {
		if (element(ly).style.visibility == 'visible') visibility = true;
	}
	return visibility;
}

// ------ 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;
}

// ------ 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(maskId) {
	if (maskId == null) {
		this.pos.set(getLayerLeft(this.id), getLayerTop(this.id));
		this.size.set(getLayerWidth(this.id), getLayerHeight(this.id));
	} else {
		this.pos.set(getLayerLeft(this.id, maskId), getLayerTop(this.id, maskId));
		this.size.set(getLayerWidth(this.id, maskId), getLayerHeight(this.id, maskId));
	}
}

function Layer_hide() {
	hideLayer(this.id);
}

function Layer_show() {
	showLayer(this.id);
}

function Layer_move(x, y, mask) {
	moveLayer(this.id, x, y, mask);
}

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
	if (IE || NN6) {
		return parseInt(screen.width / 2);
	} else {
		return parseInt(screen.availWidth / 2);
	}
}

function getCenterY() {												// get vertical center of screen
	if (IE || NN6) {
		return parseInt(screen.height / 2);
	} else {
		return parseInt(screen.availHeight / 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);




