// Select Object
// Copyright 1998 Dan Steinman
// Available at the Dynamic Duo (http://www.dansteinman.com/dynduo/)
// May 23, 1998.  Last Updated October 17, 1998.
// In order to use this code you must keep this disclaimer

ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false

function Select(name,nestref,x,y,image,width,height) {
	this.name = name
	this.nestref = nestref
	this.nest = (nestref)? nestref+'.document.' : ""
	this.x = x
	this.y = y
	this.w = width
	this.h = height
	this.image = image
	this.slideInc = 7
	this.speed = 20
	this.listH = 1
	this.optionText = new Array()
	this.optionValues = new Array()
	this.hl = -1
	this.value = null
	this.onChange = null
	this.visibility = null
	this.zIndex = null
	this.div = ''
	this.css = ''
	this.setDimensions = SelectSetDimensions
	this.setDimensions(this.w,13,3,0,4,1,0)
	this.setFont = SelectSetFont
	this.setFont('Arial',8)
	this.setColors = SelectSetColors
	this.setColors('white','black','black','white','black','black')
	this.addOptions = SelectAddOptions
	this.toggle = SelectToggle
	this.build = SelectBuild
	this.activate = SelectActivate
}
function SelectSetDimensions(optionW,optionH,optionX,optionY,textX,textY,listX) {
	this.optionW = optionW
	this.optionH = optionH
	if (arguments.length==7) {
		this.optionX = optionX
		this.optionY = optionY
		this.textX = textX
		this.textY = textY
		this.listX = listX
	}
}
function SelectSetFont(fontName,size,other) {
	this.font = fontName
	this.size = size
	this.other = (other)? other : ""
}
function SelectSetColors(bgOff,bgOn,optionOff,optionOn,text,border) {
	this.bgOff = bgOff
	this.bgOn = bgOn
	this.optionOff = optionOff
	this.optionOn = optionOn
	this.textColor = text
	this.brColor = border
}
function SelectAddOptions() {
	for (var i=0;i<arguments.length/2;i++) {
		this.optionText[i] = arguments[2*i]
		this.optionValues[i] = arguments[2*i+1]
		this.listH += this.optionH
	}
}
function SelectBuild() {
	this.css = '<STYLE TYPE="text/css">\n'+
	css(this.name+'Select',this.x,this.y,this.w,this.h,null,this.visibility,this.zIndex)+
	css(this.name+'Bar',0,0)+
	css(this.name+'BarC',0,0,this.w,this.h)+
	css(this.name+'Text',this.textX,this.textY,this.w)+
	css(this.name+'List',this.listX,-this.listH+this.h,this.optionW,this.listH,this.brColor)+
	css(this.name+'ListI',1,0,this.optionW-2,this.listH-1)
	for (var i=0;i<this.optionText.length;i++) {
		this.css += css(this.name+'Option'+i,0,i*this.optionH,this.optionW-2,this.optionH,this.bgOff)+
		css(this.name+'Option'+i+'T',this.optionX,this.optionY)+
		css(this.name+'Option'+i+'C',0,i*this.optionH,this.optionW-2,this.optionH)
	}
	this.css += '.'+this.name+'TextStyle {font-family:"'+this.font+'"; font-size:'+this.size+'pt; color:'+this.textColor+'; '+this.other+'}\n'+
	'.'+this.name+'off {font-family:"'+this.font+'"; font-size:'+this.size+'pt; color:'+this.optionOff+'; '+this.other+'}\n'+
	'.'+this.name+'on {font-family:"'+this.font+'"; font-size:'+this.size+'pt; color:'+this.optionOn+'; '+this.other+'}\n'+
	'</STYLE>'
	this.div = '<DIV ID="'+this.name+'Select">\n'+
	'<DIV ID="'+this.name+'List">\n'+
	'<DIV ID="'+this.name+'ListI">\n'
	for (var i=0;i<this.optionText.length;i++) {
		this.div += '<DIV ID="'+this.name+'Option'+i+'"><DIV ID="'+this.name+'Option'+i+'T"><SPAN CLASS="'+this.name+'off">'+this.optionText[i]+'</SPAN></DIV></DIV>\n'+
		'<DIV ID="'+this.name+'Option'+i+'C"></DIV>\n'
	}
	this.div += '</DIV>\n'+
	'</DIV>\n'+
	'<DIV ID="'+this.name+'Bar">\n'+
	'<IMG NAME="'+this.name+'Img" SRC="'+this.image+'" WIDTH='+this.w+' HEIGHT='+this.h+'>\n'+
	'<DIV ID="'+this.name+'Text"><SPAN CLASS="'+this.name+'TextStyle">'+this.optionText[0]+'</SPAN></DIV>\n'+
	'</DIV>\n'+
	'<DIV ID="'+this.name+'BarC"></DIV>\n'+
	'</DIV>\n'
	document.write(this.css)
}
function SelectActivate() {
	this.lyr = new DynLayer(this.name+'Select',((this.nestref)?this.nestref:null))
	this.lyr.clipInit()
	this.lyr.clipTo(0,this.w,this.h,0)
	this.listlyr = new DynLayer(this.name+'List',this.nest+this.name+'Select')
	this.listlyr.slideInit()
	this.barlyr = new DynLayer(this.name+'BarC',this.nest+this.name+'Select')
	if (ns4) this.barlyr.event.captureEvents(Event.MOUSEDOWN)
	this.barlyr.event.onmousedown = new Function('SelectToggle('+this.name+')')
	for (var i=0;i<this.optionText.length;i++) {
		this.optionlyr = new Array()
		this.optionlyr[i] = new DynLayer(this.name+'Option'+i+'C',this.nest+this.name+'Select.document.'+this.name+'List.document.'+this.name+'ListI')
		if (ns4) this.optionlyr[i].event.captureEvents(Event.MOUSEDOWN | Event.MOUSEOVER)
		this.optionlyr[i].event.onmousedown = new Function('SelectChange('+this.name+','+i+')')
		this.optionlyr[i].event.onmouseover = new Function('SelectOver('+this.name+','+i+')')
		this.optionlyr[i].event.onmouseout = new Function('SelectOut('+this.name+','+i+')')
	}
}
function SelectOver(o,i) {
	setBGColor(o.name+'Option'+i,o.nest+o.name+'Select.document.'+o.name+'List.document.'+o.name+'ListI',o.bgOn)
	layerWrite(o.name+'Option'+i+'T',o.nest+o.name+'Select.document.'+o.name+'List.document.'+o.name+'ListI.document.'+o.name+'Option'+i,'<SPAN CLASS="'+o.name+'on">'+o.optionText[i]+'</SPAN>')
	return false
}
function SelectOut(o,i) {
	setBGColor(o.name+'Option'+i,o.nest+o.name+'Select.document.'+o.name+'List.document.'+o.name+'ListI',o.bgOff)
	layerWrite(o.name+'Option'+i+'T',o.nest+o.name+'Select.document.'+o.name+'List.document.'+o.name+'ListI.document.'+o.name+'Option'+i,'<SPAN CLASS="'+o.name+'off">'+o.optionText[i]+'</SPAN>')
	return false
}
function SelectChange(o,i) {
	layerWrite(o.name+'Text',o.nest+o.name+'Select.document.'+o.name+'Bar','<SPAN CLASS="'+o.name+'TextStyle">'+o.optionText[i]+'</SPAN>')
	o.value = o.optionValues[i]
	eval(o.onChange)
	o.toggle(o)
}
function SelectToggle(o) {
	if (!o.opened) {
		o.lyr.clipTo(null,null,o.listH+o.h,null)
		if (o.slideInc==null) {
			o.listlyr.moveTo(null,o.h)
			o.opened = true
		}
		else o.listlyr.slideTo(null,o.h,o.slideInc,o.speed,o.name+'.opened = true')
	}
	else {
		if (o.slideInc==null) {
			o.listlyr.moveTo(null,-o.listH+o.h)
			o.lyr.clipTo(null,null,o.h,null)
			o.opened = false
		}
		else o.listlyr.slideTo(null,-o.listH+o.h,o.slideInc,o.speed,o.name+'.opened = false; '+o.name+'.lyr.clipTo(0,'+o.w+','+o.h+',0);')
	}
	return false
}
function SelectRedirect(url) {
	if (url!='') document.location.href = url
	return false
}

