// P//MOD Select Formatter Class
function PmodSelect(selId){

	// Select ID
	this.selId = selId;
	
	// Init text
	this.text = "";
	
	// z-index;
	this.zindex = 1000;
	
	// Array with Select Values
	this.values = new Array();
	
	// Function name after select value
	this.postfunction = "";
	
	// Select Box
	this.selectform = $(this.selId);

	// Select List
	this.selectList = "";
	
	// Disabled Select Box
	this.disabled = false;
	
	// Menu height
	this.height = 0;

	// Menu height
	this.width = 0;
	
	// Helper
	this.selectLink = this.selId + "_pmodSelectLink";
	this.selectMenu = this.selId + "_pmodSelectMenu";
}
	
// Init Select Class
PmodSelect.prototype.init = function() {
	$(this.selId).style.display = "none";
	if (this.values.length == 0) {
		this.read();	
	}
	else {
		$(this.id).options.length = 0;
	}
	this.write();
	$(this.selectLink).style.display = "block";
	
}

// Reading Select
PmodSelect.prototype.read = function() {
	for (var i = 0; i < this.selectform.options.length; i++) {
		var valArr = new Array();
		valArr["value"] = this.selectform.options[i].value;
		valArr["text"] = this.selectform.options[i].text;
		this.values.push(valArr);
	}
}

// Writes the new select form
PmodSelect.prototype.write = function() {
	var output = "";
	output = output + '<div class="pmodSelectBox">';
	output = output + '<div class="pmodSelectText">';
	if (this.width != 0) {
	 	output = output + '<a style="width:' + this.width + 'px" onfocus="this.blur()" class="pmodSelectTextClose" href="Javascript:pmod_showSelect(\'' + this.selectMenu + '\',\'' + this.selectLink + '\',\'' + this.selectList + '\');" id="' + this.selectLink + '">' + this.text + '</a>';
	} else {
	 	output = output + '<a onfocus="this.blur()" class="pmodSelectTextClose" href="Javascript:pmod_showSelect(\'' + this.selectMenu + '\',\'' + this.selectLink + '\',\'' + this.selectList + '\');" id="' + this.selectLink + '">' + this.text + '</a>';
	}
	output = output + '</div>';
	if ((this.height != 0) || (this.width != 0)) {
		s = "";
		if (this.width != 0)
		{
			s = s + 'width:' + this.width + 'px;';
		}
		if (this.height != 0)
		{
			s = s + 'height:' + this.height + 'px;';
		}
		output = output + '<div style="overflow:auto;' + s + 'z-index:' + this.zindex + ';display:none;" id="' + this.selectMenu + '" class="pmodSelectMenu">';
	} else {
		output = output + '<div style="z-index:' + this.zindex + ';display:none;" id="' + this.selectMenu + '" class="pmodSelectMenu">';
	}
	for (var i=0; i<this.values.length; i++) {
		output = output + '<a href="Javascript:pmod_setSelect(\'' + this.selId + '_pmodSelectLink\',\'' + this.selId + '_pmodSelectMenu\',\'' + this.values[i]["text"] + '\',\'' + this.values[i]["value"] + '\',\'' + this.selId + '\',\'' + this.postfunction + '\')">';
		output = output + this.values[i]["text"];
		output = output + '</a>';
	}
	output = output + '</div>';
	output = output + '</div>';
	document.write(output);
}

// Writes the new select form
PmodSelect.prototype.update = function() {
	for (var i = 0; i < this.values.length; i++) {
		$(this.selId).options[i] = new Option(this.values[i]["text"], this.values[i]["value"]);
	}
	var output = "";
	for (var i=0; i<this.values.length; i++) {
		output = output + '<a href="Javascript:pmod_setSelect(\'' + this.selId + '_pmodSelectLink\',\'' + this.selId + '_pmodSelectMenu\',\'' + this.values[i]["text"] + '\',\'' + this.values[i]["value"] + '\',\'' + this.selId + '\',\'' + this.postfunction + '\')">';
		output = output + this.values[i]["text"];
		output = output + '</a>';
	}
	$(this.selectMenu).innerHTML = output;
	$(this.selectLink).innerHTML = this.text;
	$(this.selId).value = "";
}

// Disabled select
PmodSelect.prototype.disable = function() {
	$(this.selectLink).removeAttribute("href");
	$(this.selectLink).className = "pmodSelectTextDisabled";
	$(this.selectLink).style.color = "#dddddd";
	this.disabled = true;
}

PmodSelect.prototype.enable = function() {
	$(this.selectLink).setAttribute('href','Javascript:pmod_showSelect(\'' + this.selectMenu + '\',\'' + this.selectLink + '\',\'' + this.selectList + '\');');
	$(this.selectLink).className = "pmodSelectTextClose";
	$(this.selectLink).style.color = "#666666";
	this.disabled = false;
}

PmodSelect.prototype.closemenu = function(){
	Effect.Fade(this.selectMenu, { duration:0.1 });
	$(this.selectLink).className = "pmodSelectTextClose";
}

function pmod_isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

// Opens the Menu
function pmod_showSelect(m,t,l) {
	if (l != "")
	{
		sArr = l.split(",");
		for (var i = 0; i < sArr.length; i++) {
			eval(sArr[i] + ".closemenu()");
		}
	}
	if ($(m).style.display == "none")
	{
		$(t).className = "pmodSelectTextOpen";
		Effect.Appear(m, { duration:0.2 });
	}
	else
	{
		Effect.Fade(m, { duration:0.1 });
		$(t).className = "pmodSelectTextClose";
	}
}

// Sets the value
function pmod_setSelect(s,m,t,v,i,a) {
	$(s).innerHTML = t;
	$(s).className = "pmodSelectTextClose";
	$(m).style.display = "none";	
	$(i).value = v;
	if (a != "") {
		f = a + "()";
		eval(f);
	}
}
