/**
 * Javascript library for utility functions of zf-espresso
 *
 * @author Marc Saric marc.saric@tuebingen.mpg.de
 * 
 * @version $Header: /users/saric/CVS/zf-espresso/zf-espresso/js/zf-espresso.js,v 1.4.4.3 2007/10/15 14:27:26 geisler Exp $
 */

/**
 * Global variables
 * FIXME: This is kind of stupid, do in a non-static and probably non-global way
 */
// Array of controller elements (for toggling of elem elements)
var	cont = new Array('cf_control', 'pf_control');
// Array of elements to be toggled
var elem = new Array('cfexp', 'cftxt', 'cfdat', 'cfxpr', 'cfsig', 'cfall', 'pftxt', 'pfdat', 'pfxpr', 'pfsig', 'pfsim', 'pffil', 'pfall');

/**
 * Popup a named window (which can be linked from multiple places)
 *
 * @author http://www.faqs.org/docs/htmltut/linking/linking_famsupp_72.html
 * @author PHPMyAdmin
 * @author Marc Saric marc.saric@tuebingen.mpg.de
 *
 * @param mixed mylink URL or instance (?) providing the link
 * @param string windowname (Unique) name of the window
 * @param string win_size Size of the window
 *
 * @return boolean
 */
function popup(mylink, windowname, win_size, win_settings) {
	var popupwindow = '';
	var href; // The URL to show in the popup
	
	// Window Settings
	if (! win_settings) {
		var win_settings = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,';	
	}
	
	
	// Generate URL
	if (typeof(mylink) == 'string') {
		href = mylink;
	}
	else {
		href = mylink.href;
	}

	// Set focus if popup not closed and FIXME?
    if (!popupwindow.closed && popupwindow.location) {
        popupwindow.focus();
    }
    // Must be closed, open it
    else {
        popupwindow = window.open(href, windowname, win_settings + win_size);
    }

	// Eeh?
    if (!popupwindow.opener) {
        popupwindow.opener = self;
    }

	// Set focus
    if (window.focus) {
        popupwindow.focus();
    }

    return false; // Return false to the onclick-event to prevent opening the link in the parent window
}

/**
 * Link back to the parent window from which a popup has been opened.
 * @author http://www.faqs.org/docs/htmltut/linking/linking_famsupp_72.html
 *
 * @param mylink URL or instance (?) providing the link
 * @param closeme
 * @param closeonly
 *
 * @return boolean
 */
function targetopener(mylink, closeme, closeonly) {
	// Safety-check for browsers which do not implement window.focus and window.opener
	if (!(window.focus && window.opener)) {
		return true;
	}
	// Focus on opener (popup-parent)
	window.opener.focus();
	// Just close the popup on click but don't do anything to the opener (like load URL)
	if (! closeonly) {
		window.opener.location.href=mylink.href;
	}
	// Additionally close the popup
	if (closeme) {
		window.close();
	}
	return false; // Return false to the onclick-event to prevent opening the link in the parent window
}

/**
 * addEvent helper function for different types of browsers
 * 
 */
 function addEvent( obj, type, fn ) {
	// NS6 and other standard compliant browsers
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
		EventCache.add(obj, type, fn);
	}
	// Take care of IE style
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn](window.event); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	// Last resort.
	// DOM 0 simple, trusty and crossbrowser method, be aware that this
	// overwrites any pre-existing events on the targeted object
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

/**
 * EventCache helper function
 *
 */
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler) {
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1) {
				item = listEvents[i];
				if(item[0].removeEventListener) {
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on") {
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent) {
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
	
/**
 * The $ function. 
 * This is a shorthand for document.getElementById
 * From http://www.dustindiaz.com/top-ten-javascript
 */ 
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/**
 * toggler function
 *
 * This toggles visibility of items based on the value of (currently two)
 * dropdown fields. This could be enhanced to an arbitrary number of dropdowns
 * and groups (of which one element is visible at a time).
 * 
 * Not tested on browsers != Firefox.
 */
var togglers = {
	
	init : function () {
		// Add all arguments as Eventhandlers
		for (var i = 0; i < cont.length; i++) {
			// onChange execute togglers.run
    		addEvent($(cont[i]),'change',this.run);
		}
	},
	run : function () {
		for (var i = 0; i < elem.length; i++) {
			$(elem[i]).style.display = 'none';
		}
        // Set selected element visible
        for (var i = 0; i < cont.length; i++) {
			$($(cont[i]).value).style.display = '';
        }
	}
}

/**
 * Initialize the toggler on pageLoad	
 */
function pageLoaders() {
	togglers.init();
	togglers.run();
}

/**
 * On page load and destruct	
 */
addEvent(window,'unload',EventCache.flush);
// Results in flashing of the window, instead call pageLoaders() directly
//addEvent(window,'load',pageLoaders);
// EOF