///////////////////////////////////////////////////////////
// Global variables
var PI = 3.141592653589;

var m_unit_icon = null;
var finish_icon = null;
var m_msg_icons = new Array();
var m_msg_icons_2 = new Array();

////////////////////////////////////////////////////////////
// Functions

///safe function to hide an element with a specified id
function hidediv(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

///safe function to show an element with a specified id
function showdiv(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function static_objects(gmap) {

//Старт

//   start_coord = new GLatLng('59.952765','30.320656');

//   var start_ops = new Object();
//   start_marker = new GIcon();
//	start_marker.image = "http://91.122.137.160:8001/images/start_movement.png";
//	start_marker.iconSize = new GSize(28, 32);
//	start_marker.iconAnchor = new GPoint(8, 20);
//	start_marker.infoWindowAnchor = new GPoint(8, 8);
//	start_ops.icon = start_marker;
//   start_ops.title = 'Старт';
//   start_ops.clickable = true;
//   start_ops.draggable = false;

//   start_marker = new GMarker(start_coord,start_ops);

//   gmap.addOverlay(start_marker);


//Финиш


//   finish_coord = new GLatLng('59.949913','30.308994');

//   var finish_ops = new Object();
//   finish_marker = new GIcon();
//   finish_marker.image = "http://91.122.137.160:8001/images/stop_movement.png";
//   finish_marker.iconSize = new GSize(28, 32);
//   finish_marker.iconAnchor = new GPoint(8, 20);
//   finish_marker.infoWindowAnchor = new GPoint(8, 8);
//   finish_ops.icon = finish_marker;
//   finish_ops.title = 'Финиш';
//   finish_ops.clickable = true;
//   finish_ops.draggable = false;
//   finish_marker = new GMarker(finish_coord,finish_ops);

//   gmap.addOverlay(finish_marker);


////

//Буйки1


//   bui1_coord = new GLatLng('59.947038','30.307159');
   buis1_coord = new GLatLng('59.949751','30.309423');
   var buis1_ops = new Object();
   buis1_marker = new GIcon();
   buis1_marker.image = "http://91.122.137.160:8001/images/marker6.png";
   buis1_marker.iconSize = new GSize(9, 9);
   buis1_marker.iconAnchor = new GPoint(8, 20);
   buis1_marker.infoWindowAnchor = new GPoint(8, 8);
   buis1_ops.icon = buis1_marker;
   buis1_ops.title = 'Буй';
   buis1_ops.clickable = true;
   buis1_ops.draggable = false;
   buis1_marker = new GMarker(buis1_coord,buis1_ops);

   gmap.addOverlay(buis1_marker);


////

//Буй1


//   bui1_coord = new GLatLng('59.947038','30.307159');
   bui1_coord = new GLatLng('59.947097','30.307159');
   var bui1_ops = new Object();
   bui1_marker = new GIcon();
   bui1_marker.image = "http://91.122.137.160:8001/images/marker6.png";
   bui1_marker.iconSize = new GSize(16, 16);
   bui1_marker.iconAnchor = new GPoint(8, 20);
   bui1_marker.infoWindowAnchor = new GPoint(8, 8);
   bui1_ops.icon = bui1_marker;
   bui1_ops.title = 'Буй';
   bui1_ops.clickable = true;
   bui1_ops.draggable = false;
   bui1_marker = new GMarker(bui1_coord,bui1_ops);

   gmap.addOverlay(bui1_marker);


////

//Буй2


   bui2_coord = new GLatLng('59.947676','30.31688');

   var bui2_ops = new Object();
   bui2_marker = new GIcon();
   bui2_marker.image = "http://91.122.137.160:8001/images/marker6.png";
   bui2_marker.iconSize = new GSize(16, 16);
   bui2_marker.iconAnchor = new GPoint(8, 20);
   bui2_marker.infoWindowAnchor = new GPoint(8, 8);
   bui2_ops.icon = bui2_marker;
   bui2_ops.title = 'Буй';
   bui2_ops.clickable = true;
   bui2_ops.draggable = false;
   bui2_marker = new GMarker(bui2_coord,bui2_ops);

   gmap.addOverlay(bui2_marker);

//Буй3


   bui3_coord = new GLatLng('59.950046','30.32300');

   var bui3_ops = new Object();
   bui3_marker = new GIcon();
   bui3_marker.image = "http://91.122.137.160:8001/images/marker6.png";
   bui3_marker.iconSize = new GSize(16, 16);
   bui3_marker.iconAnchor = new GPoint(8, 20);
   bui3_marker.infoWindowAnchor = new GPoint(8, 8);
   bui3_ops.icon = bui3_marker;
   bui3_ops.title = 'Буй';
   bui3_ops.clickable = true;
   bui3_ops.draggable = false;
   bui3_marker = new GMarker(bui3_coord,bui3_ops);

   gmap.addOverlay(bui3_marker);


////


}

/// Initialization routine
function wialon_init() {
	// create unit icon
	m_unit_icon = new GIcon();
	//m_unit_icon.image = "images/unit.png";
	//m_unit_icon.image = "images/transp_icon.gif";
	m_unit_icon.image = "images/unit_circle.png";
	//m_unit_icon.iconSize = new GSize(32, 32);
	//m_unit_icon.iconAnchor = new GPoint(16, 16);
	//m_unit_icon.infoWindowAnchor = new GPoint(16, 16);
	m_unit_icon.iconSize = new GSize(16, 16);
	m_unit_icon.iconAnchor = new GPoint(8, 8);
	m_unit_icon.infoWindowAnchor = new GPoint(8, 8);

	// create message icons
	for (i = 0; i < 360; i += 30) {
		var icon = new GIcon();
		icon.image = "images/pt_" + i + ".gif";
		icon.iconSize = new GSize(16, 16);
		icon.iconAnchor = new GPoint(8, 8);
		icon.infoWindowAnchor = new GPoint(8, 8);
		m_msg_icons.push(icon);
	}


	// create message icons 2
	//site-project code
	for (i = 0; i < 360; i += 30) {
		var icon = new GIcon();
		icon.image = "images/pt_" + i + ".png";
		icon.iconSize = new GSize(16, 16);
		icon.iconAnchor = new GPoint(8, 8);
		icon.infoWindowAnchor = new GPoint(8, 8);
		m_msg_icons_2.push(icon);
	}
	//end site-project code
}



// Creates a marker for message
function create_msg_marker(unit_name, msg, clickable) {
	if ((msg.lat == 0 && msg.lon == 0) || msg.time == 0)
		return null;
	// create marker
	var opts = new Object();
	// determine icon
	var icon = m_msg_icons[0];
	for (var x = 0; x < 360; x += 30) {
		if (Math.abs(msg.course - x) <= 15) {
			var icon = m_msg_icons[x/30];
			break;
		}
	}
	opts.icon = icon;
	var loc = "";
	if (msg.location != "")
		loc = msg.location + ", ";
	var name = "";
	if (clickable && unit_name != "")
		name = unit_name + ", ";
	opts.title = name + format_time(msg.time) + ", " + loc + msg.speed + get_text("km/h");
	if (clickable)
		opts.clickable = true;
	else
		opts.clickable = false;
	opts.draggable = false;
	var marker = new GMarker(new GLatLng(msg.lat, msg.lon), opts);
	if (clickable)
		GEvent.addListener(marker, "click", function on_marker_click() { marker.openInfoWindowHtml(get_info_text(unit_name, msg, "")); });
	return marker;
}

/// Calculate distance in meters between (lat1, lon1) and (lat2, lon2)
function calc_distance(lat1, lon1, lat2, lon2)
{
	return Math.sqrt((lat2 - lat1)*(lat2 - lat1) + (lon2 - lon1)*(lon2 - lon1)) * 111000.0;
}

/// Get info text for message
function get_info_text(unit_name, msg, descr) {
	var text = get_text("Unit: ") + unit_name + "<br>";
	text += get_text("Time: ") + format_time(msg.time) + " (" + msg.header + ")<br>"
	if (msg.location != "")
		text += get_text("Location: ") + msg.location + "<br>";
	if (msg.params != "")
		text += get_text("Parameters: ") + msg.params + "<br>";
		text += get_text("Speed: ") + msg.speed + get_text("km/h");
	if (descr != "") {
		text += "<br><br>";
		text += descr;
	}
	return text;
};

///////////////////////////////////////////////////////////
// AJAX related

/// Perform XML HTTP request to server
function perform_request(script, param, func) {
	if (!self.GXmlHttp)
		return false;
	var request = GXmlHttp.create();
	if (request == null)
		return false;
	var done = false;
	try {
		request.open("POST", "scripts/" + script + ".html", true);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Connection", "close");

		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				done = true;
				func(request.responseText ? request.responseText : "ERROR", request.status ? request.status : 0);
				request = null;
			}
		};
		if (!window.ActiveXObject) {
			request.onerror = function() {
				done = true;
				func("ERROR", 0);
				request = null;
			};
		}
		request.send(param);
	} catch (e) {
//		alert("ERROR for " + script + ": " + e.description);
		if (!done)
			func("ERROR", 0);
		request = null;
	}
	return true;
}

