/* Utility to strip the path from a pathname.
   EXAMPLE: getFilename('../../theater-studium.html')
     yields: 'theater-studium.html'
*/

function getFilename(fname)
{  
  // parameter check
  idxa=fname.lastIndexOf('/')+1;
  idxc=fname.lastIndexOf('\\')+1; //'
  idxa=(idxc>idxa) ? idxc : idxa;
  idxb=fname.length;
  if (idxa>=idxb) 
  {
    alert('Parameter fehlerhaft (getFilename() in hmt.js): '+fname+' Datei: '+location.pathname+' Useragent: '+navigator.userAgent);
    return '';
  }
  // strip path 
  return fname.substring(idxa,idxb);
}

/* Utility to extract the base of the specified file name.
   EXAMPLE: getBasename('../musik-studium-fs.html','-fs.htm')
     yields: 'musikstudium'
   
   REQUIREMENTS:
     must contain suffix
*/

function getBasename(fname,suffix)
{  
  // parameter check
  idxa=fname.lastIndexOf('/')+1;
  idxb=fname.indexOf(suffix);
  if (idxa>=idxb) 
  {
    alert('Parameter fehlerhaft (getBasename() in hmt.js): '+fname+' Suffix: '+suffix+' Datei: '+location.pathname);
    return '';
  }

  // extract base name from file name and remove dashes
  return SucheErsetze(fname.substring(idxa,idxb),'-','');
}

function completePath(p)
{
  // check if the path ends with a slash (p.e. /theater/tas/)
  if (p.length>0)
  {
	 if (p.substring(p.length-1,p.length)=='/')
    { newp=p+'index.html';}
    else
	 { newp=p;}
  }
  else {newp='index.html';}
  return newp;
}

function isAbsolute(p)
{
  if (p.length>0)
  {
    return (p.substring(0,1)=='/');
  }
  else {return false;}
}

function nav(a,b) {}

function nav2(pageid,parentid,dept) {}

function open_popup(file, popupName, options) {
	var defaultOptions= "location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,dependent=yes";
	var theOptions= options + defaultOptions;
	if (options.indexOf('width') == -1) {
		theOptions+= ",width=500";
	}
	//alert(theOptions);
	return window.open(file, popupName, theOptions);
}

function openHelp(path) {
	open_popup(path, 'hilfe', 'width=500,height=300,');
}

function open_sitemanager(file, popupName) {
	calcWindowOffsets();

	// variables for window position calculation
	var maxWidth= 360; // maximum width of the window
	var minWidth= 200; // minimum width of the window
	var width; // inner width of window
	var height; // inner height of window
	var pos_x; // left corner coordinate of window
	var pos_y; // top corner coordinate of window

	// calc width of main window. apply constraints for minimal and maximal width.
	width= Math.min(maxWidth, Math.floor(availWidth / 4.5));
	width= Math.max(minWidth, width);
	var outer_width= width + winXOfs; // outer width of window 

	height= availHeight - winYOfs; // inner height of window (not including title bar p.e.)
	
	pos_x= availWidth - outer_width; // outer coordinate
	pos_y= topOfsY;
	
	var options='';
	
	window.open(
		file, 
		popupName, 
		options + "top=" + pos_y + ",left=" + pos_x + ",width=" + 
		width + ",height=" + height + ",location=no,menubar=no,resizable=yes," +
		"scrollbars=yes,status=no,toolbar=no,dependent=yes"
	);
}

var winXOfs; // combined width of left and right stuff around inner browser area (window border) OS and browser dependent!
var winYOfs; // combined height of top and bottom stuff around inner browser area (window title bar and border) OS and browser dependent!
var availHeight;
var availWidth;
var topOfsY; // top offset (for mac os x, p.e.)
	
