
window.addEvent('domready', function(){
	$$('.top-nav-box .top-navigation-content').each(function(el){
        if (el.hasClass('selected')){
            return;
        }
        el.set('morph',{
            duration:200
        });
        el.addEvents({
                mouseenter: function(){
                    this.morph({
                        'background-position': '0px 0px'
                    });
                },
                mouseleave: function(){
                    //alert(position);
                    this.morph({
                        'background-position': '0px 20px'
                    });
                }
            });        
    });
    if (typeof index == 'undefined'){
        index = 0;
    }
    /*
    myAccordion = new Accordion($('accordion'), 'span.toggler', 'ul.content', {
        opacity: false,
        show: index || 0,
//        display: index,
        alwaysHide: true,
        onActive: function(toggler, element){
//            toggler.removeClass('active');
            toggler.setStyle('background-image', 'url(/media/public/sites/kvhh/css/img/minus_9x9.gif)');
        },
        onBackground: function(toggler, element){
//            toggler.addClass('active');
            toggler.setStyle('background-image', 'url(/media/public/sites/kvhh/css/img/plus_9x9.gif)');
        }
    });
    
    */
//    $$('span.toggler').addEvent('mouseenter', function() { this.fireEvent('click'); });
    
    Galleries.start();    
    fitColHeight();
    $$('input.search-field').each(function(element){
        new OverText(element);
    })

});
var ImageWindow = new Class({
    Implements:[Options],
    options:{
        'label':'&nbsp;'  
    },
    initialize: function(image, fromImage, options){
        this.image = $(image);
        this.fromImage = $(fromImage);
        this.boundDestroy = this.destroy.bind(this);
        this.boundDocClick = this.docClick.bind(this);
        this.buildWindow();
    },
    buildWindow: function() {
        this.window = new Element('div',{
            'styles':{
                'position':'absolute',
                'display':'none',
                'overflow':'hidden'
            },
            'class':'image-window'
        }).inject($(document.body));
        this.header = new Element('div',{
            'class':'image-window-header',
            'html':this.options.label,
            'events':{
                'click':this.boundDestroy  
            }
        }).inject(this.window);
        this.window.adopt(this.image);
    },
    show: function(){
        var from = this.fromImage.getCoordinates();
        this.window.setStyles({
            'left':from.left+'px',
            'top':from.top+'px',
            'width':from.width+'px',
            'height':from.height+'px',
            'display':'block',
            'opacity':0.1,
            'z-index':11000
        });
        this.image.setStyle('display','');
        var to = this.image.getDimensions();
        new Fx.Morph(this.window,{
            'onComplete':function() {
                document.addEvent('mousedown',this.boundDocClick);    
            }.bind(this)
        }).start({
            'width':[from.width,to.width+20],
            'height':[from.height,to.height+20],
            'left':[from.left, Math.max(20,from.left - Math.floor((to.width-from.width)/2))],
            'top':[from.top, Math.max(20,from.top - Math.floor((to.height-from.height)/2))],
            'opacity':[0.1,1]
        });
        
    },
    docClick: function() {
        this.header.fireEvent('click');
    },
    destroy: function() {
        //document.removeEvent('click',this.boundDocClick)
        this.window.set('tween',{
            'onComplete':(function() {
                this.image.setStyle('display','none').inject(document.body);
                this.window.destroy();
                
            }).bind(this)
        })
        this.window.fade('out');
    }
    
});
function showOrganigramm(){
    new ImageWindow('organigramm','organigramm-cont').show();
}
function showImage(target, source){
    //console.log(source, target);
    new ImageWindow(target,source).show();
}
function checkValue(item, command) {
    switch (command) {
        case 'focus':
            if (item.value == 'Passwort') {
                replaceInput(item,'password','',1);
            }
            break;
        case 'blur':
            if (item.value == '') {
                replaceInput(item,'text','Passwort',0);
            }
            break;
    }
}
function replaceInput(oldpass,type,value,focus){
    var newpass=document.createElement('input');
    newpass.setAttribute('type',type);
    newpass.setAttribute('name',oldpass.getAttribute('name'));
    newpass.setAttribute('id',oldpass.getAttribute('id'));
    switch(type){
        case 'password':
            newpass.setAttribute('style','color: #000;');
            break;
        case 'text':
            newpass.setAttribute('style','color: #666');
            break;
    }
    newpass.setAttribute('style',oldpass.getAttribute('style'));
    newpass.setAttribute('size',oldpass.getAttribute('size'));
    newpass.setAttribute('onfocus',oldpass.getAttribute('onfocus'));
    newpass.setAttribute('onblur',oldpass.getAttribute('onblur'));
    newpass.setAttribute('value',value);
    oldpass.parentNode.replaceChild(newpass,oldpass);
    if (focus == 1) {newpass.focus(); newpass.select();}
}
function fitColHeight() {
    var cols = $$('#col1','#col2','#col3');
    var height = 0;
    cols.each(function(col){
        var el = $(col);
        height = Math.max(height, el.clientHeight, el.offsetHeight);
    });
    if (!height){
        return;
    }
    cols.each(function(col){
        var h, id = col.get('id');
        if (id == 'col3') return;
        if (['col1','col2'].contains(id)){
            h = (height+15)+'px';
        }
        else {
            h = (height)+'px';
        }
        col.setStyle('height',h);
    });
};

