// filename: murdercount.js
// Containing the actual functions for the Hyperghetto
// Distributed under the terms of the GNU Library General Public License
// version 1.01 - 2002-11-28
// author: martin@bembelterror.de

// window.onerror = null

function homeInit() {
	// initialize browsercheck library
	is = new BrowserCheck();
	// alert warning for NS4
	if (is.ns4) {
		if (confirm('This application uses a lot of RAM which can possibly freeze or crash Netscape 4. No problems though with Mozilla or Netscape 7 or later. Please press "cancel" to skip this application and go to the theory page, or "ok" to start at your own risk.')) {}
		else document.location.href = "theory/";
		}
	// set division factor
	divFactor = (is.mac) ? 6 : 10;
	if (is.min && is.opera == false) {
		// initialize DynLayer object library
		DynLayerInit();
		// get position values
		getPositions(true);
	}
	// check RAM performance
	checkRAM();
}

function getPositions(firsttime) {
	// get window attributes
	fenster = new screenObject();
	if (fenster.visWidth && fenster.visHeight) {
		elmAvailWidth = Math.floor(fenster.visWidth / 3);
		elmAvailHeight = Math.floor((fenster.visHeight - 10 - 30) / 7);
		
		var newPosX = new Array(10, elmAvailWidth+10, 2*(elmAvailWidth+10));
		var newPosY = new Array(elmAvailHeight+8, 2*(elmAvailHeight+8), 3*(elmAvailHeight+8), 4*(elmAvailHeight+8), 5*(elmAvailHeight+8), 6*(elmAvailHeight+8));
	}
	
	// x-positions
	var origPosX = new Array(20,350,680);
	// y-positions
	var origPosY = new Array(75,170,265,360,455,550);
	
	// make sort array with random numbers
	if (firsttime) {
		for (i = 0; i < counterPos.length; i++) randomNum[i] = 0;
		for (i = 0; i < counterPos.length; i++) {
			do num = Math.round(Math.random() * counterPos.length)
			while (randomNum[num] != 0)
			randomNum[num] = i;
		}
	}
	
	// fill position array
	var num = 0;
	for (var i = 0; i < origPosX.length; i++) {
		for (var j = 0; j < origPosY.length; j++) {
			counterPos[randomNum[num]] = new Array(origPosX[i], origPosY[j]);
			if (fenster.visWidth && fenster.visHeight) newCounterPos[randomNum[num]] = new Array(newPosX[i], newPosY[j]);
			num++;
		}
	}
}
// position arrays
var counterPos = new Array(18);
var randomNum = new Array();
var newCounterPos = new Array(18);
var elmAvailWidth, elmAvailHeight, elmAvailFontsize;

function showAndPositionCounters() {
	// re-position elements
	if (is.min && is.opera == false) {
		for (var i = 1; i <= 18; i++) MurderCity[i].parent.moveTo(counterPos[i-1][0], counterPos[i-1][1]);
	}
	// show elements
	if (is.opera) document.getElementById('timeCounterDiv').style.visibility = 'visible';
	else if (is.min) timeCounter.show();
	for (var i = 1; i <= 18; i++) {
		if (is.opera) document.getElementById(MurderCity[i].parentDiv).style.visibility = 'visible';
		else if (is.min) {
			MurderCity[i].parent.show();
			// if (fenster.visWidth && fenster.visHeight) MurderCity[i].parent.slideTo(newCounterPos[i-1][0], newCounterPos[i-1][1], 25, 20);
		}
	}
	// if (fenster.visWidth && is.min && is.opera == false) timeCounter.resizeTo(fenster.visWidth, null);
	
	setTimeout('pageStart()', 10);
}

function counterInit() {
	CounterObj = new SetCounterObj;
	MurderCity[1]	= new SetMurderObj(10.53,	1,	'Medellin');
	MurderCity[2]	= new SetMurderObj(0.03,	2,	'Lagos');
	MurderCity[3]	= new SetMurderObj(0.2,		3,	'London');
	MurderCity[4]	= new SetMurderObj(0.14,	4,	'Wentworth');
	MurderCity[5]	= new SetMurderObj(5.66,	5,	'New York');
	MurderCity[6]	= new SetMurderObj(16.5,	6,	'Baixada Fluminense');
	MurderCity[7]	= new SetMurderObj(0.28,	7,	'Hong Kong');
	MurderCity[8]	= new SetMurderObj(5.89,	8,	'Moskau');
	MurderCity[9]	= new SetMurderObj(0.26,	9,	'Berlin');
	MurderCity[10]	= new SetMurderObj(0.005,	10,	'Stockholm');
	MurderCity[11]	= new SetMurderObj(13.5,	11,	'Natal (South-Africa)');
	MurderCity[12]	= new SetMurderObj(0.2,		12,	'Sidney');
	MurderCity[13]	= new SetMurderObj(8.01,	13,	'Caracas');
	MurderCity[14]	= new SetMurderObj(0.13,	14,	'Barcelona');
	MurderCity[15]	= new SetMurderObj(9.81,	15,	'Western Cape');
	MurderCity[16]	= new SetMurderObj(0.5,		16,	'Tokyo');
	MurderCity[17]	= new SetMurderObj(0.04,	17,	'Singapur');
	MurderCity[18]	= new SetMurderObj(11.1,	18,	'Soweto');
	
	if (is.ns4) startClockNs4();
	else startClock();
	for (var i = 1; i <= 18; i++) {
		MurderCity[i].murderCount();
	}
		
	showAndPositionCounters();
}
	
