var estInitialiseBookmark = false;
var bodyWidth = 0;
var bodyHeight = 0;
var ecranWidth = 0;
var ecranHeight = 0;

// Initialisation du bookmark
function initialiseBookmark() {
	setTailleBody();
	setTailleEcran();
	setBookmarkingBackground();
	setBookmarkingPosition();

}

// Positionnement centré dans la page du bloc
function setBookmarkingBackground() {
	var fond = document.getElementById("bookmarking_background");
	var tableau = getDocumentSize();
	
	if(bodyHeight < tableau[1]) bodyHeight = tableau[1];
	//if(bodyWidth < tableau[0]) bodyWidth = tableau[0];*/
	//bodyHeight = tableau[1];
	bodyWidth = tableau[0];
	//console.log(bodyWidth);
	fond.style.width = bodyWidth+"px";
	fond.style.height = bodyHeight+"px";
}

function setBookmarkingPosition() {
	// 350 * 155
	var left=ecranWidth/2-180;
	var top=ecranHeight/2-76;
	var bookmark = document.getElementById("bookmarking_content");
	var tetiere = document.getElementById("tetiere");
	if(typeof(tetiere)!="undefined") {
		var tetiere_height = tetiere.offsetHeight;
		top = top - tetiere_height;
	}
	
	
	if(top<10) top = 10;
	//console.log(top+'x'+left);
	bookmark.style.top = top+"px";;
	bookmark.style.left = left+"px";
	estInitialiseBookmark = true;
}

// Action sur le lien partager s'il existe
function openBookmarkingBox() {
	if(!estInitialiseBookmark) return false;
	//window.location.href="#global";
	setBookmarkingBackground();
	setBookmarkingPosition();
	setDisplayBookmarkingBox("block");
	return false;
}

// Action sur le lien partager s'il existe
function closeBookmarkingBox() {
	setDisplayBookmarkingBox("none");
	return false;
}

// Action sur les liens fermer 
function setDisplayBookmarkingBox(display) {
	
	var bookmarking_background = document.getElementById("bookmarking_background");
	var bookmarking = document.getElementById("bookmarking_content");
		
	// Affichage différent selon jquery
	if(display == "block") {
		if(typeof(jQuery) == 'undefined'){
			bookmarking_background.style.display = display;
			bookmarking.style.display = display;
		}
		else {
			bookmarking_background.style.display = display;
			$("#bookmarking_content").fadeIn("slow");
		}
	}
	// Masquage normal
	else {
		if(typeof(jQuery) == 'undefined'){
			bookmarking_background.style.display = display;
			bookmarking.style.display = display;
		}
		else {
			bookmarking_background.style.display = display;
			$("#bookmarking_content").fadeOut("slow");
		}
	}
	
	return false;
}

// Traitement de l'url vers laquelle il faut rediriger
function setBookmarkingUrl(elt,bookmark,id_page,id_univers) {
	var url = document.location.href;
	var url2 = getURL_base(url);
	if(url2) url = url2;
	
	url = url.replace(/#global/,"");
	// Test si un ? est présent, si oui on supprime tout
	
	var title = document.title;
	title = str_replace2(title," - ","-");
	title = str_replace2(title," ","-");
	title = str_replace2(title,"'","-");
	var source = "Linternaute.com";
	var description = "";
	var id_page= id_page;
	var id_univers= id_univers;
	// tmp
	//var id_page= "id_page";
	//var id_univers= "id_univers";
	var location = "http://redirection.benchmark.fr/selectionner/"+bookmark+"/f_url="+url+"&f_title="+title+"&f_source="+source+"&f_description="+description+"&f_id_page="+id_page+"&f_id_univers="+id_univers;
	elt.href = location;
	closeBookmarkingBox(elt.parentNode.parentNode);
}

function getDocumentSize()
{
    return new Array((document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth,(document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight);
}

// Initialise les dimensions de la fenetre
function setTailleBody(){
	if (document.doctype == null || document.documentElement.clientHeight == 0) {
	if (window.innerWidth || (document.body.offsetHeight == document.documentElement.offsetHeight && document.body.offsetWidth == document.documentElement.offsetWidth)) {
	bodyHeight = document.body.clientHeight;
	bodyWidth = document.body.clientWidth;
	} else {
	bodyHeight = document.documentElement.clientHeight;
	bodyWidth = document.documentElement.clientWidth;
	}
	}
	// FireFox
	if (window.innerWidth && window.innerHeight) {
	bodyHeight = window.innerHeight;
	bodyWidth = window.innerWidth;
	}
	var tableau = getDocumentSize();
	if(bodyHeight < tableau[1]) bodyHeight = tableau[1];
	if(bodyWidth < tableau[0]) bodyWidth = tableau[0];
}

function setTailleEcran() {  
	if( typeof( window.innerWidth ) == 'number' ) {  
	//Non-IE  
	ecranWidth = window.innerWidth;  
	ecranHeight = window.innerHeight;  
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {  
	//IE 6+ in 'standards compliant mode'  
	ecranWidth = document.documentElement.clientWidth;  
	ecranHeight = document.documentElement.clientHeight;  
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {  
	//IE 4 compatible  
	ecranWidth = document.body.clientWidth;  
	ecranHeight = document.body.clientHeight;  
	}  
}  

function str_replace2(SRs, SRt, SRu) {
  SRRi = SRs.indexOf(SRt);
  SRRr = '';
  if (SRRi == -1) return SRs;
  SRRr += SRs.substring(0,SRRi) + SRu;
  if ( SRRi + SRt.length < SRs.length)
    SRRr += str_replace2(SRs.substring(SRRi + SRt.length, SRs.length), SRt, SRu);
  return SRRr;
}

function getURL_base(url)
{
	//renvoie un URL sans les paramètres et le ?
	var S_url = url;
	stopIndex=S_url.indexOf("?");
	url_sans_param=S_url.substr(0,stopIndex);
	return url_sans_param
}
function getURL_param(url)
{
	//renvoie les param sans le ?
	var S_url = url;
	startIndex=S_url.indexOf("?");
	param=S_url.substr(startIndex+1);
	return param
} 

window.onresize = function() {
	initialiseBookmark();
}

if(navigator.appName.indexOf('Microsoft') != -1) window.attachEvent('onload', initialiseBookmark);
else window.addEventListener('load',initialiseBookmark,false);