// Copyright (c) 2003, Nathan A. Sweet. All rights reserved. Contact: misc@n4te.com
// This file may not be modified or distributed in any form without written permission.

function SelectBox ( divID, linkDiv, text ) {
	this.linkDiv = linkDiv;
	this.linkDiv.onmousedown = function ( e ) {
		e = e || window.event;
		e.cancelBubble = true;
	}

	this.id = SelectBox.getUID();

	this.div = document.getElementById( divID );
	this.div.innerHTML = "" +	
	   "<table cellspacing=0 cellpadding=0 border=0 width='100%'><tr>" +
		"<td><img src='/images/selectBox/default/boxleft.gif' width=5 height=22><br></td>" +
	   "<td width='100%' background='/images/selectBox/default/boxback.gif' class='selectText' id='" + this.id + "textCell'><nobr>" +
		( text ? text : "" ) +
	   "</nobr></td>" +
		"<td id='" + this.id + "buttonCell'>" +
			"<img src='/images/selectBox/default/boxbutton.gif' width=21 height=22 id='" + this.id + "upImage'>" +
			"<img src='/images/selectBox/default/boxbuttondown.gif' width=21 height=22 id='" + this.id + "downImage' style='display:none;'>" +
		"</td>" +
	   "</tr></table>";

	this.textCell = document.getElementById( this.id + "textCell" );
	this.buttonCell = document.getElementById( this.id + "buttonCell" );
	this.upImage = document.getElementById( this.id + "upImage" );
	this.downImage = document.getElementById( this.id + "downImage" );

	this.textCell.onmousedown = SelectBox.forwardEvent( this, "open" );
	if ( document.all ) this.textCell.onselectstart = function () { return false; }

	this.buttonCell.style.cursor = document.all ? "hand" : "pointer";
	this.buttonCell.onmousedown = SelectBox.forwardEvent( this, "open" );

	this.size();
}


SelectBox.prototype.oldMouseDown = null;


SelectBox.prototype.size = function () {
	this.linkDiv.className = "selectLinksLoad";

	this.linkDiv.style.display = "block";
	this.linkDiv.style.width = "auto";
	var width = this.linkDiv.offsetWidth + 16;  // Padding.
	var height = this.linkDiv.offsetHeight;
	this.linkDiv.style.display = "none";

	if ( height > 200 ) this.linkDiv.style.height = 200;

	if ( width - 24 > this.textCell.offsetWidth ) {
		this.textCell.style.width = width - 24;
		this.linkDiv.style.width = width;
	} else
		this.linkDiv.style.width = this.textCell.offsetWidth + 24;

	this.linkDiv.className = "selectLinks";
}


SelectBox.prototype.open = function ( e ) {
	if ( SelectBox.openInstance ) {
		if ( SelectBox.openInstance == this ) return;
		SelectBox.openInstance.close();
	}

	this.size();

	SelectBox.openInstance = this;

	this.upImage.style.display = "none";
	this.downImage.style.display = "block";
	var curParent = this.div, top = 21, left = 1;
	while ( curParent && curParent.style.position != "absolute" ) {
		top += curParent.offsetTop;
		left += curParent.offsetLeft;
		curParent = curParent.offsetParent;
	}
	this.linkDiv.style.top = top;
	this.linkDiv.style.left = left;


	if ( document.all && navigator.userAgent.indexOf("MSIE 5.") == -1 ) this.linkDiv.filters[ 0 ].Apply();
	this.linkDiv.style.display = "block";
	if ( document.all && navigator.userAgent.indexOf("MSIE 5.") == -1 ) this.linkDiv.filters[ 0 ].Play();

	this.oldMouseDown = document.body.onmousedown;
	document.body.onmousedown = SelectBox.forwardEvent( this, "close" );
	e.cancelBubble = true;
}


SelectBox.prototype.setText = function ( text ) {
	this.textCell.innerHTML = "<nobr>" + text + "</nobr>";
}


SelectBox.prototype.close = function ( e ) {
	this.setButtonUp();
	this.linkDiv.style.display = "none";
	if ( this.oldMouseDown ) document.body.onmousedown = this.oldMouseDown;
	if ( SelectBox.openInstance == this ) SelectBox.openInstance = null;
}


SelectBox.prototype.setButtonUp = function () {
	this.upImage.style.display = "block";
	this.downImage.style.display = "none";
}


SelectBox.instances = [];
SelectBox.register = function ( divID, linkDivID, text ) {
	var linkDiv = document.getElementById( linkDivID );
	linkDiv.className = "selectLinksLoad";

	SelectBox.instances[ divID ] = new SelectBox( divID, linkDiv, text );
}


SelectBox.init = function () {
	for ( var divID in SelectBox.instances )
		SelectBox.instances[ divID ].size();
}
register( SelectBox.init );

SelectBox.forwardEvent = function ( instance, method ) {
	return function ( e ) {
		if ( !e ) e = window.event;
		instance[ method ]( e );
	}
}


SelectBox.UID = 0;
SelectBox.getUID = function () {
	return "SelectBox" + SelectBox.UID++;
}


SelectBox.openInstance = null;


