/**
 * function  popUpWindow - opens a new pop up window
 *
 * Order: The function opens a new pop up window with the given size.
 * According to the OS and the browser, it returns HTML code
 *
 * @param   adresse : page uri to disply in the window;
 * @param   nom : name of the window
 * @param   largeur : width of the window
 * @param   hauteur : height of the window
 * @param   redim : "yes" to set the window resizable, "no" to set the window not resizable
 * @param       scrollin : "yes" to force scrolling, "no" to disable scrolling, "auto" to enable scrolling
 */
function popUpWindow(adresse, nom, largeur, hauteur, redim, scrolling) {
	win = window.open(adresse, nom, 'toolbar=no,location=no,status=no,menubar=no,width='+largeur+',height='+hauteur+',resizable='+redim+',scrollbars='+scrolling);
	win.focus();
}


function popUpVideo(adresse) {
	// Ouvre une fenetre video
	popUpWindow(adresse + (adresse.indexOf("?") > 0 ? "&" : "?") + "redim=1", "WS_Video", 800, 510, 'yes', 'no');
}

function popUpDebate(debate) {
	// Ouvre une fenetre debates
	popUpWindow("/experts/debates.php?doi=" + debate + "&redim=1", "WS_Debate", 800, 520, 'yes', 'no');
}

function popUpVideoHD(video) {
	// Ouvre une fenetre video HD
	popUpWindow("/video/hd/popup.php" + (video ? "?video="+video : ""), "WS_VideoHD", 540, 520, 'yes', 'no');
}

function popUpExpertOpinion(adresse) {
	// Ouvre une fenetre  avis d'expert
	popUpVideo(adresse);
}

function popUpLecture(lt) {
	// Ouvre une fenetre lectures (Flash)
	popUpWindow("/lectures/viewer.php?doi=" + lt + "&redim=1", "WS_Lecture", 1024, 768, 'yes', 'no');
}

function popUpLectureHTML(lt) {
	// Ouvre une fenetre lectures en ancienne version (HTML)
	popUpWindow("/lectures/viewer_html.php?doi=" + lt + "&redim=1", "WS_Lecture", 800, 600, 'yes', 'no');
}



function popUpTechnique(adresse) {
	// Ouvre une fenetre Operative Technique
	popUpWindow(adresse, "WS_Technique", 760, 500, 'yes', 'no');
}

function popUpStorz(adresse) {
	// Ouvre une fenetre KarlStorz Wings
	popUpWindow(adresse, "WS_Karlstorz", 760, 425, 'yes', 'no');
}






/**
 * function  openPdf - opens pdf document in a new pop up window
 * Order: The function opens a new pop up window with the given path.
 * @param   chemin : page uri to display in the window;
 */
function openPdf(chemin){
    viewpdf = window.open(chemin,"VIEW_PDF","height=550,width=640,resizable=yes,scrollbars=yes");
    viewpdf.focus();
}


function popPDF(file)
{
  if (window.pdfWindow)
     pdfWindow.focus();
  else
     pdfWindow = window.open(file);
}

function popFig (url){
	window.open('/clinicalcase/figure.php?illus='+url, 'Websurg', 'menubar=0,toolbar=0,status=0,width=545,height=545,resizable=1,scrollbars=0');
}



function swapImage(id, src) {
	if(document.getElementById(id))
		document.getElementById(id).src = src;
}

function swapDiv(ID, num) {
	for(i=1; i<=20; i++) {
		if(document.getElementById('link' + ID + i))
			document.getElementById('link' + ID + i).className = '';
		else
			break;
		mask('div' + ID + i);
	}
	if(document.getElementById('link' + ID + num))
		document.getElementById('link' + ID + num).className = 'actif';
	show('div' + ID + num);
}





function show(id) {
	if(document.getElementById(id))
		document.getElementById(id).style.display = "block";
}

function mask(id) {
	if(document.getElementById(id))
		document.getElementById(id).style.display = "none";
}

function vis_show(id) {
	if(document.getElementById(id))
		document.getElementById(id).style.visibility = "visible";
}

function vis_mask(id) {
	if(document.getElementById(id))
		document.getElementById(id).style.visibility = "hidden";
}

// Alterne l'affichage / masquage de l'element ID
function toggle(id) {
	if(document.getElementById(id)) {
		if(document.getElementById(id).style.display == "none")
			document.getElementById(id).style.display = "block";
		else
			document.getElementById(id).style.display = "none";
	}
}


