var bodyClickListeners = [];

function bodyClick() {
	for (var i = 0; i < bodyClickListeners.length; ++i)
		bodyClickListeners[i]();
}

var recentCountry = '';
var nrecent = 10;

function toggleDisplay(divname) {
	var el = document.getElementById(divname);
	if (el.style.display == "block")
		el.style.display = "none";
	else
		el.style.display = "block";
	return false;
}

function setRecentCountryFilter(uri) {
	window.location.href = uri;  
}

/****** Dialog boxes ******/

bodyClickListeners.push(hideDialog);

function popupDetails(hostname, e) {
	
	var cursor = getCursorPos(e);
	
	new Ajax.Request("/ajax/getDetails", {
		method: 'post',
		parameters: {hostname: hostname},
		onSuccess: function(transport) {
			showDialog(hostname, transport.responseText, cursor.x, cursor.y, '35em', '15em');
		}
	});
}

function showDialog(title, content, xpos, ypos, width, height) {
	hideDialog();
	hideCountryMenu();
	
	var dialog = $('dialog_tpl');
	
	title = '<a style="float: right" href="#" onclick="hideDialog(); return false;">close</a>' + title;
	$('dialog_tpl_title').update(title);
	$('dialog_tpl_content').update(content);
	
	dialog.style.left = xpos + 'px';
	dialog.style.top = ypos + 'px';
	dialog.style.width = width;
	$('dialog_tpl_content').style.height = height;
	
	dialog.show();
	showShadow(dialog);
}

function hideDialog() {
	if ($('dialog_tpl'))
		$('dialog_tpl').hide();
	hideShadow();
}

function showShadow(ddiv) {
	var ofs = ddiv.cumulativeOffset();
	var dim = ddiv.getDimensions();
	
	$('shadowbox').style.top = ofs.top + 'px';
	$('shadowbox').style.left = ofs.left +'px';
	$('shadowbox').style.width = (dim.width + 16) + 'px';
	$('shadowbox').style.height = (dim.height + 16) + 'px';
	$('shadowbox_td2').style.height = (dim.height - 16) + 'px';	
	$('shadowbox').show();
}

function hideShadow() {
	if ($('shadowbox'))
		$('shadowbox').hide();
}

/****** Country selection menu ******/

bodyClickListeners.push(hideCountryMenu);

var curCountrySel;

function popupCountryMenu(sel) {
	if ($('countrypopup').visible())
		hideCountryMenu();
	else
		showCountryMenu(sel);
}

function hideCountryMenu() {
	if ($('countrypopup')) {
		$('countrypopup').hide();
		hideShadow();
	}
	curCountrySel = undefined;
}

function showCountryMenu(sel) {
	curCountrySel = sel;
	
	hideDialog();
	
	var ot = 16;
	var ol = 0;
	
	if (navigator.userAgent.toLowerCase().indexOf("safari/") != -1) {
		ot++;
		ol++;
	}
	
	$('countrypopup').clonePosition(sel, {setWidth: false, setHeight: false, offsetTop: ot, offsetLeft: ol});
	$('countrypopup').show();
	$('countrypopup').setOpacity(0.9);
	showShadow($('countrypopup'));
}

function selCountry(country) {
	if (!curCountrySel)
		return;
	
	var img = curCountrySel.getElementsByTagName('img')[0];
	if (country)
		img.src = '/images/flags/' + country + '.gif';
	else
		img.src = '/images/flags/all.png';
	hideCountryMenu();
	setRecentCountryFilter(country);
}

function getCursorPos(e) {
	var e = e || window.event;
	var cursor = {x:0, y:0};
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	} else {
		var de = document.documentElement;
		var b = document.body;
		cursor.x = e.clientX + 
			(de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
		cursor.y = e.clientY + 
			(de.scrollTop || b.scrollTop) - (de.clientTop || 0);
	}
	
	return cursor;
}
