var HomePage = {
        homeSliderInterval: null,

        init: function () {
            HomePage.initHomeSliderInterval();
            $('div.butn').click(HomePage.butnClicked);
            $('#bannerDropDownBox').click(HomePage.xpandmeOnClick);
            $('body').not('#bannerDropDownBox').click(function(e){
                if(e.target.id!="bannerDropDownBox" && e.target.nodeName !="A"){
                    HomePage.xpandmeSlideUp();
                }
            });
        },

        initHomeSliderInterval: function() {
            HomePage.homeSliderInterval = window.setInterval("HomePage.homeSlider()", 4000);
        },

        resetHomeSliderInterval: function () {
            window.clearInterval(HomePage.homeSliderInterval);
        },

        homeSlider: function (){
            if($('#homeSlider li.selected').nextAll().length < 1) {
                var next = $('#homeSlider li:first');
            } else {
                next = $('#homeSlider li.selected').next();
            }

            HomePage.homeSliderFadeTo(next.attr("id").substr(5));
        },

        butnClicked: function() {
            HomePage.homeSliderFadeTo(this.id.substr(4));
        },

        xpandmeOnClick: function() {
            if($("#bannerExpandable :visible").length >0){
                HomePage.xpandmeSlideUp();
            }else{
                HomePage.xpandmeSlideDown();
            }
        },

        xpandmeSlideUp: function() {
            $("#bannerExpandable").stop().slideUp(200);
        },

        xpandmeSlideDown: function() {
             $("#bannerExpandable").stop()
                 .css({'display': 'none', 'height':'auto', 'overflow':'visible'})
                 .slideDown(200);
        },

        homeSliderFadeTo: function (slideNumber) {
            HomePage.resetHomeSliderInterval();

            if($('#homeSlider li img:animated').length>0) {
                return;
            } else {
                $('div.butn').removeClass('butnactive');
                $('#butn'+slideNumber).addClass('butnactive');
                $('#homeSlider li.selected img').fadeOut(500, HomePage.homeSliderFadeoutCompleted(slideNumber));
            }
        },

        homeSliderFadeoutCompleted: function(slideNumber) {
            $("#slide"+slideNumber+" img").fadeIn(500, function() {
                    $("#slide"+slideNumber).addClass('selected');
                    HomePage.initHomeSliderInterval();
            });

            $("#homeSlider li.selected").removeClass('selected');
        }
};

var WwPage = {
        init: function() {
            $("#wwAccordion").accordion({autoHeight: false});
        }
};

// uses a global RundgangTabs object defined in rundgang.ctp
var RundgangPage = {
    currentTab: 0,
    init: function() {
        RundgangPage.fadeInText();
        RundgangPage.fadeInPicture();
        $('#rundgang button.prev').bind('click', RundgangPage.prevClicked);
        $('#rundgang button.next').bind('click', RundgangPage.nextClicked);

        // preload images
        images = new Array();
        for(key in RundgangTabs) {
            images.push(RundgangTabs[key]['img']);

        }

        ImagePreloader.load(images);
    },
    prevClicked: function() {
        RundgangPage.currentTab = RundgangPage.currentTab-1;

        if(RundgangPage.currentTab<0) {
            RundgangPage.currentTab = RundgangTabs.length-1;
        }

        RundgangPage.changeTab();
    },
    nextClicked: function() {
        RundgangPage.currentTab = RundgangPage.currentTab+1;

        if(RundgangPage.currentTab>=RundgangTabs.length) {
            RundgangPage.currentTab = 0;
        }

        RundgangPage.changeTab();
    },
    changeTab: function() {
        $('#rundgang img').stop().fadeOut(300, RundgangPage.fadeInPicture);
        $('#rundgang span').stop().fadeOut(300, RundgangPage.fadeInText);
    },
    fadeInText: function () {
        $('#rundgang span').html(RundgangTabs[RundgangPage.currentTab]['text'])
            .css('opacity',0).css('display','inline').animate({'opacity':1},300);
    },
    fadeInPicture: function () {
        $('#rundgang img').attr('src', RundgangTabs[RundgangPage.currentTab]['img'])
            .css('opacity',0).css('display','inline').animate({'opacity':1},300);
    }
};

