// JavaScript Document

//Global Vars
var n = 0; 

var motion = new Array();

function searchFirstAnimation() {
	
	var minVal, pos = -1;
	if (n > 0){
		for (i=0; i<n; i++)
			if (motion[i] != null){
				minVal = motion[i].animationNumber;
				pos = i;
				break;
			}
		for (i=0; i<n; i++)
			if (motion[i] != null && motion[i].animationNumber < minVal){
				minVal = motion[i].animationNumber;
				pos = i;
			}
	}
	else
		pos = 0;
	return pos;
	
}

function searchFreePos() {
	
	var pos = -1;
	if (n > 0){
		for (i=0; i<n; i++)
			if (motion[i] == null){
				pos = i;
				break;
			}
		if (pos == -1)
			pos = n;
	}
	else
		pos = 0;
	return pos;
	
}

function searchAnimation( element ) {
	
	var pos = -1;
	for (i=0; i<n; i++)
		if (motion[i] != null && motion[i].elementName == element){
			pos = i;
			break;
		}
	return pos;
	
}

function showLink_Animation( element ) {

	var object = document.getElementById(element).style;
	var property = 'top';
	var start = (element.substr(0,1) == 's') ? 85 : 113;
	var end = (element.substr(0,1) == 's') ? 72 : 101;
	var duration = 0.1;
	var suffixe = 'px';
	
	pos = searchAnimation( element );
	
	if(pos == -1){
		pos = searchFreePos();
		
		motion[pos] = new Tween(object,property,Tween.regularEaseOut,start,end,duration,suffixe);
		motion[pos].onMotionStarted = function(){ 
			motion[pos].animationNumber = n;
			motion[pos].elementName = element;
			motion[pos].timeout = setTimeout("autoHideLink_Animation();",2000); 
			n++;
		};
		motion[pos].start();
	}

}

function autoHideLink_Animation(){

	//var object = document.getElementById(lastElement).style;
	
	var pos = searchFirstAnimation();
	
	var property = 'top';
	var start = (motion[pos].elementName.substr(0,1) == 's') ? 72 : 101;
	var end = (motion[pos].elementName.substr(0,1) == 's') ? 85 : 113;
	var duration = 0.1;
	var suffixe = 'px';

	motion[pos] = new Tween(motion[pos].obj,property,Tween.regularEaseIn,start,end,duration,suffixe);
	motion[pos].onMotionStarted = function(){ 
		motion[pos] = null; 
	}
	motion[pos].start();
	
}

/*
function hideLink_Animation(element){

	var object = document.getElementById(element).style;
	var property = 'bottom';
	var start = -1;
	var end = -15;
	var duration = 0.1;
	var suffixe = 'px';
	
	pos = searchAnimation(element);
	
	if(pos != -1){
		motion[pos] = new Tween(motion[pos].obj,property,Tween.regularEaseIn,start,end,duration,suffixe);
		motion[pos].onMotionStarted = function(){ 
			motion[pos] = null; 
		}
		motion[pos].start();
	}
	
}
*/

//-----------------------------------------------------------------------------------------------------


var nC = 0; 

var motionColl = new Array();

function searchFirstCollAnimation() {
	
	var minVal, pos = -1;
	if (nC > 0){
		for (i=0; i<nC; i++)
			if (motionColl[i] != null){
				minVal = motionColl[i].animationNumber;
				pos = i;
				break;
			}
		for (i=0; i<nC; i++)
			if (motionColl[i] != null && motionColl[i].animationNumber < minVal){
				minVal = motionColl[i].animationNumber;
				pos = i;
			}
	}
	else
		pos = 0;
	return pos;
	
}

function searchFreeCollPos() {
	
	var pos = -1;
	if (nC > 0){
		for (i=0; i<nC; i++)
			if (motionColl[i] == null){
				pos = i;
				break;
			}
		if (pos == -1)
			pos = nC;
	}
	else
		pos = 0;
	return pos;
	
}

function searchCollAnimation( element ) {
	
	var pos = -1;
	for (i=0; i<nC; i++)
		if (motionColl[i] != null && motionColl[i].elementName == element){
			pos = i;
			break;
		}
	return pos;
	
}

var colObj = null;

