/*----------------------------------------------------------
  PoiPopupFormatter Class

  Basic function:
  formats an array of locations to html elements to be used in a MapPopup
	
  @package NaviciAjaxApi
  @author Arnd Beyer, arnd.beyer@wmdata.fi
  @copyright WM-data Novo 2006
  @version $Id: PoiPopupFormatter.js,v 1.0 2005/07/26 12:44:42 arnd Exp $

  ---------------------------------------------------------*/
  
  function PoiPopupFormatterMetsa(){
  
  	var actionElements = new Array();
  	
   	var css_base = "poi_popup";
  	
  	this.setCssBase = function(newBase){
  		css_base = newBase;}
  	this.format = format;
  	this.addElement = addElement;
  	this.deleteElement = deleteElement;
  	
  	function addElement(element){
		var id = util.getFreeIndex(actionElements);
  		actionElements[id] = element;
  		return id;
  	}
  	
  	function deleteElement(id){
  		actionElements[id] = null;
  		return 0;
  	}
  
  	function format(locations){
		elements = new Array();
		
		for(var i=0; i<locations.length; i++){
			var div = document.createElement("div");
			div.className= css_base;
			if(locations[i].getAttribute("category") != null){
				var locHeadDiv =  document.createElement("div");
				locHeadDiv.className = css_base+"_head";
				var headText = locations[i].getAttribute("category");
				var subcategory = locations[i].getAttribute("subcategory");
				if(subcategory!= null){
					headText += "("+subcategory+")";
				}
				if(locations.length > 1){
					locHeadDiv.appendChild(document.createTextNode(i+1 +"/"+locations.length +" " +headText));
				}
				else{
					locHeadDiv.appendChild(document.createTextNode(headText));
				}
				div.appendChild(locHeadDiv);
			}
			
			var locText =  document.createElement("p");
			locText.className = css_base+"_text";
			locText.appendChild(document.createTextNode(locations[i].getName()));
			div.appendChild(locText);


			if(locations[i].getAttribute("info") != null && locations[i].getAttribute("info") != 'undefined'){
			
				var lengthText =  document.createElement("p");
				lengthText.className = css_base+"_text";
				lengthText.appendChild(document.createTextNode(locations[i].getAttribute("info")));
				div.appendChild(lengthText);
			
			}
			
			if(locations[i].getAttribute("route_length") != null && locations[i].getAttribute("route_length") != 'undefined'){
			
				var lengthText =  document.createElement("p");
				lengthText.className = css_base+"_text";
				lengthText.appendChild(document.createTextNode(locations[i].getAttribute("route_length") + " km"));
				div.appendChild(lengthText);
			
			}
			
			
			
			
			
			var restriction = locations[i].getAttribute("restriction");
			if(restriction != null && restriction != ""){
				var restrictionText =  document.createElement("p");
				restrictionText.className = css_base+"_restriction";
				restrictionText.appendChild(document.createTextNode(restriction));
				div.appendChild(restrictionText);	
			}
			
			var definition = locations[i].getAttribute("definition");
			if(definition != null && definition != ""){
				var definitionText =  document.createElement("p");
				definitionText.className = css_base+"_definition";
				definitionText.appendChild(document.createTextNode(definition));
				div.appendChild(definitionText);		
			}
			
			//Link in popup
			var link = locations[i].getAttribute("link");
			if(link != null && link != ""){
				var linkElem =  document.createElement("a");
				linkElem.className = css_base+"_link";
				linkElem.href= link;
				linkElem.target= "new";
//				var text = "Tietoa kohteesta";
			    var text = AjaxMapsConfig.POI_POPUP_LINK_NAME;
				
				linkElem.appendChild(document.createTextNode(text));
				div.appendChild(linkElem);
			}
			
				if(locations[i].getAttribute("wgs84_x") != null && locations[i].getAttribute("x") != 'undefined'){
				
				
					var wgs84_x_min = locations[i].getAttribute("wgs84_x");
				    var wgs84_y_min = locations[i].getAttribute("wgs84_y");
					
					wgs84_x_min = Math.floor(wgs84_x_min) + "° " + Number((wgs84_x_min - Math.floor(wgs84_x_min))*60).toFixed(5)+ "'";
					wgs84_y_min = Math.floor(wgs84_y_min) + "° " + Number((wgs84_y_min - Math.floor(wgs84_y_min))*60).toFixed(5)+ "'";
				
					var coordinateText =  document.createElement("p");
					var table = document.createElement("table");
					table.className = "poiPopupCoords";
					
					var tbBody = document.createElement("tbody");
					var row = document.createElement("tr");
					var cell = document.createElement("td");			
					
					coordinateText.className = css_base+"_coordinate";	
							
					cell.appendChild(document.createTextNode("YKJ"));				
					row.appendChild(cell);				
					cell = document.createElement("td");
					row.appendChild(cell);							
					tbBody.appendChild(row);
					
					cell = document.createElement("td");
					row = document.createElement("tr");	
					if(AjaxMapsConfig.LANGUAGE == 'fi')			
						cell.appendChild(document.createTextNode("p "+Math.round(locations[i].getAttribute("y")) + "m"));
					else if(AjaxMapsConfig.LANGUAGE == 'se')
						cell.appendChild(document.createTextNode("n "+Math.round(locations[i].getAttribute("y")) + "m"));
					else
						cell.appendChild(document.createTextNode("y "+Math.round(locations[i].getAttribute("y")) + "m"));
						
					row.appendChild(cell);
					tbBody.appendChild(row);
					
					
					row = document.createElement("tr");
					cell = document.createElement("td");
					if(AjaxMapsConfig.LANGUAGE == 'fi')			
						cell.appendChild(document.createTextNode("i "+Math.round(locations[i].getAttribute("x")) + "m"));
					else if(AjaxMapsConfig.LANGUAGE == 'se')
						cell.appendChild(document.createTextNode("o "+Math.round(locations[i].getAttribute("x")) + "m"));
					else
						cell.appendChild(document.createTextNode("x "+Math.round(locations[i].getAttribute("x")) + "m"));
					row.appendChild(cell);				
					
					
					tbBody.appendChild(row);		
					
					row = document.createElement("tr");						
					cell = document.createElement("td");					
					
					cell.appendChild(document.createElement("br"));
					cell.appendChild(document.createTextNode("WGS84"));					
					row.appendChild(cell);
					cell = document.createElement("td");
					row.appendChild(cell);				
					tbBody.appendChild(row);						
					
					cell = document.createElement("td");
					row = document.createElement("tr");								
					cell.appendChild(document.createTextNode("N "+wgs84_y_min));
					row.appendChild(cell);
					tbBody.appendChild(row);
					
					row = document.createElement("tr");				
					cell = document.createElement("td");
					cell.appendChild(document.createTextNode("E "+wgs84_x_min));
					row.appendChild(cell);				
					cell = document.createElement("td");
					tbBody.appendChild(row);					
					
					table.appendChild(tbBody);
					coordinateText.appendChild(table);
					div.appendChild(coordinateText);
				}	
			
			
				
			
			for(var j=0; j< actionElements.length; j++){
				if(actionElements[j] != null){
					var clone = actionElements[j].cloneNode(true);
					cloneEventAttributes(actionElements[j], clone);
					div.appendChild(clone);
					clone.locationObject =locations[i];
				}
			}
			elements.push(div);
		}
		return elements;
	}
	
	function cloneEventAttributes(source, target){
		target.onclick = source.onclick;
		target.ondblclick = source.ondblclick;
		target.onmousedown = source.onmousedown;
		target.onmouseup = source.onmouseup;
		target.onmouseover = source.onmouseover;
		target.onmousemove = source.onmousemove;
		target.onmouseout = source.onmouseout;
		target.onkeypress = source.onkeypress;
		target.onkeydown = source.onkeydown;
		target.onkeyup = source.onkeyup;
	}
  
}