function MoveTo(page){

 location.href=page ;
 
}



//array di selezione
var indexes=new Array();

var suggestionHide=true;
var acTimeout;

/**********************************
itemLink
***********************************/
function itemLink(id,descr,litype){

this.id=id;
this.descr=descr;
this.toShow=descr.toLowerCase();
this.tosearch=descr.toLowerCase();	
this.litype=litype;

}
itemLink.prototype.matchSearch= function(searchString){


	var weigth=0;
	var negweigth=0-weigth;
	
 if ((searchString.localeCompare(this.tosearch)>=negweigth)&&(searchString.localeCompare(this.tosearch)<=weigth)){
 		return 2;
   } //else if (this.tosearch.indexOf(searchString)!=-1) return 1;
   
   else{
     var matching=searchString.split(' ');
     var finded=true;
     for (iii=0;iii<matching.length;iii++){ 
     //alert(matching[iii]);
       if (this.tosearch.indexOf(matching[iii])==-1) finded=false; 
     }
     return finded ? 1 : 0 ;
     }
     
  return 0; 
  
}

itemLink.prototype.matchSearchDebug= function(searchString){

   return searchString.localeCompare(this.tosearch);
}

//FUNZIONE DI CALCOLO DEI LINK 
itemLink.prototype.getHref = function(){

   if(this.litype=='n')
    	{return this.id+'.shtml';}

   if(this.litype=='c')
    	{return this.id+'.shtml';}

}


//fine itemLink

/*************************************
  SEARCHENGINE
**************************************/
function SearchEngine(idTextbox, idButton,idMessageBox) {

    this.layer = null;
    this.provider = null;
    this.textbox = document.getElementById(idTextbox);
    this.button  = document.getElementById(idButton);
    this.messagebox  = document.getElementById(idMessageBox);
    
    this.alignment="right";
    
    //this.init();
    var oThis=this;
    
    //events
    this.textbox.onfocus = function () {
        oThis.hideSuggestions();
    };
    
    this.button.onclick =function(){
    	 oThis.cmpSearch();
    };
    
    //this.createDropDown();
}

SearchEngine.prototype.createDropDown = function () {

    this.layer = document.createElement("div");
    this.layer.id ="SearchSuggestionBox";
    this.layer.className = "suggestions";
    this.layer.style.visibility = "hidden";
    this.layer.style.position="absolute"; 
	this.layer.style.background="#efefef";
	this.layer.style.border="1px solid #ccc";
    //this.layer.style.width = this.textbox.offsetWidth;
    //this.layer.style.width = "100%";
    
    //Patch for IE
    //if (this.textbox.left='undefined'){    
    	this.layer.style.left=this.getLeft();
    	this.layer.style.top=this.getTop()+this.textbox.offsetHeight;
    //}else{
    //	this.layer.style.left = this.textbox.left;
    // 	this.layer.style.top = this.textbox.top+10;
    //}
    this.layer.innerHTML="XX";
	document.body.appendChild(this.layer);
	//document.appendChild(this.layer);
	
};

SearchEngine.prototype.highlightSuggestion = function (oSuggestionNode) {

    for (var i=0; i < this.layer.childNodes.length; i++) {
        var oNode = this.layer.childNodes[i];
        if (oNode == oSuggestionNode) {
            oNode.className = "current"
        } else if (oNode.className == "current") {
            oNode.className = "";
        }
    }
};

