﻿	function dialogBox( params )
	{
		var _this = this;
		
		this.Id = Math.random();
		this.parent = document.body;
		
		//Permet de bloquer le contenu du noeud parent
		this.isModal = true;
		//Noeud Html du modal
		var oModal = null;
		
		//Noeuds
		this.oDialogboxMaster = null;
		this.oDialogboxHeader = null;
		this.oDialogboxIcone = null;
		this.oDialogboxTitle = null;
		this.oDialogboxClose = null;
		this.oDialogboxContent = null;
		this.oDialogboxFooter = null;
		
		// Events...
		this.onShow = ( params.onshow ) ? params.onshow : function() { return true; };
		
		//liste de [dialogboxButton]
		this.oButtons = new Array(  );
		
		//Template
		this.templateSrc = DEFAULT_DIALOGBOX_TEMPLATE_EDIT;
		this.templateHTML = "";
		
		
		this.iconeSrc = "";
		
		//Dimensions
		this.height = 200;
		this.width = 300;
		this.zIndex = 90;
		//Datas
		
		this.close = function() { _this.hide(  ) };
		this.content = "";
		this.title = "";
		
		if( typeof params != "undefined" )
		{
			if( typeof params.isModal != "undefined" )			this.isModal = params.isModal;
			if( typeof params.parent != "undefined" )			this.parent = params.parent;
			if( typeof params.content != "undefined" )			this.content = params.content;
			if( typeof params.title != "undefined" )			this.title = params.title;
			if( typeof params.height != "undefined" )			this.height = params.height;
			if( typeof params.width != "undefined" )			this.width = params.width;
			if( typeof params.zIndex != "undefined" )			this.zIndex = params.zIndex;
			if( typeof params.iconeSrc != "undefined" )			this.iconeSrc = params.iconeSrc;
			if( typeof params.close != "undefined" )			this.close = params.close;
			if( typeof params.templateSrc != "undefined" )			this.templateSrc = params.templateSrc;
			if( typeof params.templateHTML != "undefined" )			this.templateHTML = params.templateHTML;
		}
		//alert( "dialogbox: " + this.height + "x" + this.width );
		//Creation de la fenetre
		this.initialize(  );
	}
	
	dialogBox.prototype.initialize = function(  )
	{
		
		var oDivMaster = document.createElement( "div" );
		oDivMaster.style.display = "none";
		oDivMaster.style.position = "absolute";
		//oDivMaster.style.height = this.height+"px";
		oDivMaster.style.width = this.width+"px";
		
		document.body.appendChild( oDivMaster );
		$( oDivMaster ).attr( "node", "__dialogboxMaster" );
		this.oDialogboxMaster = oDivMaster;
		this.oDialogboxMaster.id = ("dialogbox_master_"+Math.random()).replace( ".", "_" );
		
		//Charge le template 
		this.loadTemplate(  );
		
		
		//Icon
		if( this.iconeSrc != "" && this.oDialogboxIcone )
		{
			this.oDialogboxIcone.src = this.iconeSrc;
		}
		else if( this.oDialogboxIcone && this.iconeSrc == "" )
		{
			$( this.oDialogboxIcone ).hide(  );
		}
		
		
		//Initialisation des valeurs
		if( this.oDialogboxTitle )		$( this.oDialogboxTitle ).html( this.title );
		if( this.oDialogboxContent )	
		{
			$( this.oDialogboxContent ).html( this.content );

			this.oDialogboxContent.id = ( "dialogbox_content_"+Math.random() ).replace( ".", "_" );
			this.oDialogboxContent.style.display = "block";
			this.oDialogboxContent.style.overflowY = "auto";
			this.oDialogboxContent.style.height = this.height+"px";
			this.oDialogboxContent.style.width = this.width+"px";			
			//this.oDialogboxContent.style.border = "1px solid red";
		}
		
		var _this = this;
		if( this.oDialogboxClose )		
		{
			$( this.oDialogboxClose ).click( function(  ){ _this.close.call(_this) } );
		}
	}
	
	//Charge le look de la boite à partir d'un template HTML
	dialogBox.prototype.loadTemplate = function(  )
	{
		if( this.templateHTML != "" )
		{
			$( this.oDialogboxMaster ).html( this.templateHTML );
		}
		else if( this.templateSrc != "" ) //Trop long sur IE6
		{
			//alert( this.templateSrc );
			var xmlhttp = getHTTPObject();
			xmlhttp.open("GET", this.templateSrc ,false); 
			xmlhttp.send(null);
		
			//alert( $( xmlhttp.responseText ).html() );
			$( this.oDialogboxMaster ).html( $( xmlhttp.responseText ).html() );
		}	
		else
		{		
			return;
		}
		
		var tmp = $( this.oDialogboxMaster ).find( "*[ node='__dialogboxHeader' ]" );
		if( tmp.length > 0 )
		{
			this.oDialogboxHeader = tmp[ 0 ];
		}
		
		var tmp = $( this.oDialogboxMaster ).find( "img[ node='__dialogboxIcone' ]" );	
		if( tmp.length > 0 )
			this.oDialogboxIcone = tmp[ 0 ];
		
		var tmp = $( this.oDialogboxMaster ).find( "*[ node='__dialogboxTitle' ]" );	
		if( tmp.length > 0 )
			this.oDialogboxTitle = tmp[ 0 ];
		
		var tmp = $( this.oDialogboxMaster ).find( "*[ node='__dialogboxClose' ]" );			
		if( tmp.length > 0 )
		{
			this.oDialogboxClose = tmp[ 0 ];
		}
		var tmp = $( this.oDialogboxMaster ).find( "*[ node='__dialogBoxContent' ]" );
		if( tmp.length > 0 )
		{
			this.oDialogboxContent = tmp[ 0 ];
		}	
		var tmp = $( this.oDialogboxMaster ).find( "*[ node='__dialogBoxFooter' ]" );
		if( tmp.length > 0 )
			this.oDialogboxFooter = tmp[ 0 ];
		
		var oDialogButtons = $( this.oDialogboxMaster ).find( "*[ node='__dialogboxButton' ]" )
		for( var i=0;i<oDialogButtons.length;i++ )
		{
			var oCurrentButton = new dialogboxButton({
														node : oDialogButtons[ i ],
														value : $( oDialogButtons[ i ] ).val(),
														position : $( oDialogButtons[ i ] ).attr( "position" )
								})
			this.oButtons[ i ] = oCurrentButton;
			oCurrentButton.DialogBox = this;
			oCurrentButton.hide();
		}
		
	}
	dialogBox.prototype.getButton = function( p_sPosition )
	{
		for( var i=0;i<this.oButtons.length;i++ )
		{
			if( this.oButtons[ i ].position == p_sPosition )	return this.oButtons[ i ];
		}
		return null;
	}
	/*
		params = 	{ 
						positon : [attribut position du bouton], 
						value : [libelle du button], 
						click : fnClick, 
						mouseover : fnMouseover, 
						mouseout : fnMouseout 
					}
	*/
	dialogBox.prototype.setButton = function( params )
	{
		if( typeof params != "undefined" && typeof params.position != "undefined" )
		{
			var oButton = this.getButton( params.position );
			if( oButton == null )	return;
			
			if( typeof params.value != "undefined" )		
			{
				oButton.setValue( params.value );
			}
			
			if( typeof params.click != "undefined" )			oButton.click( params.click );
			if( typeof params.mouseover != "undefined" )		oButton.mouseover( params.mouseover );
			if( typeof params.mouseout != "undefined" )			oButton.mouseout( params.mouseout );
			
			oButton.show();
		}
	}	
	
	dialogBox.prototype.setClose = function( fnClose )
	{
		var _this = this;
		this.close = fnClose;
		if( this.oDialogboxClose )		
		{
			$( this.oDialogboxClose ).unbind( "click" );	// Clear old events...
			$( this.oDialogboxClose ).click( function(  ){ _this.close.call(_this) } );
			$( this.oDialogboxClose ).show(  );
		}
	}
	
	//params = { left : [left potion], top : [top potion] }
	dialogBox.prototype.setPosition = function( params )
	{
		if( typeof params != "undefined" )
		{
			if( typeof params.left != "undefined" )		$( this.oDialogboxMaster ).css( "left : " + params.left );
			if( typeof params.top != "undefined" )		$( this.oDialogboxMaster ).css( "top : " + params.top );
		}
	}
	
	//params = { width : [largeur], height : [hauteur] }
	dialogBox.prototype.setDimension = function( params )
	{
		if( typeof params != "undefined" )
		{
			if( typeof params.width != "undefined" )		$( this.oDialogboxMaster ).css( "width : " + params.width );
			if( typeof params.height != "undefined" )		$( this.oDialogboxMaster ).css( "height : " + params.height );
		}
	}
	
	//Affiche la boite au centre du parent
	dialogBox.prototype.show = function(  )
	{
		this.drawOver( this.parent );
		$( this.oDialogboxMaster ).show();
		this.onShow( this.oDialogboxMaster );	// fire event
	}	
	//Permet de savoir si la fenetre est visible
	dialogBox.prototype.isShowing = function(  )
	{
		var oNode = this.oDialogboxMaster;
		while( oNode != null )
		{
			if( oNode.style )
			{
				if( oNode.style.display == "none" )	return false;
			}
			oNode = oNode.parentNode;
			if( oNode == null )
				return true;
		}
		return true;
	}

	dialogBox.prototype.setWidth = function( w ) 
	{
		$( this.oDialogboxContent ).width( w );
		this.updateDrawCenter( this.parent );
	}
	dialogBox.prototype.setHeight = function( h ) 
	{
		$( this.oDialogboxContent ).height( h );
		this.updateDrawCenter( this.parent );
	}
	dialogBox.prototype.resizeTo = function( w, h ) 
	{
		$( this.oDialogboxContent ).width( w );
		$( this.oDialogboxContent ).height( h );
		this.updateDrawCenter( this.parent );
	}
	dialogBox.prototype.updateDrawCenter = function( refNode ) 
	{
		//$( this.oDialogboxMaster ).css( 'left', '50px' )
		
		//$( this.oDialogboxMaster ).drawCenter( refNode )
	}
	//
	// Change parentNode (this.parent) reference for drawCenter function
	//
	dialogBox.prototype.changeParentNode = function( newNodeParent ) 
	{
		$(newNodeParent).append( this.oDialogboxMaster );
		this.parent = newNodeParent;
	}

	
	//Affiche au centre du noeud parent + ombre qui bloque le parent
	dialogBox.prototype.drawOver = function( )
	{
		this.drawShadowModal(  );
		$( this.oDialogboxMaster ).show(  ).drawCenter( this.parent );
		
		
		if ( this.parent == document.body )	// Si ça bug, c'est Alex
		{
			//alert( "body" );
			if ( $(document).scrollTop() > 0 ) {
				
				var y = $( this.oDialogboxMaster ).offset().top + $(document).scrollTop();
				//alert( $(document).scrollTop() );
				$( this.oDialogboxMaster ).css( "top", y );
			}
		}
		

		//alert( '3°) modal-zindex : ' + this.oModal.style.zIndex + ' | dialogbox.zIndex : ' + this.oDialogboxMaster.style.zIndex );
		
	}
	//Affiche l'ombre qui bloque le contenu de la fenetre parent
	dialogBox.prototype.drawShadowModal = function( p_oNodeRef )
	{
		if( this.parent && this.isModal )
		{
			var oMask = document.createElement( "div" );
			oMask.style.position = 'absolute';
			$( oMask ).css( "zIndex", this.zIndex );
			
			if( $( this.parent ).css( "position" ) == "absolute" )
			{
				var iZIndex = parseInt( $( this.parent ).css( "zIndex" ) ) + 5000;
				$( oMask ).css( "zIndex", iZIndex );
			}
			
			oMask.style.backgroundColor = "#000000";
			document.body.appendChild( oMask );
			$( oMask ).css('line-height', $(this.parent).height()+'px' );
			
			var iTmpZIndex = parseInt( $( oMask ).css( "zIndex" ) ); //Dans le cas ou on force le z-Index de la fenetre et que le drawOver le met à 10000
			$( oMask ).drawOver( $(this.parent) ).show().fadeTo(0, 0.7 );
			if( parseInt( oMask.style.zIndex ) < iTmpZIndex )	$( oMask ).css( "zIndex", iTmpZIndex );
			
			
			// Si ça bug, c'est Alex
			if ( this.parent == document.body )
			{
				$( oMask ).height( $(document).height() );
			}
			
			$( this.oDialogboxMaster ).css( "zIndex", parseInt( oMask.style.zIndex ) + 1 );
			//alert( "2°)MASK z-index : " + oMask.style.zIndex + " ; this.oDialogboxMaster.style.zIndex : " + this.oDialogboxMaster.style.zIndex );
			this.oModal = oMask;
		}
	}
	
	
	//Cache la boite
	dialogBox.prototype.hide = function(  )
	{
		if( this.oModal )
		{
			$( this.oModal ).hide();
		}
		if( this.oDialogboxMaster )
			$( this.oDialogboxMaster ).hide();
	}		
	//Active la boite pour montrer que la boite est ouverte
	dialogBox.prototype.active = function(  )
	{
		//Essayer de faire le bruit de l'alerte
	}
	//Supprime le noeud HTML de la boite 
	dialogBox.prototype.remove = function(  )
	{
		if( this.oModal )
		{
			$( this.oModal ).remove();
		}
		if( this.oDialogboxMaster )
		{
			$( this.oDialogboxMaster ).remove();
		}
	}
	
	
	
	
	function dialogboxButton( params )
	{
		this.node = null;
		
		this.value = "button";
		this.position = "center";
		
		this.clickInvoke = null;
		this.mouseoverInvoke = null;
		this.mouseoutInvoke = null;
		
		if( typeof params != "undefined" )
		{
			if( typeof params.node != "undefined" )			this.node = params.node;
			if( typeof params.position != "undefined" )		this.position = params.position;
			if( typeof params.value != "undefined" )		this.value = params.value;
			if( typeof params.click != "undefined" )		this.clickInvoke = params.click;
			if( typeof params.mouseover != "undefined" )		this.mouseoverInvoke = params.mouseover;
			if( typeof params.mouseout != "undefined" )		this.mouseoutInvoke = params.mouseout;
		}
		
		this.initialize(  );
	}
	dialogboxButton.prototype.DialogBox = null;
	
	dialogboxButton.prototype.initialize = function()
	{
		if( this.node == null )	return;
		
		var _this = this;
		$( this.node ).val( _this.value );
		
		this.click( _this.clickInvoke );
		this.mouseover( _this.mouseoverInvoke );
		this.mouseout( _this.mouseoutInvoke );
	}
	
	dialogboxButton.prototype.setValue = function( p_sValue )
	{
		this.node.value = p_sValue;
	}
	
	dialogboxButton.prototype.click = function( p_fn )
	{
		if( typeof p_fn != "undefined" && p_fn != null )
		{
			this.clickInvoke = p_fn;
			var _this = this;
			$( this.node ).click( function(){ _this.clickInvoke.call(_this.DialogBox) } );
		}
	}
	dialogboxButton.prototype.mouseover = function( p_fn )
	{
		if( typeof p_fn != "undefined" && p_fn != null )
		{
			this.mouseoverInvoke = p_fn;
			var _this = this;
			$( this.node ).mouseover( function(){ _this.mouseoverInvoke.call() } );
		}
	}
	dialogboxButton.prototype.mouseout = function( p_fn )
	{
		if( typeof p_fn != "undefined" && p_fn != null )
		{
			this.mouseoutInvoke = p_fn;
			var _this = this;
			$( this.node ).mouseout( function(){ _this.mouseoutInvoke.call() } );
		}
	}	
	dialogboxButton.prototype.show = function(  )
	{
		$( this.node ).show();
	}	
	dialogboxButton.prototype.hide = function(  )
	{
		$( this.node ).hide();
	}
	dialogboxButton.prototype.remove = function(  )
	{
		$(this.node ).remove();
	}











	function getHTTPObject(  )
	{
	  var xmlhttp = false;

	  /* Compilation conditionnelle d'IE */
	  /*@cc_on
	  @if (@_jscript_version >= 5)
		 try
		 {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		 }
		 catch (e)
		 {
			try
			{
			   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E)
			{
			   xmlhttp = false;
			}
		 }
	  @else
		 xmlhttp = false;
	  @end @*/

	  /* on essaie de créer l'objet si ce n'est pas déjà fait */
	  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	  {
		 try
		 {
			xmlhttp = new XMLHttpRequest();
		 }
		 catch (e)
		 {
			xmlhttp = false;
		 }
	  }

	  if (xmlhttp)
	  {
		 /* on définit ce qui doit se passer quand la page répondra */
		 xmlhttp.onreadystatechange=function()
		 {
			if (xmlhttp.readyState == 4) /* 4 : état "complete" */
			{
			   if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */
			   {
				  /*
				  Traitement de la réponse.
				  Ici on affiche la réponse dans une boîte de dialogue.
				  */
				  //alert(xmlhttp.responseText);
			   }
			}
		 }
	  }
	  return xmlhttp;
	}
	
	
	/*
	var xmlhttp = getHTTPObject();
	xmlhttp.open("GET", "Templates/dialogBox/templateLoading.html" ,false); 
	xmlhttp.send(null);
	alert( xmlhttp.responseText );
	*/

	
