/**
* Javascript
* standard-funktionen
*
* @author heiko pfefferkorn
* @copyright 2006 i-fabrik gmbh
* @version $Id: main.js,v 1.2 2007/03/22 13:31:57 heiko Exp $
*
*/

    var SITE = {
        start: function(){
            WIDGETS.showTooltips($$('.show_tt'));
        },
        end: function() {
            WIDGETS.fixMsIELeaks();
        }
    };

	var MP_MENU = new Class({
        /* Property: setOptions
         *
         * Definieren/Setzen und/oder Überschreiben der Standardoptionen
         */
		setOptions: function(options){
			this.options = {
				clsMouseOver : 'over',
				clsActivState: 'activ',
				clsFirstChild: '',
				identMainMenu: 'main_',
				identSubMenu : 'sub_',
				hideDelay    : 2000,
				useSubmenu   : true
			};
			Object.extend(this.options, options || {});

			this.subMenuIds = {};
			this.activated  = '';
			this.active     = '';
		},
        /* Property: initialize
         *
         * Initialisierung des Menüs. Events des Haupt- und Untermenüs definieren
         */
		initialize: function(menuitems, submenuitems, options) {
			this.setOptions(options);
			if (!menuitems) return;
			var pointer = this;

			menuitems.each(function(item,i) {
				if (i==0 && pointer.options.clsFirstChild!='')
					item.addClass(pointer.options.clsFirstChild);

				var item_id     = item.getProperty('id');
				var sub_item_id = item_id.replace(pointer.options.identMainMenu, pointer.options.identSubMenu);

				// Ein Menü aktiv gesetzt, dann aktiviere Untermenü wenn vorhanden
				if (item.hasClass(pointer.options.clsActivState))
					if ($(sub_item_id))
						pointer.activated = $(sub_item_id);

				// Setze Events auf die Menüelemente
				item.addEvent('klick', function(e) {
					//if (!item.hasClass(pointer.options.clsActivState)) {
						this.addClass(pointer.options.clsMouseOver);
						$clear(pointer.timer);
						if ($(sub_item_id))
							pointer.show($(sub_item_id));
					//}
				}).addEvent('mouseout', function(e) {
					if (!item.hasClass(pointer.options.clsActivState)) {
						this.removeClass(pointer.options.clsMouseOver);
						pointer.end(e);
					}
				});
			});

			submenuitems.each(function(item) {
				// Setze Events auf das Submenü
				item.addEvent('mouseover', function(e) {
					$clear(pointer.timer);
				}).addEvent('mouseout', function(e) {
					pointer.end(e);
				});

				item.getElements('li').each(function(item2) {
					item2.addEvent('mouseover', function(e) {
						this.addClass(pointer.options.clsMouseOver);
					}).addEvent('mouseout', function(e) {
						this.removeClass(pointer.options.clsMouseOver);
					});
				});

			});

			this.start();
		},
        /* Property: start
         *
         * Einblenden eines vorher per Klasse aktiv geschaltenen Menüs.
         */
		start: function(){
			if (this.activated) {
				this.activated.setStyle('display','block');
				this.active = this.activated;
			}
		},

        /* Property: end
         *
         * Zeitgesteuertes Ausblenden eines Untermenüs
         */
		end: function(event){
			$clear(this.timer);
			this.timer = this.hideDelay.delay(this.options.hideDelay, this);
			//event.stop();
		},
        /* Property: show
         *
         * Einblenden eines Untermenüs. Analog auch gleich das Ausblenden eines vorherigen Untermenüs.
         */
		show: function(o) {
			$clear(this.timer);
			this.hideAll();

			if (o) {
				o.setStyle('display','block');
				this.active = o;
			}
		},
        /* Property: hideAll
         *
         * Blendet eventuell angezeigtes Untermenü aus.
         */
		hideAll: function() {
			if (this.active)
				this.active.setStyle('display','none');
		},
        /* Property: hideAll
         *
         * Blendet eventuell angezeigtes Untermenü aus und per Klasse aktiviertes wieder ein.
         * 'hideDelay' tritt nur in Kraft wenn kein weiteres Untermenü angefordert wurde.
         */
		hideDelay: function() {
			if (this.active)
				this.active.setStyle('display','none');
			this.start();
		}
	});
	MP_MENU.implement(new Chain);


    var WIDGETS = {
        openPopUp: function(url,wn,ft,ww,wh,wc){
            if(window.screen)
                if(wc){
                    var wl = (screen.width-ww)/2;
                    var wt = (screen.height-wh)/2;
                    ft+=(ft!='')?',':'';
                    ft+=',left='+wl+',top='+wt;
                }
            window.open(url,wn,ft+((ft!='')?',':'')+'width='+ww+',height='+wh);
        },
        /* showTooltip
         *
         * MouseOver-Tipps initialisieren
         *
         * @param  object  o  Array aller Objekte die mit einem Tipp versehen werden sollen
         * @return /
         *
         * example: $S('.show_tt');
         */
        showTooltips: function(o){
            if(!o) return;

            var siteTips = new Tips(o, {
                maxTitleChars:100,
                onShow: function(tip){
                    new Fx.Style(tip, 'opacity', {
                        timeOut  : 10,
                        duration : 100
                    }).custom(0,0.6)
                }
            });
        },
	    fixMsIELeaks: function() {
			if (document.all && window.attachEvent) {
				var elProps = ["data", "onmouseover", "onmouseout", "onmousedown", "onmouseup", "ondblclick", "onclick", "onselectstart", "oncontextmenu"];
				var all = document.all;
				for (var i=0, el; el=all[i]; i++)
					for (var j=0, elProp; elProp=elProps[j]; j++)
						el[elProp] = null;
			}
        }
    };

    window.addEvent('load', function(){
        if( self.parent.frames.length!=0 )
            self.parent.location = self.location;
    });
    window.addEvent('domready', SITE.start);
    window.addEvent('unload', SITE.end);