

Sinfo_MapToolbarItem = function(par_Sinfo_MapToolbar, par_Title, par_Alt, par_ShapeId,
		                        par_AreadId)
{
	var oSinfo_MapToolbar = par_Sinfo_MapToolbar;
	var oThis = this;
	
	var oShapeId = par_ShapeId;
	var oTitle = par_Title;
	
	this.getTitle = function()
	{
		return oTitle;
	}
	
	this.getShapeId = function()
	{
		return oShapeId;
	}
	
	 var areaId = par_AreadId;
	    
	    this.setAreaId = function(par_AreaId)
	    {
	    	areaId = par_AreaId;
	    }
	    
	    this.getAreaId = function()
	    {
	    	return areaId;
	    }
	
	 
	    
	var prevVisible = null;
	
	var mydiv = document.createElement('div');		
	mydiv.setAttribute('title', par_Alt);
	setAttribute_class(mydiv, 'LocationListPoint');
	//mydiv.setAttribute('className', 'LocationListPoint');

	oSinfo_MapToolbar.getHtmlContainer().appendChild(mydiv);
	
	//oSinfo_MapToolbar.getHtmlContainer().innerHTML = '<div class="LocationListPoint">' + par_Title + '</div>';
	mydiv.innerHTML = par_Title; 
	
	var clickFunction = function()
	{
		oSinfo_MapToolbar.getSinfoMap().onLegendClick(
				par_ShapeId, par_AreadId
				);
		oSinfo_MapToolbar.setSelectedShape(par_ShapeId);
	}
	var clickEvent = function()
	{
		clickFunction();
		oSinfo_MapToolbar.setSearchText(oThis.getTitle());
	}
	
	this.getClickFunction = function()
	{
		return clickFunction;
	}
	
	SinfoNet_ClientObjManager.Util.addEvent(mydiv, 'click', clickEvent,true );
	
	var mouseOverFunction = function()
	{
		try {
		var shape = oSinfo_MapToolbar.getSinfoMap().getShapeById(par_ShapeId);
		prevVisible = shape.isVisible();
		shape.setVisible(true);
		shape.setSelected(true);
		} catch (e) {}
	}
	SinfoNet_ClientObjManager.Util.addEvent(mydiv, 'mouseover', mouseOverFunction,true );
	
	var mouseOutFunction = function()
	{
		try {
			//oSinfo_MapToolbar.getSinfoMap().getShapeById(par_ShapeId).hide();
			oSinfo_MapToolbar.getSinfoMap().getShapeById(par_ShapeId).setSelected(false);
			if (!prevVisible)
			{
				oSinfo_MapToolbar.getSinfoMap().getShapeById(par_ShapeId).setVisible(false);
			}
		} catch (e) {}
	}
	SinfoNet_ClientObjManager.Util.addEvent(mydiv, 'mouseout', mouseOutFunction,true );
	
	
	this.setSelected = function(par_boolean)
	{
		if (par_boolean)
		{
			//mydiv.setAttribute('class', 'LocationListPointSelected');
			mydiv.className="LocationListPointSelected";
		}
		else
		{
			//mydiv.setAttribute('class', 'LocationListPoint');
			mydiv.className="LocationListPoint";
		}
	}
	
	this.getHTMLContainer = function()
	{
		return mydiv;
	}
	
}

/**
 * The object that manage the map location toolbar
 */
