/**
* Javascript
* standard-funktionen
*
* @author heiko pfefferkorn
* @copyright 2007 i-fabrik gmbh
* @version $Id: main.js,v 1.1 2007/07/17 10:51:57 michael Exp $
*
*/

		function startDimm() {
			new indexSite($$('#navi_start li'), $$('#bilder_start .bild_holder'),{start:0});
		}

		function suchbegriffBlur(input_field) {
			if (input_field.value == '') {input_field.value = 'Suchbegriff';}
		}

		function suchbegriffFocus(input_field) {
			if (input_field.value == 'Suchbegriff') {input_field.value = '';}
		}

    /* fixMsIELeaks
     *
     * Fix some IE memory leaks.
     * http://youngpup.net/2005/0221010713
     */
    function fixMsIELeaks() {
        if (document.all && window.attachEvent) {
            var elProps = [
                "data", "onmouseover", "onmouseout",
                "onmousedown", "onmouseup", "ondblclick",
                "onclick", "onselectstart", "oncontextmenu"
            ];
            var all = d.all;
            for (var i = 0, el; el = all[i]; i++) {
                for (var j = 0, elProp; elProp = elProps[j]; j++) {
                    el[elProp] = null;
                }
            }
        }
    }

    var SITE = {
        start: function() {

            WIDGETS.showTooltips($$('.show_tt'));
            WIDGETS.blurAllLinks([$$('a')]);
            //WIDGETS.setMouseOverClass([$$('input.input_button'),$$('button')],'over');
            //WIDGETS.rewriteTableTag($$('#con_inhalt table'),0,0,0);

            // Menü initialisieren
            $$('#navi_main li').each(function(item) {
                item.addEvent('mouseover', function() { this.addClass('over'); }).addEvent('mouseout', function() { this.removeClass('over'); });
            });

						// Slimbox initialisieren
						Slimbox.init({
							fxResize      : Fx.Transitions.backOut,
							opacityOverlay: 0.7,
							textNavImage  : 'Bild %d von %dd'
						});

            // Slimbox initialisieren
            Slimbox.init({
              fxResize      : Fx.Transitions.backOut,
              opacityOverlay: 0.8,
              textNavImage  : 'Bild %d von %dd'
            });
        },
        end: function() {
            fixMsIELeaks();
        }
    };

    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);
        },
        blurAllLinks: function(o) {
            if (!o) return;
            o.each(function(elm) {
                elm.each(function(item) {
                    item.addEvent('click', function(){ this.blur(); });
                });
            });
        },
        setMouseOverClass: function(o,c) {
            if (!o) return;
            o.each(function(elm) {
                elm.each(function(item) {
                    item.addEvent('mouseover', function(){ this.addClass(c); });
                    item.addEvent('mouseout', function(){ this.removeClass(c); });
                });
            });
        },
        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.8)
                }
            });
        },
        rewriteTableTag: function(o,b,cp,cs) {
            if (!o) return;

            o.each(function(tbl) {
                tbl.setProperties({
                    border     : b,
                    cellpadding: cp,
                    cellspacing: cs
                });
            });
        },
        imgOverEvent: function(selector,out,over) {
            $$(selector).each(function(img) {
                var img = $(img);
                if ($type(img.src)) {
                    if (img.src.indexOf(out)>0) {
                        img.addEvent('mouseover',function() {
                            img.src = img.src.replace(out, over);
                        }).addEvent('mouseout', function() {
                            img.src = img.src.replace(over, out);
                        });
                    }
                }
            });
        }
    };

    var SITE_ACCORDION = {
        start: function(o_toggler,o_toggler_a,o_stretcher,mode) {
            if ( !((o_toggler.length>0 && o_stretcher.length>0) && (o_toggler.length==o_stretcher.length)) )
                return;

            var t = this;
            this.togglers    = o_toggler;
            this.togglers_a  = o_toggler_a;
            this.stretchers  = o_stretcher;
            this.hide_always = false;

            switch (mode) {
                case 'open':
                    t.open();
                    break;
                case 'hide_always':
                    this.hide_always = true;
                default:
                    t.toggle();
                    break;
            }
        },
        /* Property: open
         *
         * Inaktives komplett geöffnetes 'Accordion'. Mousout- und Mouseoverstatus setzen.
         */
        open: function() {
            var t = this;
            this.stretchers.each(function(item,i) {
                item.addEvent('mouseover', function() {
                    this.addClass('accordion_over');
                    t.togglers[i].addClass('h4_ov');
                });
                item.addEvent('mouseout', function() {
                    this.removeClass('accordion_over');
                    t.togglers[i].removeClass('h4_ov');
                });
            });
        },
        toggle: function() {
            var t     = this;
            var first = true;
            this.stretchers.each(function(item) {
                item.setStyles({
                    height  : '0',
                    overflow: 'hidden'
                });
            });

            this.togglers.each(function(tog, i) {
                tog.defaultColor = tog.getStyle('background-color');
            });

            this.acc = new Fx.Accordion(t.togglers, t.stretchers, {duration: 350, opacity: false, start: true, alwaysHide:t.hide_always, transition: Fx.Transitions.quadOut,
                onActive: function(tog, i) {
                    tog.addClass('h4_cur');
                },
                onBackground: function(tog, i) {
                    tog.removeClass('h4_cur');
                }
            });

            if (!t.checkHash(t.togglers_a)) {
                if (first)
                    t.acc.showThisHideOpen(0);
            }
        },
        checkHash: function(o) {
            var t     = this;
            var found = false;

            if (o.length>0) {
                o.each(function(link, i) {
                    link.addEvent('click', function(){ this.blur(); });
                    if (window.location.hash.test(link.hash)) {
                        t.acc.showThisHideOpen(i);
                        t.togglers[i].addClass('h4_cur');
                        found = true;
                    }
                });
            }
            return found;
        }
    };

    var indexSite = new Class({
        setOptions: function(options){
            this.options = {
            	start   : 0,
            	duration: 200
            };
            Object.extend(this.options, options || {});
        },
        initialize: function(itemsMenu, itemsImg, options){
            if (!itemsMenu || !itemsImg)
                return;

            this.setOptions(options);

            this.itemsMenu   = itemsMenu;
            this.itemsImg    = itemsImg;
            this.active      = (this.options.start<0) ? 0: this.options.start;
            this.last_active = this.active;

            var pointer = this;

			// Init Buttons
            this.itemsMenu.each(function(item,i) {
                item.addEvent('mouseover', function() {
                    if (i!=pointer.active) {
                    	pointer.last_active = pointer.active;
                    	pointer.active      = i;

                        pointer.itemsMenu[pointer.last_active].removeClass('on');
                        this.addClass('on');
                        pointer.setImage(pointer.active);
                    }
                });
            });

			// Init Bilder
            this.itemsImg.each(function(item) {
            	item.setOpacity(0);
            });

			// Setze Standardbutton und -bild
            this.itemsMenu[this.active].addClass('on');
            this.itemsImg[this.active].setStyle('z-index',this.itemsImg.length+1);
            new Fx.Style(this.itemsImg[this.active], 'opacity', {duration:pointer.options.duration}).start(0,1);
        },
        setImage: function(n) {
            var pointer = this;

            this.itemsImg[this.last_active].setStyle('z-index', pointer.itemsImg.length);
            this.itemsImg[n].setStyles({
                'z-index': pointer.itemsImg.length+1,
                'display': 'block'
            });

            new Fx.Style(this.itemsImg[n], 'opacity', {
                duration  : pointer.options.duration,
                onComplete: function() {
                	pointer.itemsImg[pointer.last_active].setOpacity(0);
                	pointer.itemsImg[pointer.last_active].setStyle('display','none');
                }
            }).start(0,1);
        }
    });
    indexSite.implement(new Chain);

    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);