var FirmengruppePage = {
        init: function() {
            $('map area').bind('mouseenter', FirmengruppePage.mapOver);
            $('body').bind('mousemove',function(e) {
            	if( !$('#firmengruppeWeltkarte').hasClass('locked')
            		&& !$(e.target).hasClass('overlay')
            		&& !$(e.target).parent().hasClass('detailMap')
            		&& !$(e.target).hasClass('detailMap')
            		&& !$(e.target).parent().attr('name') != 'weltkarte') {
            		$('.overlay').hide();
            		$('.detailMap').hide();
            	}
            });
        },
        mapOver: function(ev) {
        	if($(ev.target).parent().attr('name') == 'weltkarte') {
	            var t = ev.target.hash.substr(1);
	            $('#'+t+'Details').css('display','block');
	            $('#'+t+'DetailsMap').css('display','block');
        	}
        	else if($(this).parent().hasClass('detailMap')) {
        		var t = $(this).parent().attr('id').substr(0,$(this).parent().attr('id').length-10);
        		$('#'+t+'Details').css('display','block');
        		$('#'+t+'DetailsMap').css('display','block');
        	}
        	$('#firmengruppeWeltkarte').addClass('locked');
        	window.setTimeout(function() {
        		$('#firmengruppeWeltkarte').removeClass('locked');
        	}, 200);
        },
        overlayLeft: function(ev) {
        		
        }
};

/*
 * Wird im Page Layout "tabbed" verwendet und in der Spezifikation als "Ablauf" bezeichnet.
 */
