function hookEvent(element, eventName, callback)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.addEventListener)
  {
    if(eventName == 'mousewheel')
      element.addEventListener('DOMMouseScroll', callback, false);  
    element.addEventListener(eventName, callback, false);
  }
  else if(element.attachEvent)
    element.attachEvent("on" + eventName, callback);
}
function wheel(event){
    var delta = 0;
    if (!event) /* For IE. */
            event = window.event;
    if (event.wheelDelta) { /* IE/Opera. */
            delta = event.wheelDelta/120;
            /** In Opera 9, delta differs in sign as compared to IE.
             */
            if (window.opera)
                    delta = -delta;
    } else if (event.detail) { /** Mozilla case. */
            /** In Mozilla, sign of delta is different than in IE.
             * Also, delta is multiple of 3.
             */
            delta = -event.detail/3;
    }
    /** If delta is nonzero, handle it.
     * Basically, delta is now positive if wheel was scrolled up,
     * and negative, if wheel was scrolled down.
     */
    if (delta)
		scroll.mouseScroll(delta);
    /** Prevent default actions caused by mouse wheel.
     * That might be ugly, but we handle scrolls somehow
     * anyway, so don't bother here..
     */
    if (event.preventDefault)
            event.preventDefault();
	event.returnValue = false;
}

