/*
EM 2006
www.BePartners.it
*/


function getPix ()
{
	var f = document.getElementById("pics");
	var d = f.getElementsByTagName("DIV");
	var p = [];
	for ( var i=0; i<d.length; i++ )
		if ( d[i].style.backgroundImage ) { p.push(d[i]); }
	return p;
}


/*
function delayFn ( f, dt, a, o )
{
	var t = setTimeout(execFn, dt, f, a, o);
	return t;
}


function execFn ( f, a, o )
{
	var o = o?o:this;
	f.apply(o,a);
}
*/


function fade ( o, d )
{
	if ( !o || !o.style || o.style.opacity=='undefined' ) return false;
	var a = d + parseFloat(o.style.opacity);
	if ( a>=1 )  a=1.0;
	else if ( a<=0 )  a=0.0;
	else
	{
		var t = setTimeout(fade, 50, o, d);
	}
	o.style.opacity = String(a).substr(0,6);
}



function Transition ( o )
{
	this.o = o;
	this.setup_f = new Function("this.o.style.filter = 'progid:DXImageTransform.Microsoft.fade'+'(duration=1.2)'; this._f=this.o.filters;");
	this.begin_f = new Function("if (this._f.length) this._f[0].Apply();");
	this.end_f = new Function("if (this._f.length) this._f[0].Play();");

	this.setup_l = new Function("var l = doubleLayer(this.o); this._bg=l[0]; this._fg=l[1];");
	this.begin_l = new Function("bg","setStyleBgi(this._bg, this._fg.style.backgroundImage); fade(this._fg,-1); setStyleBgi(this._fg, bg); fade(this._fg,0.04);");
	this.end_l = new Function("");

//	this.go = new Function("f,p,bg","this.begin(bg); f();");

	var f = o.filters;

	if ( f )
		this.setup_f();
	else
		this.setup_l();

	if ( f )//&& f.length )
	{
		this.begin = this.begin_f;
		this.end = this.end_f;
	}
	else
	{
		this.begin = this.begin_l;
		this.end = this.end_l;
	}

}


function doubleLayer ( o )
{
	with (o.style) { position="relative"; }
	d1 = o.cloneNode(true);
	d2 = o.cloneNode(true);
	d1.id = o.id+"_bg";
	d2.id = o.id+"_fg";
	with (d1.style) { position="absolute"; top="0px"; left="0px"; zIndex="0"; height="100%"; width="100%"; border="none"; opacity="1.0"; }
	with (d2.style) { position="absolute"; top="0px"; left="0px"; zIndex="1"; height="100%"; width="100%"; border="none"; opacity="1.0"; }
	o.appendChild(d1);
	o.appendChild(d2);
	return [d1,d2];
}


function setStyleBgi ( o, bg )
{
	o.style.backgroundImage = bg;
}


function setPic ( o, bg )
{
//	o.fx.go(setStyleBgi,[bg]);
	o.fx.begin(bg);
	o.style.backgroundImage = bg;
	o.fx.end();
}



function swapPix ( a, b )
{
	var i = a.style.backgroundImage.replace(/m(\.[^.]+\))?$/, "$1");
	var j = b.style.backgroundImage.replace(/(\.[^.]+\))?$/, "m$1");
	setPic (a, j);
	setPic (b, i);
	a.ix = b.n;
}


function rotatePix ()
{
	swapPix ( this.p[this.ix+1], this );
	this.ix = (1+this.ix) % (this.p.length-1);
}


function rotate ()
{
	rotate.p[0].rotate();
}
rotate.p = null;




function rotate_onload ()
{
	var p = getPix();
	p[0].p = p;
	p[0].ix = 0;
	p[0].auto = new Function("clearInterval(this.t); this.t=setInterval('rotate()',7000);")
	p[0].rotate = rotatePix;
	p[0].onclick = new Function("this.auto(); this.rotate()");
	p[0].t = null;
	p[0].fx = new Transition(p[0]);
	for (var i=1,j=[];i<p.length;i++)
	{
		p[i].p=p;  p[i].n=i;
		p[i].onclick=new Function("this.p[0].auto(); swapPix(this, this.p[0])");
		p[i].fx = new Transition(p[i]);
		j[i]=new Image();
		j[i].src = p[i].style.backgroundImage.replace( /(url\(["' ]*)?(.+)m(\.[a-z]+)['") ]*$/, "$2$3" );
	}

	rotate.p = p;
	p[0].auto();
}

onload = rotate_onload;