Sinfo_MapToolbar = function(par_SinfoMap)
{
	var oSinfoMap = par_SinfoMap;
	
	var inputText = document.getElementById(par_SinfoMap.getId() + '_ResortSearchInput');
	var itemsContainer = document.getElementById(par_SinfoMap.getId() + '_LocationList');
	var oThis = this;
	
	var items = new Array();
	
	this.setSearchText = function(par_Text)
	{
		//alert('setSearchText');
		inputText.value = par_Text;
		for(var i = 0; i < items.length; i++)
		{
			var item = items[i];
			var itemText = item.getTitle().substring(0,par_Text.length).toLowerCase();
			if (itemText == par_Text.toLowerCase())
			{
				var scrolly = item.getHTMLContainer().offsetTop // magic number: scroll offset fix
				itemsContainer.scrollTop = scrolly;
				//alert(scrolly);
				//window.console.debug(scrolly);
				
				break;
			}
		}
	}
	
	this.onKeyUp = function(par_Text)
	{
		//alert('onKeyUp');
		inputText.value = par_Text;
		var found = false;
		var lastItemTextIndex =  999;
		var itemFound = null;
		for(var i = 0; i < items.length; i++)
		{
			var item = items[i];
			var itemText = item.getTitle().toLowerCase();
			//console.log(itemText)
			var itemTextIndex = itemText.indexOf(par_Text.toLowerCase());
			//console.log(itemTextIndex)
			//console.log(lastItemTextIndex)
			if (itemTextIndex >= 0 && itemTextIndex < lastItemTextIndex)
			{
				lastItemTextIndex = itemTextIndex;
				itemFound = item;
				found = true;
			}
		}
		if (!found)
		{
  		   oSinfoMap.setSelectedAreaId('');
		}
		else
		{
			var scrolly = itemFound.getHTMLContainer().offsetTop; // magic number: scroll offset fix
			itemsContainer.scrollTop = scrolly;

			oThis.getSinfoMap().onLegendSelect(itemFound.getAreaId());
			oThis.setSelectedShape(itemFound.getShapeId(),'no');
		}
	}
	
	var inputKeyUp = function()
	{
		oThis.onKeyUp(inputText.value);
	}
	
	
	SinfoNet_ClientObjManager.Util.addEvent(inputText, 'keyup', inputKeyUp, true);
	
	this.addItem = function(par_Title, par_Alt, par_ShapeId, par_AreaId)
	{
		items[items.length]= new Sinfo_MapToolbarItem(oThis,par_Title, par_Alt, par_ShapeId,
				par_AreaId);
	}
	this.getHtmlContainer = function()
	{
		return itemsContainer;
	}
	this.getSinfoMap = function()
	{
		return oSinfoMap;
	}
	this.getShapeByAreaId = function(par_AreaId)
	{
		//alert("Sinfo_MapToolBar.getShapeByAreaId : " + par_AreaId)
		for(var i = 0; i < items.length; i++)
		{
			var item = items[i];
			//window.console.debug(item);
			//window.console.debug(item.getShapeId);
			if (item.getAreaId() == par_AreaId)
			{
				//oThis.setSearchText(item.getTitle());
				return item;
			}
		}
	}
	/*
	 * par_nosettext: if not empty, the name of the location is copied to the input field
	 */
	this.setSelectedShape = function(par_ShapeId, par_nosettext)
	{
		//alert("Sinfo_MapToolBar.setSelectedShape par_ShapeId: " + par_ShapeId);
		
		//inputText.value = oSinfoMap.getShapeById(par_ShapeId).getTitle();
		for(var i = 0; i < items.length; i++)
		{

			var item = items[i];
			//window.console.debug(item);
			//window.console.debug(item.getShapeId);
			if (item.getShapeId() == par_ShapeId)
			{
				//oThis.setSearchText(item.getTitle());
				item.setSelected(true);
				if (!par_nosettext)
				{
				   //alert(par_nosettext);	
				   inputText.value = item.getTitle();
				}
			}
			else
			{
				item.setSelected(false);
			}
		}
	}
	this.unselect = function()
	{
		
		//inputText.value = oSinfoMap.getShapeById(par_ShapeId).getTitle();
		for(var i = 0; i < items.length; i++)
		{
			var item = items[i];
    		item.setSelected(false);
		}
	}
}


var myNaviVisible = true;
var maxContentHeight = 0;

function hideLocationList(mapid){
	document.getElementById(mapid + "_LocationListBoxContainer").style.display = "none";
	document.getElementById(mapid + "_myMapNavigationButtonIcon").src = "/maps/images/LocationListOpen.gif";
	document.getElementById(mapid + "_myMapNavigationWrapper").style.height = document.getElementById(mapid + "_myMapNavigation").offsetHeight + "px";
	//document.getElementById(mapid + "_myMapNavigationWrapper").style.backgroundColor = "transparent";
}

