function Settare() {	
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
				//anchor.onclick = function () {open_img(this); return false;}
				var ui = anchor.getAttribute('href');
				var contenuto = anchor.innerHTML.replace(/src=/, 'onClick="inizia(\''+ui+'\'); return false;" src=');
				anchor.innerHTML=contenuto;
			}
		}
}

var url_img = ''; // l'immagine che devo aprire

function inizia(elem){
   url_img = elem;
   open_img();
}

var h_utile ='';
var w_utile ='';
var opera = (window.opera) ? true : false;

function open_img(){
	
	h_utile = (document.all && !window.opera) ? document.documentElement.offsetHeight : self.innerHeight;
	w_utile = Math.round(document.body.clientWidth / 2);
	var h_bg = (document.documentElement.scrollHeight > h_utile) ? document.documentElement.scrollHeight : h_utile;
	
	backg = document.createElement("DIV");
	backg.setAttribute("id", "baco");
	backg.style.backgroundColor='#000';
	backg.style.zIndex=100;
	backg.style.width=1+'%';
	backg.style.height=h_bg+'px';
	backg.style.position='absolute';
	backg.style.left=0;
	backg.style.top=0;
	document.body.appendChild(backg);
	fade('baco',85);
    sfondo();
	
}

/*
function open_img(){
	
	h_utile = (document.all && !window.opera) ? document.documentElement.offsetHeight : self.innerHeight;
	w_utile = Math.round(document.body.clientWidth / 2);
	
	backg = document.createElement("DIV");
	backg.setAttribute("id", "baco");
	backg.style.backgroundColor='#000';
	backg.style.zIndex=100;
	backg.style.width=1+'%';
	backg.style.height=document.documentElement.scrollHeight+'px';
	backg.style.position='absolute';
	backg.style.left=0;
	backg.style.top=0;
	document.body.appendChild(backg);
	fade('baco',85);
    sfondo();
	
}
*/

//dati accelerazione
var x=1;
var t=2;
var v = 6;

function sfondo(){
   if(x>100) x = 100;
   document.getElementById("baco").style.width = x+'%';
   if(x<100){
   x = x*(t/v)*t*t;
   window.setTimeout("sfondo()", 7);
   }else{
   x=1;
   create_box();
   }
}
	
function create_box(){

	boxel = document.createElement("DIV");
	boxel.setAttribute("id", "box");
	//boxel.setAttribute("style", "background-color:#fff; text-align:right; padding:10px; z-index:102; position:fixed; left:"+w_utile+"px; top:"+yy+"px;");
	boxel.style.backgroundColor='#fff';
	boxel.style.padding=10+'px';
	boxel.style.zIndex=102;
	boxel.style.position='absolute';
	boxel.style.left=w_utile-20+'px';
	boxel.style.top=Math.round(h_utile / 2) + document.documentElement.scrollTop+'px';
	boxel.innerHTML = '<img id="preload" name="preload" src="img/loading.gif">';
	var nascondi = (opera)? '' : ' style="display:none"';
	boxel.innerHTML += '<img id="George" name="George" src="'+url_img+'" '+nascondi+'>';
	document.body.appendChild(boxel);
	preload();
}

function preload(){
   if((document.images['George'].width!=0 & document.images['George'].height!=0) | document.images['George'].complete){
	  window.setTimeout("view_image();", 200);
	  if(opera) document.getElementById("George").style.display="none";

   }else{
      window.setTimeout("preload();", 100);
   }
}

function view_image(){
	
      fade('box',0,'piu');

      document.getElementById("preload").style.display="none";
	  document.getElementById("George").style.display="";
	  document.getElementById("box").innerHTML += '<br><br><input type="button" id="buttonClose" name="buttonClose" value="X chiudi" onClick="chiudi_box()">';

      x_new = w_utile - Math.round((document.images['George'].width+20) / 2);
      y_new = Math.round(h_utile / 2) + document.documentElement.scrollTop - Math.round((document.images['George'].height+50) / 2);
      document.getElementById("box").style.left = x_new+'px';
      document.getElementById("box").style.top = y_new+'px';
}

function chiudi_box(){
   cRemoveElement('box');
   cRemoveElement('baco');
}

function cRemoveElement(id) {
   var node = document.getElementById(id);
   
   if ((node) && (node.parentNode)) {
     var removed = node.parentNode.removeChild(node); 
     return true;
   }

   return false;
}

function fade(Target,opacity,option) {
                fadeTarget = document.getElementById(Target);
                if (opacity <= 100) {
                        if (fadeTarget.style.MozOpacity!=null) {
                                fadeTarget.style.MozOpacity = (opacity/100);
                        } else if (fadeTarget.style.opacity!=null) {
                                fadeTarget.style.opacity = (opacity/100);
                        } else if (fadeTarget.style.filter!=null) {
                                fadeTarget.style.filter = "alpha(opacity="+opacity+")";
                        }
				
				        if(option!='undefined'){
				            if(option=='piu'){
				               opacity += 5;
				            }else if (option=='meno'){
				               opacity -= 5;
				            }
							var velocity = (navigator.appVersion.indexOf("MSIE")!=-1) ? 5 : 15; 
				            window.setTimeout("fade('"+Target+"',"+opacity+",'"+option+"')", velocity);
				        }
				}
}

function addEvent(el, evType, fn, useCapture){
   if (el.addEventListener){
     el.addEventListener(evType, fn, useCapture); 
   } else if (el.attachEvent){
     el.attachEvent('on' + evType, fn);
   } else {
     el['on' + evType] = fn;
   }
}

addEvent(window,'load',Settare);
//window.onload = Settare;
