
//Define global variables
var firstTime = true;
var what = null;
var check = false;

function init(){
	// Running Init
	if (document.layers) {
		//  alert ("Running Netscape 4");
		layerRef="document.layers";
		styleSwitch="";
		visibleVar="show";
		what ="ns4";
	}
	else if(document.all){
		//  alert ("Running IE");
		layerRef="document.all";
		styleSwitch=".style";
		visibleVar="visible";
		what ="ie";
	}
	else if(document.getElementById){
		//  alert ("Running Mozilla");
		layerRef="document.getElementByID";
		styleSwitch=".style";
		visibleVar="visible";
		what="moz";
	}
	else{
		//  alert("Older than 4.0 browser.");
		what="none";
	}
	
	check = true;
}

function getLayerStyleRef(layerName){
	if(firstTime){
			init();
			firstTime = false;
	}
	if(check){
		if (what =="none"){
			return null;
		}
		else if (what == "moz"){
			return document.getElementById(layerName).style;
		}
		else{
			return eval(layerRef+'["'+layerName+'"]'+styleSwitch);
		}
	}
	else {// alert ("Please wait for the page to finish loading.");
		return null;
	}
}

function getLayerRef(layerName){
	if(firstTime){
			init();
			firstTime = false;
	}
	if(check){
		if (what =="none"){
			return null;
		}
		else if (what == "moz"){
			return document.getElementById(layerName);
		}
		else{
			return eval(layerRef+'["'+layerName+'"]');
		}
	}
	else {// alert ("Please wait for the page to finish loading.");
		return null;
	}
}

function showError(){
	window.alert("Veuillez télécharger une version récente de votre navigateur, il n'est pas supporté!");
}

// Turns the layers on and off
/*function showLayer(layerName){
	if(check){
		if (what =="none"){
			return;
		}
		else if (what == "moz"){
			document.getElementById(layerName).style.display="block";
		}
		else{
		  eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.display="block"');
		}
 }
	else {// alert ("Please wait for the page to finish loading.");
		return;
	}
}

function hideLayer(layerName){
	if(check){
		if (what =="none"){
			return;
		}
		else if (what == "moz"){
			document.getElementById(layerName).style.display="none";
		}
		else{
		  eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.display="none"');
		}
	}
	else {// alert ("Please wait for the page to finish loading.");
		return;
	}
}*/

function showLayer(layerName){
	var ref = getLayerStyleRef(layerName);
	if(ref == null){
		showError();
	}
	else{
		ref.display="block";
	}
}

function hideLayer(layerName){
	var ref = getLayerStyleRef(layerName);
	if(ref == null){
		showError();
	}
	else{
		ref.display="none";
	}
}

function switchLayerVisibility(layerName){
	var ref = getLayerStyleRef(layerName);
	if(ref == null){
		showError();
	}
	else{
		if(ref.display == "block"){
			hideLayer(layerName);
		}
		else{
			showLayer(layerName);
		}
	}
}

function setLayerOpacity(layerName, opacity_percent){
		var opacity;
		if(opacity_percent < 0){
			opacity = 0;
		}else if(opacity_percent > 100){
			opacity = 1;
		}else{
			opacity = (opacity_percent / 100);
		}
		var ref = getLayerStyleRef(layerName);
		if(ref == null){
			showError();
			return;
		}
		if(what == "moz"){
			ref.opacity = opacity;
		} else {
			//ref.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity_percent;
			ref.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity = " + opacity_percent + ")";
		}
}

function setLayerSize(layerName, newWidth, newHeight){
	var ref = getLayerStyleRef(layerName);
	if(ref == null){
		showError();
		return;
	}
	if(newWidth != null){
		ref.width = newWidth + 'px';
	}
	if(newHeight != null){
		ref.height = newHeight + 'px';
	}
}

function centerLayer(layerName){
	var ref = getLayerStyleRef(layerName);
	if(ref == null){
		showError();
		return;
	}
	var layerWidth = ref.width.toString().replace(/px/, '');
	var layerHeight = ref.height.toString().replace(/px/, '');
	//alert(layerName + ": " + layerWidth);
	ref.position = "absolute";
	if(what == "moz"){
		ref.top = ((window.innerHeight - layerHeight) / 2) + "px";
		ref.left = ((window.innerWidth - layerWidth) / 2) + "px";
		//alert("Top: " + ref.top + "| Left: " + ref.left);
	} else {
		ref.top = ((document.body.offsetHeight - layerHeight) / 2) + "px";
		ref.left = ((document.body.offsetWidth - layerWidth) / 2) + "px";
	}
}

function moveLayer(layerName, x, y){
	var ref = getLayerStyleRef(layerName);
	if(ref == null){
		showError();
		return;
	}
	var myLeft = ref.left.toString().replace(/px/, '');
	var myTop = ref.top.toString().replace(/px/, '');

	ref.position = "absolute";
	ref.left = eval(myLeft + x) + "px";
	ref.top = eval(myTop + y) + "px";
}

function setLayerSizeToWindow(layerName){
	var ref = getLayerStyleRef(layerName);
	if(ref == null){
		showError();
		return;
	}
	
	ref.position = "absolute";
	ref.top = "0px";
	ref.left = "0px";
	
	if(what == "moz"){
		ref.height = window.innerHeight + "px";
		ref.width = window.innerWidth + "px";
	} else {
		ref.height = document.body.offsetHeight + "px";
		ref.width = document.body.offsetWidth + "px";
	}
}

function set_focus(id){
	if(document.all != null){
		document.all[id].focus();
	}else{
		document.getElementById(id).focus();
	}
}

function popUpWin (url, win, width, height, options) {
	var leftPos = (screen.availWidth - width) / 2;
	var topPos = (screen.availHeight - height) / 2;
	options += 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
	return window.open(url, win, options);
}