function pageStart() {
	// start iteration
	counterID[0] = (is.ns4) ? setInterval("startClockNs4()", 100) : setInterval("startClock()", 50);
	counterID[1] = setInterval("MurderCity[1].murderCount()", MurderCity[1].msFiveDigitRate);
	counterID[2] = setInterval("MurderCity[2].murderCount()", MurderCity[2].msFiveDigitRate);
	counterID[3] = setInterval("MurderCity[3].murderCount()", MurderCity[3].msFiveDigitRate);
	counterID[4] = setInterval("MurderCity[4].murderCount()", MurderCity[4].msFiveDigitRate);
	counterID[5] = setInterval("MurderCity[5].murderCount()", MurderCity[5].msFiveDigitRate);
	counterID[6] = setInterval("MurderCity[6].murderCount()", MurderCity[6].msFiveDigitRate);
	counterID[7] = setInterval("MurderCity[7].murderCount()", MurderCity[7].msFiveDigitRate);
	counterID[8] = setInterval("MurderCity[8].murderCount()", MurderCity[8].msFiveDigitRate);
	counterID[9] = setInterval("MurderCity[9].murderCount()", MurderCity[9].msFiveDigitRate);
	counterID[10] = setInterval("MurderCity[10].murderCount()", MurderCity[10].msFiveDigitRate);
	counterID[11] = setInterval("MurderCity[11].murderCount()", MurderCity[11].msFiveDigitRate);
	counterID[12] = setInterval("MurderCity[12].murderCount()", MurderCity[12].msFiveDigitRate);
	counterID[13] = setInterval("MurderCity[13].murderCount()", MurderCity[13].msFiveDigitRate);
	counterID[14] = setInterval("MurderCity[14].murderCount()", MurderCity[14].msFiveDigitRate);
	counterID[15] = setInterval("MurderCity[15].murderCount()", MurderCity[15].msFiveDigitRate);
	counterID[16] = setInterval("MurderCity[16].murderCount()", MurderCity[16].msFiveDigitRate);
	counterID[17] = setInterval("MurderCity[17].murderCount()", MurderCity[17].msFiveDigitRate);
	counterID[18] = setInterval("MurderCity[18].murderCount()", MurderCity[18].msFiveDigitRate);
	}
var start = new Date(2003, 01, 01);
var now = new Array();
var timeDifference = new Array();
var counterID = new Array();
var MurderCity = new Array();
var divFactor = 1;
	
function startClock() {
	now[0] = new Date();
	timeDifference[0] = now[0] - start;
	document.getElementById('timeCounterNodeDiv').innerHTML = CounterObj.getCurrentTime();
	}

function startClockNs4() {
	now[0] = new Date();
	timeDifference[0] = now[0] - start;
	document.layers['timeCounterDiv'].document.open();
	document.layers['timeCounterDiv'].document.write('<div class="bigBoldTime">' + CounterObj.getCurrentTime() + '</div>');
	document.layers['timeCounterDiv'].document.close();
	}
	
function murderIterate() {
	now[this.num] = new Date();
	timeDifference[this.num] = now[this.num] - start;
	document.getElementById(this.div).innerHTML = this.getCurrentMurders();
	}
	
function murderIterateNs4() {
	// alert(this.parentDiv + ',' + this.div + ',' + this.getCurrentMurders())
	now[this.num] = new Date();
	timeDifference[this.num] = now[this.num] - start;
	document.layers[this.parentDiv].document.layers[this.div].document.open();
	document.layers[this.parentDiv].document.layers[this.div].document.write('<div class="bigBoldText">' + this.getCurrentMurders() + '</div>');
	document.layers[this.parentDiv].document.layers[this.div].document.close();
	}
	
function SetCounterObj() {
	this.getCurrentTime = currentTime;
	}
	
function SetMurderObj(mrpd, num, city) {
	this.num = num;
	this.div = (is.ns4) ? 'murderCount' + this.num  + 'Div' : 'murderCountText' + this.num  + 'Div';
	this.parentDiv = 'cityCounter' + this.num  + 'Div';
	this.parent = eval('cityCounter' + this.num);
	this.place = 'cityPlace' + this.num + 'Div';
	// this.city = city;
	this.mrpd = mrpd;
	// this.mrpms = this.mrpd / 86400000;
	this.msFiveDigitRate = Math.floor(864 / this.mrpd);
	if (this.msFiveDigitRate < (testDifference / divFactor)) this.msFiveDigitRate = Math.floor(testDifference / divFactor);
	// alert(testDifference + ',' + divFactor + ',' + this.msFiveDigitRate)
	this.getCurrentMurders = currentMurders;
	this.murderCount = (is.ns4) ? murderIterateNs4 : murderIterate;
	}
	