/// Perform XML HTTP request to server microsoft(no google)
function perform_request_microsoft(script, param, func) {

	var request;
	if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
        // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }

	if ( !request )
		return false;

	var done = false;
	try {
		request.open("POST", "scripts/" + script + ".html", true);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Connection", "close");

		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				done = true;
				func(request.responseText ? request.responseText : "ERROR", request.status ? request.status : 0);
				request = null;
			}
		};
		if (!window.ActiveXObject) {
			request.onerror = function() {
				done = true;
				func("ERROR", 0);
				request = null;
			};
		}
		request.send(param);
	} catch (e) {
		if (!done)
			func("ERROR", 0);
		request = null;
	}
	return true;
}

/// Parse XML string
function parse_xml(xml) {
	try {
		// code for IE
		var xmlDoc = null;
		if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.loadXML(xml);
		}
		// code for Mozilla, etc.
		else if (document.implementation &&
			document.implementation.createDocument) {
			// parse the string to a new doc
			return (new DOMParser()).parseFromString(xml, "text/xml");
		} else {
			alert('Your browser cannot parse XML documents.');
		}
		return xmlDoc;
	} catch (e) {
		return null;
	}
	return GXml.parse(xml);
}

/// Store client GUI option on server
function store_cl_option(opt_name, opt_value) {
	perform_request("store_option", "option=" + opt_name + "&value=" + opt_value, on_fake_result);
}

