var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

var minTop = new Array();
var maxTop = new Array();
var minLeft = new Array();
var maxLeft = new Array();
var delta = 1;
var isMoving = false;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function getRectPositions(id){
	clip = id.style.clip;
	
	clip = clip.replace("rect(","");
	clip = clip.replace("px)","");
	
	for(i = 0; i < 3; i++){
		if (browser == "Explorer" || browser == "Safari"){
			clip = clip.replace("px ","|");		
		}
		else{
			clip = clip.replace("px, ","|");		
		}
	}
	return clip.split("|");
}


function goTop(content_block, idBox, waitTime){
	//Recupero delle dimensioni della maschera
	id = document.getElementById(content_block);
	//Recupero della posizione della maschera
	thisTop = Number(id.style.top.replace("px",""));
	
	//Se é possibile spostarsi a sinistra
	if (thisTop < maxTop[idBox]){
		//Assegnazione delle nuove dimensioni della maschera
		arrayRect = getRectPositions(id);
		if (browser == "Explorer" || browser == "Safari"){
			id.style.clip = "rect("+ (Number(arrayRect[0]) - delta) +"px "+ arrayRect[1] +"px "+ (Number(arrayRect[2]) - delta) +"px "+ arrayRect[3] +"px)";
		} else {
			id.style.clip = "rect("+ (Number(arrayRect[0]) - delta) +"px, "+ arrayRect[1] +"px, "+ (Number(arrayRect[2]) - delta) +"px, "+ arrayRect[3] +"px)";
		}		
		//Assegnazione della nuova posizione della maschera	
		id.style.top = Number(thisTop + delta) + 'px';
		
		if (isMoving){
			timerID = setTimeout ('goTop(\''+content_block+'\', '+idBox+', '+waitTime+')', waitTime);
		}
	}
}
function goRight(content_block, idBox, waitTime){
	//Recupero delle dimensioni della maschera
	id = document.getElementById(content_block);
	//Recupero della posizione della maschera
	left = Number(id.style.left.replace("px",""));
	
	//Se é possibile spostarsi a sinistra
	if (left > minLeft[idBox]){
		//Assegnazione delle nuove dimensioni della maschera
		arrayRect = getRectPositions(id);		
		if (browser == "Explorer" || browser == "Safari"){
			id.style.clip = "rect("+ arrayRect[0] +"px "+ (Number(arrayRect[1]) + delta) +"px "+ arrayRect[2] +"px "+ (Number(arrayRect[3]) + delta) +"px)";
		} else {
			id.style.clip = "rect("+ arrayRect[0] +"px, "+ (Number(arrayRect[1]) + delta) +"px, "+ arrayRect[2] +"px, "+ (Number(arrayRect[3]) + delta) +"px)";
		}	
		//Assegnazione della nuova posizione della maschera	
		id.style.left = Number(left - delta) + 'px';
		
		if (isMoving){
			timerID = setTimeout ('goRight(\''+content_block+'\', '+idBox+', '+waitTime+')', waitTime);
		}
	}
}
function goBottom(content_block, idBox, waitTime){
	//Recupero delle dimensioni della maschera
	id = document.getElementById(content_block);
	//Recupero della posizione della maschera
	thisTop = Number(id.style.top.replace("px",""));
	
	//Se é possibile spostarsi a sinistra
	if (thisTop > minTop[idBox]){
		//Assegnazione delle nuove dimensioni della maschera
		arrayRect = getRectPositions(id);
		if (browser == "Explorer" || browser == "Safari"){
			id.style.clip = "rect("+ (Number(arrayRect[0]) + delta) +"px "+ arrayRect[1] +"px "+ (Number(arrayRect[2]) + delta) +"px "+ arrayRect[3] +"px)";
		} else {
			id.style.clip = "rect("+ (Number(arrayRect[0]) + delta) +"px, "+ arrayRect[1] +"px, "+ (Number(arrayRect[2]) + delta) +"px, "+ arrayRect[3] +"px)";
		}		
		//Assegnazione della nuova posizione della maschera	
		id.style.top = Number(thisTop - delta) + 'px';
		
		if (isMoving){
			timerID = setTimeout ('goBottom(\''+content_block+'\', '+idBox+', '+waitTime+')', waitTime);
		}
	}
}
function goLeft(content_block, idBox, waitTime){
	//Recupero delle dimensioni della maschera
	id = document.getElementById(content_block);
	//Recupero della posizione della maschera
	left = Number(id.style.left.replace("px",""));
	
	//Se é possibile spostarsi a sinistra
	if (left < maxLeft[idBox]){
		//Assegnazione delle nuove dimensioni della maschera
		arrayRect = getRectPositions(id);
		if (browser == "Explorer" || browser == "Safari"){
			id.style.clip = "rect("+ arrayRect[0] +"px "+ (Number(arrayRect[1]) - delta) +"px "+ arrayRect[2] +"px "+ (Number(arrayRect[3]) - delta) +"px)";
		} else {
			id.style.clip = "rect("+ arrayRect[0] +"px, "+ (Number(arrayRect[1]) - delta) +"px, "+ arrayRect[2] +"px, "+ (Number(arrayRect[3]) - delta) +"px)";
		}		
		//Assegnazione della nuova posizione della maschera	
		id.style.left = Number(left + delta) + 'px';
		
		if (isMoving){
			timerID = setTimeout ('goLeft(\''+content_block+'\', '+idBox+', '+waitTime+')', waitTime);
		}
		else{
			isMoving = false;
		}
	}
}

