var z_index = 5;
function show_hyde(id) {
	if($(id).style.visibility == 'hidden' || document.getElementById(id).style.visibility == ''){
		$('focuser').setStyle({ 'visibility' : 'visible'});
		$(id).setStyle({ 'zIndex' : z_index.toString(), 'visibility' : 'visible' });
		z_index++;
	}
	else{
		$('focuser').setStyle({ 'visibility' : 'hidden'});
		$(id).setStyle({ 'visibility' : 'hidden'});
	}
}
function show(id) {
	$(id).setStyle({ 'zIndex' : z_index.toString(), 'visibility' : 'visible' });
	z_index++;
}
function hyde(id) {
	$(id).setStyle({ 'visibility' : 'hidden'});
}
function rePosition(arr) {
	arr.each( function(id){
		if(!$(id)){return false;}
		if($(id).getHeight() > document.viewport.getHeight()){return false;}
		var top = Math.round( document.viewport.getScrollOffsets().top + (document.viewport.getHeight()/2) - ($(id).getHeight()/2) );
		//if(top < 0){top = document.viewport.getScrollOffsets().top;}
		$(id).setStyle({ 'top' : (top + 'px')});
	});
}
function center(arr){
	arr.each( function(id){
		if(!$(id)){return false;}
		var left = Math.round((document.viewport.getWidth()/2)-($(id).getWidth()/2));
		$(id).setStyle({ 'left' : (left + 'px')});
	});
}
/*function setSndPrivForm( fromid, toid, subject, tonome, mode ) {
	document.getElementById('snd_priv_fromid').value = fromid;
	document.getElementById('snd_priv_toid').value = toid;
	document.getElementById('snd_priv_subject').value = subject;
	document.getElementById('snd_priv_toname').value = tonome;
	document.getElementById('snd_priv_toname').readOnly = true;
	document.getElementById('snd_priv_mode').value = mode;
	if( mode == 'mail' ) {
		document.getElementById('send_priv_titolo').firstChild.data = ". . : INVIA UNA NUOVA E-MAIL : . .";
	}
	else {
		if( mode == 'mexpriv') {
			document.getElementById('send_priv_titolo').firstChild.data = ". . : INVIA UN MESSAGGIO PRIVATO : . .";
		}
	}
}
function setContact(who,icq,msn,yim,aim,fromid,toid,subject)
{
	var regexp = /'/;
	who = who.replace(regexp,"\\'");
	subject = subject.replace(regexp,"\\'");
	
	document.getElementById('contact_titolo').firstChild.data = ". . : CONTATTA '"+who.toUpperCase()+"' : . ."; 
	document.getElementById('a_mexpriv').href = "javascript:show('send_priv');setSndPrivForm("+fromid+","+toid+",'"+subject+"','"+who+"','mexpriv');";
	document.getElementById('a_mail').href = "javascript:show('send_priv');setSndPrivForm("+fromid+","+toid+",'"+subject+"','"+who+"','mail');";
	if( icq == '' )	{
		document.getElementById('a_icq').style.display = 'none';
	}
	else {
		document.getElementById('a_icq').style.display = 'inline';
		document.getElementById('a_icq').href = "http://wwp.icq.com/scripts/search.dll?to="+icq;
	}
	if( msn == '' ) {
		document.getElementById('a_msn').style.display = 'none';
	}
	else {
		document.getElementById('a_msn').style.display = 'inline';
		document.getElementById('a_msn').href = "javascript:window.alert('"+msn+"')";
	}
	if( yim == '' ) {
		document.getElementById('a_yim').style.display = 'none';
	}
	else {
		document.getElementById('a_yim').style.display = 'inline';
		document.getElementById('a_yim').href = "http://edit.yahoo.com/config/send_webmesg?.target="+yim+"&.src=pg";
	}
	if( aim == '' ) {
		document.getElementById('a_aim').style.display = 'none';
	}
	else {
		document.getElementById('a_aim').style.display = 'inline';
		document.getElementById('a_aim').href = "aim:goim?screenname="+aim+"&message=Hello+Are+you+there?";
	}
}*/
function openclose(id)
{
	if($(id).style.display == 'none' || $(id).style.display == ''){
	 $(id).setStyle({ 'display' : 'block'});
	}
	else{
		$(id).setStyle({ 'display' : 'none'});
	}
}
/*function baz_sort(ordina_per, direzione){
	document.sort_frm.ordina_per.value = ordina_per;
	document.sort_frm.direzione.value = direzione;
	document.sort_frm.submit();
}
function open_zeroprices(frm_name)
{
	var regexp = /\blibri\W\d+\W\Wprezzo\W/;
	var regexp2 = /\W\W/;
	var frm_elements = document.forms[frm_name].elements;
	for(var i = 0; i < frm_elements.length; i++)
	{
		if( regexp.test( frm_elements[i].name) )
		{
			if( frm_elements[i].value == 0 ){
				openclose('dettagli_'+ frm_elements[i].name.substring(6,frm_elements[i].name.search(regexp2)) );
			}
		}
	}
}
var arr = new Array();
function check(id)
{
	var chkb = document.getElementById('chkb_'+id);
	var tr = document.getElementById('tr_'+id);
	if( arr[id] == undefined ){ arr[id] = tr.style.backgroundColor; }
	if( chkb.checked )
	{
		chkb.checked = false;
		tr.style.backgroundColor = arr[id];
	}
	else
	{
		chkb.checked = true;
		tr.style.backgroundColor = '#9999FF';
	}
}*/

//16 - 05 - 08
Event.observe(window, 'load', function() {
	center($$('.popup'));
	rePosition($$('.popup'));
	var arrayPageSize = getPageSize();
	$('focuser').setStyle({ 'height' : (arrayPageSize[1] + 'px')});
	
	
});
Event.observe(window, 'resize', function() {
	center($$('.popup'));
	rePosition($$('popup'));
	var arrayPageSize = getPageSize();
	$('focuser').setStyle({ 'height' : (arrayPageSize[1] + 'px')});
});
Event.observe(window, 'scroll', function() {
	rePosition($$('.popup'));
});


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
