/*
  windetail.js

  Funktion winDetail() öffnet ein neues Browserfenster und stellt die Seite (filename) dar
  alle anderen Funktionen sind intern
*/
var myInterval = "";  //globale Variable für ein Interval vom Hauptfenster aus
var myWin = "";
var stdwinSize = { //Standartfenstergroesse
    left   : (window.screenX||60) + 20,
    top    : (window.screenY||40) + 20,
    width  : 700,
    height : 500 }

function showGallery(filename, preview) {
	var docRoot = pathSplit();
	//winDetail(docRoot+"galerie/galerie.html?img="+preview, "gallerie",null,null,665,580);
	winDetail(filename+"?img="+preview, "gallerie",null,null,760,680);
}



function pathSplit() {
/* return relative path to document_root */
var baseUrl, path;
	if (location.href.indexOf(".html"))
		baseUrl = location.href.replace(/[^\/]+.html/, "");
	else if (location.href.indexOf(".php4"))
		baseUrl = location.href.replace(/[^\/]+.php4/, "");
	else if (location.href.indexOf(".php"))
		baseUrl = location.href.replace(/[^\/]+.php/, "");

	if (location.protocol=="file:") {
		baseUrl = baseUrl.split("web/")[1];
		path=baseUrl.replace(/([a-z0-9]+\/)/g, "../");
/*
		path="";
		for(var p=1; p<baseUrl.split("/").length;p++) {
			path += "../";
		}
*/
	} else {
		path = "/";
	}

	return path;
}

/*
   Parameter: filename, [winClass], [left, top, width, height]
*/

function winDetail(filename) {
  //holt sich die Argumente der Funktion
  var winClass = "";
  //winClass Unterscheidung, hier neue Klassen definieren
  switch(winDetail.arguments[1])
   {
    case "gallerie":
    	winClass = "status=no,location=no,menubar=no,toolbar=no,scrollbars=no,resizable=yes";
		break;
    case "vorschau":
    	winClass = "status=no,location=no,menubar=no,toolbar=no,scrollbars=no,resizable=no";
		break;
    default:
    	winClass = "status=no,location=no,menubar=yes,toolbar=no,scrollbars=yes,resizable=yes";
		break;
   }
  //Groessenangaben setzten, wenn angegeben, sonst Standartwerte
  var left   = isNumber(winDetail.arguments[2]) ? winDetail.arguments[2] : stdwinSize.left;
  var top    = isNumber(winDetail.arguments[3]) ? winDetail.arguments[3] : stdwinSize.top;
  var width  = isNumber(winDetail.arguments[4]) ? winDetail.arguments[4] : stdwinSize.width;
  var height = isNumber(winDetail.arguments[5]) ? winDetail.arguments[5] : stdwinSize.height;

  //schaut, ob Fenster noch auf Bildschirm passt
  var divSize = 0;
  if((divSize = top  + height - screen.availHeight) > 0) { height -= divSize }
  if((divSize = left + width  - screen.availWidth ) > 0) { width  -= divSize }
  //oeffnet Fenster mit angegebenen Parametern
  if (window.myWin) {
	  if (window.myWin.closed) {
		myWin = window.open(filename, winDetail.arguments[1], winClass + ",width=" + width + ",height=" + height + ",top=" + top + ",left=" + left, true);
	  } else {
		myWin.location = filename;
	  }
  } else {
	myWin = window.open(filename, winDetail.arguments[1], winClass + ",width=" + width + ",height=" + height + ",top=" + top + ",left=" + left, true);
  }
  window.myWin = myWin
  //setzt Interval im Hauptfenster, welches die Funktion focusWin aufruft
  myInterval = window.setInterval("focusWin(window.myWin)",10);
}

function focusWin(handle) {
  //ueberprueft, ob handle ein windowhandle ist
  if (typeof(handle) != 'object' || !handle.document || !handle.location) {
     window.clearInterval(myInterval); return false;
  }
  //schaut, ob das Fenster offen ist und gibt dann den Focus
  if (!handle.closed) {
     handle.focus();
     window.clearInterval(myInterval);
  }
  return true;
}

//Hilfsfunktion entscheidet, ob Varibale eine Nummer ist
function isNumber(number) {
  return (number && typeof(number) == 'number' && !isNaN(number));
}

