///////////////////////////////////////////////////////////////////////
///
///////////////////////////////////////////////////////////////////////
function myDropDown( ID_Html )
{
	////////////////////////////////////////////////////////////////////////////
	this.HTMLElement 		= null;
	this.items 				= new myDropDownItems( this );
	this._HTMLlien 			= null;
	this._HTMLdiv 			= null;
	this._HTMLtext 			= null;
	this._HTMLiframe		= null;
	this._builded 			= false;
	this._selectedItem		= false;
	
	// Type de navigateur
	var ua 			= navigator.userAgent;
	this.isMSIE 	= (navigator.appName == "Microsoft Internet Explorer");
	this.isMSIE5 	= this.isMSIE && (ua.indexOf('MSIE 5') != -1);
	this.isMSIE5_0 	= this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
	this.isMSIE6 	= this.isMSIE && (ua.indexOf('MSIE 6') != -1);
	this.isMSIE7 	= this.isMSIE && (ua.indexOf('MSIE 7') != -1);
	this.isGecko 	= ua.indexOf('Gecko') != -1; // Will also be true on Safari
	this.isSafari 	= ua.indexOf('Safari') != -1;
	this.isOpera 	= window['opera'] && opera.buildNumber ? true : false;
	this.isMac 		= ua.indexOf('Mac') != -1;
	this.isNS7 		= ua.indexOf('Netscape/7') != -1;
	this.isNS71 	= ua.indexOf('Netscape/7.1') != -1;
	
	////////////////////////////////////////////////////////////////////////////
	if( ID_Html.toString() == "" )
		throw "Veuillez indiquer un id d'élément HTML.";
	
	////////////////////////////////////////////////////////////////////////////
	this.windowLoaded = function (e)
	{
		if( document.getElementById(ID_Html) == null )
			throw "Veuillez indiquer un id d'élément HTML valide !";
		
		this.HTMLElement = document.getElementById(ID_Html);
		
		this._construireHTML();
	}
	YAHOO.util.Event.addListener(window, "load", this.windowLoaded , "" , this , true  );
	
	////////////////////////////////////////////////////////////////////////////
	this.bodyClicked = function (e)
	{
		if( this.isSafari == false )
		{
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		}
		else
		{
			//alert("bodyClicked");
		}

		this.items.cacher();
	}
	YAHOO.util.Event.addListener(document.body, "click", this.bodyClicked , "" , this , true  );
	YAHOO.util.Event.addListener(window, "click", this.bodyClicked , "" , this , true  );
	
	////////////////////////////////////////////////////////////////////////////
	this.ajouter = function ( newItem )
	{
		return this.items.ajouter( newItem );
	}
	
	this.supprimer = function ( curItem )
	{
		return this.items.supprimer( curItem );
	}
	
	this._construireHTML = function ()
	{
		if( this.items.items.length <= 0 )
			return false;
		
		
		this._HTMLlien 				= document.createElement("a");
		this._HTMLlien.className 	= "dropdown_title_hover";
		this._HTMLlien.href 		= "#";
		this._HTMLlien.items 		= this.items;
		this._HTMLlien.dropdown		= this;
		this._HTMLlien.onclick 		= 	function(e)
										{ 
											if (!e) 
												var e = window.event;
											e.cancelBubble = true;
											if (e.stopPropagation)
												e.stopPropagation();

											this.items.switchAffichage();
											return false;
										};
		
		this._HTMLdiv 				= document.createElement("div");
		this._HTMLdiv.className 	= "dropdown_title";
		
		
		this._HTMLtext 				= document.createTextNode("");
		this._HTMLtext.data			= this.getSelectedItem().texte;
		
		this._HTMLdiv.appendChild( this._HTMLtext );
		this._HTMLlien.appendChild( this._HTMLdiv );
		
		this.HTMLElement.appendChild( this._HTMLlien );
		
		var tt = this.items._construireHTML();
		
		// Ajoutons une iframe en background pour passer par dessus les inputs
		if( this.isMSIE6 ){
			this._HTMLiframe 						= document.createElement("iframe");
			this._HTMLiframe.src					= "javascript:void(0)";
			this._HTMLiframe.frameBorder			= "no";
			this._HTMLiframe.scrolling				= "no";
			this._HTMLiframe.style.position			= "absolute";
			this._HTMLiframe.style.top				= (tt.style.top)+"";
			this._HTMLiframe.style.left				= "0px";
			this._HTMLiframe.style.borderStyle		= "none";
			this._HTMLiframe.style.backgroundColor	= "transparent";
			this._HTMLiframe.style.zIndex			= 0;
			this._HTMLiframe.style.display			= "none";
			this.HTMLElement.appendChild(this._HTMLiframe);
		}
		
		this.HTMLElement.appendChild( tt );
				
		this._builded = true;
		
		return true;
	}
	
	this._mettreAJourTexte = function ()
	{
		this._HTMLtext.data = this.getSelectedItem().texte;
	}
	
	this.getSelectedItem = function ( )
	{
		return this.items.getSelectedItem();
	}
}
///////////////////////////////////////////////////////////////////////
///
///////////////////////////////////////////////////////////////////////
function myDropDownItems( myDropDown )
{
	
	////////////////////////////////////////////////////////////////////////////
	this.items 				= new Array();
	this.parent 			= myDropDown;
	this._HTMLdiv 			= null;
	this._estVisible		= false;
	
	////////////////////////////////////////////////////////////////////////////
	this.ajouter = function ( newItem )
	{
		newItem.myDropDownItems = this;
		this.items.push( newItem );
	}
	
	this.supprimer = function ( curItem )
	{
		for( var i = 0; i < this.items.length; i++ )
		{
			if( this.items[i] == curItem )
			{
				this.items[i] = null;
				this._corrigerItems();
				return true;
			}
		}
	
		return false;
	}
	
	this._corrigerItems = function ()
	{
		var n = new Array();
		
		for( var i = 0; i < this.items.length; i++ )
		{
			if( this.items[i] != null )
			{
				n.push(this.items[i]);
			}
		}
		
		this.items = n;
	}
	
	this._construireHTML = function ()
	{
		if( this._HTMLdiv != null )
			return _HTMLdiv;
		
		this._HTMLdiv 				= document.createElement("div");
		this._HTMLdiv.className 	= "dropdown_items";
		
		
		for( var i=0; i < this.items.length; i++ )
		{
			this._HTMLdiv.appendChild( this.items[i]._construireHTML() );
		}
		
		return this._HTMLdiv;
	}
	
	this.estVisible = function ()
	{
		return this._estVisible;
	}
	
	this.switchAffichage = function ()
	{
		if( this.estVisible() )
		{
			this.cacher();
		}
		else
		{
			this.afficher();
		}
	}
	
	this.afficher = function ()
	{
		if( this._HTMLdiv == null )
			return false;
		
		this._HTMLdiv.style.display = "block";
		
		// Rendre l'iframe à 100% de hauteur et largeur
		if( this.parent.isMSIE6 )
		{
			this.parent._HTMLiframe.style.display	= "block";
			this.parent._HTMLiframe.style.width		= this._HTMLdiv.clientWidth;
			this.parent._HTMLiframe.style.height	= this._HTMLdiv.clientHeight;
		}
		
		this._estVisible = true;
	}
	
	this.cacher = function ()
	{
		if( this._HTMLdiv == null )
			return false;
		
		if( this.parent.isMSIE6 )
			this.parent._HTMLiframe.style.display	= "none";
		
		this._HTMLdiv.style.display 			= "none";
		this._estVisible 						= false;
		this.parent._mettreAJourTexte();
	}
	
	this._unSelectAll = function ()
	{
		for( var i = 0; i < this.items.length; i++ )
		{
			this.items[i]._isSelected = false;
		}
	}
	
	this.getSelectedItem = function ()
	{
		for( var i = 0; i < this.items.length; i++ )
		{
			if( this.items[i]._isSelected )
				return this.items[i];
		}
		return this.items[0];
	}
}
///////////////////////////////////////////////////////////////////////
///
///////////////////////////////////////////////////////////////////////
function myDropDownItem( texte , lien , isSelected )
{
	////////////////////////////////////////////////////////////////////////////
	this.myDropDownItems 	= null;
	this.texte 				= null;
	this.lien 				= null;
	this._HTMLlien 			= null;
	this._HTMLdiv 			= null;
	this._HTMLtext 			= null;
	this._HTML 				= null;
	this._isSelected		= isSelected;
	
	this.texte 				= texte;
	this.lien 				= lien;
	
	
	////////////////////////////////////////////////////////////////////////////
	this._construireHTML = function ()
	{
		if( this._HTML != null )
			return this._HTML;
		
		this._HTMLlien 				= document.createElement("a");
		this._HTMLlien.className 	= "dropdown_item_hover";
		this._HTMLlien.href 		= this.lien;
		
		this._HTMLdiv 				= document.createElement("div");
		this._HTMLdiv.className 	= "dropdown_item";
		this._HTMLdiv.myObject		= this;
		if( this.myDropDownItems.parent.isMSIE6 )
		{
			this._HTMLdiv.onmouseover	= function(){ this.style.backgroundColor="#405BA6"; this.myObject.onmouseOver(); }
			this._HTMLdiv.onmouseout	= function(){ this.style.backgroundColor="#FFFFFF"; }
		}
		else
		{
			this._HTMLdiv.onmouseover	= function(){ this.myObject.onmouseOver(); }
		}
		this._HTMLdiv.onclick		= function(){ return false; }
		
		this._HTMLtext 				= document.createTextNode("");
		this._HTMLtext.data			= this.texte;
		
		this._HTMLdiv.appendChild( this._HTMLtext );
		this._HTMLlien.appendChild( this._HTMLdiv );
		
		this._HTML = this._HTMLlien;
		
		return this._HTML;
	}
	
	this.isSelected = function () 
	{
		return this._isSelected;
	}
	
	this.setSelected = function () 
	{
		this.myDropDownItems._unSelectAll();
		this._isSelected = true;
	}
	
	this.onmouseOver = function ()
	{
		this.setSelected( );
	}
}