var currentElevatorLevel = 1;
var elevatorMaxLevels = 0;
var elevatorDoorsEnabled = false;

$(document).ready(function() {
        
        /*
        $('#navHome').click(function(){
            window.location.href = window.location.pathname;
        })
        */

        // ---------------------------------------------
        // Elevator Door FX
        // ---------------------------------------------
        if (elevatorDoorsEnabled) {
            $('.boxContainer').hover(function(){
//                var teaser = $(this).parent();
                var teaser = $(this);

                var width = teaser.outerWidth() / 2;
                teaser.find('.elevatorDoorLeft').css('width',width);
                teaser.find('.elevatorDoorRight').css('width',width);
                teaser.find('.elevatorDoorLeft').animate({'margin-left' : -width},{easing: 'easeOutCirc', queue:false,duration:2000});
                teaser.find('.elevatorDoorRight').animate({'margin-left' : 2*width},{easing: 'easeOutCirc', queue:false,duration:2000});

                //$(this).find('.teaserLinkTextWrapper').stop().fadeTo('normal', 0);
                //$(this).find('.teaserLinkTextWrapper').animate({right : width*2}, {easing: 'easeOutCirc', queue:false, duration: 1000});
            }, function(){
                var teaser = $(this);
                
                var width = teaser.outerWidth() / 2;
                teaser.find('.elevatorDoorLeft').animate({'margin-left' : 0},{easing: 'easeOutCirc',queue:false,duration:2000});
                teaser.find('.elevatorDoorRight').animate({'margin-left' : width},{easing: 'easeOutCirc',queue:false,duration:2000});

                //$(this).find('.teaserLinkTextWrapper').stop().fadeTo('normal', 1);
                //$(this).find('.teaserLinkTextWrapper').animate({right: '-14px'}, {easing: 'easeOutCirc', queue:false, duration: 1000});
            });
        }
        $('.boxcontainer_linkme').click(function(){
            if ($(this).children('.loginBox').length != 0) {
                var href = $(this).find('.teaserBigLinkLayer').attr('href');
                if(href!='') window.location = window.location.protocol + '//' + window.location.host + '/' + href;
            }
        });

        // ---------------------------------------------
        // Main Menu FX
        // ---------------------------------------------
        $('.navButton').hover(function() {
            //$(this).addClass('navButtonActive');

            // show subnav
            $(this).find('.navSub').css('top', $(this).position().top + 30);
            $(this).find('.navSub').css('left', $(this).position().left);
            $(this).css('background', 'url(./assets/img/menu_button_active_bg.gif) repeat-x');
            $(this).find('.navSub').show();

        }, function () {
            //$(this).removeClass('navButtonActive');
            
            // hide subnav
            if ($(this).hasClass('navButtonActive') != true) {
                $(this).css('background', 'url(./assets/img/menu_button_bg.gif) repeat-x');
            }
            $(this).find('.navSub').hide();

        });

        // ---------------------------------------------
        // Main Menu FX End Element
        // ---------------------------------------------
        if( $('#menu').find('.last').hasClass('navButtonActive') ){
            $('#navEnd').css('background', 'url(./assets/img/menu_right_end_hover.png) repeat-x');
        } else {
            $('#menu').find('.last').hover(function() {
                    $('#navEnd').css('background', 'url(./assets/img/menu_right_end_hover.png) repeat-x');
                }, function () {
                    $('#navEnd').css('background', 'url(./assets/img/menu_right_end.png) repeat-x');
                });
        }
        

        // ---------------------------------------------
        // Elevator Sites FX
        // ---------------------------------------------

        $(window).scroll(windowScrollHandler);
        
        jQuery.each($('.elevatorFloor'), function(i, val) {
            var splitResult = $('#' + val.id).attr('id').split("_");
            elevatorMaxLevels = Number(splitResult[1]);
        });


        $('.elevatorNavigationElement').click(function() {

            var splitResult = $(this).attr('id').split("_");
            var selectedLevel = Number(splitResult[1]);
            var scrollFaktor = selectedLevel - 1;

            $('#elevatorCurrentLevelBorder').animate({top: 30 + scrollFaktor * 38},{easing: 'easeOutCirc', queue:false,duration:1000});
            if (selectedLevel == 1) {
                // first one? ok, we scroll to the top of the site.
                $.scrollTo( '0px', 800 );
            } else {
                $.scrollTo( $('#elevatorFloor_'+selectedLevel), 800 );
            }

            currentElevatorLevel = selectedLevel;
            

        });

        // elevator nav up click
        $('#elevatorControlUp').click(function() {

            if (currentElevatorLevel == 1) return false;

            if (currentElevatorLevel == 2) {
                // the next upwards level is the first one. so we can scroll to the almost top of the site
                scrollElevatorControlFrame("up");
                $.scrollTo( '0px', 800 );
                currentElevatorLevel--;

            } else {
                // regurlar updwards scrolling to the next level
                scrollElevatorControlFrame("up");
                currentElevatorLevel--;
                $.scrollTo( $('#elevatorFloor_'+currentElevatorLevel), 800 );
            }
            return true;
		   
        });

        // elevator nav down click
        $('#elevatorControlDown').click(function() {
            // check if we are already in basement
            if ($('#elevatorFloor_' + (currentElevatorLevel + 1)).length > 0) {
                currentElevatorLevel++;
                scrollElevatorControlFrame("down");
                $.scrollTo( $('#elevatorFloor_'+currentElevatorLevel), 800 );
            }
        });

        function scrollElevatorControlFrame(direction) {


            if (direction == "up") {
                $('#elevatorCurrentLevelBorder').animate({top: '-=38'},{easing: 'easeOutCirc', queue:false,duration:1000});
            } else {
                $('#elevatorCurrentLevelBorder').animate({top: '+=38'},{easing: 'easeOutCirc', queue:false,duration:1000});
            }

        }

        function windowScrollHandler() {

            if ($(':animated').length) {
                return false;
            }

            var formerElevatorLevel = currentElevatorLevel;
            currentElevatorLevel = 1;

            // run through all elevatorFloor elements to set currentElevatorLevel
            jQuery.each($('.elevatorFloor'), function(i, val) {
                    var floorOffset = $('#' + val.id).offset();
                    // if element top is over the window scroll top, the elevatorLevel goes up
                    if (floorOffset.top < $(window).scrollTop() && currentElevatorLevel < elevatorMaxLevels ) {
                            currentElevatorLevel++;
                    }

            });

            if (formerElevatorLevel != currentElevatorLevel) {
                var scrollFaktor = currentElevatorLevel - 1;
                // $('#elevatorCurrentLevelBorder').animate({top: 30 + scrollFaktor * 38},{easing: 'easeOutCirc', queue:false,duration:1000});
                $('#elevatorCurrentLevelBorder').css({top: 30 + scrollFaktor * 38});
            }

            //$('#elevatorControlUp').html(currentElevatorLevel);
            //$('#elevatorControlDown').html(elevatorMaxLevels);
            return true;

        }

        // ---------------------------------------------
        // Gallery stuff
        // ---------------------------------------------
        
        var galleryItems = new Array();
        $.each($("a[rel^='galleryItem']"), function(i, val) {
           galleryItems[i] = $("#" + this.id).attr("rel");
        });
        var uniqueGalleryItems = $.unique(galleryItems);
        $.each(uniqueGalleryItems, function(i, val) {
            $("a[rel='" + val + "']").colorbox({
                title: function(){
                    jQuery('#cboxCurrent').hide().remove();
                },
                onComplete: function(){
                    //set alt tag
                    $('#cboxPhoto').attr("alt",$('#cboxTitle').html());

                    //set dl buttons
                    var hasDownloadClass = '';
                    var downloadUrl = 'noModel';
                    downloadUrl = jQuery(this).attr('name');
                    if(downloadUrl.substring(downloadUrl.length - 7) != 'noModel'){
                        hasDownloadClass = '.downloadURL, '
                        var newButtons = '<a class="downloadURL" href="'+downloadUrl+'">download</a>';
                    }

                    $('#cboxContent').append(newButtons);
                }
            });
        });
        
        // ---------------------------------------------
        // Layout Korrekturen
        // ---------------------------------------------
        $("#contentBoxLeft > .teaserBoxSmall:even").css("margin-right", "2px");

        // Rasterverlauf an Seitenhoehe anpassen.
        var raster_verlauf_start = $("#layoutContainer").height() - $("#raster_verlauf").height();
        $("#raster_verlauf").css("top", raster_verlauf_start-(raster_verlauf_start % 3)+3);

        // ---------------------------------------------
        // Formular Pflichtfeldabhaengigkeit
        // ---------------------------------------------

        // Email wird Pflichtfeld im Katalogform, wenn Newsletter gewollt wird
        if ($("#orderCatalogue #receiveNewsletter").is(':checked')) {
            $("#emailRequiredMark").show();
        } 
        $("#orderCatalogue #receiveNewsletter").change(function() {
           if ($(this).is(':checked')) {
               $("#emailRequiredMark").show();
           } else {
               $("#emailRequiredMark").hide();
           }
        });

});


