var allSliders = new Array();
var mouseDocX = 0;

function $ (id) {
	return document.getElementById(id);
}


function HorizontalSlider (varName, min, max, labelFreq, isPercentage) {
	
	this.varName = varName;
	this.min = min;
	this.max = max;
	this.labelFreq = labelFreq;
	this.isPercentage = isPercentage;
	this.arrowWidth = 15;
	this.value = 0;
	this.interval = null;
	this.div;
	this.resultBox;
	
	allSliders.push(this);
	
	this.drawInside = function (elementId) {
		var e = $(elementId);
		var that = this;
		
		this.div = document.createElement('div');
		this.div.setAttribute('id', this.varName + "Slider");
		this.div.className = 'hSlider';
		this.resultBox = document.createElement('div');
		this.resultBox.setAttribute('id', this.varName + "Result");
		this.resultBox.className = 'hSliderResult';
		
		e.appendChild(this.div);
		this.div.appendChild(this.resultBox);
		
		for (var i=this.min; i <= this.max; i++) {
			if (i % this.labelFreq == 0) {
				var temp = document.createElement('div');
				var width = this.div.offsetWidth/((this.max - this.min)/labelFreq);
				temp.className = 'hSliderScaleSection';
				temp.style.width = width + "px"; 
				if (i >= 1000) {
					temp.innerHTML = (i/1000) + 'k';
				}
				else {
					temp.innerHTML = i;	
				}
				if (this.isPercentage) {
					temp.innerHTML+= '%';
				}
				this.div.appendChild(temp);
				temp.style.left = (((i/this.labelFreq) * width) - width/2) + "px";
			}
		}
		
		this.div.onmousedown = function (ev) {
			ev = (ev || window.event);
			stopAllSliders();
			that.interval = setInterval(that.varName + ".updatePosition()", 10);
			return false;
		};
		
		this.div.onmouseup = function () {
			stopAllSliders();	
		};
		
		e.onselectstart = function() {return false;}
		this.setValue(0);
	};
	
	
	this.updatePosition = function () {
		var x = mouseDocX - this.div.offsetLeft;
		var width = this.div.offsetWidth;
		if (x < 0) {
			x = 0;	
		}
		if (x > width) {
			x = width;
		}
		this.setValue(Math.round((this.min + ((x/width) * this.max)) * 100) / 100);
	};
	
	
	this.setValue = function (value) {
		var pixelVal = (this.max - this.min)/this.div.offsetWidth;

		this.value = "" + value + "";
		this.div.style.backgroundPosition = ((this.value/pixelVal) - Math.ceil(this.arrowWidth/2)) + 'px 0px';
		
		if (this.value == Math.floor(this.value)) {
			this.value += ".00";	
		}
		if (this.value.charAt(this.value.length - 2) == '.') {
			this.value+= "0";	 
		}
		this.resultBox.innerHTML = addCommas(this.value);

		$('drakeTotal').innerHTML = getDrakeTotal();
	};
	
	
	this.getCalcValue = function () {
		if (this.isPercentage) {
			return this.value/100;	
		}
		return this.value;
	};
	
	
	this.stop = function () {
		clearInterval(this.interval);
	};
}
	

function getDrakeTotal () {
	var drakeTotal = allSliders[0].getCalcValue();
	for (var i=1; i < allSliders.length; i++) {
		drakeTotal*= allSliders[i].getCalcValue();
	}		
	return addCommas(Math.round(drakeTotal * 10)/10);
}


function stopAllSliders () {
	for (var i=0; i < allSliders.length; i++) {
		allSliders[i].stop();
	}
}


function updateMousePos (ev) {
	if (window.event) {
		mouseDocX = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	}
	else {
		mouseDocX = ev.pageX;
	}
}


function stopEvent (ev) {
	if (ev) {
		if (ev.stopPropagation) ev.stopPropagation();
		else ev.cancelBubble = true;
	}			
}

			
function getWindowWidth () {
	if (window.innerWidth) {
		return window.innerWidth;
	}
	else if (doc.body && doc.body.clientWidth) {
		return doc.body.clientWidth;
	}
	else if (doc.docElement && doc.docElement.clientWidth) {
		return doc.docElement.clientWidth;
	}
	return 0;
}


function addCommas (nStr)	{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}		



function loadDrakeValues (select) {
	
	var valuesString = select.options[select.selectedIndex].value;
	
	if (valuesString) { 
		var vals = valuesString.split('|');
		
		for (var i=0; i < allSliders.length; i++) {
			allSliders[i].setValue(vals[i]);
		}
		select.options[0].text = "[" + select.options[select.selectedIndex].text + "]";
		select.selectedIndex = 0;
	}
}



function showTextOnly () {
	var text = '<pre>';
	var sliders = $('sliders');
	var equation = $('equation');
	var textOnly = $('textOnlyValues');
	
	text+= "New Milky Way stars per year = " + allSliders[0].value + "\n";
	text+= "Proportion of stars which have planets = " + allSliders[1].value + "%\n";
	text+= "Average number of life-compatible satellites = " + allSliders[2].value + "\n";
	text+= "Percentage of planets where life does appear = " + allSliders[3].value + "%\n";
	text+= "Percentage where intelligent life evolves = " + allSliders[4].value + "%\n";
	text+= "Percentage of civilizations which send signals into space = " + allSliders[5].value + "%\n";
	text+= "Average years that civilizations will send signals = " + allSliders[6].value + "\n\n";
	
	text+= "Average civilizations in our galaxy = " + getDrakeTotal() + "\n</pre>";

	textOnly.innerHTML = text;
	equation.style.display = 'none';
	sliders.style.display = 'none';
	textOnly.parentNode.style.display = 'block';
}


function showSliders () {
	var sliders = $('sliders');
	var equation = $('equation');
	var textOnly = $('textOnlyValues');
	
	equation.style.display = 'none';
	sliders.style.display = 'block';
	textOnly.parentNode.style.display = 'none';	
}


function showEquation () {
	var sliders = $('sliders');
	var equation = $('equation');
	var textOnly = $('textOnlyValues');
	
	equation.style.display = 'block';
	sliders.style.display = 'none';
	textOnly.parentNode.style.display = 'none';		
}


if (document.addEventListener) {
	document.addEventListener('mouseup', stopAllSliders, false);
	document.addEventListener('mousemove', updateMousePos, false);
}
else if (document.attachEvent) {
	document.documentElement.attachEvent('onmouseup', stopAllSliders);
	document.documentElement.attachEvent('onmousemove', updateMousePos);
}


var stars = new HorizontalSlider("stars", 0,     10,    2, false);
var pws =   new HorizontalSlider("pws",   0,    100,   25, true);
var lsp =   new HorizontalSlider("lsp",   0,     10,    2, false);
var lhp =   new HorizontalSlider("lhp",   0,    100,   25, true);
var ilp =   new HorizontalSlider("ilp",   0,     20,    5, true);
var isc =   new HorizontalSlider("isc",   0,    100,   25, true);
var isd =   new HorizontalSlider("isd",   0,  20000, 5000, false);