function move(direction, idBox, waitTime, content_block, idImage, strImage){
	document.getElementById(idImage).src = strImage;
	isMoving = true;
	switch (direction){
		case 'top':
			goTop(content_block, idBox, waitTime);
			break;
		case 'right':
			goRight(content_block, idBox, waitTime);
			break;
		case 'bottom':
			goBottom(content_block, idBox, waitTime);
			break;
		case 'left':
			goLeft(content_block, idBox, waitTime);
			break;
	}
}

function stopMove(idImage, strImage){
	document.getElementById(idImage).src = strImage;
	isMoving = false;
}

function printContents(contents_position, content_block, idBox){
	//Se nel sito ci sono blocchi di contenuti mobili
	if (document.getElementById(contents_position) != null){
		content = getAnchorPosition(contents_position);
		id = document.getElementById(content_block);
		arrayRect = getRectPositions(id);			
		
		//Posizionamento del blocco con i contenuti alla posizione stabilitą dal design
		contentBlock = document.getElementById(content_block);
		contentBlock.style.top = content.y - arrayRect[0] + 'px';
		contentBlock.style.left = content.x - arrayRect[3] + 'px';	
		contentBlock.style.visibility = 'visible';		
		
		//Calcolo delle posizioni minime e massime dello spostamento del blocco dei contenuti		
		minTop[idBox] = content.y - (id.offsetHeight - arrayRect[2]) - arrayRect[0];
		maxTop[idBox] = content.y;
		minLeft[idBox] = content.x - (id.offsetWidth - arrayRect[1]) - arrayRect[3];
		maxLeft[idBox] = content.x;
		
		heightReservedSpace = id.offsetHeight - arrayRect[2];
		widthReservedSpace = id.offsetWidth - arrayRect[1];
				
		//Se il blocco con i contenuti é pił alto dello spazio assegnato
		if (heightReservedSpace > 0){
			//Riservare dello spazio per poter muovere la pagina senza muovere la finestra
			document.getElementById(content_block + '_reserved_space').style.height = heightReservedSpace + 'px';		
			//Attivare le frecce le frecce del movimento
			botton = $(content_block + '_botton_up');
			if(botton != null){
				botton.style.visibility = 'visible';
			}
			botton = $(content_block + '_botton_down');
			if(botton != null){
				botton.style.visibility = 'visible';
			}
		}
		//Se il blocco con i contenuti é pił largo dello spazio assegnato
		if (widthReservedSpace > 0){
			//Attivare le frecce le frecce del movimento
			botton = $(content_block + '_botton_left');
			if(botton != null){
				botton.style.visibility = 'visible';
			}
			botton = $(content_block + '_botton_right');
			if(botton != null){
				botton.style.visibility = 'visible';
			}	
		}
	}
}