function showLocationList(mapid){
	document.getElementById(mapid + "_LocationListBoxContainer").style.display = "inline";
	document.getElementById(mapid + "_myMapNavigationButtonIcon").src = "/maps/images/LocationListClose.gif";
	document.getElementById(mapid + "_myMapNavigationWrapper").style.height = document.getElementById(mapid + "_myMapNavigation").offsetHeight + "px";
	//document.getElementById(mapid + "_myMapNavigationWrapper").style.backgroundColor = "#D7D7D7";
}

function changeLocationListVisibility(mapid){
    if(myNaviVisible == true){
        hideLocationList(mapid);
        myNaviVisible = false;
    }else{
        showLocationList(mapid);
        myNaviVisible = true;
    }
}

function scrollTo(searchinput,mapid){
    var insertedText = searchinput.value;
    if(insertedText != ""){
        //var comparationText = insertedText.substring(0,1);
        var comparationText = insertedText;
        //alert(firstchar)
        manageLocationListElements(comparationText,mapid);
     }else{
        manageLocationListElements("",mapid);
     }
}

function trim(sString) 
{
    return sString.replace(/^\s+|\s+$/, '');
}

function manageLocationListElements(comparationStr,mapid){
    document.getElementById("LocationList").scrollTop = 0;
    var LocationListElements = document.getElementById("LocationList").getElementsByTagName("DIV");
    totalContentHeight = 0;
    for (tagcounter=0;tagcounter<LocationListElements.length;tagcounter++){
        if((LocationListElements[tagcounter].className == "LocationListPoint")||(LocationListElements[tagcounter].className == "LocationListPointSelected")){
            if(comparationStr){
	            var tagtext = trim(LocationListElements[tagcounter].innerHTML);
	            var tagfirstletter = tagtext.substring(0,comparationStr.length).toLowerCase();
	            if(tagfirstletter != comparationStr.toLowerCase()){
	                LocationListElements[tagcounter].style.display = "none";
	                if(totalContentHeight>0){
	                    totalContentHeight = totalContentHeight - parseInt(LocationListElements[tagcounter].offsetHeight);
	                }
	            }else{
	                LocationListElements[tagcounter].style.display = "inline";
	                totalContentHeight = totalContentHeight + parseInt(LocationListElements[tagcounter].offsetHeight);
	            }
            }else{
                LocationListElements[tagcounter].style.display = "inline";
                totalContentHeight = totalContentHeight + parseInt(LocationListElements[tagcounter].offsetHeight);
                //alert(totalContentHeight)
            }
        }
    }
    restyleMapNavi(mapid);
}

function restyleMapNavi(mapid){
    if(maxContentHeight == 0){
        maxContentHeight = document.getElementById("LocationListBox").offsetHeight;
    }
    if(totalContentHeight > maxContentHeight){
        document.getElementById("LocationList").style.height = maxContentHeight + "px";
      	document.getElementById("LocationListBox").style.height = maxContentHeight + "px";
        if(document.getElementById("jScrollPaneContainer")){
	        document.getElementById("jScrollPaneContainer").style.height = maxContentHeight + "px";
	    }
		//$('#LocationList').jScrollPane({showArrows:true});
    }else{
        var newLocationListWidth = "115px";
        document.getElementById("LocationList").style.width = newLocationListWidth;
        document.getElementById("LocationListBox").style.height = document.getElementById("LocationList").offsetHeight;

        //$('#LocationList').jScrollPane({showArrows:false});
    }
    document.getElementById(mapid + "_myMapNavigationWrapper").style.visibility = "visible";
    document.getElementById(mapid + "_myMapNavigationWrapper").style.left = document.getElementById(mapid + "_map").offsetWidth-document.getElementById(mapid + "_myMapNavigation").offsetWidth + "px";
    
}


