// ---------------------------------------------------------------
// Class Popup
// (C) Copyright by Publicform GmbH (www.publicform.de)
//
// Version: 2.3
// Date: 2004-07-29
// Author: Andreas Doelling (doelling@publicform.de)
//
// Pops up a browser window, if it gets a click or keypress event
// on an HTML node with the CSS class "popup [type]".
// ---------------------------------------------------------------

function Popup(type) {
	this.type = type;
	this.id = '';
	switch(type) {
		case "flash":	this.href = "about:blank";
						this.windowName = "GBPopUp";
						this.windowSettings = "width=790,height=550";
						break;
		case "form":	this.href = "about:blank";
						this.windowName = "Formular";
						this.windowSettings = "width=355,height=510";
						break;
		case "custom":	this.href = "about:blank";
						this.windowName = Popup.arguments[1];
						this.windowSettings = Popup.arguments[2];
						break;

	}
	
	
	// -----------------------------------------------------
	// Method setId
	// Using this method you can tell the Popup object to serve only
	// HTML nodes which have a certain id.
	// This is the only way to have different custom popup links
	// in one page!
	// Expects a RegExp strin as paramater, e.g. "myID" or "^myLink_[0-9]{1,2}$".
	// -----------------------------------------------------
	this.setId = function(id) {
		this.id = id;
	}
	
	
	// -----------------------------------------------------
	// Method click
	// Handles click events propagated by a Controller object.
	// -----------------------------------------------------
	this.click = function(evt) {
		if(this != arguments.callee.scope){
	      if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
			} else {
		  		return  arguments.callee.scope.click(evt);
			}
	    }
		var srcElement = (evt.srcElement)? evt.srcElement : evt.target;
		if(this.isResponsible(srcElement)) {
			this.popup();
			return false;
		} else {
			return true;
		}
		return false;
	}
	this.click.scope = this;
	
	
	// -----------------------------------------------------
	// Method keypress
	// Handles keypress events propagated by a Controller object.
	// -----------------------------------------------------
	this.keypress = this.click;
	this.keypress.scope = this;
	
	
	// -----------------------------------------------------
	// Method isResponsible
	// Checks if the current Popup instance is responsible for
	// handling the propagated event, i.e. if the source elements
	// CSS class name is "popup_"+[type].
	// -----------------------------------------------------
	this.isResponsible = function(srcElement) {
		var className = srcElement.className;
		var classRegExp = new RegExp("popup\\s+"+this.type);
		if(className.match(classRegExp)) {
			if(this.id == '' || srcElement.id.match(new RegExp(this.id))) {
				this.href = srcElement.getAttribute("href");
				return true;
			}
		}
		var parent = (srcElement.tagName == "BODY")? srcElement : srcElement.parentNode;
		while(parent.tagName != "BODY" && parent.tagName != "A") {
			srcElement = parent;
			parent = parent.parentNode;
		}
		if(parent.tagName == "A" && parent.className.match(classRegExp) && (this.id == '' || parent.id.match(new RegExp(this.id)))) {
			this.href = parent.getAttribute("href");
			return true;
		} else {
			return false;
		}
	}
	
	
	// -----------------------------------------------------
	// Method popup
	// Opens a popup window.
	// -----------------------------------------------------
	this.popup = function(srcElement) {
		var popupWindow = window.open(this.href, this.windowName, this.windowSettings);
		popupWindow.focus();
		return false;
	}
}








