// JavaScript Document
// Accordion vars
var timeAccDown = 400;
var timeAccUp = 350;
var timeAccDelay = 250;
var ie =(document.all)?true:false;

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 ;
}

function showOverlay(){
	$('body').css('overflow','hidden');
	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_tab"><a id="overlay_close" href="javascript:void(0);" onclick="hideOverlay()"></a></div>' ;
	overlay.style.display = 'block' ;

	//window.onresize();
}

function hideOverlay(){
	if($('.contenu_production').index() != -1) $('.contenu_production').children('iframe').attr('src',$('.contenu_production').children('iframe').attr('src').replace('autoplay=0','autoplay=1'));
	//if($('.content-marketing-alternatif').index() != -1) $('#content-marketing').children('li').children('iframe').attr('src',$('#content-marketing').children('li').children('iframe').attr('src').replace('autoplay=0','autoplay=1'));
	$('body').css('overflow','auto');
	var overlay = document.getElementById('overlay');
	overlay.style.display = 'none' ;
	overlay.innerHTML = '';
	$('.legacy>div>div').attr('class','contenu-legal');	
}

function updateOverlayPosition(){
	var sizes = getWindowBounds();
	var content = document.getElementById('overlay_content');
	if(content){
		var width = content.offsetWidth ;
		var scrollWidth = $(document).scrollLeft();
		var scrollHeight = $(document).scrollTop();
		if(!ie) var height = content.offsetHeight;
		else var height = (sizes.height - $(window).height() + content.offsetHeight)/2;
		content.style.marginLeft = '-' + Math.round(width/2-scrollWidth) + 'px' ;
		$('#overlay_content').css('margin-top',-Math.round(height-scrollHeight)+'px');
	}
	window.onresize();
}

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];
}

function in_array (needle, haystack, argStrict) {
    var key = '',
        strict = !! argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}