function currentTime() {
	this.diffDate = new Date(timeDifference[0]);

	this.years = this.diffDate.getFullYear() - 1970;
	if (this.years < 10) this.years = '0' + this.years;

	this.hours = this.diffDate.getHours();
	if (this.hours < 10) this.hours = '0' + this.hours;
	
	this.days = Math.floor(timeDifference[0] / 86400000);
	if (parseInt(this.hours) == 0) this.days = this.days + 1;
	if (parseInt(this.days) < 10) this.days = '00' + this.days;
	else if (parseInt(this.days) < 100) this.days = '0' + this.days;
	
	this.minutes = this.diffDate.getMinutes();
	if (this.minutes < 10) this.minutes = '0' + this.minutes;
	
	this.seconds = this.diffDate.getSeconds();
	if (this.seconds < 10) this.seconds = '0' + this.seconds;
	
	this.milliseconds = this.diffDate.getMilliseconds();
	if (this.milliseconds < 10) this.milliseconds = '00' + this.milliseconds;
	else if (parseInt(this.milliseconds) < 100) this.milliseconds = '0' + this.milliseconds;
	
	return('Online since ' + this.years + ':' + this.days + ':' + this.hours + ':' + this.minutes + ':' + this.seconds + ':' + this.milliseconds);
	}
	
function currentMurders() {
	this.totalRate = (timeDifference[this.num] / 86400000) * this.mrpd;
	this.totalRate = this.totalRate.toString();
	this.totalRateBefPoint = this.totalRate.substr(0, this.totalRate.indexOf('.'));
	this.totalRateAfterPoint = this.totalRate.substr(this.totalRate.indexOf('.') + 1, 5);
	if (this.totalRateBefPoint == '') this.totalRateBefPoint = '0';
	// else if (parseInt(this.totalRateBefPoint) < 10) this.totalRateBefPoint = '00' + this.totalRateBefPoint;
	// else if (parseInt(this.totalRateBefPoint) < 100) this.totalRateBefPoint = '0' + this.totalRateBefPoint;
	
	this.totalRate = this.totalRateBefPoint + ',' + this.totalRateAfterPoint;
	return(this.totalRate);
	}
	
function checkRAM() {
	testCounter++;
	if (testCounter > 0 && testCounter <= 100) setTimeout("checkRAM()",1);
	else if (testCounter == 0) {
		testStart = new Date();
		setTimeout("checkRAM()",1);
		}
	else if (testCounter == 101) {
		testEnd = new Date();
		testDifference = testEnd - testStart;
		// alert(testDifference);
		counterInit();
		}
	}
var testCounter = -1;
var testStart, testEnd, testDifference;

// stops all counter on mouse over "theory" link
function killAll() {
	if (is.ns4) {
		var countNum = 0;
		while (counterID[countNum]) {
			clearInterval(counterID[countNum]);
			countNum++;
			}
		}
	}

// re-starts counters
function startAll() {
	if (is.ns4) pageStart();
	}


/////////////////////////////
// other helpful functions //
/////////////////////////////

// reloadPage() causes NS4 to reload on resize due to known Netscape resize bug,
// positions bottom bar on the bottom screen edge in NS6 and IE4+
function reloadPage(init) {
	// initialize function on first call
	if (init == true) {
		// set resize event
		window.onresize = reloadPage;
	}
	// do something if window size has changed
	else if (window.innerWidth != fenster.visWidth || window.innerHeight != fenster.visHeight) {
		// re-position right column
		if (is.dom) {
			fenster = new screenObject();
			// keepRight();
		}
		// reload page in NS4
		else if (is.ns4) {
			window.location.reload();
		}
	}
}
	
// screenObject() creates universally accessible screen attribute objects
function screenObject() {
	// bottom edge
	if (window.pageYOffset) {
		this.bottom = window.pageYOffset;}
	else if (document.body && document.body.scrollHeight) {
		this.bottom = document.body.scrollHeight;}
	else {this.bottom = false;}

	// height	
	if (window.outerHeight) {
		this.height = window.outerHeight;}
	else if (document.body && document.body.offsetHeight) {
		this.height = document.body.offsetHeight;}
	else {this.height = false;}
		
	// visible height
	if (window.innerHeight) {
		this.visHeight = window.innerHeight;}
	else if (document.body) {
		this.visHeight = document.body.clientHeight;}
	else {this.visHeight = false;}
	
	// width
	if (window.outerWidth) {
		this.width = window.outerWidth;}
	else if (document.body && document.body.offsetWidth) {
		this.width = document.body.offsetWidth;}
	else {this.width = false;}
		
	// visible width
	if (window.innerWidth) {
		this.visWidth = window.innerWidth;}
	else if (document.body && document.body.clientWidth) {
		this.visWidth = document.body.clientWidth;}
	else {this.visWidth = false;}
		
	// scroll position
	if (window.pageYOffset) {
		this.scrollTop = window.pageYOffset;}
	else if (document.body && document.body.scrollTop) {
		this.scrollTop = document.body.scrollTop;}
	else {this.scrollTop = 0}
}