var Galleries = {
    start: function() {
        $$('.mooflow').each(function(gallery){
            Galleries.init(gallery)
        });   
    },
    init: function(container){

        var mf = new MooFlow($(container), {
            startIndex: 0,
            useSlider: true,
            useAutoPlay: true,
            useCaption: true,
            useResize: true,
            useMouseWheel: true,
            useKeyInput: true,
            useViewer:true,
            onClickView: function(obj){
                var img = new Element('img',{src:obj.src, title:obj.title, alt:obj.alt, styles:obj.coords}).setStyles({'position':'absolute','border':'none'});
                var title = (obj.title || '')+(obj.title && obj.alt ? ' - ' : '')+(obj.alt || '');
                var link = new Element('a',{'class':'remooz-element','href':obj.href,'title':title, styles:{'border':'none'}});
                $(document.body).adopt(link.adopt(img));
                var remooz = new ReMooz(link, {
                    centered: true,
                    resizeFactor: 0.8,
                    origin: link.getElement('img'),
                    onCloseEnd: function(){link.destroy()}
                });
                remooz.open();
            }
        });
        
    }
    
};

window.addEvent('domready',function() {
    var checkOver = function(element){
        if (!Browser.Engine.trident4){
            return element.scrollWidth > element.clientWidth;    
        }
        else {
            return true;
        }
    }
    if (Browser.Engine.trident){
        $$('.arztsuche-form-content select').each(function(select) {
            //alert(select.clientWidth+'-'+select.scrollWidth);
            var over = checkOver(select);
            if (!over){
                return;
            }
            var parent = select.getParent();  
            if (Browser.Engine.trident4){
                parent.setStyles({
                   'position':'relative',
                   'height':'21px',
                   'display':'inline-block',
                   'width': '266px',
                   'overflow':'hidden'
                   //'border-left':'2px solid #000'
                });
                var inStyle = {
                    'position':'absolute',
                    'width':'auto',
                    'right':'0'
                }
                var outStyle= {
                    'position':'',
                    'width':'266px'
                }
                select.addEvents({
                    'mouseenter':function(){select.setStyles(inStyle)},
                    'mouseleave':function(event) {
                        if (window.event.toElement) {
                            select.setStyles(outStyle);
                            return;
                        }
                    },
                    'blur':function(){select.setStyles(outStyle)},
                    'change':function(){select.setStyles(outStyle)}
                });
                /*
                select.addEvent('mouseenter',function(event) {
                    select.setStyles({
                        'position':'absolute',
                        'width':'auto',
                        'right':'0'
                    });
                })
                select.addEvent('mouseout',function(event) {
                    if (window.event.toElement) {
                        select.setStyles(outStyle);
                        return;
                    }
                })
                select.addEvent('change',function(event) {
                    select.setStyles(outStyle);
                })
                */
            }
            else{
                parent.setStyles({
                   'position':'relative',
                   'height':'21px',
                   'display':'inline-block',
                   'width': '266px',
                   'overflow':'hidden'
                   //'border-left':'2px solid #000'
                });
                select.setStyles({
                    //'position':'absolute',
                    'right':'0',
                    'width':'266px'
                });
                select.attachEvent('onfocusin',function() {
                    select.setStyles({
                        'position':'absolute',
                        'width':'auto'
                    });
                    parent.setStyle('border-left','2px solid #000');
                })
                select.attachEvent('onfocusout',function() {
                    select.setStyles({
                        'position':'',
                        'width':'266px'
                    });
                    parent.setStyle('border-left','');
                })
            }
            /*
            if (Browser.Engine.trident4){
                parent.setStyle('border','1px solid #000');
                select.setStyle('border','0 none');
            }
            */
        });
        
    }
    return;
    var select = $('ffachgebiet');
    if (!select){
        return;
    }
    var str = '';
    var parent = select.getParent();
    
    parent.setStyles({
       'position':'relative',
       'height':'22px',
       'border':'1px solid red',
       'display':'inline-block',
       'width': '263px',
       'overflow':'hidden'
    });
    select.setStyles({
        'position':'absolute',
        'right':0,
        'width':'auto'
    });
    return;
    var width = select.getStyle('width');
    select.addEvents({
        'click':function(){
            select.setStyles({
                'position':'absolute',
                'left':select.offsetLeft,
                'width':'auto'
                //'left':(offset.x)+'px',
                //'top':offset.y+'px',
            });
            select.click();
        },
        'blur': function() {
            select.setStyles({
                'position':'static',
                'left':'auto',
                'width':width
                //'left':(offset.x)+'px',
                //'top':offset.y+'px',
            });
        }
    })
    //console.log(offset);
})


