var all_canvas = document.getElementsByTagName("canvas");
var svg = document.getElementsByTagName("svg");
for (var i=0; i < all_canvas.length; i++) {
all_canvas[i].addEventListener("contextmenu", menu, true);
all_canvas[i].addEventListener("click", show_canvas, true);
}
for (var i=0; i < svg.length; i++) {
svg[i].addEventListener("contextmenu", menu, true);
}
document.addEventListener("contextmenu", menu, true);
function menu(e) {
e.preventDefault();
if(this.tagName=="CANVAS") {
canvas = this;
text = '<h2>Einzelansicht</h2><img src="' + this.toDataURL("image/png") + '" class="img_popup" style="max-width:90%;"><br><span align="right" style="color:grey;float:right;position:relative;top:-10px;left:-20px;"><a id="share" href="mailto:?cc=&bcc=&subject=Geteilter Opolytics-Inhalt&body=Sie finden den geteilten Inhalt unter <?php echo $_SERVER['SERVER_NAME']."/opolytics/shared/?data=".intval(max(preg_replace('/^$/', '0', preg_replace("/expires.txt|index.php|.png/","",scandir("shared")))) + 1) ?>.png">✉</a> <a href="#" id="embed"><img src="img/embed.jpg" height="13"></a> <a href="#" id="print"><img src="img/print.png" height="13"></a></span><form method="post" accept-charset="utf-8" id="exportform" action="view.php?action=share" style="display:none;"><input id="sharecanvas" name="upload" type="hidden"/></form><br>';
if(screen.width < 640) {
document.getElementById("contextmenu").innerHTML = '<a href="javascript:popup(text,parseFloat(screen.availWidth / 2 - 600),150,1200);sharelisteners();">Einzelansicht</a><hr><a href="javascript:print_canvas()">Diagramm drucken</a><hr><a href="error.php" id="download_link_png" download>Exportieren (PNG)</a><hr><a href="javascript:about_opolytics();">Über Opolytics</a>';
} else {
document.getElementById("contextmenu").innerHTML = '<a href="javascript:popup(text,10,screen.availHeight / 2 - 125,screen.availWidth - 20,220);sharelisteners();">Einzelansicht</a><hr><a href="javascript:print_canvas()">Diagramm drucken</a><hr><a href="error.php" id="download_link_png" download>Exportieren (PNG)</a><hr><a href="javascript:about_opolytics();">Über Opolytics</a>';
}
document.getElementById("download_link_png").href = this.toDataURL("image/png");
}
//if(this.tagName=="svg") {
//popup('<img src="data:image/svg+xml;base64,' + window.btoa(this.outerHTML) + '">',200,200,200,200);
//}
else if(typeof(this.tagName)=="undefined") {
document.getElementById("contextmenu").innerHTML = '<a href="' + window.location.href + '" download>Bericht exportieren</a><hr><a href="javascript:window.print();">Bericht drucken</a><hr><a href="javascript:about_opolytics();">Über Opolytics</a>';
}
document.getElementById("contextmenu").style.display = "block";
document.getElementById("contextmenu").style.left = parseFloat(e.pageX + 5) + "px";
document.getElementById("contextmenu").style.top = e.pageY + "px";
document.addEventListener("click", hide_menu, true);
}
function hide_menu() {
document.getElementById("contextmenu").style.display = "none";
}
function about_opolytics() {
document.getElementById("contextmenu").style.display = "block";
document.getElementById("contextmenu").innerHTML = '<b>Opolytics</b><br>Opolytics ist ein Projekt, dass Tracking möglich machen soll, ohne dass dadurch die Privatsphäre der Nutzer eingeschränkt wird.';
}
function close_message() {
	document.getElementById("message_element").style.display = "none";
}
function message(message, timeout) {
document.getElementById("message_element").style.left = parseFloat(screen.width / 2 - 200) + "px";
if(navigator.onLine==false) {
document.getElementById("message_element").innerHTML = 'Sie sind offline. <a href="javascript:know_more();">Was heisst das?</a>';
document.getElementById("message_element").style.display = "block";
console.log("Your Browser is now offline");
console.error("Connection error. Network functions disabled.");
delete_message = setTimeout(function() { close_message(); }, 3000);
}
if(navigator.onLine==true) {
document.getElementById("message_element").innerHTML = "Sie sind wieder online";
document.getElementById("message_element").style.display = "block";
console.log("Your Browser is now online");
setTimeout(function() { close_message(); }, 1000);
}
if(message!="" && message!="[object Event]" && timeout!=0 && timeout!="") {
document.getElementById("message_element").innerHTML = message;
document.getElementById("message_element").style.display = "block";
console.log("Opolytics Message: " + message);
setTimeout(function() { close_message(); }, timeout);
return true;
}
if(timeout=="0" || timeout=="" || message!="") {
return false;
}
}
window.addEventListener('online',  message);
window.addEventListener('offline', message);
//document.addEventListener('error', message("Es ist ein Fehler aufgetreten", 100), false);
function know_more() {
clearTimeout(delete_message);
document.getElementById("message_element").innerHTML = '<b>Sie sind nun offline.</b><br>Das heisst Sie können keine neuen Inhalte laden. Weiterhin können Sie die nun angezeigten Informationen anzeigen lassen.<br><br><a href="javascript:message();">&times Schliessen</a>';
}
function popup(message, left, top, width, height) {
elem = document.getElementById("popup");
elem.innerHTML = '<span style="text-align:right;float:right;padding-right:10px;padding-right:10px;" align="right"><a href="javascript:close_popup();" style="font-size:20pt;color:grey;text-decoration:none;">&Cross;</a></span><span style="padding-top:60px;">' + message + '</span>';
if(screen.width > (left + width)) {
	elem.style.top = top + "px";
	elem.style.left = left + "px";
	elem.style.width = width + "px";
	elem.style.height = height + "px";
}
else {
	elem.style.top = "0px";
	elem.style.left = "0px";
	elem.style.right = "0px";
	elem.style.bottom = "0px";
	elem.style.zIndex = 100;
}
elem.style.display = "block";
}
function close_popup() {
document.getElementById("popup").style.display = "none";
}
function show_canvas() {
canvas = this;
text = '<h2>Einzelansicht</h2><img src="' + this.toDataURL("image/png") + '" class="img_popup" style="max-width:100%;"><br><span align="right" style="color:grey;float:right;position:relative;top:-10px;left:-20px;"><a id="share" href="mailto:?cc=&bcc=&subject=Geteilter Opolytics-Inhalt&body=Sie finden den geteilten Inhalt unter <?php echo $_SERVER['SERVER_NAME']."/opolytics/shared/?data=".intval(max(preg_replace('/^$/', '0', preg_replace("/expires.txt|index.php|.png/","",scandir("shared")))) + 1) ?>.png">✉</a> <a href="#" id="embed"><img src="img/embed.jpg" height="13"></a> <a href="#" id="print"><img src="img/print.png" height="13"></a></span><form method="post" accept-charset="utf-8" id="exportform" action="view.php?action=share" style="display:none;"><input id="sharecanvas" name="upload" type="hidden"/></form><br>';
popup(text,parseFloat(screen.availWidth / 2 - 600),150,1200);
setTimeout("sharelisteners()",100);
}
function sharelisteners() {
document.getElementById("share").addEventListener("click", upload, true);document.getElementById("embed").addEventListener("click", upload, true);document.getElementById("print").addEventListener("click", print_canvas, true)
document.getElementById("sharetwo").addEventListener("click", upload);
}
function upload() {
if(this.id.indexOf("two")==-1) {
document.getElementById("sharecanvas").value = canvas.toDataURL("image/png");
}
if(this.id.indexOf("two")!=-1) {
document.getElementById("sharecanvastwo").value = canvas.toDataURL("image/png");
}
if(this.id=="share" || this.id=="sharetwo") {
document.getElementById("exportform").action = "view.php?action=share&mode=share";
}
if(this.id=="embed" || this.id=="embedtwo") {
document.getElementById("exportform").action = "view.php?action=share&mode=embed";
}
document.getElementById("exportform").submit();
}
function print_canvas() {
canvas_window = window.open(canvas.toDataURL("image/png"));canvas_window.close();canvas_window.print();
}
if(document.getElementById("map")) {
document.addEventListener("load", function() {
  document.getElementById("map").contentWindow.parent = window;
});
}