﻿//copyright:grf studio
function grf_fx_pos(idbase){
	var Elementi=new Array()
	var tempo=new Date().getTime()
	var MouseX=0
	var MouseY=0
	var This=this
	var MouseGiu=false
	setInterval(Esecuzione,16)
	document.onmousemove=RilevaPosizioneMouse;function RilevaPosizioneMouse(){MouseX=window.event.clientX;MouseY=window.event.clientY;if(MouseGiu==false){return true};return false}
	document.onselect=Disabilita
	//function ScriviStatus(t){document.getElementById(idbase+'_status').value=t};document.write('<textarea style="position:absolute;top:0px;left:0px;z-index:1000" id="'+idbase+'_status"></textarea>')

	this.mostra=function(id){
		if(document.getElementById(id).filters.alpha==undefined){document.getElementById(id).style.filter='alpha(opacity=0)'}
		document.getElementById(id).style.display='block'
		if(Elementi[id]==undefined){Elementi[id]=new Elemento(id)}
		Elementi[id].daScoprire=true
		Elementi[id].daNascondere=false
		Elementi[id].contoRovescia=0
		for(e in Elementi){document.getElementById(Elementi[e].id).style.zIndex=This.zIndex}
		document.getElementById(id).style.zIndex=This.zIndex+1
	}
	this.nascondi=function(id){
		if(Elementi[id]==undefined){Elementi[id]=new Elemento(id)}
		if(document.getElementById(id).filters.alpha==undefined){document.getElementById(id).style.filter='alpha(opacity=100)';Elementi[id].alfa=This.rallentamento*100}
		Elementi[id].daScoprire=false	
		Elementi[id].daNascondere=true
		Elementi[id].contoRovescia=0
	}
	this.posiziona=function(id,x,y){
		if(x<0){x=document.getElementById(id).parentNode.offsetWidth/2-document.getElementById(id).offsetWidth/2}
		if(y<0){y=document.getElementById(id).parentNode.offsetHeight/2-document.getElementById(id).offsetHeight/2}
		document.getElementById(id).style.top=y
		document.getElementById(id).style.left=x
		Elementi[id].contoRovescia=0
	}
	
	this.attivaTrascinamento=function(idTrascinatore,idTrascinato){
		if(Elementi[idTrascinatore]==undefined){Elementi[idTrascinatore]=new Elemento(idTrascinatore,idTrascinato)}
		Elementi[idTrascinatore].idCollegati[0]=idTrascinato
		document.getElementById(idTrascinatore).style.cursor='move'
		document.getElementById(idTrascinatore).onmousedown=AvviaSpostamento
		document.getElementById(idTrascinatore).onmouseup=ArrestaSpostamento
		document.getElementById(idTrascinatore).onmousemove=Disabilita
		function AvviaSpostamento(){
			MouseGiu=true
			for(e in Elementi){if(Elementi[e].idCollegati[0]!=null){document.getElementById(Elementi[e].idCollegati[0]).style.zIndex=This.zIndex}}
			document.getElementById(Elementi[this.id].idCollegati[0]).style.zIndex=This.zIndex+1
			Elementi[this.id].spostamentoAvviato=true
			Elementi[this.id].offsetX=document.getElementById(Elementi[this.id].id).offsetLeft
			Elementi[this.id].offsetY=document.getElementById(Elementi[this.id].id).offsetTop
			Elementi[this.id].mouseX=window.event.clientX
			Elementi[this.id].mouseY=window.event.clientY
			return false
		}
		function ArrestaSpostamento(){
			MouseGiu=false
			Elementi[this.id].spostamentoAvviato=false
		}
		function Sposta(){
			if (Elementi[this.id].spostamentoAvviato==false){return false}
			if((window.event.offsetX>=0)&&(window.event.offsetX<=document.getElementById(this.id).offsetWidth)){
				document.getElementById(Elementi[this.id].idCollegati[0]).style.left=document.getElementById(Elementi[this.id].idCollegati[0]).offsetLeft+window.event.offsetX-Elementi[this.id].spostamentoX
			}
			if((window.event.offsetY>=0)&&(window.event.offsetY<=document.getElementById(this.id).offsetHeight)){			
				document.getElementById(Elementi[this.id].idCollegati[0]).style.top=document.getElementById(Elementi[this.id].idCollegati[0]).offsetTop+window.event.offsetY-Elementi[this.id].spostamentoY
			}
			return false
		}
		
		
	}
	
	function Disabilita(){return false}
	
	function Esecuzione(){
		var diff=new Date().getTime()-tempo
		tempo=new Date().getTime()
		for(e in Elementi){
			if(Elementi[e].contoRovescia<=0){
				if(Elementi[e].daScoprire==true){
					Elementi[e].alfa+=diff
					document.getElementById(Elementi[e].id).filters.alpha.opacity=Math.round(Elementi[e].alfa/This.rallentamento)
					if(Elementi[e].alfa>This.rallentamento*100){Elementi[e].daScoprire=null;document.getElementById(Elementi[e].id).filter=''}
				}
				if(Elementi[e].daNascondere==true){
					Elementi[e].alfa+=-diff
					document.getElementById(Elementi[e].id).filters.alpha.opacity=Math.round(Elementi[e].alfa/This.rallentamento)
					if(Elementi[e].alfa<=0){Elementi[e].daNascondere=null;document.getElementById(Elementi[e].id).filter='';document.getElementById(Elementi[e].id).style.display='none';}
				}
				if(Elementi[e].spostamentoAvviato==true){
					document.getElementById(Elementi[e].idCollegati[0]).style.left=Elementi[e].offsetX+MouseX-Elementi[e].mouseX
					document.getElementById(Elementi[e].idCollegati[0]).style.top=Elementi[e].offsetY+MouseY-Elementi[e].mouseY
				}
			}else{Elementi[e].contoRovescia+=-1}
		}
	}
	
	function Elemento(id,idCollegato){
		this.alfa=0
		this.daScoprire=false
		this.daNascondere=false
		this.spostamentoAvviato=false
		this.contoRovescia=0 //evita perdita focus
		this.offsetX=0
		this.offsetY=0
		this.mouseX=0
		this.mouseY=0
		this.id=id
		this.idCollegati=new Array()
		this.idCollegati[0]=idCollegato
		
	}
	
	this.zIndex=1000
	this.rallentamento=3
}

