function addEvent(obj, eventName, fn)
{
    var prev = obj[eventName];
    obj[eventName] = prev ? function() { fn() ; prev() } : fn;
}


function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Non è stato possibile impostare un gestore di eventi");
  }
}

addEvent(window,'load',initGall,false);
addEvent(window,'load',initPrivacy,false);
addEvent(window,'load',hanlePopUp,false);

	function initGall(){
		if (document.getElementById('gallery')) {
		var apics=document.getElementById('gallery').getElementsByTagName('a');
		for (i=0;i<apics.length;i++){
			apics[i].id="gd-"+i;
			apics[i].onclick=new Function( "drawGall('" + apics[i].id + "');return false;");
			}
		}
	}

		function drawGall(id){
		var theLay=document.getElementById('layout');
		var bg=document.createElement('div');
		bg.setAttribute('id','bg-gallery');
		var zoomed=document.createElement('div');
		zoomed.setAttribute('id','czoomed');
		zoomed.style.visibility="hidden";
		var btn=document.createElement('p');
		btn.setAttribute('id','close-gall');
		btn.innerHTML="<a href=\"#\" id=\"btn-close-gall\" onclick=\"remGall();return false;\">Close</a>";
		zoomed.appendChild(btn);
		var thepic=document.createElement('img');
		thepic.setAttribute('id','zoomed');

		zoomed.appendChild(thepic);
		theLay.appendChild(zoomed);
		theLay.appendChild(bg);

		thepic.onload=function(){dimensiona(this)};
		thepic.src=document.getElementById(id).href;
		bg.style.height=theLay.offsetHeight+"px";
	}

function remGall(){
//alert("ggg");
	var fondo=document.getElementById('layout');
	var juy=document.getElementById('czoomed');
	var nero=document.getElementById('bg-gallery');
	fondo.removeChild(juy);
	fondo.removeChild(nero);
	}

function dimensiona(p){
	var zoomed=document.getElementById('czoomed');
 	//var they = new Fx.Styles('zoomed', {duration: 500, transition: fx.linear,onComplete:function(){thew.custom({'width': [0, widthPic]});}});
	 var thew = new Fx.Styles('zoomed', {duration: 500, transition: fx.linear,onComplete:function(){they.custom({'height': [0, heightPic]});}});
	var they = new Fx.Styles('zoomed', {duration: 500, transition: fx.linear,onComplete:appearClose});
	var widthPic=p.width;
	var heightPic=p.height;
	p.width=0;
	p.height=0;
	var newposition=(((994)-(widthPic))/2);
	zoomed.style.left=newposition+"px";
	zoomed.style.visibility="visible";
	//they.custom({'height': [0, heightPic]});
	thew.custom({'width': [0, widthPic]});
	}
function appearClose(){
	//var theBtn = new Fx.Styles('close-gall', {duration: 1500, transition: fx.linear});
	var btnclose=document.getElementById('close-gall');
	btnclose.style.display="block";
	//theBtn.custom({'height': [0, 20]});

	}

function check() {
  var i=0;
  var formOk=true;
  var nomeform=document.getElementById('registrazione');
  var els = nomeform.elements;
  for (i=0; i<els.length;i++) {
    if (els[i].type!='radio' && (els[i].name=='nome' || els[i].name=='cognome' || els[i].name=='user' || els[i].name=='password' || els[i].name=='mail')){
      if (els[i].value==''){
        alert("Attenzione: il campo "+els[i].name+"  deve essere compilato");
        els[i].focus();
        formOk=false;
         return formOk;
      }
    }
    if (els[i].type=='radio' && els[i].name=='privacy'){
      if (els[i].value=='2' && els[i].checked){
        alert("Attenzione: il campo "+els[i].name+"  deve essere accettato");
        els[i].focus();
        formOk=false;
        return formOk;
      }
    }
  }//chiude ciclo principale
  return formOk;
}


function initPrivacy(){
	var btn = document.getElementById("policy");
	btn.onclick = function(){
		setPrivacy();
		return false;
		}
	}

function setPrivacy(){
	var con = document.getElementById("layout");
	var priv = document.getElementById("privacyL");
	var opL = document.createElement("div");
	opL.setAttribute("id","op-layer");
	var h = con.offsetHeight;
	opL.style.height = h + "px";
	con.appendChild(opL);
	priv.style.visibility = "visible";
	remPrivacy();
	}

function remPrivacy(){
	var btnRem = document.getElementById("closeP");
	btnRem.onclick = function(){
	var con = document.getElementById("layout");

	var obj1 = document.getElementById("op-layer");
	var obj2 = document.getElementById("privacyL");
	obj2.style.visibility = "hidden";
	con.removeChild(obj1);
	}

	}

function hanlePopUp(){
	if(document.body.className=='ita hp'){
		var popUp = document.createElement('div');
		popUp.setAttribute('id','advice');
		popUp.setAttribute('class','advice_it');
		popUp.innerHTML = '<p><a href="#">&nbsp;</a></p>';
		document.getElementById('layout').appendChild(popUp);
		var closePopUp = document.getElementById('advice').firstChild.firstChild ;
		closePopUp.onclick = function(){
			document.getElementById('layout').removeChild(popUp);
		}
	}
	if(document.body.className=='eng hp'){
		var popUp = document.createElement('div');
		popUp.setAttribute('id','advice');
		popUp.setAttribute('class','advice_en');
		popUp.innerHTML = '<p><a href="#">&nbsp;</a></p>';
		document.getElementById('layout').appendChild(popUp);
		var closePopUp = document.getElementById('advice').firstChild.firstChild ;
		closePopUp.onclick = function(){
			document.getElementById('layout').removeChild(popUp);
		}
	}
}