function hasClass(elt, classname) {
	if (elt) {
		var classes = elt.className.split(' ');
		for (var i = 0; i < classes.length; ++i)
			if (classes[i] == classname)
				return true;
	}
	return false;
}

function addClass(elt, classname) {
	if (elt) {
		if (!hasClass(elt, classname)) {
			var classes = elt.className.split(' ');
			classes[classes.length] = classname;
			elt.className = classes.join(' ');
		}
	}
}

function removeClass(elt, classname) {
	if (elt) {
		var classes = elt.className.split(' ');
		var newclasses = new Array();
		for (var i = 0; i < classes.length; ++i) {
			if (classes[i] != classname)
				newclasses[newclasses.length] = classes[i];
		}
		elt.className = newclasses.join(' ');
	}
}

function replaceClass(elt, oldClass, newClass) {
	removeClass(elt, oldClass);
	addClass(elt, newClass);
}

var thumbcharts;
var thumbchartTabs;

function activateThumbchart(chartIdx) {
	if (!thumbcharts) {
		thumbcharts = new Array();
		var container = document.getElementById('thumbcharts');
		for (var i = 0; i < container.childNodes.length; ++i)
			if (container.childNodes[i].nodeName.toLowerCase() == 'div')
				thumbcharts[thumbcharts.length] = container.childNodes[i];
		thumbchartTabs = new Array();
		var tableCells = document.getElementById('thumbchartTabs').rows[0].cells;
		for (i = 0; i < tableCells.length; ++i)
			if (hasClass(tableCells[i], 'tab'))
				thumbchartTabs[thumbchartTabs.length] = tableCells[i];
	}
	
	for (var j = 0; j < thumbcharts.length; ++j)
		if (j == chartIdx) {
			thumbcharts[j].style.display = 'block';
			replaceClass(thumbchartTabs[j], 'inactive', 'active')
		} else {
			thumbcharts[j].style.display = 'none';
			replaceClass(thumbchartTabs[j], 'active', 'inactive')
		}
}

function floatCenter(elId) {
	elt = document.getElementById(elId);
	if (elt) {
		elt.style.position = 'absolute';
		elt.style.display = 'block';
		var w = elt.offsetWidth;
		var dw = document.body.offsetWidth;
		elt.style.top = '100px';
		elt.style.left = Math.floor((dw-w)/2)
	}
}

function hide(elId) {
	elt = document.getElementById(elId);
	if (elt) {
		elt.style.display = 'none';
	}
}

function toggleLayer(id) {
	var elt = document.getElementById(id);
	//if (elt.style.display && elt.style.display != 'none')
	if (elt.offsetHeight && elt.style.display == 'block')
		slideClosed(id, .1);
	else
		slideOpen(id, .1);
}

function switchTab(active, group) {
	var tab;
	var content;
	for (var i = 0; i < group.length; ++i) {
		tab = document.getElementById(group[i]);
		content = document.getElementById(group[i] + 'tab');
		if (group[i] != active) {
			if (!hasClass(content, 'hidden')) addClass(content, 'hidden');
			removeClass(tab, 'active');
		} else {
			removeClass(content, 'hidden');
			if (!hasClass(tab, 'active')) addClass(tab, 'active');
		}
	}
}

var timer;

function slideOpen(id, seconds) {
	if (timer) clearTimeout(timer);
	if (!seconds) seconds = .1;
	var elt = document.getElementById(id);
	elt.style.display = 'block';
	elt.style.visibility = 'visible';
	var height = elt.offsetHeight;
	elt.style.height = '0px';
	elt.style.overflow = 'hidden';
	var steps = seconds * 1000 / 25;
	var step = Math.floor(height / steps);
	slideTo(elt, height, step, 25);
}

function slideClosed(id, seconds) {
	if (timer) clearTimeout(timer);
	if (!seconds) seconds = .1;
	var elt = document.getElementById(id);
	var height = elt.offsetHeight;
	elt.style.overflow = 'hidden';
	var steps = seconds * 1000 / 25;
	var step = Math.floor(height / steps);
	slideTo(elt, 0, -step, 25);
}

function slideTo(elt, height, step, interval) {
	var currheight = elt.offsetHeight + step;
	if (step < 0) {
		if (currheight < 0) currheight = 0;
		elt.style.height = currheight + 'px';
		if (currheight > 0)
			timer = setTimeout(function() { slideTo(elt, height, step, interval) }, interval);
		else {
			timer = null;
			elt.style.display = 'none';
			elt.style.height = null;
		}
	} else {
		if (currheight > height) currheight = height;
		elt.style.height = currheight + 'px';
		if (currheight < height)
			timer = setTimeout(function() { slideTo(elt, height, step, interval) }, interval);
		else
			timer = null;
	}
}