/** calculate window offsets */
function calcWindowOffsets() {
	if (FFOX) {
		winXOfs= 8; 
		winYOfs= 34;
	} else if (IE) {
		if (isWin && OSVersion == 'XP') {
			// win xp
			// window title bars with win xp theme applied are a good deal larger than with w2k theme
			winXOfs= 10; 
			winYOfs= 38;
		} else if (isWin && (OSVersion == '2000' || OSVersion == '98')) { 
			// win 2k or 98
			winXOfs= 12;
			winYOfs= 28;
		} else if (isWin) {
			// other windows os's
			winXOfs= 10;
			winYOfs= 38;
// FIXME: Concerning IE on Mac OS, I cannot tell MacOS9 from MacOSX right now.
//		} else if (OS == 'Mac' && OSVersion == 'X') {
//			// mac os x
//			winXOfs= 5;
//			winYOfs= 52;
		} else if (isMac) {
			// all mac os's
			winXOfs= 30;
			winYOfs= 35;
		} else {
			// other os's
			winXOfs= 10;
			winYOfs= 38;
		}
	} else if (NS6plus) {
		if (isWin && OSVersion == 'XP') {
			// win xp
			winXOfs= 12;
			winYOfs= 36;
		} else if (isWin && (OSVersion == '2000' || OSVersion == '98')) {
			// win 2k or 98
			winXOfs= 10;
			winYOfs= 30;
		} else if (isWin) {
			// other windows os's
			winXOfs= 12;
			winYOfs= 36;
		} else if (isMac && OSVersion == 'X') {
			// mac os x
			winXOfs= 4;
			winYOfs= 40;
		} else if (isMac) {
			// other mac os's
			winXOfs= 15;
			winYOfs= 32;
		} else if (isLinux) {
			// linux
			winXOfs= 10;
			winYOfs= 30;
		} else {
			// other os's
			winXOfs= 12;
			winYOfs= 36;
		}
	} else if (SAF && isMac) {
		winXOfs= 5;
		winYOfs= 27;
	} else if (KONQ) {
		winXOfs= 14;
		winYOfs= 34;	
	} else {
		// other browsers
		winXOfs=12; 
		winYOfs=38;
	}

	// note about window positioning:
	// window position coordinates in ie are absolute; i.e.
	// if the taskbar is at the left side of the screen, a window
	// positioned at x coordinate 0 occupies the same space as the taskbar.
	// Whereas in Opera and Mozilla based browsers, the coordinates are relative.
	
	// availHeight and availWidth are always correct in ie,
	// but show wrong results in mozilla based browsers under windows
	// when taskbar is non standard (more than one row of icons or positioning
	// other than at the bottom or top of the screen.

	// in mac os x:
	// window positioning in the vertical works like this:
	// coordinates are absolute beginning on the first pixel row on the screen (in the menu bar).
	// windows are automatically moved down so that they don't bite the menu bar.

	if (isMac && OSVersion == 'X') {
		topOfsY= 22;
	} else {
		topOfsY= 0;
	}
	availHeight= screen.availHeight - topOfsY;
	availWidth= screen.availWidth;
}

function condtext(pre, post, text) {
	if (text.length<=0) return '';
	return pre + text + post;
}

/*  Copyright 2004 Patrick R. Michaud (pmichaud@pobox.com)
    The function insMarkup() is part of PmWiki; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  See pmwiki.php for full details.

    This file provides Javascript functions to support WYSIWYG-style
    editing.  The concepts are borrowed from the editor used in Wikipedia,
    but the code has been rewritten from scratch to integrate better with
    PHP and PmWiki's codebase.  
    
    The code has been modified by www.hmt.edu
*/
function insMarkup(areaobj, mopen, mclose, mtext, mtextpre, mtextpost) {
  if (areaobj.setSelectionRange > '') {
    var p0 = areaobj.selectionStart;
    var p1 = areaobj.selectionEnd;
    var top = areaobj.scrollTop;
    var str = mtext;
    var cur0 = p0 + mopen.length;
    var cur1 = p0 + mopen.length + str.length;
    while (p1 > p0 && areaobj.value.substring(p1-1, p1) == ' ') p1--; 
    if (p1 > p0) {
      str = condtext(mtextpre, mtextpost, areaobj.value.substring(p0, p1));
      cur0 = p0 + mopen.length + str.length + mclose.length;
      cur1 = cur0;
    }
    areaobj.value = areaobj.value.substring(0,p0)
      + mopen + str + mclose
      + areaobj.value.substring(p1);
    areaobj.focus();
    areaobj.selectionStart = cur0;
    areaobj.selectionEnd = cur1;
    areaobj.scrollTop = top;
  } else if (document.selection) {
    var str = condtext(mtextpre, mtextpost, document.selection.createRange().text);
    if (str == '') str = mtext;
    areaobj.focus();
    document.selection.createRange().text = mopen + str + mclose;
  } else { areaobj.value += mopen + mtext + mclose; }
  return;
}

function watchit(url) {
	alert('Die Abo-Funktion wird Ihnen kostenlos von ChangeNotes.com zur Verfügung gestellt. \nBeachten Sie bitte, dass wir keine Garantie für die korrekte Funktion dieses Dienstes übernehmen.');
	alert('Bekannte Probleme: Der Service von ChangeNotes.com meldet in den Benachrichtigungsmails oft den Fehler "Page could not be downloaded. Error: name lookup time-outed". \nDie Ursache dieser Fehler wird zurzeit noch untersucht; sie liegt aber nicht bei unserem Server. Sie können diese Fehlermeldungen ignorieren.');
	watchitWin = window.open(
		'http://www.changenotes.com/addapage.php?url='+escape(document.location),
		'watchitWindow',
		'scrollbars=yes,resizable=yes,toolbar=no,directories=no,status=no,menubar=no,height=550,width=600'
	);
	watchitWin.focus();
	return false;
}