function show_bar(IDBar) {
	vis_show(IDBar);
	if(nav['Name']=="Internet Explorer")
		vis_show(IDBar + '_maskselect');

	if(lk = document.getElementById('link_'+IDBar)) {
		lk.style.position = "absolute";
		a = getSize(lk);
		placeMenu("link_"+IDBar, IDBar, -a.w+4, -getHeight(IDBar));
	}

}
function mask_bar(IDBar) {
	vis_mask(IDBar);
	if(nav['Name']=="Internet Explorer")
		vis_mask(IDBar + '_maskselect');

	if(lk = document.getElementById('link_'+IDBar)) {
		lk.style.position = "static";
		lk.style.top = "auto";
		lk.style.left = "auto";
	}

}


function toggleTOC(IDtableau, IDtexte, type) {
	if(document.getElementById(IDtableau)) {
		toggle(IDtableau);
		if(document.getElementById(IDtexte)) {
			if(document.getElementById("tg_view"))
				tg_view = document.getElementById("tg_view");

			if(document.getElementById(IDtableau).style.display == "none") {
				if(tg_view)
					tg_view.value = (tg_view.value).replace(type, "");
				document.getElementById(IDtexte).innerHTML = "expand<img align=middle style='margin-left:4px' src='/img/btn_expand.gif' border=0 alt='Expand list'>";
			}
			else {
				if(tg_view && (tg_view.value).indexOf(type) < 0)
					tg_view.value = tg_view.value + type;
				document.getElementById(IDtexte).innerHTML = "collapse<img align=middle style='margin-left:4px' src='/img/btn_collapse.gif' border=0 alt='Collapse list'>";
			}
		}
	}
}





function setURLValue(url, nomParam, valeurParam) {
// ajoute (ou modifie si existant dans l'URL) un paramètre d'URL
// Si [argument 1] = ""  ->  url = document.location.href
// exemple : setURLValue("", "s", 50);

	if(url=="")
		url = document.location.href;

	url2set = nomParam + "=" + valeurParam;

	if ( url.indexOf("?") == -1 ) {
		return url + "?" + url2set;
	}
	if ( (pos=(url.indexOf("?"+nomParam+"="))) == -1 && (pos=(url.indexOf("&"+nomParam+"="))) == -1 ) {
		return url + "&" + url2set;
	}
	
	url1 = url.substring(0, pos + 1);
	url2 = url.substring(pos + 1 + nomParam.length + 1);
	
	if ( (pos=(url2.indexOf("&"))) == -1 )
		return url1 + url2set;
	
	url2 = url2.substring(pos);

	return url1 + url2set + url2;
}

function setURLValueForLink(ID, var_name, val) {
	if(document.getElementById(ID))
		document.getElementById(ID).href = setURLValue(document.getElementById(ID).href, var_name, val);
}




function getWindowHeight() {
	var windowHeight = 0;
	if(typeof(window.innerHeight)=='number')
		windowHeight = window.innerHeight;
	else if(document.documentElement && document.documentElement.clientHeight)
		windowHeight = document.documentElement.clientHeight;
	else if(document.body && document.body.clientHeight)
		windowHeight = document.body.clientHeight;
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth = 0;
	if(typeof(window.innerWidth)=='number')
		windowWidth = window.innerWidth;
	else if(document.documentElement && document.documentElement.clientWidth)
		windowWidth = document.documentElement.clientWidth;
	else if(document.body && document.body.clientWidth)
		windowWidth = document.body.clientWidth;
	return windowWidth;
}

function getDocumentHeight() {
	return getWindowHeight();
}
function getDocumentWidth()	{
	return getWindowWidth();
}





/*	FONCTIONS DE ZOOM (compatible seulement sur IE6 et plus)	*/

currentZoom = 1;
pasZoom = 0.25;

function zoom(z) {
	currentZoom = z;
	document.getElementById('WeBSurg').style.zoom = z;
//	document.getElementById('q').value = z;
}

function zoom_in() {
	val = Math.round((currentZoom+pasZoom)*100)/100;
	if(val < 10)
		zoom(val);
	else
		alert("Unable to zoom in again !");
}

function zoom_out() {
	val = Math.round((currentZoom-pasZoom)*100)/100;
	if(val > 0.2)
		zoom(val);
	else
		alert("Unable to zoom out again !");
}


/*	FONCTIONS DE CONVERSION	*/

function sec2time(sec) {
	sec = Math.round(sec);
	time = Array();
	time['h'] = 0;
	time['m'] = 0;
	while(sec >= 60) {
		sec -= 60;
		if(time['m']++ >= 60) {
			time['h'] ++;
			time['m'] = 0;
		}
	}
	
	time_string = sec + " sec";
	if(time['m'])
		time_string = time['m'] + " min " + time_string;
	if(time['h'])
		time_string = time['h'] + " h " + time_string;

	return time_string;
}