var TabheaderController = {
    currentTab: 0,
    count: null,
    itemWidth: 37,
    maxItems: 9,
    init: function() {
        TabheaderController.count = $('#tabHeader .tabcontent').length;
        $('#tabHeader .tabBox').css('width',TabheaderController.count>TabheaderController.maxItems?TabheaderController.maxItems*37-2:TabheaderController.count*37-2);

        var tabs = $('#tabHeader .tab span');

        // evil history fix
        if($('#tabHeader').attr('class')=='document-37') {
            TabheaderController.currentTab = $('#tabHeader .tab span').length-1;
        } else {
            TabheaderController.currentTab = tabs.eq(0).html()==''?1:0;
        }

        TabheaderController.fadeInContent();
        TabheaderController.fadeInPicture();
        $('#tabHeader .tab').not('.editable').bind('click', this.tabClicked);
        TabheaderController.checkButtons();
        $('#tabHeader div.prev').bind('click', this.prevClicked);
        $('#tabHeader div.next').bind('click', this.nextClicked);

        TabheaderController.scrollPosition = 0;
        $('#tabHeader .tabBox').attr('scrollLeft',1);

        if(TabheaderController.currentTab > 1) {
            while(TabheaderController.scrollRight(null, 0)>0) {}
        }

        TabheaderController.checkArrows();
        $('#tabHeader .bigArrowLeftIcon').bind('click', TabheaderController.scrollLeft);
        $('#tabHeader .bigArrowRightIcon').bind('click', TabheaderController.scrollRight);
    },
    checkArrows: function() {
        if($('#tabHeader .tabBox').attr('scrollLeft')==1) {
            $('#tabHeader .bigArrowLeftIcon').css('display','none');
        } else {
            $('#tabHeader .bigArrowLeftIcon').css('display','inline-block');
        }

        if($('#tabHeader .tabBox').attr('scrollLeft')+$('#tabHeader .tabBox').width()<=$('#tabHeader .tab:last').attr('offsetLeft')) {
            $('#tabHeader .bigArrowRightIcon').css('display','inline-block');
        } else {
            $('#tabHeader .bigArrowRightIcon').css('display','none');
        }
    },
    scrollLeft: function(ev) {
        var items = 3;

        if(TabheaderController.scrollPosition - items < 0) {
            items = TabheaderController.scrollPosition;
        }
        TabheaderController.scrollPosition -= items;

        $('#tabHeader .tabBox').scrollTo('-='+TabheaderController.itemWidth*items+'px',600, {easing:'swing', onAfter:TabheaderController.checkArrows,axis:'x'});
        return items;
    },
    scrollRight: function(ev, duration) {
        duration = duration === undefined ? 600 : duration;
        var items = 3;

        if(TabheaderController.scrollPosition + items > TabheaderController.count - TabheaderController.maxItems) {
            items = TabheaderController.count - TabheaderController.scrollPosition -TabheaderController.maxItems;
        }
        TabheaderController.scrollPosition += items;

        $('#tabHeader .tabBox').scrollTo('+='+TabheaderController.itemWidth*items+'px', duration, {easing:'swing', onAfter:TabheaderController.checkArrows, axis:'x'});
        return items;
    },
    checkButtons: function(){
        if(TabheaderController.currentTab <= 0)
            $('#tabHeader div.prev').addClass('inactive');
        else
            $('#tabHeader div.prev').removeClass('inactive');

        if(TabheaderController.currentTab >= TabheaderController.count-1)
            $('#tabHeader div.next').addClass('inactive');
        else
            $('#tabHeader div.next').removeClass('inactive');
    },
    tabClicked: function(ev) {
        var key = $(ev.target).attr('id').match(/[0-9]+/);
        TabheaderController.currentTab = key;
        TabheaderController.checkButtons();
        TabheaderController.changeTab();
    },
    prevClicked: function() {
        if(TabheaderController.currentTab > 0){
            TabheaderController.currentTab--;

            if($('#tabHeader .tabBox .tab').eq(TabheaderController.currentTab).attr('offsetLeft')<$('#tabHeader .tabBox').attr('scrollLeft')) {
                TabheaderController.scrollLeft();
            }

            TabheaderController.checkButtons();
            TabheaderController.changeTab();
        }
    },
    nextClicked: function() {
        if(parseInt(TabheaderController.currentTab)+1 < TabheaderController.count){
            TabheaderController.currentTab++;

            if($('#tabHeader .tabBox .tab').eq(TabheaderController.currentTab).attr('offsetLeft')>$('#tabHeader .tabBox').attr('scrollLeft')+parseInt($('#tabHeader .tabBox').css('width'))) {
                TabheaderController.scrollRight();
            }

            TabheaderController.checkButtons();
            TabheaderController.changeTab();
        }
    },
    changeTab: function() {
        $('#tabHeader .tabBox .tab').removeClass('active');
        $('#tabHeader div.hide:visible').stop().fadeOut(300, this.fadeInPicture);

        if($('#tabHeader .tabcontent:visible').length>0) {
            if(jQuery.support.opacity) {
                $('#tabHeader .tabcontent:visible').stop().fadeOut(300, this.fadeInContent);
            } else {
                if(TabheaderController.ctTimeout) {
                    clearTimeout(TabheaderController.ctTimeout);
                }
                TabheaderController.ctTimeout = setTimeout("$('#tabHeader .tabcontent:visible').css('display','none');TabheaderController.fadeInContent();",300);
            }
        } else {
            this.fadeInContent();
        }
    },
    fadeInContent: function() {
        $('#tabHeader .tabBox .tab').eq(TabheaderController.currentTab).addClass('active');

        if(jQuery.support.opacity) {
            $('#tabcontent-'+TabheaderController.currentTab).css('opacity',0).css('display','block').animate({'opacity':1},
                        300, null, function() {
                    $(this).css('filter','');
            });
        } else {
            $('#tabcontent-'+TabheaderController.currentTab).css('display','block');
        }
    },
    fadeInPicture: function() {
        $('#tabimg-'+TabheaderController.currentTab).css('opacity',0).css('display','inline').animate({'opacity':1},300,
                null, function() {
            $(this).css('filter','');
    });
    }
};

var ImagePreloader = {
        load: function (images) {
            for(var i=0; i<images.length; i++) {
                imageObj = $('<img src="'+images[i]+'">');
                $('#preloading').append(imageObj);
            }
       }
};

