/*
Coppenrath & Wiese JS Implementation (see methods.js for classes etc)
by http://www.comspace.de
Dependencies: 
    Mootools Core 1.2.3+
        - * (rc before optimizing)
    Mootools More
        -Slider (mooflow)
        -Assets (mooflow)
        -Fx.Accordion
        - (rc before optimizing)
*/

window.addEvent('domready', function(){
    /*
    
        -coverflow
        -content/marginal
            -accordions: content/marginal
            -box_imgs hover
        -navigation/search
            -navigation: hover animations
            -search: clear input
            -search: animated form
    */

    //.onlyjs => display:none;
    $$('.onlyjs').removeClass('onlyjs');
    
    
    //initialize coverFlow
    (new cfSwitch({el:$('MooFlow'), forceJS:true}));
    

    
    /******************************
    
    CONTENT / MARGINAL
    
    *******************************/
    
    function getParameterByName(name)
    {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if(results == null)
            return "";
        else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    
    //ACCORDIONS
    //content accordions, originally the last element was to be active
    $$('.box_accordion').each(function(accordion){
        var togglers = accordion.getElements('.togglers a');
        if(!togglers[0])return;
        var activeAcc = 0;
            accordion.className.split(' ').each(function(accClass){//set open element via class: open_4 opens the fourth
                    if(accClass.test(/^open_\d+$/)){
                        activeAcc = parseInt(accClass.split('_')[1], 10) - 1;
                    }
            });

        var myAccordion = new Fx.Accordion(togglers, accordion.getElements('.sections > div'), {opacity:false, display:activeAcc})
            .addEvent('active', function(toggler, dings){
                toggler.addClass('active');
            }).addEvent('background', function(toggler){
                toggler.removeClass('active');
            })
        
        
        //decode get parameter "tab":
        var tab = getParameterByName('tab');
        if(tab !== "") {
            tab = tab.toLowerCase();
            togglers.each(function(tog, i){
                var togid = tog.get('text').toLowerCase().trim();
                if(togid == tab){
                    activeAcc = i;
                }
            });
        }
    
        togglers[activeAcc].addClass('active');

        myAccordion.display(activeAcc);        
        (function(){myAccordion.display(activeAcc);}).delay(600);//ie. sorry  
        accordion.store('accordion', myAccordion);
    });
    
    //marginal accordions
    $$('.box_inneraccordion').each(function(accordion){
        var togglers = accordion.getChildren('h2'), sections = accordion.getChildren('div');
        if(!togglers[0])return;
        var    last = sections.length-1;
        if(accordion.getParent('.col_cnt') || true){
            if(accordion.getParent('.col_cnt')){
                last = 0;
            }
            if(accordion.hasClass('noneOpen')){
                last = -1;
            }else{
                accordion.className.split(' ').each(function(accClass){//set open element via class: open_4 opens the fourth
                    if(accClass.test(/^open_\d+$/)){
                        last = parseInt(accClass.split('_')[1], 10) - 1;
                    }
                });
            }
        }

        
if(accordion.hasClass('sortiment')){

            if($('active_accordion_id')){
                last = $('active_accordion_id').innerHTML;
            }else{
                last = -1;
            }
        }


        var myAccordion = new Fx.Accordion(togglers, sections, {opacity:false, show:last, returnHeightToAuto: true, alwaysHide: accordion.hasClass('sortiment') })
            .addEvent('active', function(toggler, section){
                toggler.addClass('active');
                section.addClass('active');

                if(toggler.hasClass('scrolltome') || toggler.getParent('.accordionscroll'))(function(){var scroller = new Fx.Scroll(window).toElement(toggler);}).delay(500);

            }).addEvent('background', function(toggler, section){
                toggler.removeClass('active');
                section.removeClass('active');
            });
           
        accordion.store('accordion', myAccordion);


        if(window.location.hash !== ""){
        var nhash = window.location.hash.replace('#', '').toLowerCase();
        togglers.each(function(tog, i){
            var togid = tog.get('text').toLowerCase().trim();
            if(togid == nhash){
                last = i;
            }
            
        });
    }



        if(togglers[last]) {
            togglers[last].addClass('active');
            sections[last].addClass('active');
            sections[last].setStyle('height', 'auto');
        }    });
    
    
    //change img on hover - reads alternative src from rel of (some) parent link.
    $$('.box_imgs img').each(function(img){
        img.addEvents({
            'mouseenter':function(ev){this.changeSrc(this.getParent('a').rel);}, 
            'mouseleave':function(ev){this.resetSrc();}
        });
    });

    //history book starter

    $$('#frame_cnt div.col_marginal a.historyLink').each(function(el){
    
        var original = $$('.box_history').getNext('a.link');
        if(original){
            el.addEvent('click', function(ev){
                ev.stop();
                original.fireEvent('click', ev);
            });
        }
    });
    /******************************
    
    NAVIGATION / SEARCH
    
    *******************************/
    
    //menu hover animations add-on
    (new CPMenuFade($('frame_nav').getElement('ul')));
     //clear search input on hover/click
    (new InputToggle({hover:false}));
    
    //search animation add-on
    (new showSearch());


    document.body.getElements('.print_link').each(function(el){el.addEvent('click', function(ev){ev.stop(); window.print();})});
        
    $$('#frame_cnt a.popup').addEvent('click', function(ev){
            ev.stop();
            if(this.hasClass('scroll')){
                popGenericScroll(ev.target.href);
                return;
            }
            popGeneric(ev.target.href);
        });

   
    var lightboxNr = 0;
    $$('#frame_cnt .opn_lightbox').each(function(el){
        var gname = 'lbimg'+lightboxNr;
        lightboxNr++;
        
        el.getElements('a').filter(function(e){return e.getElements('img').length && !(/pdf$/.test(e.href))}).set('rel', 'lightbox['+gname+']');
        
    });
    $$('a[href*=http://www.youtube.com/watch]').each(function(a){a.set('rel', 'lightbox')});

    if(Mediabox && Mediabox.scanPage)Mediabox.scanPage();
});

/* worldwide map */
window.addEvent('domready', function(){
    var map = new Cwmap();
});


/* home curtains */
window.addEvent('domready', function(){
    var curtain = document.id('dessert-home-positioned');
    if(!curtain){
        return;
    }
    curtain.addEvent('mousemove', function(ev){
        this.removeEvents('mousemove');
        this.getElement('.curtain-left').set('tween', {duration:1500, transition:Fx.Transitions.Sine.easeInOut}).tween('left', -473);
        this.getElement('.curtain-right').set('tween', {duration:1500, transition:Fx.Transitions.Sine.easeInOut}).tween('right', -518);
        this.getElement('.curtain-text').set('tween', {duration:1500, transition:Fx.Transitions.Sine.easeInOut}).tween('left', -250);
    });
});

/*
 * dessert hover
 * 
 * */
window.addEvent('domready', function(){
    var bgel;
    //trident engine renders background change unbearably slow
    if(Browser.Engine.name == 'trident' && $$('#frame_wrapper .dessert-links a').length){
        bgel = new Element('div', {'class':'iebgshim'}).store('orgbg', 'none');
        $('frame_stage').grab(bgel, 'top');
    }
    $$('#frame_wrapper .dessert-links a').each(function(el, i){
        var preloadimg;
        if(Browser.Engine.name != 'trident'){
            bgel = el.getParent('#frame_stage');
            if(i == 0){
                bgel.store('orgbg', bgel.getStyle('background-image'));
            }
        }
        
        if(!el.rel){return;}
        //preload...
        preloadimg = Asset.image(el.rel);
        el.addEvents({
            mouseenter: function(){bgel.setStyle('background-image', 'url('+this.rel+')');},
            mouseleave: function(){ bgel.setStyle('background-image', bgel.retrieve('orgbg'));}
        })
    });
});
   /*         window.addEvent('domready', function(){
                tippContainer();
            });
            
            function tippContainer(){
            
                // define necessary container
                var main_container =             document.getElement('.col_marginal .view_pictures');
                
                if ( main_container !== null ) {
                    tipp_active =                 main_container.getElement('.active'),
                    tipp_navigation =            main_container.getElement('.tipp_navigation_container'),
                    tipp_container =             main_container.getElements('.tipp'),
                    tipp_grey_box =             tipp_active.getElement('.grey_box'),
                    tipp_active_position =         0,
                    tipp_navigation_fix_height = 55;
                    
                    if ( tipp_navigation !== null && tipp_active !== null && tipp_grey_box !== null ) {
                        tipp_navigation.setStyle('display', 'block');
                        
                        var tipp_navigation_container_height =     tipp_navigation.getStyle('height').toInt(),
                            tipp_active_height =                 tipp_active.getStyle('height').toInt(),
                            tipp_grey_box_height =                 tipp_grey_box.getStyle('height').toInt(),
                            tipp_height =                         tipp_active_height + tipp_grey_box_height, 
                            tipp_navigation_fix_height =         tipp_navigation_container_height + tipp_navigation_fix_height;    
                        
                        // get position active tipp
                        tipp_container.each(function(element, i) {
                            if (element.hasClass('active')) tipp_active_position = i;
                        });                     
                        
                        // set height
                        if ( tipp_active_position < 4 ) {
                            tipp_navigation.setStyle('top', tipp_height);
                            
                            tipp_container.each(function(element, i) {
                                if ( i > 3 ) element.setStyle('margin-top', tipp_navigation_fix_height);
                            });
                            
                        }else{
                            if (tipp_height >= 142 ) { var fixheight = 10 } else { var fixheight = 25; }
                            tipp_height = (tipp_height + tipp_active_height) + fixheight;
                            tipp_navigation.setStyle('top', tipp_height);
                        }
                        
                        main_container_height =     main_container.getStyle('height').toInt();
                        main_container_height =     main_container_height + tipp_navigation_fix_height;
                        tipp_active.getParent().setStyle('height', main_container_height); // only parent!
                    }
                }
            }
*/



/*
 * marginal menu "career"
 * 
 * */
window.addEvent('domready', function(){
    var el = $('view_pictures');
    if(!el){return}
    var act = el.getElement('.tipp.active');
    if(!el || act ){return;} //only without active page...
    
    var els = el.getElements('.tipp').set('tween', {duration: 'short'});
    
    els.addEvent('mouseenter', function(ev){
        this.store('hover', true);
        otherels = els.filter(function(el){return !el.retrieve('hover');});
        otherels.setStyle('opacity',0.5);
        this.store('hover', false);
        
        
        this.setStyle('opacity', 1);
    });
    els.addEvent('mouseleave', function(ev){
        
        els.setStyle('opacity', 0.5);
    });
    
    el.addEvent('mouseleave', function(ev){
        els.setStyle('opacity', 1);
    })
    
    
});
 

/*
 * Inline lightbox
 * */
window.addEvent('domready', function(ev){
    $$('.box_hidden_lightbox').each(function(el){
        if(!el.id){ return; }
        var h = el.get('data-height'),
            rel = h ? ('lightbox[inline 532 '+h.toInt()+']') : 'lightbox[inline]';
        
                
        $$('a[href$='+el.id+']').each(function(a){
            a.rel = rel;
            a.addEvent('click', function(ev){ev.stop()}).mediabox();
        });
    });
});
 

/*
 * Fx.Scroll
 * */
 //MooTools More, <http://mootools.net/more>. Copyright (c) 2006-2009 Aaron Newton <http://clientcide.com/>, Valerio Proietti <http://mad4milk.net> & the MooTools team <http://mootools.net/developers>, MIT Style License.
MooTools.More={version:"1.2.5.1",build:"254884f2b83651bf95260eed5c6cceb838e22d8e"};Fx.Scroll=new Class({Extends:Fx,options:{offset:{x:0,y:0},wheelStops:true},initialize:function(b,a){this.element=this.subject=document.id(b);
this.parent(a);var d=this.cancel.bind(this,false);if($type(this.element)!="element"){this.element=document.id(this.element.getDocument().body);}var c=this.element;
if(this.options.wheelStops){this.addEvent("start",function(){c.addEvent("mousewheel",d);},true);this.addEvent("complete",function(){c.removeEvent("mousewheel",d);
},true);}},set:function(){var a=Array.flatten(arguments);if(Browser.Engine.gecko){a=[Math.round(a[0]),Math.round(a[1])];}this.element.scrollTo(a[0]+this.options.offset.x,a[1]+this.options.offset.y);
},compute:function(c,b,a){return[0,1].map(function(d){return Fx.compute(c[d],b[d],a);});},start:function(c,g){if(!this.check(c,g)){return this;}var e=this.element.getScrollSize(),b=this.element.getScroll(),d={x:c,y:g};
for(var f in d){var a=e[f];if($chk(d[f])){d[f]=($type(d[f])=="number")?d[f]:a;}else{d[f]=b[f];}d[f]+=this.options.offset[f];}return this.parent([b.x,b.y],[d.x,d.y]);
},toTop:function(){return this.start(false,0);},toLeft:function(){return this.start(0,false);},toRight:function(){return this.start("right",false);},toBottom:function(){return this.start(false,"bottom");
},toElement:function(b){var a=document.id(b).getPosition(this.element);return this.start(a.x,a.y);},scrollIntoView:function(c,e,d){e=e?$splat(e):["x","y"];
var h={};c=document.id(c);var f=c.getPosition(this.element);var i=c.getSize();var g=this.element.getScroll();var a=this.element.getSize();var b={x:f.x+i.x,y:f.y+i.y};
["x","y"].each(function(j){if(e.contains(j)){if(b[j]>g[j]+a[j]){h[j]=b[j]-a[j];}if(f[j]<g[j]){h[j]=f[j];}}if(h[j]==null){h[j]=g[j];}if(d&&d[j]){h[j]=h[j]+d[j];
}},this);if(h.x!=g.x||h.y!=g.y){this.start(h.x,h.y);}return this;},scrollToCenter:function(c,e,d){e=e?$splat(e):["x","y"];c=$(c);var h={},f=c.getPosition(this.element),i=c.getSize(),g=this.element.getScroll(),a=this.element.getSize(),b={x:f.x+i.x,y:f.y+i.y};
["x","y"].each(function(j){if(e.contains(j)){h[j]=f[j]-(a[j]-i[j])/2;}if(h[j]==null){h[j]=g[j];}if(d&&d[j]){h[j]=h[j]+d[j];}},this);if(h.x!=g.x||h.y!=g.y){this.start(h.x,h.y);
}return this;}});
