var mapita = new Array();

function toggleMap() {
	gm = $("gmap");
	
	gleft = pw() < gm.offsetWidth  ? '20px' : (pw() - gm.offsetWidth)/2  + 'px';
	gtop  = ph() < gm.offsetHeight ? '20px' : (ph() - gm.offsetHeight)/2 + 'px';
	
	if (parseInt(gm.style.left) < 0) {
		gm.style.left = gleft;
		gm.style.top  = gtop;
	}
	else {
		gm.style.left = "-2000px";
	}
}
function pw() {
	return window.innerWidth  != null ? window.innerWidth  : document.body.clientWidth  != null ? document.body.clientWidth  : null;
}
function ph() {
	return window.innerHeight != null ? window.innerHeight : document.body.clientHeight != null ? document.body.clientHeight : null;
}
function mapOrNot(cb, force) {
	//alert(cb.checked);
	
	var url = 'mapOrNot.php?id=' + cb.id + '&checked=' + ((cb.checked || force) ? 1 : 0) + '&data=' + encodeURI(cb.getAttribute('gmapaddr'));
	new Ajax.Request(url,
		{
			method: 'get',
			asynchronous: false,
			onSuccess: function(transport){
				var response = transport.responseText;
				if (response) {
					//
				}
			},
			onFailure: function(){
				alert('mapOrNot failed!');
			}
		}
	);
}
function mapOne(id) {
	//$('gmapaddr').innerHTML = $('mapit'+id).getAttribute('gmapaddr');
	$('gmapaddr').innerHTML = $('gmapaddr'+id).innerHTML;
	mapit(1);
}
function mapit(nosession) {
	var sessionexists = false;
	if (!nosession) {
		var url = 'mapOrNot.php?getsessionmapit=1';
		new Ajax.Request(url,
			{
				method: 'get',
				asynchronous: false,
				onSuccess: function(transport){
					var response = transport.responseText;
//alert(response);
					if (response == 1) {
						sessionexists = true;
					}
				},
				onFailure: function(){
					alert('mapOrNot failed!');
				}
			}
		);
	}
	
	var gmapaddr = 'gmapaddr';

//alert($(gmapaddr) + ',' + $(gmapaddr).innerHTML + ',' + sessionexists + ',' + nosession);
	if (($(gmapaddr) && $(gmapaddr).innerHTML && nosession) || (sessionexists && !nosession)) {
		url = '/mapit.html?nosession=' + nosession;
		w = window.open(url, 'mapit', 'width=800,height=600,location=yes');
		w.focus();
	}
	else {
		alert('Please check at least one search result');
	}
}
function checkEm(force) {
	var inps = document.getElementsByTagName('input');
	for (i=0; i<inps.length; i++) {
		inp = inps[i];
		if (inp.className == 'mapit') {
			if (force || mapita[inp.id]) {
				inp.checked = true;
				mapOrNot(inp);
			}
		}
	}
}

