$(function () {

    // Bilder, die vorab geladen werden müssen
    var images = [ '/img/linie_oben.png', '/img/bg/startseite.jpg', 'img/home/1_1.png', 'img/home/1_2.png', 'img/home/1_3.png',
        'img/home/1_4.png', 'img/home/1_5.png', 'img/home/2_1.png',
        'img/home/2_2.png', 'img/home/2_3.png', 'img/home/2_4.png',
        'img/home/2_5.png', 'img/home/3_1.png', 'img/home/3_2.png',
        'img/home/3_3.png', 'img/home/3_4.png', 'img/home/3_5.png' ];


    // Warte 250ms vor dem Vorladen der Bilder.
    // Anschließend Starte das Einblenden der Bilder
    setTimeout(function () {
        $.preLoadImages(images, function () {
            fadeAllIn();
        });
    }, 250);

    // Zufälliges Einblenden der Bilder
    // mit anschließendem Einblenden des Slogans
    // Wartezeit vorher: 1 Sekunde
    // Laufzeit: 1 Sekunde
    function fadeAllIn() {
        setTimeout(
            function () {
                var v = $("div.home div.content img.small").css(
                    'visibility', 'hidden'), cur = 0;
                for (var j, x, i = v.length; i; j = parseInt(Math.random()
                    * i), x = v[--i], v[i] = v[j], v[j] = x)
                    ;
                function fadeInNextLI() {
                    v.eq(cur++).css('visibility', 'visible').hide()
                        .fadeIn();
                    if (cur != v.length)
                        setTimeout(fadeInNextLI, 50);
                }

                fadeInNextLI();

                // Slogan einblenden
                setTimeout(fadeInMainSlogan, 800);

                function fadeInMainSlogan() {
                    $("div.home div.content img.slogan").css('visibility',
                        'visible').hide().fadeIn();
                }

                // Hauptseite einblenden
                setTimeout(showMainContent, 1200);


            }, 1000);
    }

    // Übergang zur normalen Seite
    // Wartezeit vorher: 3 Sekunden
    // Laufzeit: 1 Sekunden
    function showMainContent() {
        setTimeout(function () {

            // Bilder ausblenden
            setTimeout(function () {
                $('div.home div.content').fadeOut();
            }, 100);

            // Container ausblenden
            setTimeout(function () {
                $('div.home').fadeOut();
            }, 500);

            $('div.main_content').load('content/home.html', function () {
                scrollPane();
            });

            // Hauptcontainer einblenden
            setTimeout(function () {
                $('div.main_content').css('visibility', 'visible').hide().fadeIn();
            }, 1000);

            // Menü oben links einblenden
            setTimeout(function () {
                $('div.submenu').css('visibility', 'visible').hide().fadeIn();
            }, 1500);

            // Menü unten links einblenden            	 
            setTimeout(function () {
                $('div.teaser').css('visibility', 'visible').hide().fadeIn();
            }, 2000);

        }, 3000)
    }

    /*
     $('div.teaser ul li').mouseover(function() {
     var source = $(this).find('img').attr('src');
     $(this).find('img').attr('src', source.replace(/.png/g, '_active.png'));
     });

     $('div.teaser ul li').mouseout(function() {
     var source = $(this).find('img').attr('src');
     $(this).find('img').attr('src', source.replace(/_active.png/g, '.png'));
     });
     */


    // Standorte Hover
    $('.standorte_hover span').live('mouseover', function () {
        var id = $(this).attr('id');

        jQuery('<div/>', {
            id:'hover_info'
        }).appendTo('div.standorte_hover');

        $('#hover_info').width(190);
        //$('#hover_info').height(110);
        $('#hover_info').css('position', 'absolute');

        var left = parseInt($(this).css('left').replace(/px/g, '')) + 20;
        var top = parseInt($(this).css('top').replace(/px/g, '')) + 20;

        $('#hover_info').css('left', left + 'px');
        $('#hover_info').css('top', top + 'px');
        $('#hover_info').css('border', '1px solid #fff');
        $('#hover_info').css('padding', '10px');
        $('#hover_info').addClass('black_window');

        var text = '<b>FCI Facility Concept GmbH</b><br />';

        switch (id) {
            case 'willich':
                text += 'Hans-Böckler-Str. 16<br />47877 Willich<br />Fon: +49 2154 4812-200<br />Fax: +49 2154 4812-499';
                break;
            case 'hannover':
                text += 'Podbielskistraße 164 UG<br />30177 Hannover<br />Fon: +49 511 640749910<br />Fax: +49 511 640749919';
                break;
            case 'guetersloh':
                text += 'Bonhoefferstraße 1<br />33332 Gütersloh<br />Fon: +49 5241 905122<br />Fax: +49 5241 905284';
                break;
            case 'hamburg':
                text += 'Grüner Deich 15<br />20097 Hamburg<br />Fon: +49 40 2385497710<br />Fon: +49 40 2385497719';
                break;
            case 'muenchen':
                text += 'Dietlindenstraße 15<br />80802 München<br />Fon: +49 89 36036755<br />Fax: +49 89 36036700';
                break;
            case 'karlsruhe':
                //text += 'Marienstraße 28<br />76137 Karlsruhe<br />Fon: +49 721 56841050<br />Fax: +49 721 56841052';
                text += 'Werner-von-Siemens-Str. 2-6<br />76646 Bruchsal<br />Fon: +49 7251 505170<br />Fax: +49 7251 5051729';
                break;
            case 'duesseldorf':
                text += 'Fritz-Vomfelde Straße 26<br />40547 Düsseldorf<br />Fon: +49 211 53819254<br />Fax: +49 211 53819255';
                break;
            case 'berlin':
                text += 'Friedrichstraße 153 a<br />10117 Berlin<br />Fon: +49 30 22002323';
                break;
            case 'dresden':
                text += 'Fon: +49 351 21760360<br />Fax: +49 351 21760361';
                break;
        }

        $('#hover_info').html(text);

    });

    $('.standorte_hover span').live('mouseout', function () {
        $('#hover_info').remove();
    });

});

// Content nachladen
function loadContent(page, menu) {

    // Alle Menüpunkte auf inaktiv setzen
    $('div.main_menu ul li').removeClass('active');
    $('div.submenu ul li').removeClass('active');
    $('div.teaser ul li').removeClass('active');

    switch (menu) {
        case 'mainmenu':
            $('div.main_menu ul li#mainmenu_' + page).addClass('active');
            break;
        case 'submenu':
            $('div.submenu ul li#submenu_' + page).addClass('active');
            break;
        case 'teaser':
            $('div.teaser ul li#teaser_' + page).addClass('active');
            break;

    }

    var height = 590;

    switch (page) {
        case 'home':
            height = 452;
            break;
        case 'standorte':
            height = 446;
            break;
        case 'ssn_group':
            height = 424;
            break;
        case 'kontakt':
            height = 340;
            break;
        case 'fmservice':
            height = 340;
            break;
    }

    $('div.main_content').fadeOut(400, function () {
        $('div.main_content').load('content/' + page + '.html', function () {

            $('.main_content').height(height);
            $('.main_content .text').height(height - 100);
            $('.main_content .text .longtext').height(height - 102);

            $('div.main_content').stop().fadeIn(400, function () {
                scrollPane();
            });
        });
    });
}

// automatischer Scrollbalken für Content-Container
function scrollPane() {
    $('.longtext').jScrollPane({
        showArrows:true,
        horizontalGutter:30,
        verticalGutter:30
    });
}
