// class Item 2.0
// requires: event_register.js, DOMcacher.js
//    La classe Item si adatta alla manipolazione dei tag, tuttavia e' stata studiata per
//  l'utilizzo in caso di interpolazioni e azioni che richiedono l'uso dei timer, 
//  sincronizzando gli eventi di distinti tag paralleli. (gestione concorrenti)

if(typeof Item=="undefined")
{
	function Item(id,speed,beginVal,targetVal,shad_id){
	this.getId=function(){ return _id; }
		this.current=new Number(0);
		if(typeof id=="undefined")
			return null;
		var _id=new String(id);
		var _obj=DOM.getFromId(_id);
		this.speed=new Number(speed);
		if(typeof beginVal=="undefined")
			this.beginValue=null;
		else
			this.beginValue=new Number(beginVal);
		if(typeof targetVal=="undefined")
			this.targetValue=null;
		else
			this.targetValue=new Number(targetVal);
		this.firstLoop=new Boolean(true);
		this.special=null; //libero: puo' contenere qualsiasi cosa (generalmente una funzione)
		if(typeof shad_id=="undefined")
			this.shadowId="";
		else
			this.shadowId=shad_id;
		
		this.init=function(){
			this.current=0;
			this.firstLoop=true;
		};
		
		this.Obj=function(){
			return _obj;
		};
		
		this.setInnerHTML=function(html){
			_obj.innerHTML=html;
		};
		
		Array.prototype.contains=function(item){
			for(var i=0;i<this.length;i++)
				if(this[i].id==item.getId())
					return true;
			return false;
		};
		
		// Array Remove - By John Resig (MIT Licensed)
		Array.prototype.remove = function(from, to) {
			var rest = this.slice((to || from) + 1 || this.length);
			this.length = from < 0 ? this.length + from : from;
			return this.push.apply(this, rest);
		};

		Array.prototype.removeItem=function(item){
			var par=item.getId();
			for(var i=0;i<this.length;i++)
			{
				var local=this[i].getId();
				if(local==par)
				{
					this.remove(i);
					break;
				}
			}
		};
		
		this.setOpacity=function(value){		// value tra 0 e 1
			var him=_obj.style;
			him.opacity=value;
			him.MozOpacity=value;
			him.KhtmlOpacity=value;
			him.filter="alpha(opacity="+(value*100)+")";
		};
		
		this.move=function(dx,dy){
			var myobj=_obj;
			var myleft=parseInt(this.left());
			var mytop=parseInt(this.top());
			if(dx)
				myobj.style.left=(myleft+dx)+"px";
			if(dy)
				myobj.style.top=(mytop+dy)+"px";
		};
		
		this.moveAt=function(x,y){
			var obj=_obj;
			obj.style.left=x+"px";
			obj.style.top=y+"px";
		};
		
		this.moveAtX=function(x){
			var obj=_obj;
			obj.style.left=x+"px";
		};
		
		this.moveAtY=function(y){
			var obj=_obj;
			obj.style.top=y+"px";
		};
		
		this.resize=function(dw,dh){
			var him=_obj;
			var w=parseInt(this.width());
			var h=parseInt(this.height());
			him.style.width=(w+dw)+"px";
			him.style.height=(h+dh)+"px";
		};
		
		this.resizeAt=function(w,h){
			var him=_obj;
			him.style.width=w+"px";
			him.style.height=h+"px";
		};
		
		this.resizeAtW=function(w){
			var him=_obj;
			him.style.width=w+"px";
		};
		
		this.resizeAtH=function(h){
			var him=_obj;
			him.style.height=h+"px";
		};
		
		this.left=function(){
			var obj=_obj;
			var left;
			if(obj.style.left=="")
				left=obj.offsetLeft;					//in assenza della proprieta css (conta i margini)
			else
				left=parseInt(obj.style.left.slice(0,-2));
			return left;
		};
		
		this.top=function(){
			var obj=_obj;
			var top;
			if(obj.style.top=="")
				top=parseInt(obj.offsetTop);		//in assenza della proprieta css (conta i margini)
			else
				top=parseInt(obj.style.top.slice(0,-2));
			return top;
		};
		
		this.width=function(){
			var obj=_obj;
			var width;
			if(obj.style.width=="")
				width=obj.offsetWidth;
			else
				width=parseInt(obj.style.width.slice(0,-2));
			return width;
		};
		
		this.height=function(){
			var obj=_obj;
			var height;
			if(obj.style.height=="")
				height=obj.offsetHeight;
			else
				height=parseInt(obj.style.height.slice(0,-2));
			return height;
		};
		
		this.midWidth=function(){
			return this.width()/2;
		};
		
		this.midHeight=function(){
			return this.height()/2;
		};
		
		this.wMidPivot=function(){
			var obj=_obj;
			return obj.offsetLeft+(obj.offsetWidth/2);
		};
		
		this.hMidPivot=function(){
			var obj=_obj;
			return obj.offsetTop+(obj.offsetHeight/2);
		};
		
		this.offsetLeft=function(){
			return parseInt(_obj.offsetLeft);
		};
		
		this.offsetTop=function(){
			return parseInt(_obj.offsetTop);
		};
		
		this.offsetWidth=function(){
			return parseInt(_obj.offsetWidth);
		};
		
		this.offsetHeight=function(){
			return parseInt(_obj.offsetHeight);
		};
		
		this.placeShadow=function(deltaX,deltaY,stretchW,stretchH){
			/// ridimensiona ombra spostandola come richiesto
			/// shad_ext_id = pezzo da aggiungere nell'id dell'ombra (aggiunge all'id dell'item stesso)
			/// deltaX, deltaY = ammontare dello spostamento dell'ombra
			/// stretchW, stretchH = ridimensionamento, rispetto all'item, dell'ombra
			
			if(typeof this.shadowId!="undefined" && this.shadowId!="")
			{
				var dx=20,dy=20,stw=0,sth=0;
				var polaroid=new Item(_id);
				var shad=new Item(this.shadowId);
				
				if(typeof deltaX!="undefined")
					dx=deltaX;
				if(typeof deltaY!="undefined")
					dy=deltaY;
				if(typeof stretchW!="undefined")
					stw=stretchW;
				if(typeof stretchH!="undefined")
					sth=stretchH;
				
				//shad.moveAt(dx,dy);
				shad.moveAt(polaroid.offsetLeft()+dx,polaroid.offsetTop()+dy);
				shad.resizeAt(polaroid.offsetWidth()+stw,polaroid.offsetHeight()+sth);
			}
		};
		
		this.visible=function(){
			var obj=_obj;
			if(obj.style.display!="none" && obj.style.visibility!="hidden")
				return true;
			else
				return false;
		};
		
		this.show=function(){
			_show();
		};
		function _show(){
			if(_obj)
			{
			 if(_obj.tagName.toLowerCase()=="img" ||
				_obj.tagName.toLowerCase()=="a" ||
				_obj.tagName.toLowerCase()=="span")
			 {
				_obj.style.display="inline";
			 }
			 else
				_obj.style.display="block";
			}
		};
		
		this.hide=function(){
			_hide();
		};
		function _hide(){
			_obj.style.display="none";
		};
	}
}

