/**
* 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,captions,sel,objname,nbl) {
	/* Attributs */
	this.id = id;
	this.name = id;
	this.fname = fname;
	this.objname = objname;
	if (vals.constructor.toString().indexOf("Array") == -1 || captions.constructor.toString().indexOf("Array") == -1 || vals.length != captions.length) {
		vals = new Array("&nbsp;");
		captions = new Array("&nbsp;");
	}
		
	this.values = vals;
	this.labels = captions;
	if ((sel < 0) || (sel >= this.values.length) || (typeof sel != "number"))
		sel = 0;
	this.selIndex = sel;
	this.firstSel = sel;
	this.cssTopline = "ddTopline";
	this.cssList = "ddLbItem";
	this.cssSelected = "ddLbSelected";
	this.lineHeight = 18;
	if (nbl == 0 || nbl > this.values.length) {
		this.nbl = this.values.length;
		this.scrollbar = false;
	}
	else {
		this.nbl = nbl;
		this.scrollbar = true;
	}
	this.innerH = this.nbl*this.lineHeight
	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.labels[this.selIndex]+'</li>';
	this.output += '</ul>';
	this.output += '<div class="ddListbodyBax" id="'+this.id+'_UlLb" style="visibility:hidden;height:'+this.innerH+'px;">';
	this.output += '<ul class="ddListbody" id="'+this.id+'_UlLb_scrollIt"';
	this.output += this.scrollbar ? ' style="float:left;top:0px;">' : '>';
	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;
		if (i == this.values.length-1) {
			this.output += ' style="border-bottom-left-radius:4px;border-bottom-right-radius:4px;';
			this.output += this.scrollbar ? '"' : 'padding-right:20px;"';
		}
		else if (!this.scrollbar)
			this.output += ' style="padding-right:20px;"';
		this.output += ' onmouseover="'+this.objname+'.ddMouseover(this.id);">'+this.labels[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>';
	if (this.scrollbar) {
		//this.output += '<div style="float:left;width:20px;height:180px;"></div>';
		this.output += '<div id="'+this.id+'_UlLb_scrollBar" class="scrollBax" style="height:'+(this.innerH-16)+'px;">';
		this.output += '<div id="'+this.id+'_UlLb_scrollBtn" class="scrollBtn" onmousedown="dragscroll(event);"></div></div>';
	}
	this.output += '</div>';
	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";
		var pos = getPos(document.getElementById(this.id+"_UlTl"));
		var lb = this.name.match("status") ? parseInt(pos.l)+1 : parseInt(pos.l);
		document.getElementById(this.name+"_UlLb").style.left = lb.toString()+"px";
		if (!navigator.userAgent.match("MSIE"))
			var scrolltop = window.pageYOffset;
		else
			var scrolltop = document.body.scrollTop;
		document.getElementById(this.name+"_UlLb").style.top = (parseInt(pos.t)+19-scrolltop).toString()+"px";
		document.getElementById(this.name+"_UlLb").style.display = "block";
		if (document.getElementById(this.name+"_UlLb_scrollIt").offsetTop == 0 && this.scrollbar) {
			if (this.selIndex < this.values.length-this.nbl)
				scrolleDown(this.selIndex*2,9,8,this.id+"_UlLb");
			else
				scrolleDown((this.values.length-this.nbl)*2,9,8,this.id+"_UlLb");
		}
		document.getElementById(this.name+"_UlTl").className = "ddToplineOpen";
		if (document.getElementById("paramForm").elements["opendList"].value != this.objname) {
			if (document.getElementById("paramForm").elements["opendList"].value != '')
				eval(document.getElementById("paramForm").elements["opendList"].value.toString()+".ddClose();");
			document.getElementById("paramForm").elements["opendList"].value = this.objname;
		}
	}
}

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+"_UlTl").style.borderBottomLeftRadius = "5px";
		document.getElementById(this.name+"_UlTl").style.borderBottomRightRadius = "5px";
		document.getElementById(this.name+"_UlTl").style.backgroundColor = "#DED0B8";*/
		document.getElementById(this.name+"_UlTl").className = "ddTopline";
		//document.getElementById(this.name+"_UlTl").style.borderBottom = "1px solid #dadada";
		document.getElementById(this.name+"_LiLb_"+this.selIndex).className = this.cssSelected;
		document.getElementById("paramForm").elements["opendList"].value = '';
	}
}

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];
		var pid = this.name.substr(6,this.name.length-6);
  	document.getElementById(this.fname).elements[this.name].value = this.values[n];
  	if (this.selIndex != parseInt(n) && this.name.match("status"))
  		document.getElementById("updBtn_"+pid).style.display = "block";
  	this.selIndex = n;
  	document.getElementById(this.name+"_LiTl").innerHTML = this.labels[n];
  	this.ddClose();
  	if (navigator.userAgent.match("MSIE"))
  		this.ddReloadList();
	}
}

function ddSetWidth () {
	with (this) {
		var lw = (document.getElementById(this.id+"_UlLb").offsetWidth-20);
		document.getElementById(this.id+"_UlTl").style.width = lw.toString()+"px";
		document.getElementById(this.id+"_UlLb").style.width = (20+lw).toString()+"px";
		document.getElementById(this.id+"_UlLb").style.display = "none";
		document.getElementById(this.id+"_UlLb").style.visibility = "visible";
		document.getElementById(this.id+"_UlLb").style.textAlign = "center";
		document.getElementById(this.id+"_UlLb").style.textIndent = "0px";
		document.getElementById(this.id+"_UlLb").style.paddingRight = "0px";
	}
}

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.labels[i]+'</li>';
  		if (navigator.userAgent.match("MSIE"))
  			liste += '</a>';
  	}
  	document.getElementById(this.id+"_UlLb").innerHTML = liste;
	}
}

function ddReset () {
	with (this) {
		var fid = this.name + "_LiLb_" + this.firstSel.toString();
		this.ddSelect(fid);
	}
}

function ddFollowScroll () {
	with (this) {
		var pos = getPos(document.getElementById(this.id+"_UlTl"));
		if (!navigator.userAgent.match("MSIE"))
			var scrolltop = window.pageYOffset;
		else
			var scrolltop = document.body.scrollTop;
		document.getElementById(this.name+"_UlLb").style.top = (parseInt(pos.t)+19-scrolltop).toString()+"px";
	}
}

function captureScroll () {
	if (document.getElementById("paramForm").elements["opendList"].value != "") {
		var obj = document.getElementById("paramForm").elements["opendList"].value;
		eval(obj.toString()+".ddFollowScroll();");
	}
}

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.ddSetWidth = ddSetWidth;
ddList.prototype.ddReloadList = ddReloadList;
ddList.prototype.ddReset = ddReset;
ddList.prototype.ddFollowScroll = ddFollowScroll;
window.onscroll = captureScroll;



