/************************************************************************************
@ Author : Nicolas Castelli
@ Mail : nicolas.castelli2@gmail.com
@ Date of creation : 2008-01-01
@ Filename : MyModalDBox.js
@ Use : Javascript pour la création de fenêtres modales, inspirées par :
http://javascript.about.com/
************************************************************************************/
//Classe pour la gestion des fenêtres modales
MyModalDBox = function() {
	//**** VARIABLES PRIVÉES
	//Fenetre opaque
	var idOpacity = "MODAL_OPACITY";
	//Conteneur fenetre
	var idBox = "MODAL_BOX";
	//Fenetre modale
	var id = "";
	//Composants HTML
	var compBt_Modal_Close = "";
	var compBt_Modal_Action = "";


	//**** VARIABLES PUBLIQUES



	//**** FONCTIONS PRIVÉES
	var getPageSizeWithScroll = function() {
		if (window.innerHeight && window.scrollMaxY)
		{// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight)
		{ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
		}
		else
		{ // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
			xWithScroll = document.body.offsetWidth;
	  }
		arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
		//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
		return arrayPageSizeWithScroll;
	}

	//Pour dissimuler les select, les frames et les object
	var treatSelectComp = function(visible) {
		tag = document.getElementsByTagName('select');
		for(var i = tag.length-1; i >= 0; i--)
		{
			//Specifique pour mon site, en attendant de trouver où le mettre ailleur
			if(tag[i].id != "LANG")
				tag[i].style.visibility = visible;
		}
	}

	//Calcule la position de la fenetre de stats en fonction du navigateur
	var getWindowPos = function(idAnchor) {
		var xPos = $(idAnchor).offsetLeft;
		var yPos = $(idAnchor).offsetTop - 150;
		//Evidemment, spécifique pour IE qui fait rien comme les autres
		if(globalBrowser.navigator == "Internet Explorer")
		{
			xPos = $(idAnchor).offsetLeft + 100;
			yPos = $(idAnchor).offsetTop + 400;
			/*
			//IE6
			if(globalBrowser.navVersion == 6)
			{
				yPos = yPos + 200;
				xPos = $(idAnchor).offsetLeft 2 30;
			}
			//IE7 et au-delà
			else if(globalBrowser.navVersion >= 7)
			{
				yPos = yPos + 150;
				xPos = $(idAnchor).offsetLeft*2.7;
			}
			*/
		}
		var arrPos = new Array(xPos, yPos);
		return arrPos;
	}

	//Pour cacher la fenêtre modale
	var hideModal = function() {
		$(id).style.display = 'none';
		$(idBox).style.display = 'none';
		$(idOpacity).style.display = 'none';
		treatSelectComp('visible');
	}

	//********************* ACTIONS DE LA FENETRE MODALE	***************************//
	//Pour lancer la sauvegarde du deck
	var executeModalAction = function()	{
		switch(id) {
			case "MODAL_SAVE":
				launchSave();
			break;
			case "MODAL_EXPORT":
				launchExport();
			break;
		}
	}

	//Lancer une sauvegarde de deck
	var launchSave = function() {
		//Si un nom de deck a bien été saisi, on valide
		var deckName = $("TXT_MODAL_DECKNAME").value;
		if(deckName && deckName !=  "")
		{
			$("HD_DECKNAME").value = deckName;
			submitForm('formDeck');
			hideModal();
		}
	}

	//Pour lancer l'export du deck
	var launchExport = function()
	{
		var items = new Array('MODAL_EXPORT_MTGO', 'MODAL_EXPORT_APP', 'MODAL_EXPORT_MWS', 'MODAL_EXPORT_OCTGN');
		var item = "";
		//Valeur de l'indice sélectionné
		var iItem = -1;
		for(var i=0; i < items.length; i++)
		{
			item = $(items[i]);
			if(item.checked)
			{
				iItem = i;
				break;
			}
		}
		//Si un item a été sélectionné, on valide
		if(iItem >= 0)
		{
			$("HD_EXPORT_FORMAT").value = $(items[iItem]).value;
			submitForm('formDeck');
			hideModal();
		}
	}


	//**** FONCTIONS PUBLIQUES
	this.construct = function(idElt) {
		id = idElt;
		compBt_Modal_Close = "BT_" + id + "_CLOSE";
		compBt_Modal_Action = "BT_" + id + "_ACTION";
		//Initialisation des boutons des actions
		if($(compBt_Modal_Close))
			$(compBt_Modal_Close).onclick = function() { hideModal(); };
		if($(compBt_Modal_Action))
			$(compBt_Modal_Action).onclick = function() { executeModalAction(); };
	}

	//Pour montrer la fenêtre modale
	//Elle est positionnée juste au dessus de l'objet "idAnchor"
	this.showModal = function(idAnchor) {
		var tPageSize = getPageSizeWithScroll();
		var pageHeight = tPageSize[0];
		var pageWidth = tPageSize[1];
		var XY = getWindowPos(idAnchor);
		var xPos = XY[0];
		var yPos = XY[1];
		//On positionne la fenetre opaque.
		$(idOpacity).style.width = pageHeight + 'px';
		$(idOpacity).style.height = pageWidth + 'px';
		//On cache les combo box
		treatSelectComp('hidden');
		//On affiche la fenetre opaque.
		$(idOpacity).style.display = 'block';

		//On positionne puis on affiche la fenetre modale
		$(idBox).style.top = yPos + 'px';
		$(idBox).style.left = xPos + 'px';
		$(idBox).style.display = 'block';
		$(id).style.display = 'block';
		return false;
	}

} //Fin de la classe