/// Fake function executed when AJAX request completed
function on_fake_result(result_text, result_code) {
}

/// Mouse click for block visibility switching
function switch_display(img_id, block_id, opt_name) {
	var img = document.getElementById(img_id);
	var block = document.getElementById(block_id);
	if (img != null && block != null) {
		if (block.style.display == "") {
			block.style.display = "none";
			img.src = "images/block_show.gif";
			store_cl_option(opt_name, "hide");
		} else {
			block.style.display = "";
			img.src = "images/block_hide.gif";
			store_cl_option(opt_name, "show");
		}
	}
}

/// Push event
function push_event(txt) {
	var t = new Date();
	var tbl = document.getElementById("ajax_events_table");
	if (tbl == null)
		return;
	var row = tbl.insertRow(0);
	var col = document.createElement("td");
	var txt_node = document.createElement("span");
	txt_node.style.fontSize = "xx-small";
	txt_node.innerHTML = t.toLocaleTimeString() + ": " + txt;

	col.appendChild(txt_node);
	row.appendChild(col);
	while (tbl.rows.length > 10)
		tbl.deleteRow(tbl.rows.length - 1);
}

////////////////////////////////////////////////////////////
// Wialon Web object classes

/**
	W_Unit - object that represents unit.
*/
function W_Unit(node) {
	// set attributes
	this.id = parseInt(node.getAttribute("id"));
	this.access_level = parseInt(node.getAttribute("access_level"));
	this.mileage = parseInt(node.getAttribute("mileage"));
	var cmds = node.getAttribute("commands");
	if (cmds != undefined)
		this.commands = cmds.split(",");
	else
		this.commands = new Array();
	var links = node.getAttribute("links");
	if (links != undefined)
		this.links = links.split(",");
	else
		this.links = new Array();

	this.pos_marker = null;
	this.pos_direction = null;
	this.label_marker = null;
	this.unit_icon = null;

	this.image_path = node.getAttribute("image_path");
	this.last_msg = new W_UnitMsg("");
	this.position = new W_UnitMsg("");
	this.m_trace_markers = new Array();
	this.ignition = parseInt(node.getAttribute("ign"));

		/**Create/Update marker for unit position*/
	this.create_pos_marker = function () {
		this.pos_marker = null;
		this.label_marker = null;
		if (!this.position.lat && !this.position.lon)
			return;
		// create marker
		var txt = get_info_text(this.name, this.position, this.description);
		txt += "<br><center><a href='#' onclick='select_unit_in_list(" + this.id + ");'>" + get_text("Select unit in list") + "</a></center>";
		var cmd_id = m_next_unit_cmd_id;
		m_next_unit_cmd_id += 1;
		txt += "<table>" + get_unit_cmds_html(cmd_id, this.id) + "</table>";
		//txt += this.last_msg.speed;
		var opts = new Object();

		if (this.image_path != "") {
			// check do we need to create icon
			if (this.unit_icon == null) {
				// create unit icon
				this.unit_icon = new GIcon();
				this.unit_icon.image = this.get_image_url();
				this.unit_icon.iconSize = new GSize(32, 32);
				this.unit_icon.iconAnchor = new GPoint(8, 20);
				this.unit_icon.infoWindowAnchor = new GPoint(8, 8);

   		}
			opts.icon = this.unit_icon;
		} else
			opts.icon = m_unit_icon;
		var loc = "";
		if (this.position.location != "")
			loc = this.position.location + ", ";
		opts.title = this.name + ", " + format_time(this.position.time) + ", " + loc + this.position.speed + get_text("km/h");
		opts.clickable = true;
		opts.draggable = false;

		var coord = new GLatLng(this.position.lat, this.position.lon);

		this.pos_marker = new GMarker(coord, opts);

//site-project code
		var opts2 = new Object();
		direction_icon = new GIcon();
      direction_icon.iconSize = new GSize(32, 32);
      direction_icon.iconAnchor = new GPoint(8, 20);
      for (var x = 0; x < 360; x += 30) {
         if (Math.abs(this.position.course - x) <= 15) {
      	  //direction_icon = m_msg_icons_2[x/30];
      	  direction_icon = m_msg_icons[x/30];
            break;
      	}
      }
      opts2.icon = direction_icon;
		opts2.clickable = true;
		opts2.draggable = false;
      this.pos_direction = new GMarker(coord, opts2);
//end site-project code

		var marker = this.pos_marker;
		var unit_x = this;
		GEvent.addListener(marker, "click", function on_marker_click() { marker.openInfoWindowHtml(txt); load_unit_commands(unit_x, cmd_id); update_unit_exec_cmd_btn(unit_x, cmd_id);});

		GEvent.addListener(this.pos_direction, "click", function on_marker_click() { marker.openInfoWindowHtml(txt); load_unit_commands(unit_x, cmd_id); update_unit_exec_cmd_btn(unit_x, cmd_id);});


		var label_class = "unit_label_stay";
		if (this.ignition)
			label_class = "unit_label_move";
		var larr = this.name.split(" ");
		var label = "";
		if(m_show_map_on_unit_name) {
			for (var i = 0; i < larr.length; i++) {
				if (label != "")
					label += "&nbsp;" + larr[i];
				else
					label += larr[0];
			}
			label += "&nbsp;(" + this.last_msg.speed + get_text("km/h") + ")";
		}

		var dx = label.length*(-2);

		//ll = "<div id='label_"+this.id+"'>"+label+"</div>";

		this.label_marker = new ELabel(coord, label, label_class, new GSize(dx, 26));
		//var label_id = 'label_'+this.id;
		//GEvent.addListener(this.label_marker, "click", function on_marker_click() { this.label_marker.openInfoWindowHtml(txt); load_unit_commands(unit_x, cmd_id); update_unit_exec_cmd_btn(unit_x, cmd_id);});
	};
	/**Show/Hide unit from gmap*/
	this.show_marker = function(gmap, to_show) {
		if (gmap == null)
			return;
		if (to_show) {
			if (m_trace_unit_msgs) {
				// show all trace markers
				for (var i = 0; i < this.m_trace_markers.length; i++)
					gmap.addOverlay(this.m_trace_markers[i]);
			}
			if (this.pos_marker != null)
				gmap.addOverlay(this.pos_marker);

         //site-project code
         if (this.pos_direction != null)
				gmap.addOverlay(this.pos_direction);
         //site-project code end

			if (this.label_marker != null) {
				gmap.addOverlay(this.label_marker)
			}
		} else {
			if (this.pos_marker != null)
				gmap.removeOverlay(this.pos_marker);
         //site-project code
         if (this.pos_direction != null)
				gmap.removeOverlay(this.pos_direction);
         //site-project code end
			if (this.label_marker != null)
				gmap.removeOverlay(this.label_marker);
			// hide all trace markers
			for (var i = 0; i < this.m_trace_markers.length; i++)
				gmap.removeOverlay(this.m_trace_markers[i]);
		}
	};
	/**Update unit position*/
	this.set_position = function(pos, gmap) {
		if (gmap != null)
			this.show_marker(gmap, false);
		// add current position to history for tracing purposes
		var arr = new Array();
		/*if (this.position != null) {
			var m = create_msg_marker(this.name, this.position, false);
			if (m != null)
				arr.push(m);
			for (var i = 0; arr.length < 5 && i < this.m_trace_markers.length; i++)
				arr.push(this.m_trace_markers[i]);
			this.m_trace_markers = arr;
		}*/
		if (pos != null)
			this.position = new W_UnitMsg(pos);
		this.create_pos_marker();

		var unit_sel = document.getElementById("sel_unit");
		if (unit_sel != null) {
			if (unit_sel.selectedIndex != -1) {
				var sel_id = unit_sel.options[unit_sel.selectedIndex].value;
				if (m_show_only_sel_object == true && sel_id != this.id)
					return;
			}
		}

		if (gmap != null)
			this.show_marker(gmap, true);
	};
	/**Load properties from node*/
	this.load_properties = function(node) {
		this.name = node.getAttribute("name");
		this.description = node.getAttribute("description");
		this.hw_type = node.getAttribute("hw_type");
		this.unique_id = node.getAttribute("unique_id");
		this.inet_link = node.getAttribute("inet_link");
		this.msg_filtering = node.getAttribute("msg_filter");
		this.image_path = node.getAttribute("image_path");
		this.mileage = parseInt(node.getAttribute("mileage"));
		this.ignition = parseInt(node.getAttribute("ign"));
	}
	/**Load last message from node*/
	this.set_last_msg = function(node, gmap) {
		var msg = node.getAttribute("last_msg");
		if (msg == null)
			return;
		var mil = node.getAttribute("mileage");
		if (mil != null && mil != "")
			this.mileage = parseInt(mil);
		this.last_msg = new W_UnitMsg(msg);
		if (this.last_msg.lat != 0 || this.last_msg.lon != 0)
			this.set_position(msg, gmap);
		else if (node.getAttribute("pos") != null)
			this.set_position(node.getAttribute("pos"), gmap);
	};
	/**Get image URL*/
	this.get_image_url = function() {
		if (this.image_path == "")
			return "/images/unit.png";
		return "/images/units/" + this.image_path;
	}
	/**Are we moving now*/
	this.is_moving = function() {
		return this.position != null && this.ignition == 1;
	}
	// continue initialization
	this.load_properties(node);
	this.set_last_msg(node, null);
	this.create_pos_marker();
}

