/*
	E.M. 2006
	www.BePartners.it
*/

function setFramedLinks ( contId )
{
	var o = document.getElementById(contId);
	if ( !o ) return [];
	var a = o.getElementsByTagName('A');

	var frame = createFrame("loadframe", "main");

	for ( var j=null, i=0; i<a.length; i++ )
	{
		j = a.item(i);
		if ( j.href.search(/\.(jpg|jpeg|png|gif)/) < 0 ) { continue };
		j.frame = frame;

		j.onclick = openFrame;
		j.frame.a.frame = frame ;
		j.frame.a.onclick = closeFrame ;
	}
	return a;
}



function setContainers ( ids )
{
	for (var i=0, a=[]; i<ids.length; i++)
	{
		a.push(setFramedLinks(ids[i]));
	}
	return a;
}



function openFrame ( e )
{
	if ( !e ) e = window.event;
	return !loadFrame(this, e);
}

function closeFrame ( e )
{
	if ( !e ) e = window.event;
	return this.frame.hide();
}


function createFrame ( fid, pid )
{
	var f = document.getElementById(fid);
	if ( f )  return f;

	f = document.createElement('DIV');
	f.setAttribute('id',fid);
	f.setAttribute('style',"position:absolute; top:0; left:0; visibility:hidden; z-index:-999; "
					+"padding:10px; background:#5f5f5f none; border:1px solid #242526");
	var a = document.createElement('A');
	a.setAttribute('href',"#");

	var img = document.createElement('IMG');
	if ( img.style )
		img.style.display = 'block';
	else
		img.setAttribute('style',"display:block;");
	f.img = a.appendChild(img);
	f.imgs = [];
	f.a = f.appendChild(a);
	f.t = null;
	f.s = false;

	f.hide = new Function("this.s=false; with(this.style){ visibility='hidden'; zIndex='-999'; return this.s;}");
	f.show = new Function("this.s=true; with(this.style){ visibility='visible'; zIndex='999'; padding='10px'; background='#5f5f5f none'; border='1px solid #242526'; position='absolute'; }");

	f.loadImg = loadImg;

	f.chgpos = moveFrame;
	f.realign = alignFrame;
	f.pop = popFrame;

	f.getDim = getDim;
/*	var parent = document.getElementById(pid);
	if ( !parent )  parent = document.body;
	f = parent.appendChild(f);*/
	f = document.body.appendChild(f);
	f.hide();

	return f;
}



function loadImg ( u )
{
	if ( !u ) return false;

	for ( var i=0, img=null; i<this.imgs.length; i++ )
	{
		if ( this.imgs[i].src == u )
		{
			img = this.imgs[i]
			break;
		}
	}

	if ( !img )
	{
		img = document.createElement('IMG');
		if ( img.style )
			img.style.display = 'block';
		else
			img.setAttribute('style',"display:block;");
		img.src = u;

		this.imgs.push(img);
	}

	this.a.removeChild(this.img);
	this.img = this.a.appendChild(img);
	return img;
}


function getPageScroll ()
{
	if (window.scrollY != undefined) return window.scrollY; // MZ
	if (document.documentElement.scrollTop != undefined) return document.documentElement.scrollTop; //IE
	if (window.pageYOffset != undefined) return window.pageYOffset; // NS4
	return false;
}


function getOffset ( o1, o2 )
{// pos. di o1 risp. al progen. comune con o2
	var q = {x:0,y:0};
	var p = o1;
	do
	{
		q.x += p.offsetLeft;
		q.y += p.offsetTop;
		p = p.offsetParent;
	}
	while ( p && p != o2.offsetParent );
	return q;
}



function getWinDims ()
{
	var w = window; d = document; p = {x:0,y:0};
	if ( d.documentElement && d.documentElement.clientWidth )
	{
		p.x = d.documentElement.clientWidth;
		p.y = d.documentElement.clientHeight;
	}
	else if ( d.body && d.body.clientWidth )
	{
		p.x = d.body.clientWidth;
		p.y = d.body.clientHeight;
	}
	else if ( w.innerWidth )
	{
		p.x = w.innerWidth;
		p.y = w.innerHeight;
	}

	return p;
}


function waitFn ( f )
{
	var o = this;
	var v;
	if ( v = f.call(o) )
	{
		return v;
	}
	else
	{
		window.setTimeout(function(){f.call(o)},250);
		return false;
	}
}


function moveFrame ( d, f )
{
	if (!f) var f = this;

	var p = getPos(f.t, f);// Pos. e ingombri del link, nel riferim. di "f"

	var x = p[1].x + 8;
	var y = parseInt( (p[1].y + p[0].y - d.y)/2 );

	var w = getWinDims();
	var h = getPageScroll();
	if ( x > w.x - (d.x + 8) )  x = Math.max(0, p[0].x - (d.x + 8));
	if ( y > w.y + h - d.y )  y = Math.max(h, w.y + h - d.y);
	if ( y < h )  y = h;

	f.style.left = ''+x+'px';
	f.style.top = ''+y+'px';	
}


function alignFrame ( f )
{
	if (!f) var f = this;

	var d = f.getDim();
	f.t.style.cursor = "progress";
	if ( !d ) // aspetta di sapere le dim. dell'imm.
	{
		d = {x:50,y:50};
		window.setTimeout(function(){alignFrame.call(f)},50);
	}
	else if ( !d.c )
	{
		window.setTimeout(function(){alignFrame.call(f)},250);
	}
	else
	{
		f.t.style.cursor = "pointer";
	}
	f.chgpos(d);
}


function popFrame ( f )
{
	if (!f) var f = this;

	f.realign();
	f.show();

	return true;
}



function getDim ()
{
	var i = this.img;
	if ( !(i.width && i.height && i.src==this.t.href) )  return false;
	d = new Object();
	d.x = parseInt(i.width) + 22;
	d.y = parseInt(i.height) + 22;
	d.c = i.complete;
	return d;
}



function getPos ( o, o2 )
{
	if (!o2) var o2 = document.documentElement;
	var p1 = getOffset(o,o2);
	var p2 = {x:0,y:0};
	p2.x = p1.x + o.offsetWidth;
	p2.y = p1.y + o.offsetHeight;
	return [p1,p2];
}



function loadFrame ( a, e )
{
	var f = a.frame;

	if ( f.s && f.t == a ) { f.hide(); return true; }

	f.hide();

	f.t = a;
	f.a.href = a.href;

	f.loadImg(a.href);

	f.pop();

	return true;
}




function frame_onload ()
{
	setContainers(["colori","gallery","head"]);
}
