﻿/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is VisKort.
*
* The Initial Developer of the Original Code is
* IT- og Telestyrelsen / Danish National IT and Telecom Agency.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):

* Lars Klindt Mogensen
* Morten Bødtkjer
* Niels Kinnerup
* Thomas Bergstedt
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

/** 
* Class: VisKort.Utils
*
* Class containing utility methods
*/
VisKort.Utils = OpenLayers.Class({

	/**
	* Constructor: VisKort.Utils
	*/
	initialize: function ()
	{
	},

	/**
	* Method: getTemplatedContent
	* Method to get data <String> formatted according to a template.
	* HTML comments in the template are replaced by parameters with the same name
	*
	* Parameters:
	* parameters - <Object> name-value collection of parameters
	* template - <String>
	*/
	getTemplatedContent: function (parameters, template)
	{
		var resultContent = template;
		if (parameters) {
			for (var name in parameters) {
				var r = new RegExp("<!--#" + name + "#-->", 'gi');
				var v = parameters[name];
				resultContent = resultContent.replace(r, v);
			}
		}
		return resultContent;
	},

	/**
	* Method: ShowLayers
	* Method to set layer visibility for layers in list
	* Parameters:
	* map - <VisStedet.Map>
	* list_of_layers - <Array> List of layer names
	*/
	ShowLayers: function (map, list_of_layers)
	{
		var layers = map.layers;

		// Find Active baselayer to be active and deselect all layers 
		for (var j = 0; j < layers.length; j++) {
			if (layers[j].isBaseLayer && layers[j].visibility) {
				list_of_layers.push(layers[j].name);
			}
			layers[j].setVisibility(false);
		}

		// Set active layers and basemap
		try {
			for (var i = 0; i < list_of_layers.length; i++) {
				temp_layer = map.getLayersByName(list_of_layers[i])[0];
				temp_layer.setVisibility(true);
				if (temp_layer.isBaseLayer) {
					map.setBaseLayer(temp_layer);
				}
			}
		} catch (e) {
		}
	},

	/*
	* Mehtod: disableStandardLayers
	*
	*/
	disableStandardLayers: function (map)
	{
		var layers = map.layers;
		for (var j = 0; j < layers.length; j++) {
			if (layers[j].CLASS_NAME == "OpenLayers.Layer.WFS" || layers[j].CLASS_NAME == "OpenLayers.Layer.Markers") {
				layers[j].setVisibility(false);
			}
		}
	},

	/*
	* Method: activateLayers
	*
	*/
	activateLayers: function (current_map, listOfLayers)
	{
		for (var i = 0; i < listOfLayers.length; i++) {
			temp_layer = current_map.getLayersByName(listOfLayers[i]);
			if (temp_layer) {
				temp_layer[0].setVisibility(true);
			}
		}
	},

	/*
	* Method: deactivateLayers
	*
	*/
	deactivateLayers: function (listOfLayers)
	{
		for (var i = 0; i < listOfLayers.length; i++) {
			temp_layer = map.getLayersByName(listOfLayers[i]);
			if (temp_layer) {
				temp_layer[0].setVisibility(false);
			}
		}
	},

	/**
	* Method: Setmap
	* Method to set zoomlevel and center coordinate
	*
	* Parameters:
	* map - <VisStedet.Map>
	* zoomlevel - <Number>
	* panToEasting - <Number>
	* panToNorthing - <Number>
	*/
	Setmap: function (map, zoomlevel, panToEasting, panToNorthing)
	{
		var lonlat = new OpenLayers.LonLat(panToEasting, panToNorthing);

		if (viskort.applicationModeGlobal) {
			lonlat = viskort.convertLonLat(lonlat, "EPSG:25832", "EPSG:102113");
		}

		map.setCenter(lonlat, zoomlevel);
	},

	/**
	* Method: SetPrintmap
	* Method to set map.
	* 
	*/
	SetPrintmap: function (map, zoomlevel, panToEasting, panToNorthing)
	{
		map.setCenter(new OpenLayers.LonLat(panToEasting, panToNorthing), zoomlevel);
	},

	/**
	* Method: ResetPrintInfo
	* Method to reset printinfo
	*/
	ResetPrintInfo: function ()
	{
		var print_Info = document.getElementById("print_information");
		if (print_Info) {
			print_Info.innerHTML = "";
		}
		var print_detail_info = document.getElementById("print_detailed_information");
		if (print_detail_info) {
			print_detail_info.innerHTML = "";
		}
		var print_coordinat = document.getElementById("print_marker_coordinats");
		if (print_coordinat) {
			print_coordinat.innerHTML = "";
		}
		var print_coordinat_div = document.getElementById("print_find_nearest");
		if (print_coordinat_div) {
			print_coordinat_div.innerHTML = "";
		}
		var print_detail_infobox = document.getElementById("print_detailed_infobox");
		if (print_detail_infobox) {
			print_detail_infobox.innerHTML = "";
		}
		var printinfo_icon = document.getElementById("print_info_icon");
		if (printinfo_icon) {
			printinfo_icon.innerHTML = "";
		}
	},

	/**
	* Method: ResetDetailPrintInfo  
	* Reset's detailinfo for print
	*/
	ResetDetailPrintInfo: function ()
	{
		var print_detail_info = document.getElementById("print_detailed_information");
		if (print_detail_info) {
			print_detail_info.innerHTML = "";
		}
	},

	/**
	* Method: ResetPrintDetailInfobox
	* Reset's info for map info box
	*/
	ResetPrintDetailInfobox: function ()
	{
		var print_detail_infobox = document.getElementById("print_detailed_infobox");
		if (print_detail_infobox) {
			print_detail_infobox.innerHTML = "";
		}
	},

	/**
	* Method: ResetPrintFindnearestCoordinats
	* Reset list with coordinat to findnearest markers
	*/
	ResetPrintFindnearestCoordinats: function ()
	{
		var print_coordinat_div = document.getElementById("print_find_nearest");
		if (print_coordinat_div) {
			print_coordinat_div.innerHTML = "";
		}
	},

	/**
	* Method: SetPrintInfo
	* Method to set print info
	*
	* Parameters:
	* info - <String>
	*/
	SetPrintInfo: function (info)
	{
		var print_info = document.getElementById("print_information");
		if (print_info) {
			print_info.innerHTML = info;
		}
	},

	/**
	* Method: SetPrintDetailInfo
	* Method to set print detailinfo
	*
	* Parameters:
	* info - <String>
	*/
	SetPrintDetailInfo: function (info)
	{
		var print_detail_info = document.getElementById("print_detailed_information");
		if (print_detail_info) {
			print_detail_info.innerHTML = info;
		}
	},

	/**
	* Method: AddToPrintdetail_infobox
	*
	* Parameters:
	* info - <String>
	*/
	AddToPrintdetail_infobox: function (info)
	{
		var print_detail_infobox = document.getElementById("print_detailed_infobox");
		if (print_detail_infobox) {
			print_detail_infobox.innerHTML += info;
		}
	},

	checkzoomLevel: function (zoomlevel)
	{
		if (zoomlevel < viskort.zoomlevels) {
			return zoomlevel;
		} else {
			return viskort.zoomlevels - 1;
		}
	},

	CLASS_NAME: "VisKort.Utils"
});


