// JavaScript Document

var pos=0;
var lock=0;
var showrand=0;

function shrinkLogo() {
	new Effect.Morph('logo', { style:'width:8px; height:20px', duration:0.5, beforeStart: function () { lockLogo=1 }, afterFinish: function () { document.getElementById('logo').src="img/logo_piccolo.png"; lockLogo=0 } } );
}

function growLogo() {
	document.getElementById('logo').src="img/logo.png"; 
	new Effect.Morph('logo', { style:'width:20px; height:50px', duration:0.3, beforeStart: function () { lockLogo=1 }, afterFinish: function () { lockLogo=0 } } );
}

/* Random url */
var chiave = Math.round(200*Math.random())+1;

function randomUrl() {
	var rand = Math.round(200*Math.random())+1;
	if (rand==chiave && showrand==0) {
		showrand=1;
		var valore = Math.round(5*Math.random());
		updHistory(2,valore);
	}
	setTimeout("randomUrl()",1000);
}
/* end */

function goInt(page) {
	new Effect.SlideUp('photoBox', { scaleX:true, scaleY:false, scaleFrom:100, scaleTo:0, duration:0.3 } );
	new Effect.SlideUp('prodWrap', { duration:0.3 } );
	setTimeout("location.href='"+page+"'",400);
}

function goIntNow(page) {
	location.href=page;
}

function apri() {
	new Effect.SlideUp2('photoBox', { scaleX:true, scaleY:false, scaleFrom:0, scaleTo:100 });
	new Effect.SlideDown('prodWrap' );
}

function loadImage(imgURL) {
	var target = document.getElementById('mainImg');
	var img = new Image();
	img.onload = function () {
		target.src=imgURL;
		document.getElementById('photoBox').style.height="416px";
/*		setTimeout(function(){new Effect.SlideUp2('photoBox', { scaleX:true, scaleY:false, scaleFrom:0, scaleTo:100, afterFinish: function () { lock=0 } })},200); */
		setTimeout(function(){new Effect.Morph('photoBox', {style:'width:520px;', afterFinish: function () { lock=0 } })},200); 
	}
	img.src = imgURL;
}


function switchImg(img) {
	if (lock==0) {
		updHistory(1,img);
/*		new Effect.Shrink('photoBox', { direction:'left', duration:0.5, beforeStart: function () { lock=1 } } ); */

		new Effect.Morph('photoBox', { style:'width:0px; height:0px', duration:0.4, beforeStart: function () { lock=1 } } ); 
		setTimeout(function(){loadImage("foto/"+img)},500);
		var imgURL="foto/"+img;
		for (i=0; i<el.length; i++) {
			if (el[i].getAttribute("href",2)==imgURL) {pos=i;}
		}
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function nextImage() {
	if ((pos+1)<el.length) {
		var dove=el[pos+1].getAttribute("href",2);
		dove=dove.replace("foto/", "");
		switchImg(dove);
	}
}

function prevImage() {
	if ((pos)>0) {
		var dove=el[pos-1].getAttribute("href",2);
		dove=dove.replace("foto/", "");
		switchImg(dove);
	}
}


// Keys handling //

nn=(document.layers)?true:false;
ie=(document.all)?true:false;
function keyDown(evt) {
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt) {
		var charCode = (evt.charCode) ? evt.charCode :
					   ((evt.keyCode) ? evt.keyCode :
					   ((evt.which) ? evt.which : 0));
		if (charCode == 39) nextImage(); // Destra
		if (charCode == 37) prevImage(); // Sinistra
	}    
}
document.onkeydown=keyDown;
if(nn) document.captureEvents(Event.KEYDOWN);

/*
document.onmousemove = (function() {
	var onmousestop = function() {
		setTimeout("Effect.Appear('logo', {duration: 1.4 })",1500);
	}, thread;

	return function() {
		clearTimeout(thread);
		thread = setTimeout(onmousestop, 3000);
	};
})();
*/