/**
	W_Unit - object that represents units group.
*/
function W_UnitsGroup(name, id, access_level, units) {
	// set attributes
	this.id = parseInt(id);
	this.name = name;
	this.access_level = parseInt(access_level);
	this.units = units.split(",");
}

/**
	W_UnitMsg - object represents message from unit.
*/
function W_UnitMsg(line) {
	var arr = line.split(",");
	if (arr.length >= 7) {
		this.header = arr[0];
		this.time = parseInt(arr[1]);
		this.lon = parseFloat(arr[2]);
		this.lat = parseFloat(arr[3]);
		this.speed = parseInt(arr[4]);
		this.course = parseInt(arr[5]);
		this.params = arr[6];
		if (arr.length >= 8) {
			this.location = arr[7];
			for (var i = 8; i < arr.length; i++)
				this.location += ", " + arr[i];
		} else
			this.location = "";
	} else {
		this.header = "";
		this.time = 0;
		this.lon = 0;
		this.lat = 0;
		this.speed = 0;
		this.course = 0;
		this.location = "";
		this.params = "";
	}
}

/////////////////////////////////////////////////
// Labels support
// Version 0.2      the .copy() parameters were wrong
// version 1.0      added .show() .hide() .setContents() .setPoint() .setOpacity() .overlap
// version 1.1      Works with GMarkerManager in v2.67, v2.68, v2.69, v2.70 and v2.71
// version 1.2      Works with GMarkerManager in v2.72, v2.73, v2.74 and v2.75

