
// WINDOW //

window.getSize=function(){
	var t,l;
	if(document.all&&!window.print){
		// ie 5.2 mac
		return [null,null]
	}else{
		// other browsers
		if(self.outerHeight){
			return [self.outerWidth,self.outerHeight];
		}else if(self.screenTop){
			return [self.screenLeft,self.screenTop];
		}else{
			return [null,null];
		}
	}
	return [l,t];
}
window.getBodyVisibleSize=function(){
	var w,h;
	if(window.innerWidth){
		w=window.innerWidth;
		h=window.innerHeight;
	}else if(document.documentElement&&document.documentElement.clientWidth){
		w=document.documentElement.clientWidth;
		h=document.documentElement.clientHeight;
	}else if(document.body.clientWidth){
		w=document.body.clientWidth;
		h=document.body.clientHeight;
	}
	return [w,h];
}

window.getScrollAmount=function(){
	var x,y;
	if (typeof self.pageYOffset!="undefined"){
		// all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	}else if (typeof document.documentElement!="undefined" && typeof document.documentElement.scrollTop!="undefined"){
		// Explorer 6 Strict
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}else if (typeof document.body!="undefined"){
		// all other Explorers
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return [x,y];
}

// ELEMENT //

Element.getSize=function(obj){
	obj=$(obj);
	return [Element.getWidth(obj),Element.getHeight(obj)];
}
Element.getPos=function(obj){
	// usare con cautela, i browser non supportano bene questa funzionalitą
	obj=$(obj);
	var unDef=[true,true];
	var x = obj.offsetLeft;
	var y = obj.offsetTop;
	if(obj.style.position=="absolute"){
		var l=parseInt(obj.style.left);
		var t=parseInt(obj.style.top);
		unDef=[isNaN(l),isNaN(t)];
		x=(unDef[0])?null:l;
		y=(unDef[1])?null:t;
		/*if(!unDef[0]&&!unDef[1])*/return [x,y];
	}
	while(obj.offsetParent){
		if(obj==document.getElementsByTagName('body')[0]){break;}
		else{
			if(unDef[0])x=x+obj.offsetParent.offsetLeft;
			if(unDef[1])y=y+obj.offsetParent.offsetTop;
			obj=obj.offsetParent;
		}
	}
	return [x,y];
}
Element.withinWindow=function(obj){
				
				var adjust=[0,0,0,0];
						
				var area=window.getBodyVisibleSize();
				var quantoScroll=window.getScrollAmount();
				
				var whB=[area[0]+quantoScroll[0],area[1]+quantoScroll[1]];	//body
				var whL=this.getSize(obj);									//layer
				var xyL=this.getPos(obj);									//layer
				var x=xyL[0],y=xyL[1];
				var sotto=false,sopra=false,sx=false,dx=false;
				// destra
				if(xyL[0]+whL[0]>=whB[0]){
//					alert((xyL[0]+whL[0])+">="+(whB[0])+" esce a destra");
					dx=true;
				}
				// sinistra
				if(x<quantoScroll[0]){
//					alert(x+"<"+quantoScroll[0]+" esce a sinistra")
					sx=true;
				}
				// sotto
				if(xyL[1]+whL[1]>=whB[1]){
//					alert((xyL[1]+whL[1])+">="+whB[1]+" esce sotto");
					sotto=true;
				}
				// sopra
				if(y<quantoScroll[1]){
//					alert(y+"<"+quantoScroll[1]+" esce sopra");
					sopra=true;
				}
				
				return [sopra,dx,sotto,sx];
				
}
Element.setPos=function(obj,coord){
	obj=$(obj);
	var x=coord[0],y=coord[1],adjust=[0,0];
	if(arguments.length>2){
		var opt=arguments[2],w=Element.getWidth(obj),h=Element.getHeight(obj);
		if(typeof opt.adjust != "undefined"){
			adjust=opt.adjust;
		}
		if(typeof opt.align != "undefined"){
			switch(opt.align){
				case "center":	x-=w/2;	break;
				case "right":	x-=w;	break;
			}
		}
		if(typeof opt.align != "undefined"){
			switch(opt.valign){
				case "middle":	y-=h/2;	break;
				case "bottom":	y-=h;	break;
			}
		}
		if(false){
		if(typeof opt.exit == "undefined" || typeof opt.exit == "boolean" ){
			if(!opt.exit){
				
				
				
				
				
				
				
				
				
				
				
				var adjust=[50,8,50,8];
						
				var area=window.getBodyVisibleSize();
				var quantoScroll=window.getScrollAmount();
				
				var whB=[area[0]+quantoScroll[0],area[1]+quantoScroll[1]];	//body
				var whL=this.getSize(obj);			//layer
				/* */
				var xyL=this.getPos(obj);			//layer
				var x=xyL[0],y=xyL[1];
				this.setPos(q,x-adjust[0],0);
				var xyL=this.getPos(obj);			//layer
				var x=xyL[0],y=xyL[1];
				/* start padding adjust */
				whL[1]+=adjust[1];
				/* start padding adjust */
				var sotto=false,sopra=false,sx=false,dx=false;
		/*
				if(xyL[0]+whL[0]>whB[0]){
					alert("esce a destra");
					dx=true;
					x=whB[0]-whL[0];
				}
				if(x<=quantoScroll[0]){
					alert("esce a sinistra")
					sx=true;
					x=quantoScroll[0];
				}
		*/
				if(xyL[1]+whL[1]>whB[1]){
		//			alert("esce sotto");
					sotto=true;
					y=whB[1]-whL[1];
				}
				if(y<=quantoScroll[1]){
		//			alert("esce sopra");
					sopra=true;
					y=quantoScroll[1];
				}
				var xdiff=x-xyL[0];
				var ydiff=y-xyL[1];
				
				if(sotto||sopra||sx||dx){
					/* start left adjust */
					x-=adjust[0];
					/* end left adjust */
					this.setPos(q,x,ydiff);
				}
		
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
			}
		}
		}
	}
	obj.style.left=x+"px";
	obj.style.top=y+"px";
}

// MATH //

Math.gra2rad=function(G){return G*Math.PI/180;}
Math.rad2gra=function(R){return R*180/Math.PI;}

// UTILS //

collection2array=function(q){
	var result=[],i=0;
	for(var i=0;i<q.length;i++){result[i]=q[i];}
	return result;
}