var Scroll = function(id,time,step)
{
	this.t=document.getElementById(id);
	this.time=time?time:10;
	this.step=step?step:1;
	hookEvent(this.t,'mousewheel',wheel);
}
Scroll.prototype = {
	mouseScroll: function(delta)
	{
		scroll.t.scrollTop = (parseInt(scroll.t.scrollTop)-delta*10); 
	},
	scrollStop:function()
	{
		scroll.scrolling=false;
	},
	scrollUp:function()
	{
		scroll.scrolling='up';
		scroll.scroll();
	},
	scrollDown:function()
	{
		scroll.scrolling='down';
		scroll.scroll();
	},
	scroll:function()
	{
		if (!scroll.scrolling)
			return;

		if (scroll.scrolling=='up')
			scroll.t.scrollTop = (parseInt(scroll.t.scrollTop) - scroll.step);
		else if(scroll.scrolling=='down')
			scroll.t.scrollTop = (parseInt(scroll.t.scrollTop) + scroll.step);
		window.setTimeout(scroll.scroll,scroll.time);
	}
}
var Transition = function (configs)
{
	
	this._basePath=configs.basePath;
	this._n=configs.n;
	this._className=configs.className;
	this._container=configs.container;
	YAHOO.util.Event.onAvailable('otto-over', function(){ YAHOO.util.Event.addListener("otto-over", "mouseover", this.start,null, this); YAHOO.util.Event.addListener("otto-over", "mouseout", this.stop,null, this);  },null, this);  
		
}
Transition.prototype={
	levels:null,current:null,c:0,
	start:function()
	{
		this.remove();
		this.stopped=false;
		document.getElementById(this._container).style.display='block';
		this.add();
	},
	add:function()
	{
		var n;
		while (true)
		{
			n=Math.round(Math.random()*this._n)
			if (n!=this.current)
			{
				this.current=n;
				break;
			}
		}
		this.levels[this.c]=new Object();
		this.levels[this.c].obj=document.createElement('div');
		this.levels[this.c].obj.className=this._className;
		YAHOO.util.Dom.setStyle(this.levels[this.c].obj,'background',"url('"+this._basePath+this.current+".jpg')");
		YAHOO.util.Dom.setStyle(this.levels[this.c].obj,'opacity',0.0);
		YAHOO.util.Dom.setStyle(this.levels[this.c].obj,'z-index',this.c+1);

		document.getElementById(this._container).appendChild(this.levels[this.c].obj);
		
		this.levels[this.c].anim = new YAHOO.util.Anim(this.levels[this.c].obj, {
			opacity: {from:0.0,to: 1.0}
		}, 1, YAHOO.util.Easing.easeOut);
		this.levels[this.c].anim.onComplete.subscribe(function() 
		{   

			if (this.stopped)
				return true;
			YAHOO.lang.later(300,this,this.add);
		},null,this);
		
		this.levels[this.c].anim.animate();
		this.c++;
	},
	stop:function()
	{
		this.stopped=true;
		document.getElementById(this._container).style.display='none';
	},
	remove:function()
	{
		var i;
		for (i=0;i<this.c;i++)
		{
			this.levels[i].anim.onComplete.unsubscribeAll();
			this.levels[i].anim.stop();
			this.levels[i].obj.parentNode.removeChild(this.levels[i].obj);
		}
		this.levels=new Array();
		this.c=0;
	}
}
var transition=new Transition({container:'otto',className:'otto',n:15,basePath:'http://www.myeverybodys.net/8/img/otto/'});
var Prodotti = function (c){ this.current=c;}
Prodotti.prototype={
	level:100,current:0,anum:null,
	open:function(p)
	{
		if (p==this.current)
			return;
		if (this.anim)
		{
			if (this.anim.isAnimated())
				this.anim.stop();
			delete this.anim;
		}
		var prod=YAHOO.util.Dom.get('p'+p);
		
		YAHOO.util.Dom.setStyle(prod,'opacity',0.0);
		YAHOO.util.Dom.setStyle(prod,'z-index',this.level+1);
		YAHOO.util.Dom.setStyle(prod,'display','block');
		
		this.current=p;
		this.level++;
		
		this.anim = new YAHOO.util.Anim(prod, {
			opacity: {from:0.0,to: 1.0}
		}, 0.5, YAHOO.util.Easing.easeOut);
		this.anim.animate();
	}
}
var MultiTransition = function (configs)
{
	this._basePath=configs.basePath;
	this._n=configs.n;
	this._className=configs.className;
	this._containers=configs.containers;
	this._toLoad=0;
	this.current=new Array();
	this.levels=new Array();
	this.cl=new Array();
	for (var i=0;i<this._containers.length;i++)
	{
		this.levels[i]=new Array();
		this.current[i]=null;
		this.cl[i]=0;
	}
}
MultiTransition.prototype={
	levels:null,current:null,cl:null,
	pick:function(x,skip)
	{
		var n,i,ok;
		while (true)
		{
			ok=true;
			n=Math.round(Math.random()*this._n)
			for (i=0;i<this._containers.length;i++)
			{
				if (skip && i==x)
					continue;
				
				if (n==this.current[i])
				{
					ok=false;
					break;
				}
				
			}
			if (ok)
				break;
		}
		this.current[x]=n;
		return this.current[x];	
	},
	start:function()
	{
		this._toLoad=this._containers.length;
		for (var i=0;i<this._containers.length;i++)
		{
			var l=this._create(i,true);
			
			YAHOO.util.Dom.setStyle(this.levels[i][l].obj,'visibility','hidden');
			YAHOO.util.Event.addListener(this.levels[i][l].obj, "load",this._display,i, this);
			
			this.levels[i][l].obj.src=this._basePath+this.current[i].toString()+".jpg";
			this._containers[i].appendChild(this.levels[i][l].obj);
		}
		this.last=0;
		
	},
	_create:function(i,skip)
	{
		this.pick(i,skip);
		
		var l=this.cl[i];
		this.levels[i][l]=new Object();
		this.levels[i][l].obj=document.createElement('img');
		YAHOO.util.Dom.setStyle(this.levels[i][l].obj,'position','absolute');
		YAHOO.util.Dom.setStyle(this.levels[i][l].obj,'z-index',l+1);	
		
		this.levels[i][l].anim = new YAHOO.util.Anim(this.levels[i][l].obj, {
			opacity: {from:0.0,to: 1.0}
		}, 1.3, YAHOO.util.Easing.easeOut);
		this.cl[i]++;
		return l;
	},
	_display:function(e,i)
	{
		this._toLoad--;
		YAHOO.util.Event.removeListener(this.levels[i][0].obj, "load");
		YAHOO.util.Dom.setStyle(this.levels[i][0].obj,'visibility','visible');
		
		if (this._toLoad==0)
			YAHOO.lang.later(1000,this,this.transition);
			
	},
	_transition:function(e,arg)
	{
		this.levels[arg.container][arg.level].anim.animate();
	},
	transition:function()
	{
		var x;
		while (true)
		{
			x=Math.round(Math.random()*(this._containers.length-1))
			if (x!=this.last)
				break;
		}
		this.last=x;

		var l=this._create(x,false);
	
		this.levels[x][l].anim.onComplete.subscribe(function() 
		{   
			this.remove(x);
			YAHOO.lang.later(700,this,this.transition);
		},null,this);
		
		YAHOO.util.Event.addListener(this.levels[x][l].obj, "load",this._transition,{container:x,level:l}, this);
		YAHOO.util.Dom.setStyle(this.levels[x][l].obj,'opacity',0.0);
		this.levels[x][l].obj.src=this._basePath+this.current[x].toString()+".jpg";
		this._containers[x].appendChild(this.levels[x][l].obj);	
	},
	remove:function(x)
	{
		var i=this.cl[x]-2;
		
		this.levels[x][i].anim.onComplete.unsubscribeAll();
		this.levels[x][i].obj.parentNode.removeChild(this.levels[x][i].obj);
		delete this.levels[x][i].anim;
		delete this.levels[x][i].obj;
		
	}
}