function ELabel(point, html, classname, pixelOffset, percentOpacity, overlap) {
	// Mandatory parameters
	this.point = point;
	this.html = html;

	// Optional parameters
	this.classname = classname||"";
	this.pixelOffset = pixelOffset||new GSize(0,0);
	if (percentOpacity) {
		if(percentOpacity<0){percentOpacity=0;}
		if(percentOpacity>100){percentOpacity=100;}
	}
	this.percentOpacity = percentOpacity;
	this.overlap=overlap||false;
}

function load_labels() {
	ELabel.prototype = new GOverlay();

	ELabel.prototype.initialize = function(map) {
		var div = document.createElement("div");
		div.style.position = "absolute";
		div.className = "label_outlet";
		div.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
		map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div);
		this.map_ = map;
		this.div_ = div;
		if (this.percentOpacity) {
			if(typeof(div.style.filter)=='string'){div.style.filter='alpha(opacity:'+this.percentOpacity+')';}
			if(typeof(div.style.KHTMLOpacity)=='string'){div.style.KHTMLOpacity=this.percentOpacity/100;}
			if(typeof(div.style.MozOpacity)=='string'){div.style.MozOpacity=this.percentOpacity/100;}
			if(typeof(div.style.opacity)=='string'){div.style.opacity=this.percentOpacity/100;}
		}
		if (this.overlap) {
			var z = GOverlay.getZIndex(this.point.lat());
			this.div_.style.zIndex = z;
		}
	}

	ELabel.prototype.remove = function() {
		this.div_.parentNode.removeChild(this.div_);
	}

	ELabel.prototype.copy = function() {
		return new ELabel(this.point, this.html, this.classname, this.pixelOffset, this.percentOpacity, this.overlap);
	}

	ELabel.prototype.redraw = function(force) {
		var p = this.map_.fromLatLngToDivPixel(this.point);
		var h = parseInt(this.div_.clientHeight);
		this.div_.style.left = (p.x + this.pixelOffset.width) + "px";
		this.div_.style.top = (p.y +this.pixelOffset.height - h) + "px";
	}

	ELabel.prototype.show = function() {
		this.div_.style.display="";
	}

	ELabel.prototype.hide = function() {
		this.div_.style.display="none";
	}

	ELabel.prototype.setContents = function(html) {
		this.html = html;
		this.div_.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
		this.redraw(true);
	}

	ELabel.prototype.setPoint = function(point) {
		this.point = point;
		if (this.overlap) {
			var z = GOverlay.getZIndex(this.point.lat());
			this.div_.style.zIndex = z;
		}
		this.redraw(true);
	}

	ELabel.prototype.setOpacity = function(percentOpacity) {
		if (percentOpacity) {
			if(percentOpacity<0){percentOpacity=0;}
			if(percentOpacity>100){percentOpacity=100;}
		}
		this.percentOpacity = percentOpacity;
		if (this.percentOpacity) {
			if(typeof(this.div_.style.filter)=='string'){this.div_.style.filter='alpha(opacity:'+this.percentOpacity+')';}
			if(typeof(this.div_.style.KHTMLOpacity)=='string'){this.div_.style.KHTMLOpacity=this.percentOpacity/100;}
			if(typeof(this.div_.style.MozOpacity)=='string'){this.div_.style.MozOpacity=this.percentOpacity/100;}
			if(typeof(this.div_.style.opacity)=='string'){this.div_.style.opacity=this.percentOpacity/100;}
		}
	}

	ELabel.prototype.getPoint = function() {
		return this.point;
	}
	ELabel.prototype.U = function() {
		return this.point;
	}
	ELabel.prototype.V = function() {
		return this.point;
	}
	ELabel.prototype.W = function() {
		return this.point;
	}
	ELabel.prototype.X = function() {
		return this.point;
	}
	ELabel.prototype.Y = function() {
		return this.point;
	}
	ELabel.prototype.Z = function() {
		return this.point;
	}
}

