/* ---------------------------
	GESTION DES FENËTRES
--------------------------- */

var id_window = 1;
var nb_window = 0;

// CREER UNE FENÊTRE
function createWindow(titre, contenu)
{
	// Instances principales
	var corps = document.getElementById("corps2");
	
	// Création éventuelle de la zone invisible
	if(nb_window == 0) {
		document.getElementById("fondCache").className = "fondCache";
	}
	// Incrémente nb de fenêtres
	nb_window++;
	
	// Création de la fenêtre
	corps.innerHTML = '<div style="display:none;" class="window" id="__wwindow'+id_window+'"><div class="title" id="__wwindow_title'+id_window+'">'+titre+'</div><div class="content"id="__wwindow_content'+id_window+'">'+contenu+'</div></div>' + corps.innerHTML;	

	$('__wwindow'+id_window).appear({duration:0.1});

	// Positionne la fenêtre
	windowPosition(id_window);

	// Incrémente le compteur de fenêtres
	var i = id_window;
	id_window++;
	
	return i;
}

function windowTitleIdForNb(nb) {
	return '__wwindow_title'+nb;
}

function windowContentIdForNb(nb) {
	return '__wwindow_content'+nb;
}

function windowIdForNb(nb) {
	return '__wwindow'+nb;
}

// MODIFIE LE CONTENU D'UNE FENÊTRE
function setContentWindow(id_window, contenu) 
{
	var fenetre = document.getElementById('__wwindow_content'+id_window);
	fenetre.innerHTML = contenu;
	windowPosition(id_window);
}



// RETOURNE LE CONTENU D'UNE FENÊTRE
function getContentWindow(id_window) 
{
	var fenetre = document.getElementById('__wwindow_content'+id_window);
	return fenetre.innerHTML;
}




// SUPPRIME UNE FENÊTRE
function deleteWindow(id_window)
{
	// Recherche des instances principales
	var fenetre = document.getElementById('__wwindow'+id_window);
	var corps = document.getElementById("corps2");
	
	if(!fenetre) return;
	
	// Décrémente nb de fenêtre
	nb_window--;
	
	// Suppression éventuelle de la zone invisible
	if(nb_window == 0) document.getElementById("fondCache").className = "";
	
	/*// Suppression de la fenpetre
	$('__wwindow'+id_window).fade({duration:0.3, afterFinish:
		function(){
			corps.removeChild(fenetre);
			if(nb_window == 0) document.getElementById("fondCache").className = "";
		}
		
		}
	);*/
	

	corps.removeChild(fenetre);
	
}




// FENETRE D'ALERTE
function windowAlert(text, title, ok)
{
	if(!title) title = "Information";
	if(!ok) ok = "Ok";

	var id = createWindow(title, "");
	setContentWindow(id, text + "<div align='center'><input class='button' type='button' value='"+ok+"' onclick='deleteWindow("+id+");' /></div>");
	return id;
}



// FENETRE DE CONFIRMATION
function windowConfirm(callback, text, title, ok, nok, rand)
{
	if(!title) title = "Information";
	if(!ok) ok = "Ok";
	if(!nok) nok = "Annuler";

	var id = createWindow(title, "");
	var name = String(parseInt(Math.random()*500000));
	
	if(rand && (Math.round(Math.random())))	setContentWindow(id, text + "<div align='center'><input class='button2' type='button' value='"+nok+"' onclick='deleteWindow("+id+");' name='_wwindow_btncnf"+name+"' /> <input style='display:none;' class='button' type='button' onclick='deleteWindow("+id+");alert(1)' value='"+ok+"' name='_wwindow_btncnf"+name+"' /> <input class='button' type='button' value='"+ok+"' onclick='deleteWindow("+id+"); var ii = "+name+"; "+callback+"' name='_wwindow_btncnf"+name+"' /></div>");
	else setContentWindow(id, text + "<div align='center'><input class='button' type='button' value='"+ok+"' onclick='deleteWindow("+id+"); var ii = "+name+"; "+callback+"' name='_wwindow_btncnf"+name+"' /> <input class='button2' type='button' value='"+nok+"' onclick='deleteWindow("+id+");' name='_wwindow_btncnf"+name+"' /> <input style='display:none;' class='button' type='button' onclick='deleteWindow("+id+");alert(1)' value='"+ok+"' name='_wwindow_btncnf"+name+"' /></div>");
	
	return id;
}


// POSITIONNE LA FENETRE EN HAUTEUR
function windowPosition(id_window)
{
	var fenetre = document.getElementById('__wwindow'+id_window);
	
	if(((typeof FACEBOOK == 'undefined') || (FACEBOOK == false)) || ((typeof SMALL_DESIGN == 'undefined') || (SMALL_DESIGN == false))) {
		fenetre.style.top = Math.max(0, parseInt(getInfo("scrollTop") -300 + (getInfo("innerHeight")/2) - (fenetre.clientHeight / 2))) + 'px';
	}
	else
	{
		//new Effect.ScrollTo('top'); 
		if(typeof executeCallbackWhenFBIsDefined == 'function') { 
			executeCallbackWhenFBIsDefined(function() {
				FB.Canvas.scrollTo(0,35);
				fenetre.style.top = '50px';
			});
		}
		
		return;
		
		//alert(window.parent.document);
		//alert(window.parent.clientHeight);
		//alert(FB.Canvas.getPageInfo().offsetTop);
		//fenetre.style.position = 'absolute';
		//fenetre.style.top = headerSize() + 200;
		//alert($('design_header').viewportOffset()[1]);
	}
}



// POSITIONNE LA FENETRE ALEATOIREMENT
function windowPositionRand(id_window)
{
	var fenetre = document.getElementById('__wwindow'+id_window);
	
	fenetre.style.left = parseInt(Math.random() * 40) + '%';
	
	var minTop = Math.max(0, parseInt(getInfo("scrollTop") -300)) + 37;
	var maxTop = minTop + getInfo("innerHeight") - Math.max(0, (300 - getInfo("scrollTop"))) - fenetre.clientHeight - 60;
	
	var top = minTop + parseInt(Math.random() * parseInt(maxTop - minTop));
	
	fenetre.style.top = top + 'px';

}






// FENETRE DE CONFIRMATION
function windowConfirmBots(callback, text, title, ok, nok)
{
	if(nb_window > 0) return;
	var id = windowConfirm(callback, text, title, ok, nok, 1)

	windowPositionRand(id);
}