function showLink_CollAnimation( element ) {

	var object = document.getElementById(element).style;
	var property = 'height';
	var start = 0;
	var end = 50;
	var duration = 0.5;
	var suffixe = 'px';

	colObj = element;
	
	var colAniShow = new Tween(object,property,Tween.regularEaseOut,start,end,duration,suffixe);
	colAniShow.start();
	
	/*
	pos = searchCollAnimation( element );
	
	if(pos == -1){
		pos = searchFreeCollPos();
		
		motionColl[pos] = new Tween(object,property,Tween.regularEaseOut,start,end,duration,suffixe);
		motionColl[pos].onMotionStarted = function(){ 
			motionColl[pos].animationNumber = nC;
			motionColl[pos].elementName = element;
			motionColl[pos].timeout = setTimeout("autoHideLink_CollAnimation();",3000); 
			nC++;
		};
		motionColl[pos].start();
	}
	*/
}

function autoHideLink_CollAnimation(){

	if(colObj != null){
		var object = document.getElementById(colObj).style;
	
		var property = 'height';
		var start = 50;
		var end = 0;
		var duration = 0.5;
		var suffixe = 'px';
		
		var colAniHide = new Tween(object,property,Tween.regularEaseIn,start,end,duration,suffixe);
		colAniHide.start();
	}
	
	/*
	var pos = searchFirstCollAnimation();

	motionColl[pos] = new Tween(motionColl[pos].obj,property,Tween.regularEaseIn,start,end,duration,suffixe);
	motionColl[pos].onMotionStarted = function(){ 
		motionColl[pos] = null; 
	}
	motionColl[pos].start();
	*/
}


//-----------------------------------------------------------------------------------------------------

var menuOpen = 0, languagesTimer;

function resetLanguagesMenuTimer() {
	clearTimeout(languagesTimer);
	languagesTimer = setTimeout("hideLanguagesMenu();",2000); 
}

function showLanguagesMenu() {
	
	var object = document.getElementById('languages_submenu');
	var start = 0;
	var end = 100;
	var duration = 0.5;
	
	//OpacityTween(Object,easing,startOpacity,endOpacity,duration);
	if(!menuOpen){
		object.style.display = 'block';
		var showMenu = new OpacityTween(object,Tween.regularEaseOut, start, end, duration);
		showMenu.onMotionStarted = function(){ 
			menuOpen = 1;
			languagesTimer = setTimeout("hideLanguagesMenu();",2000); 
		};
		showMenu.start();
	}
	else
		resetLanguagesMenuTimer();
	
}

function hideLanguagesMenu() {
	
	var object = document.getElementById('languages_submenu');
	var start = 100;
	var end = 0;
	var duration = 0.5;
	
	//OpacityTween(Object,easing,startOpacity,endOpacity,duration);
	var hideMenu = new OpacityTween(object,Tween.regularEaseIn, start, end, duration);
	hideMenu.onMotionFinished = function() {
		menuOpen = 0;
		object.style.display = 'none';
	}
	hideMenu.start();
	
}

//--------------------------------------------------------------------------------------

var winW = 630, winH = 460;

//Prende le dimensioni della finestra del browser
function getScreenDim() {

	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}
	
}

//--------------------------------------------------------------------------------------

//Recupera la posizione del puntatore del mouse

// Se il browser non è IE => il browser è NS.
var IE = document.all ? true : false;

if (!IE) 
	document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tmpX = 0;
var tmpY = 0;

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tmpX = event.clientX + document.body.scrollLeft;
    tmpY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tmpX = e.pageX;
    tmpY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tmpX < 0)
  	tmpX = 0;
  if (tmpY < 0)
  	tmpY = 0;

  return true;
}

//--------------------------------------------------------------------------------------

var scrollBarOpen = 0, scrollBarTimer;

function resetScrollBarTimer() {
	clearTimeout(scrollBarTimer);
	scrollBarTimer = setTimeout("hideScrollBar();",2000); 
}

function showScrollBar() {
	
	var object = document.getElementById('scrollBar');
	var start = 0;
	var end = 100;
	var duration = 0.5;
	
	//OpacityTween(Object,easing,startOpacity,endOpacity,duration);
	if(!scrollBarOpen){
		object.style.display = 'block';
		var showScrollBar = new OpacityTween(object,Tween.regularEaseOut, start, end, duration);
		showScrollBar.onMotionStarted = function(){ 
			scrollBarOpen = 1;
			scrollBarTimer = setTimeout("hideScrollBar();",2000); 
		};
		showScrollBar.start();
	}
	else
		resetScrollBarTimer();
	
}

function hideScrollBar() {
	
	var object = document.getElementById('scrollBar');
	var start = 100;
	var end = 0;
	var duration = 0.5;
	
	//OpacityTween(Object,easing,startOpacity,endOpacity,duration);
	var hideScrollBar = new OpacityTween(object,Tween.regularEaseIn, start, end, duration);
	hideScrollBar.onMotionFinished = function() {
		scrollBarOpen = 0;
	}
	hideMenu.start();
	
}