function getViewport() {
	var viewport = {x:0,y:0,width:0,height:0};

	if (typeof window.scrollX != 'undefined') {
		viewport.x = window.pageXOffset;
		viewport.y = window.pageYOffset;
		viewport.width = window.innerWidth;
		viewport.height = window.innerHeight;
		return viewport;
	} else {
		if (document.documentElement &&
		   (typeof document.documentElement.scrollLeft != 'undefined') &&
		   (document.documentElement.scrollLeft != 0)) {
			viewport.x = document.documentElement.scrollLeft;
			viewport.y = document.documentElement.scrollTop;
			viewport.width = document.documentElement.clientWidth;
			viewport.height = document.documentElement.clientHeight;
			return viewport;
		} else {
	if (document.body &&
	(typeof document.body.scrollLeft != 'undefined')) {
		viewport.x = document.body.scrollLeft;
		viewport.y = document.body.scrollTop;
		
		if (document.compatMode == "CSS1Compat") {
			viewport.width = document.body.parentNode.clientWidth;
			viewport.height = document.body.parentNode.clientHeight;
		} else {
			viewport.width = document.body.clientWidth;
			viewport.height = document.body.clientHeight;
		}

		return viewport;
	}
	}
	}
	
	return null;
}

function $(){
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') {
			element = document.getElementById(element);
		}
		if (arguments.length == 1) {
			return element;
		}
		elements.push(element);
	}
	return elements;
}


function hide() {
	for (i = 0; i < arguments.length; i++) {
		var e = $(arguments[i]);
		if (e) {
			e.style.display = "none";
		}
	}
}

function show() {
	for (i = 0; i < arguments.length; i++) {
		var e = $(arguments[i]);
		if (e) {
			e.style.display = "";
		}
	}
}

function showBenefits() {
	show('cover', 'benefits');
	var vp = getViewport();
	$('cover').style.width = vp.width + 'px';
	$('benefits').style.top = (vp.y + screen.height/2 - 290) + 'px';
	return false;
}

function el(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (window[id]) {
		return window[id];
	}

	return null;
}

var vu = new Array();
var vd = new Array();
var score = new Array();

function restoreUp(num) {
	if (vu[num] == 1) {
		score[num] = score[num] - 1;
		vu[num] = 0;
		el('up' + num).innerHTML = "<img src='/news/images/arrow-up.gif' alt='scor: +1'>";
	}
}
function restoreDown(num) {
	if (vd[num] == 1) {
		score[num] = score[num] + 1;
		vd[num] = 0;
		el('down' + num).innerHTML = "<img src='/news/images/arrow-down.gif' alt='scor: -1'>";
	}
}
function updateScore(num) {
	el('score' + num).innerHTML = score[num];
}
function sendAction(num, action) {
	var http = getHTTPObject();
	http.open("GET", "/news/action.cgi?rand=" + Math.random() + "&news=" + num + "&action=" + action, true);
	http.onreadystatechange = function()
	{
		if (http.readyState == 4) {
			updateScore(num);
		}
	}
	http.send(null);
}
function voteUp(num) {
	restoreDown(num);
	if (!vu[num]) {
		score[num] = score[num] + 1;
		vu[num] = 1;
		el('up' + num).innerHTML = "<img src='/news/images/arrow-up-sel.gif' alt='scor: +1'>";
		sendAction(num, "voteup");
	} else {
		score[num] = score[num] - 1;
		vu[num] = 0;
		el('up' + num).innerHTML = "<img src='/news/images/arrow-up.gif' alt='scor: +1'>";
		sendAction(num, "votezero");
	}
}
function voteDown(num) {
	restoreUp(num);
	if (!vd[num]) {
		score[num] = score[num] - 1;
		vd[num] = 1;
		el('down' + num).innerHTML = "<img src='/news/images/arrow-down-sel.gif' alt='scor: -1'>";
		sendAction(num, "votedown");
	} else {
		score[num] = score[num] + 1;
		vd[num] = 0;
		el('down' + num).innerHTML = "<img src='/news/images/arrow-down.gif' alt='scor: -1'>";
		sendAction(num, "votezero");
	}
}

var hashN = new Object;
function el(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (window[id]) {
		return window[id];
	}

	return null;
}
function getHTTPObject() {
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	return xmlhttp;
}
