// JavaScript Document

function createRequestObject() {
	var ro;
	if (window.XMLHttpRequest) ro = new XMLHttpRequest();// general
	else if (window.ActiveXObject) { //ie
		try { ro = new ActiveXObject("Msxml2.XMLHTTP"); } // ie7 +
		catch (e) { ro = new ActiveXObject("Microsoft.XMLHTTP"); } // ie 6 -
	} 
	return ro;
}
var http = createRequestObject();


function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number')windowHeight = window.innerHeight;
	else if (document.body && document.body.clientHeight) windowHeight = document.body.clientHeight;
	else  if (document.documentElement && document.documentElement.clientHeight)  windowHeight = document.documentElement.clientHeight;
	return windowHeight;
}	
function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') windowWidth = window.innerWidth;
	else if (document.body && document.body.clientWidth) windowWidth = document.body.clientWidth;
	else  if (document.documentElement && document.documentElement.clientWidth)  windowWidth = document.documentElement.clientWidth;
	return windowWidth;
}	
function getWindowBounds(){
	var avail_width = getWindowWidth() ;
	var avail_height = getWindowHeight() ;
	var obj = {"width":avail_width, "height":avail_height};
	return obj ;
}

var height_from_flash = 780 ;

function setPageHeight(heightPage) {
	height_from_flash = heightPage;
	redim();
}

function redim(){
	var height_goal;
	var width_goal;
	var table = document.getElementById("br_website");
	if(table){
		var height = getWindowHeight();
		if(height < height_from_flash) height_goal = height_from_flash + 'px' ;
		else height_goal = '100%' ;
		table.style.height = height_goal ;
		var width = getWindowWidth();
		if(width < 980) width_goal = 980 + 'px' ;
		else width_goal = '100%' ;
		table.style.width = width_goal ;
	}
	var table = document.getElementById("main_content");
	if(table){
		var height = getWindowHeight();
		if(height < height_from_flash) {height_goal = height_from_flash + 'px' ;}
		else height_goal = 'auto' ;
		table.style.height = 'auto' ;
		var width = getWindowWidth();
		if(width < 980) width_goal = 980 + 'px' ;
		else width_goal = '100%' ;
		table.style.width = '100%' ;
	}

	//var error_output = document.getElementById("error") ;
	//error_output.innerHTML = width_goal + ' * ' + height_goal;
	//error_output.innerHTML = height + ' <> ' + height_goal;
}

function showOverlay(){
	var overlay = document.getElementById('overlay');
	overlay.innerHTML = '<div id="overlay_background"></div>';
	var background = document.getElementById('overlay_background') ;
	if(!sBgColor) sBgColor = "#000000" ;
	background.style.backgroundColor = sBgColor ;
	overlay.innerHTML += '<div id="overlay_content"></div>' ;
	var content = document.getElementById('overlay_content');
	content.innerHTML = '<div id="overlay_close"><a href="javascript:void(0);" onclick="hideOverlay()"><img src="media/popins/commons/close.gif" alt="close" title="close" /></a></div><div id="overlay_tab"></div>' ;
	overlay.style.display = 'block' ;
	
	var ypix = window.scrollY;
	overlay.style.top = ypix + 'px' ;
	$("body").css("overflow", "hidden");
	document.ontouchmove = function(e){ e.preventDefault(); }
	
	window.onresize();
}

function hideOverlay(){
	var overlay = document.getElementById('overlay');
	overlay.style.display = 'none' ;
	overlay.innerHTML = '';
	$("body").css("overflow", "auto");
	document.ontouchmove = undefined ;
}

function updateOverlayPosition(){
	var content = document.getElementById('overlay_content');
	if(content){
		var width = content.offsetWidth ;
		var height = content.offsetHeight ;
		content.style.marginLeft = '-' + Math.round(width/2) + 'px' ;
		content.style.marginTop = '-' + Math.round(height/2) + 'px' ;
	}
}

function updateOverlayContent(pContent, pTitle, pInfos){
	showOverlay();
	var content = document.getElementById('overlay_tab');
	if(pTitle) content.innerHTML = '<h1>' + pTitle + '</h1>' ;
	if(pContent) content.innerHTML += pContent ;
	if(pInfos) content.innerHTML += '<div id="overlay_infos"><h2>infos</h2><p>' + pInfos + '</p></div>' ;
	updateOverlayPosition() ;
}

function count_chars(field, number, target){
	var string = field.value ;
	var limit = (number ? number : 100);
	var length = string.length ;
	if(length > limit){
		field.value = string.substr(0, limit);
	}
	var display = document.getElementById(target);
	display.innerHTML = field.value.length ;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
		} 
		while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
