/**
* Auteur : Sébastien Braissant - The DevTowah
* Date de création : 15/08/2009
**/

/**
* Fonction définissant la pseudo-classe ddList, et constructeur de la classe
**/

function ddList (id,fname,vals,sel,objname) {
	/* Attributs */
	this.id = id;
	this.name = id;
	this.fname = fname;
	this.objname = objname;
	if (vals.constructor.toString().indexOf("Array") == -1)
		vals = new Array("&nbsp;");
	this.values = vals;
	if ((sel < 0) || (sel >= this.values.length) || (typeof sel != "number"))
		sel = 0;
	this.selIndex = sel;
	this.cssTopline = "ddTopline";
	this.cssList = "ddLbItem";
	this.cssSelected = "ddLbSelected";
	this.output = '';
	this.output += '<ul class="ddTopline" id="'+this.id+'_UlTl" onclick="'+this.objname+'.ddSwitch();" ';
	if (navigator.userAgent.match("MSIE"))
		this.output += 'style="width:84px;" ';
	this.output += '><li class="ddTlClosed" id="'+this.id+'_LiTl">'+this.values[this.selIndex]+'</li>';
	this.output += '</ul>';
	this.output += '<ul class="ddListbody" id="'+this.id+'_UlLb" style="display:none;">';
	var clique = "";
	var lid = "";
	for (var i=0;i < this.values.length;i++) {
		if (navigator.userAgent.match("MSIE")) {
			lid = this.name + "_LiLb_" + i.toString();
			this.output += '<a href="javascript:'+this.objname+'.ddSelect(\''+lid+'\');" class="link_'+this.cssList+'">';
			clique = '';
		}
		else {
			clique = ' onclick="'+this.objname+'.ddSelect(this.id);"';
		}
		this.output += '<li class="';
		if (this.selIndex == i)
			this.output += this.cssSelected;
		else
			 this.output += this.cssList;
		this.output += '" id="'+this.id+'_LiLb_'+i.toString()+'"'+clique+' onmouseover="'+this.objname+'.ddMouseover(this.id);">'+this.values[i]+'</li>';
		if (navigator.userAgent.match("MSIE"))
			this.output += '</a>';
	}
	
	/*this.output += '<li class="ddLbItem" id="'+this.id+'_LiLb_0">small</li>';
	this.output += '<li class="ddLbItem" id="'+this.id+'_LiLb_1">medium</li>';
	this.output += '<li class="ddLbItem" id="'+this.id+'_LiLb_2">large</li>';
	this.output += '<li class="ddLbSelected" id="'+this.id+'_LiLb_3">extra-large</li>';*/
	this.output += '</ul>';
	this.output += '<input type="hidden" id="'+this.id+'" name="'+this.id+'" value="'+this.values[this.selIndex]+'"/>';
	this.output += '<input type="hidden" id="lastHover" name="lastHover" value="" />';
}

	/* Méthodes */

function ddOpen () {
	with (this) {
		document.getElementById(this.name+"_UlTl").style.backgroundPosition = "2px -11px";
		document.getElementById(this.name+"_UlLb").style.display = "block";
	}
}

function ddClose () {
	with (this) {
		document.getElementById(this.name+"_UlTl").style.backgroundPosition = "2px 6px";
		document.getElementById(this.name+"_UlLb").style.display = "none";
		document.getElementById(this.name+"_LiLb_"+this.selIndex).className = this.cssSelected;
	}
}

function ddSwitch () {
	with (this) {
		if (document.getElementById(this.name+"_UlLb").style.display == "none")
			ddOpen();
		else
			ddClose();
	}
}

function ddMouseover (fid) {
  with (this) {
  	if (!document.getElementById(fid).className.match("Selected")) {
  		document.getElementById(this.name+"_LiLb_"+this.selIndex).className = this.cssList;
  	}
  	var tmp = fid.split("_");
  	document.getElementById(this.fname).elements["lastHover"].value = tmp[2];
  }
}

function ddMouseout (fid) {
	with (this) {
		
	}
}

function ddSelect (fid) {
	with (this) {
		var tmp = fid.split("_");
  	var n = tmp[2];
  	document.getElementById(this.fname).elements[this.name].value = this.values[n];
  	this.selIndex = n;
  	document.getElementById(this.name+"_LiTl").innerHTML = this.values[n];
  	this.ddClose();
  	if (navigator.userAgent.match("MSIE"))
  		this.ddReloadList();
	}
}

function ddReloadList () {
	with (this) {
		var clique = "";
  	var lid = "";
  	var liste = "";
  	for (var i=0;i < this.values.length;i++) {
  		if (navigator.userAgent.match("MSIE")) {
  			lid = this.name + "_LiLb_" + i.toString();
  			liste += '<a href="javascript:'+this.objname+'.ddSelect(\''+lid+'\');" class="link_'+this.cssList+'">';
  			clique = '';
  		}
  		else {
  			clique = ' onclick="'+this.objname+'.ddSelect(this.id);"';
  		}
  		liste += '<li class="';
  		if (this.selIndex == i)
  			liste += this.cssSelected;
  		else
  			 liste += this.cssList;
  		liste += '" id="'+this.id+'_LiLb_'+i.toString()+'"'+clique+' onmouseover="'+this.objname+'.ddMouseover(this.id);">'+this.values[i]+'</li>';
  		if (navigator.userAgent.match("MSIE"))
  			liste += '</a>';
  	}
  	document.getElementById(this.id+"_UlLb").innerHTML = liste;
	}
}

ddList.prototype.ddOpen = ddOpen;
ddList.prototype.ddClose = ddClose;
ddList.prototype.ddSwitch = ddSwitch;
ddList.prototype.ddMouseover = ddMouseover;
ddList.prototype.ddMouseout = ddMouseout;
ddList.prototype.ddSelect = ddSelect;
ddList.prototype.ddReloadList = ddReloadList;