/// Format time absolute value
function format_time_b3(abs_val, allow_short, allow_short_time) {
	if (!abs_val)
		return "";
	if (typeof allow_short == "undefined")
		allow_short = true;
	if (typeof allow_short_time == "undefined")
		allow_short_time = false;

	var d = new Date(abs_val * 1000);
	var cd = new Date();
	if (allow_short_time)
		var res = check_time(d.getHours()) + ":" + check_time(d.getMinutes()) + ":" + check_time(d.getSeconds());
	else
		var res = check_time(d.getHours()) + ":" + check_time(d.getMinutes());
	if (allow_short && cd.getDate() == d.getDate() && cd.getMonth() == d.getMonth() && cd.getYear() == d.getYear())
		return res;
	// append date
	return d.getFullYear() + "-" + check_time(d.getMonth() + 1) + "-" + check_time(d.getDate()) + " " + res;
}

/// Format date absolute value
function format_date(abs_val) {
	if (!abs_val)
		return "";
	var d = new Date(abs_val * 1000);
	return d.getFullYear() + "-" + check_time(d.getMonth() + 1) + "-" + check_time(d.getDate());
}
// show server time for page monitoring
function load_server_time() {
	var ct = new Date();
 	var result_text = check_time(ct.getHours()) + ":" + check_time(ct.getMinutes()) + ":" + check_time(ct.getSeconds());
	var element = document.getElementById("span_server_time");
	if (element) {
		element.innerHTML = result_text;
		window.setTimeout("load_server_time();", 10000);
	}
}