// Required functions are css(), setBGColor() and layerWrite()

// Changes the background color of a layer
function setBGColor(id,nestref,color) {
	if (ns4) {
		var lyr = (nestref)? eval('document.'+nestref+'.document.'+id):document.layers[id]
		lyr.document.bgColor = color
	}
	else if (ie4) document.all[id].style.backgroundColor = color
}

// Re-writes the contents of a layer
function layerWrite(id,nestref,text) {
	if (ns4) {
		var lyr = (nestref)? eval('document.'+nestref+'.document.'+id):document.layers[id]
		lyr.document.open()
		lyr.document.write(text)
		lyr.document.close()
	}
	else if (ie4) document.all[id].innerHTML = text
}

// Generic CSS function to return CSS syntax with the given parameters
function css(id,left,top,width,height,color,vis,z) {
	var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'; top:'+top+'; ' : '#'+id+' {position:relative; '
	if (width!=null) str += 'width:'+width+'; '
	if (height!=null) str += 'height:'+height+'; clip:rect(0,'+width+','+height+',0); '
	if (color!=null) str += 'background-color:'+color+'; layer-background-color:'+color+'; '
	if (vis!=null) str += 'visibility: '+vis+'; '
	if (z!=null) str += 'z-index: '+z+'; '
	str += '}\n'
	return str
}