SearchEngine.prototype.getLeft = function () {

    var oNode = this.textbox;
    var iLeft = 0;

    while((oNode!=null)&&(oNode.tagName != "BODY")) {
    //alert('left ' + oNode);
    //while(oNode.tagName != "BODY") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;
    }

    return iLeft;
};
SearchEngine.prototype.getTop = function () {

    var oNode = this.textbox;
    var iTop = 0;

    while((oNode!=null)&&(oNode.tagName != "BODY")) {
    //while(oNode.tagName != "BODY") {
     //alert('top ' + oNode);
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }

    return iTop;
};
SearchEngine.prototype.show = function (res) {

    intxt="";
    for(i=0;i<res.length;i++){
         intxt +='<div class="suggestionItem" style="font-size:9px;font-family:Tahoma, Verdana, Arial, sans-serif;text-transform:capitalize;">';
         intxt +='<a href="'+res[i].getHref()+'" onmouseover="suggestionHide=false;" onmouseout="suggestionHide=true;" style="color:#666;">';
         intxt +=res[i].toShow+'</a>';
         intxt +='</div>';
	}
	//intxt +='<div><a href="javascript:autos.hideSuggestions();">'+'Nascondi'+'</a></div>';
    this.layer.innerHTML=intxt;
    
    if (this.alignment=="right"){
      this.layer.style.left = (this.getLeft()-(this.layer.offsetWidth-this.textbox.offsetWidth)) + "px";
    }else{
      this.layer.style.left = this.getLeft() + "px";
    }
    this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
    
    //fade effect
    changeOpac(0,this.layer.id);
    this.layer.style.visibility = "visible";
	fade(this.layer.id,0,100);
};

SearchEngine.prototype.hideSuggestions = function () {
    
     if((this.layer!=null)&&(this.layer.style.visible=="visible")){
    	fade(this.layer.id,100,0);
	 }
};

//funzione di lancia dall'esterno
SearchEngine.prototype.outlinkSearch = function (textSearch){
 this.textbox.value=textSearch;
 this.cmpSearch();
}

//funzione di ricerca
SearchEngine.prototype.cmpSearch = function (){

	//timeout reset
	if (acTimeout) window.clearTimeout(acTimeout);

	//instanzio il div
	if (this.layer==null)
		this.createDropDown();

	//sistemo la stringa 
	searchString =this.textbox.value;
	searchString=searchString.toLowerCase();
	
	while (searchString.substring(0,1) == ' '){
        searchString = searchString.substring(1, searchString.length);
    }
    while (searchString.substring(searchString.length-1, searchString.length) == ' '){
        searchString = searchString.substring(0,searchString.length-1);
    }
    
	res =new Array();
	ires=0;
    
  	for(i=0;i<indexes.length;i++){
  	      indexes[i].descr=indexes[i].descr.toLowerCase();
	     weight=indexes[i].matchSearch(searchString);
		if (weight==1){
			res[ires]=indexes[i];
			ires++;

		}else if (weight==2){
			res=new Array();
			res[0]=indexes[i];
			break;
		}
	}	
  	
  	
 	if (res.length==0){
 		suggestionHide=true;	
 	    acTimeout=window.setTimeout("resetSearchMessage();",5000);
    	this.messagebox.innerHTML="La ricerca non genera risultati";
    	
  	}else if (res.length==1){
    	
    	location.href=res[0].getHref();
    
    }else if (res.length<20){
    	 suggestionHide=true;
         acTimeout=window.setTimeout("resetSearch(this);",5000);
	     this.messagebox.innerHTML=""; 
	     this.show(res);  	
  	
  	}else{
     	//msg="Trovati pi&ugrave; valori, definisci la ricerca<br>";
     	suggestionHide=true;
     	acTimeout=window.setTimeout("resetSearchMessage(this);",5000);
	    this.messagebox.innerHTML="Trovati troppi valori, definisci meglio la ricerca<br>";

 	 }
  
}

//fine SEARCHENGINE


//Standalone
function resetSearchMessage(oMessageBox){
		document.getElementById("SearchMsg").innerHTML="";
}


function resetSearch(oMessageBox){
	if (suggestionHide==true){
		document.getElementById("SearchMsg").innerHTML="";
		//fade out
		fade("SearchSuggestionBox",100,0)
		//document.getElementById("SearchSuggestionBox").style.visibility = "hidden";
    }else{
    	//suggestionHide=true;
     	acTimeout=window.setTimeout("resetSearch(this);",5000);
    }
}


function fade(id,opacStart, opacEnd) {

	var speed = Math.round(500 / 100);
	var timer = 0;

	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
			setTimeout("setVisibility(" + i + ",'" + id + "')",(timer * speed));		
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
			setTimeout("setVisibility(" + i + ",'" + id + "')",(timer * speed));		
	}
}

function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function setVisibility(opacity,id){
	if (opacity>=100) document.getElementById(id).style.visible="visible";
	if (opacity<=0)   document.getElementById(id).style.visible="hidden";
}