var BottlepackPage = {
        currentType: 0,
        init: function() {
            for(k in machinetypes) {
                $("#maschinentypen ul").append('<li id="machinetype-'+k+'">'+machinetypes[k]['label']+'</li>');
            }

            $('#maschinentypen li').bind('click', BottlepackPage.typeClicked);
            this.fadeInPicture();
            BottlepackPage.itemWidth = $('#maschinentypen ul li').eq(0).width()
                +parseInt($('#maschinentypen ul li').eq(0).css('padding-left'))
                +parseInt($('#maschinentypen ul li').eq(0).css('margin-right'));

            BottlepackPage.numberOfItems = $('#maschinentypen ul li').length;
            BottlepackPage.scrollPosition = 0;

            BottlepackPage.checkArrows();
            $('#maschinentypen .bigArrowLeftIcon').bind('click', BottlepackPage.scrollLeft);
            $('#maschinentypen .bigArrowRightIcon').bind('click', BottlepackPage.scrollRight);

            // preload images
            images = new Array();
            for(key in machinetypes) {
                images.push(machinetypes[key]['img']);
            }

            ImagePreloader.load(images);
        },
        checkArrows: function() {
            if($('#maschinentypen .wrapper').attr('scrollLeft')==0) {
                $('#maschinentypen .bigArrowLeftIcon').css('display','none');
            } else {
                $('#maschinentypen .bigArrowLeftIcon').css('display','inline-block');
            }
            if($('#maschinentypen .wrapper').attr('scrollLeft')+$('#maschinentypen .wrapper').width()<=$('#maschinentypen li:last').attr('offsetLeft')) {
                $('#maschinentypen .bigArrowRightIcon').css('display','inline-block');
            } else {
                $('#maschinentypen .bigArrowRightIcon').css('display','none');
            }
        },
        scrollLeft: function(ev) {
            var items = 3;
            if(BottlepackPage.scrollPosition - items < 0) {
                items = BottlepackPage.scrollPosition;
            }
            BottlepackPage.scrollPosition -= items;

            $('#maschinentypen .wrapper').scrollTo('-='+BottlepackPage.itemWidth*items+'px',600, {easing:'swing', onAfter:BottlepackPage.checkArrows});
        },
        scrollRight: function(ev) {
            var items = 3;
            if(BottlepackPage.scrollPosition + items > BottlepackPage.numberOfItems - 5 ) {
                items = BottlepackPage.numberOfItems - BottlepackPage.scrollPosition -5;
            }
            BottlepackPage.scrollPosition += items;

            $('#maschinentypen .wrapper').scrollTo('+='+BottlepackPage.itemWidth*items+'px',600, {easing:'swing', onAfter:BottlepackPage.checkArrows});
        },
        typeClicked: function(ev) {
            var id = $(ev.target).attr('id');
            var index = id.substr(12);
            BottlepackPage.currentType = index;
            BottlepackPage.changeType();
        },
        changeType: function() {
            $('#maschinentypen li').removeClass("active");
            $('#maschinentypen img').stop().fadeOut(300, BottlepackPage.fadeInPicture);
        },
        fadeInPicture: function () {
            $('#machinetype-'+BottlepackPage.currentType).addClass("active");
            $('#maschinentypen img').attr('src', machinetypes[BottlepackPage.currentType]['img'])
                .css('opacity',0).css('display','block').animate({'opacity':1},300);
        }
};


$(document).ready(function(){
    if($('#homeSlider').length > 0) {
        HomePage.init();
    }

    if($('#wwAccordion').length > 0) {
        WwPage.init();
    }

    if($('#rundgang').length > 0) {
        RundgangPage.init();
    }

    if($('#firmengruppeWeltkarte').length > 0) {
        FirmengruppePage.init();
    }

    if($('#tabHeader').length > 0) {
        TabheaderController.init();
    }

    if($('#maschinentypen').length > 0) {
        BottlepackPage.init();
    }
});
function UnCryptMailto(s) {
	var n=0;
	var r="";
	for(var i=0; i < s.length; i++) {
		n=s.charCodeAt(i);
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(1));
	}
	return r;
 }
 function linkTo_UnCryptMailto(s)	{
	location.href=UnCryptMailto(s);
 }