var getDocumentWidth = function()
{
   return (window.innerWidth)?window.innerWidth:((document.all)?document.documentElement.offsetWidth:null);
};

var getDocumentHeight = function()
{
   return (window.innerHeight)?window.innerHeight:((document.all)?document.documentElement.offsetHeight:null);
};

// function delete window for display
function hide_dlg_alarm(id) {
	var div = document.getElementById(id);
	if (!div)
		return;
	if (div.style.display != "none")
		div.style.display = "none";
}

// function create window & add window for display
function show_dlg_alarm(id, html) {

	var left = parseInt(getDocumentWidth()/2);
	var top = parseInt(getDocumentHeight()/2);

	var elem = document.getElementById(id);
	if (elem != null) {
		var div = document.getElementById(id);
		if (!div)
			return;
		if (div.style.display == "none")
			div.style.display = "block";
		div.style.left = left + "px";
		div.style.top = top + "px";
		div.innerHTML = html;

	} else {
		var div = document.createElement("div");
		if (!div)
			return;
		div.id = id;
		div.style.position = "absolute";
		div.style.display = "block";
		div.style.left = left + "px";
		div.style.top = top + "px";
		div.style.fontSize = "12px"
		div.style.padding = "15px";
		div.style.zIndex = 100;
		div.style.opacity = 1;
		div.style.color = "#000000";
		div.style.border = "1px solid #3f5854";
		div.style.background = "#B2BCBB";
		div.innerHTML = html;
		document.body.appendChild(div);
	}
}

// function create window & add window for display - SP edition
function show_dlg_alarm_2(id, html) {

	var left = parseInt(getDocumentWidth()/2);
	//var top = parseInt(getDocumentHeight()/2);
	var top = 225;

	var elem = document.getElementById(id);
	if (elem != null) {
		var div = document.getElementById(id);
		if (!div)
			return;
		if (div.style.display == "none")
			div.style.display = "block";
		div.style.left = left + "px";
		div.style.top = top + "px";
		div.innerHTML = html;

	} else {
		var div = document.createElement("div");
		if (!div)
			return;
		div.id = id;
		div.style.position = "absolute";
		div.style.display = "block";
		div.style.left = left + "px";
		div.style.top = top + "px";
		div.style.fontSize = "11px";
		div.style.padding = "15px";
		div.style.fontFamily = "Tahoma";
		div.style.zIndex = 100;
		div.style.opacity = 1;
		div.style.color = "#000000";
		div.style.border = "1px solid #B2BCBB";
		div.style.background = "#ffffff";
		div.innerHTML = html;
		document.body.appendChild(div);
	}
}