function sendHttpRequest(url, callback) {
	var ua = navigator.userAgent.toLowerCase();
	var request;
	if (!window.ActiveXObject)
		request = new XMLHttpRequest();
	else if (ua.indexOf('msie 5') == -1)
		request = new ActiveXObject("Msxml2.XMLHTTP");
	else
		request = new ActiveXObject("Microsoft.XMLHTTP");
	request.onreadystatechange = function() {
			if (request.readyState != 4)
				return;
			if (request.status == 200)
				callback(request);
		};
	request.open("GET", url, true);
	request.send(null);
}

function pollVote() {
	var f = document.getElementById('pollform');
	var answer;
	if (f) {
		for (var i = 0; i < f.elements['answer'].length; ++i)
			if (f.elements['answer'][i].checked)
				answer = f.elements['answer'][i].value;
		if (answer)
			sendHttpRequest('/polls/ajaxvote.php?p='+f.elements['poll_id'].value+'&o='+answer,
				function(request) {
					var results = eval(request.responseText);
					showPollResults(results);
				});
		else getPollResults();
	}
	return false;
}

function getPollResults() {
	var f = document.getElementById('pollform');
	if (f) {
		sendHttpRequest('/polls/ajaxvote.php?p='+f.elements['poll_id'].value,
			function(request) {
				var results = eval(request.responseText);
				showPollResults(results);
			});
	}
}

function showPollResults(results) {
	var votingpane = document.getElementById('pollvoting');
	if (votingpane && !hasClass(votingpane, 'hidden')) addClass(votingpane, 'hidden');
	var resultspane = document.getElementById('pollresults');
	if (resultspane) removeClass(resultspane, 'hidden');

	// Find total votes
	var total = 0;
	for (var i = 0; i < results.length; ++i)
		total += results[i].option_votes;

	// Draw pretty bar chart
	var rcont = document.getElementById('pollresultscontent');
	while(rcont.firstChild) rcont.removeChild(rcont.firstChild);
	for (i = 0; i < results.length; ++i) {
		var row = document.createElement('div');
		row.className = 'pollresultrow';

		var rowLabel = document.createElement('div');
		rowLabel.className = 'pollresultlabel';
		rowLabel.innerHTML = results[i].option_text;

		var rowPercent = document.createElement('div');
		rowPercent.className = 'pollresultpercent';
		rowPercent.innerHTML = '0%';

		var rowGraph = document.createElement('div');
		rowGraph.className = 'pollresultgraph';
		rowGraph.innerHTML = '0';

		row.appendChild(rowLabel);
		row.appendChild(rowPercent);
		row.appendChild(rowGraph);

		rcont.appendChild(row);

		animatePollGraph(results[i].option_votes, total, rowPercent, rowGraph);
	}

	var totalDiv = document.createElement('div');
	totalDiv.className = 'pollresulttotal';
	totalDiv.innerHTML = total + ' votes total';
	rcont.appendChild(totalDiv);
}

function animatePollGraph(votes, total, percentDiv, graphDiv, currPercent) {
	if (total) {
		var percent = Math.round((votes / total) * 100);
		if (!currPercent) currPercent = 0;
		if (!votes) votes = 0;
		if (currPercent < percent) {

			var inc = 3;
			var pctDone = (currPercent / percent) * 100;
			if (pctDone > 90) inc = 1;
			else if (pctDone > 70) inc = 2;

			percentDiv.innerHTML = currPercent + '%';
			graphDiv.innerHTML = Math.round(votes * (pctDone / 100));
			graphDiv.style.backgroundPosition = (Math.round(currPercent * 1.15) - 115) + 'px 0px';

			currPercent += inc;
			setTimeout(function () { animatePollGraph(votes, total, percentDiv, graphDiv, currPercent); }, 25);
		} else {
			percentDiv.innerHTML = percent + '%';
			graphDiv.innerHTML = votes;
			graphDiv.style.backgroundPosition = (Math.round(percent * 1.15) - 115) + 'px 0px';
		}
	}
}

function showPollVoting() {
	var resultspane = document.getElementById('pollresults');
	if (resultspane && !hasClass(resultspane, 'hidden')) addClass(resultspane, 'hidden');
	var votingpane = document.getElementById('pollvoting');
	if (votingpane) removeClass(votingpane, 'hidden');
}

function trackLinks(container, prefix) {
	for (var i = 0; i < container.childNodes.length; ++i) {
		if (container.childNodes[i].nodeName.toUpperCase() == 'A') {
			if (container.childNodes[i].href) {
				container.childNodes[i].href = prefix + escape(container.childNodes[i].href);
				container.childNodes[i].target = '_blank';
			}
		} else if (container.childNodes[i].nodeType == 1) {
			trackLinks(container.childNodes[i], prefix);
		}
	}
}

function replaceLink(link, text) {
	var textNode = document.createTextNode(text);
	link.parentNode.replaceChild(textNode, link);
}