var mailcollector_window;
var bail_out;
var wait_bail_out;
var interval_handle;
var timeout_bail_out;
var new_img_src;

function hmtfw_ping_window(reference) {
	var successful= true;
	try {
		reference.hmtfw_ping();
	} 
	catch(e) {
		 successful= false;
	};
	if (successful) {
		return reference; 
	} else {
		return null;
	}
}

function hmtfw_get_mailcollector_options() {
	calcWindowOffsets();

	// variables for window position calculation
	var exactWidth= 360; // exact width of the window
	var exactHeight= 225; // exact height of the window
	var width; // inner width of window
	var height; // inner height of window
	var pos_x; // left corner coordinate of window
	var pos_y; // top corner coordinate of window

	width= exactWidth;
	var outer_width= width + winXOfs; // outer width of window 

	height= exactHeight + winYOfs; // inner height of window (not including title bar p.e.)
	var outer_height= height; // outer height of window 
	
	// placement values:
	// 0: automatic (place it where there is space)
	// 1: upper right corner
	var placement= 0; // place it where there is space  
	
	switch (placement) {
		case 0:
			var parent_pos_x;
			var parent_pos_y;
			var parent_width;
			var parent_height;
			
			if (window.screenX != null && window.outerWidth) {
				parent_pos_x= window.screenX;
				parent_width= window.outerWidth;
				parent_pos_y= window.screenY;
				parent_height= window.outerHeight;
			}
			else if (window.screenLeft) {
				if (document.documentElement) {
					parent_pos_x= window.screenLeft;
					parent_width= document.documentElement.offsetWidth;
					parent_pos_y= window.screenTop;
					parent_height= document.documentElement.offsetHeight;
				}
				else if (document.body && document.body.offsetWidth) {
					parent_pos_x= window.screenLeft;
					parent_width= document.body.offsetWidth;
					parent_pos_y= window.screenTop;
					parent_height= document.body.offsetHeight;
				}
			}
			
			// ms (more space) left: true if horizontally the current window has more free space on the left side.
			// ms top: analogue.
			var ms_left= (parent_pos_x) > (availWidth - parent_width - parent_pos_x);
			var ms_top= (parent_pos_y) > (availHeight - parent_height - parent_pos_y);
			
			if (ms_left) {
				pos_x= 0;
			} else {
				pos_x= availWidth - outer_width; // outer coordinate
			}
			if (ms_top) {
				pos_y= topOfsY;
			} else {
				pos_y= availHeight - outer_height + topOfsY;
			}
			break;
			
		case 1:
			pos_x= availWidth - outer_width; // outer coordinate
			pos_y= topOfsY;
			break;
	}
	
	return "top=" + pos_y + ",left=" + pos_x + ",width=" + 
		width + ",height=" + height + ",";
}

function hmtfw_add_mail(addr, img_id) {
	var wnd_options= hmtfw_get_mailcollector_options();
	new_img_src= '/img/structure/mail-plus-ok4.gif';
	
	// wait for the previous call to this method to be completed.
	if (interval_handle || timeout_bail_out) {
		alert('Die Funktion wird gerade ausgeführt. Bitte warten Sie einen Moment und probieren es nochmals.');
		return;
	}
	
	
	mailcollector_window= hmtfw_ping_window(mailcollector_window);
	if (!mailcollector_window) {
		mailcollector_window= open_popup('', 'hmt_mailcollector', wnd_options);
		mailcollector_window= hmtfw_ping_window(mailcollector_window);
		if (!mailcollector_window) {
			mailcollector_window= open_popup('/?mailcollector', 'hmt_mailcollector', wnd_options);
		}
	}

	interval_handle= setInterval("try { mailcollector_window.hmtfw_add_mail_int('" + escape(addr) + "'); clearInterval(interval_handle); clearTimeout(timeout_bail_out); timeout_bail_out= null; interval_handle= null; try { gE('" + img_id + "').src=new_img_src; } catch(e) {} } catch(e) {}", 100);
	timeout_bail_out= setTimeout("alert('aborting'); clearInterval(interval_handle); timeout_bail_out= null; interval_handle= null;", 10000);
}

function hmtfw_get_mail_url() {
	var addresses= gE("mailcollector").value;
//	var separator= gE("separator").value;
//	var recipient= "Undisclosed-Recipient:;";
	var recipient= "";
	var subject= "";
//	var bcc= addresses.replace(/\r?\n$/, "");
//	while (bcc.indexOf("\n") >= 0) {
//		bcc= bcc.replace(/\r?\n/, separator + " ");
//	}

	var bcc;
	var bccs= "";
	var pos;
	pos= addresses.indexOf("\n");
	while (pos >= 0) {
		bcc= addresses.substring(0, pos);
		bcc= bcc.replace(/^\s|\s$/, "");
		addresses= addresses.substring(pos + 1, addresses.length);
		if (bcc.length > 0) {
			bccs+= "&bcc=" + escape(bcc);
		}
		pos= addresses.indexOf("\n");
	}
	addresses= addresses.replace(/^\s|\s$/, "");
	if (addresses.length > 0) {
		bccs+= "&bcc=" + escape(addresses);
	}
	return "mailto:" + escape(recipient) + "?subject=" + escape(subject) + bccs;
}	

function hmtfw_add_mail_int(addr) {
	gE("mailcollector").value+= addr + "\n";
}

function hmtfw_ping() {
	return true;
}

var context_window;

function hmtfw_get_context_options() {
	calcWindowOffsets();

	// variables for window position calculation
	var exactWidth= 360; // exact width of the window
	var exactHeight= 225; // exact height of the window
	var width; // inner width of window
	var height; // inner height of window
	var pos_x; // left corner coordinate of window
	var pos_y; // top corner coordinate of window

	width= exactWidth;
	var outer_width= width + winXOfs; // outer width of window 

	height= exactHeight + winYOfs; // inner height of window (not including title bar p.e.)
	var outer_height= height; // outer height of window 

	// placement values:
	// 0: automatic (place it where there is space)
	// 1: upper right corner
	// 3: lower left corner
	var placement= 3; // place it where there is space  

	switch (placement) {
		case 0:
			var parent_pos_x;
			var parent_pos_y;
			var parent_width;
			var parent_height;

			if (window.screenX != null && window.outerWidth) {
				parent_pos_x= window.screenX;
				parent_width= window.outerWidth;
				parent_pos_y= window.screenY;
				parent_height= window.outerHeight;
			}
			else if (window.screenLeft) {
				if (document.documentElement) {
					parent_pos_x= window.screenLeft;
					parent_width= document.documentElement.offsetWidth;
					parent_pos_y= window.screenTop;
					parent_height= document.documentElement.offsetHeight;
				}
				else if (document.body && document.body.offsetWidth) {
					parent_pos_x= window.screenLeft;
					parent_width= document.body.offsetWidth;
					parent_pos_y= window.screenTop;
					parent_height= document.body.offsetHeight;
				}
			}

			// ms (more space) left: true if horizontally the current window has more free space on the left side.
			// ms top: analogue.
			var ms_left= (parent_pos_x) > (availWidth - parent_width - parent_pos_x);
			var ms_top= (parent_pos_y) > (availHeight - parent_height - parent_pos_y);

			if (ms_left) {
				pos_x= 0;
			} else {
				pos_x= availWidth - outer_width; // outer coordinate
			}
			if (ms_top) {
				pos_y= topOfsY;
			} else {
				pos_y= availHeight - outer_height + topOfsY;
			}
			break;

		case 1:
			pos_x= availWidth - outer_width; // outer coordinate
			pos_y= topOfsY;
			break;

		case 3:
			pos_x= 0; //topOfsX; 
			pos_y= availHeight - outer_height;
			break;
	}

	return "top=" + pos_y + ",left=" + pos_x + ",width=" + 
		width + ",height=" + height + ",";
}

function hmtfw_open_context() {
	var wnd_options= hmtfw_get_context_options();

	context_window= hmtfw_ping_window(context_window);
	if (!context_window) {
		context_window= open_popup('', 'hmt_context', wnd_options);
		context_window= hmtfw_ping_window(context_window);
		if (!context_window) {
			context_window= open_popup('/?kontext', 'hmt_context', wnd_options);
		}
	}
}

function hmtfw_refresh_context(close_window) {
	var wnd_options= hmtfw_get_context_options();

	// wait for the previous call to this method to be completed.
	if (interval_handle || timeout_bail_out) {
		alert('Die Funktion wird gerade ausgeführt. Bitte warten Sie einen Moment und probieren es nochmals.');
		return;
	}

	context_window= hmtfw_ping_window(context_window);
	if (!context_window) {
		context_window= open_popup('', 'hmt_context', wnd_options);
		context_window= hmtfw_ping_window(context_window);
		if (!context_window) {
			return;
		}
	}
	var close_stmt= "";
	if (close_window) {
		close_stmt= "window.close();";
	}
	interval_handle= setInterval("try { context_window.hmtfw_refresh_context_int(); " + close_stmt + "clearInterval(interval_handle); clearTimeout(timeout_bail_out); timeout_bail_out= null; interval_handle= null; } catch(e) {}", 100);
	timeout_bail_out= setTimeout("alert('Popup konnte nicht geöffnet werden. Bitte Popup-Blocker für diese Website deaktivieren.'); clearInterval(interval_handle); timeout_bail_out= null; interval_handle= null;", 10000);
}

