$(document).ready(function() {



    jQuery.each(
        jQuery.browser, function(i){
        if($.browser.msie  && (jQuery.browser.version.substr(0,3)=="6.0"))
        {
            $("a.lightbox").attr
            (
                {
                    target: '_blank'
                }
            );
        }
        else
        {
            $("a.lightbox").fancybox
            (
                {
                'zoomSpeedIn': 300,
                'zoomSpeedOut': 300,
                'overlayShow': true
                }
            );
        }
    }
    );

    $('#navi-main > li').not('.last').each
    (
        function()
        {
            $(this).css('margin-right','0');
            w = $(this).width()+60+'px';
            $(this).css('width',w);
        }
    );

    footerBillitonWidth = $('#footer-billiton p').width();
    $('#footer-billiton p').css
    (
        {
            'width': footerBillitonWidth,
            'display': 'block',
            'margin': '0 auto'
        }
    );

    /*bilder aus onmouseover/onmouseout extrahieren und einfuegen*/
    $('.teaser-title img').each(
        function()
        {
            imgSrc = $(this).parent().html().split('\'');
            $(this).hide();

            imgAct = $('<img>').attr(
                {
                    src: imgSrc[1],
                    title: '',
                    alt: '',
                    'class': 'teaser-title-act'
                }
            );
            imgNo = $('<img>').attr(
                {
                    src: imgSrc[3],
                    title: '',
                    alt: '',
                    'class': 'teaser-title-no'
                }
            );
            $(imgNo).insertAfter($(this));
            $(imgAct).insertAfter($(this));
        }
    );

    //alle slider ausser dem ersten einklappen & hintergrund fuer .teaser-frame setzen
    $('.teaser-entry').not('.teaser-entry:first').addClass('no');
    $('.teaser-entry:first').addClass('act');


    $('.teaser').kwicks
    (
        {
            // 8 elements:
            // max : 402,
            // 9 elements:
            max : 407,
            sticky : true,
            duration: 500,
            easing: 'jswing'
        }
    );



    $('.teaser-entry').hover
    (
        function()
        {
            $('.teaser-entry').removeClass('act').addClass('no');
            $(this).addClass('act').removeClass('no');
        }
    );


    $('#navi-belly #callback h2')
    .click
    (
        function()
        {
            $('#navi-belly').toggleClass('hovered');
        }
    )
    .hover
    (
        function()
        {
            $(this).toggleClass('hovered');
        },
        function()
        {
            $(this).toggleClass('hovered');
        }
    );

    if ($('#callback .tx-pilmailform-pi1').children().is('#thankyou')) {
         $('#navi-belly').toggleClass('hovered');
    }


    /*navi-belly*/
    firmaVal = 'Unternehmen...';
    nameVal = 'Name...';
    telVal = 'Tel.Nr. (inkl. Vorwahl)...';
    errors = '';

    $('#callback #firma').val(firmaVal);
    $('#callback #name').val(nameVal);
    $('#callback #tel').val(telVal);

    $('#callback .mailform input').focus(function() { $(this).val(''); });

    divAlert = $('<div>').attr(
        {
            id: 'alert',
            title: 'Bitte prüfen Sie Ihre Eingabe',
            style: 'display: none;'
        }
    );

    $("#alert")
    .dialog
    (
        {
            bgiframe: true,
            modal: true,
            width: 552,
            autoOpen: false,
            buttons:
            {
                OK: function()
                {
                    $(this).dialog('close');
                }
            }
        }
    );
    $('#content').append(divAlert);

    $('#callback form')
    .bind
    (
        'submit', function(event)
        {
            if ( ($('#callback #firma').val() == '') || ($('#callback #firma').val() == firmaVal) )
            {
                errors += '<li>* Unternehmen eintragen.</li>' + "\n";
            }

            if ( ($('#callback #name').val() == '') || ($('#callback #name').val() == nameVal) )
            {
                errors += '<li>* Namen eintragen</li>' + "\n";
            }

            if ( ($('#callback #tel').val() == '') || ($('#callback #tel').val() == telVal) || ($('#callback #tel').val().match(/^[\d\-\+()\/\s]+$/) == null) )
            {
                errors += '<li>* Telefonnummer eintragen</li>';
            }

            if (errors != '')
            {
                $('#alert').html('<ul>' + errors + '</ul>');

                var dialog = $('#alert').data('alertDialog');
                if (!dialog)
                {
                    dialog = $('#alert').dialog({autoOpen: false});
                    $('#alert').data('alertDialog', dialog);
                }
                dialog.dialog('open');
                errors = '';

                return false;
            }
        }
    );


    $('#search-input').focus(function() { $(this).val(''); });

    $('#search-form')
    .bind
    (
        'submit', function(event)
        {
            if(  ($('#search-input').val() == 'Suchen...') || ($('#search-input').val() == '') )
            {
                return false;
            }
        }
    );

    /*
    $('.teaser-entry').hover
    (
        function()
        {
            $('.teaser-entry').removeClass('act').addClass('no');
            $(this).addClass('act').removeClass('no');

            $('.teaser-entry').not($(this)).animate
            (
                {
                    width:"80px"
                },
                {
                    queue:false,
                    duration:1000,
                    easing: 'jswing'
                }
            );
            $(this).animate
            (
                {
                    width:"447px"
                },
                {
                    queue:false,
                    duration:1000,
                    easing: 'jswing'
                }
            );
        }
    );
    */

    //$('.btn-browse-page-back, .btn-browse-page-fwd').parent().bind('click dblclick', centerThumbs);

    // hide all bubbletext
    //$('.bubble .csc-texttext').hide();

    // 1. bild ist trigger (2. kind wegen <a>)
    $('.cols-2-col .csc-textpic:nth-child(2) .csc-textpic-imagewrap').addClass('js-trigger');

    // mit js html bearbeiten
    // spans für ecken einfügen
    // sauberer wäre die Abfrage, ob ein a-Tag um das bild ist
    $('.cols-2-col').addClass('tooltip-container clearfix').find('dt').prepend('<span class="corners corners-lt"></span><span class="corners corners-rt"></span><span class="corners corners-rb"></span><span class="corners corners-lb"></span>');
    //$('.cols-2-col .csc-textpic dl:hidden').addClass('js-popup');

    //$('.cols-2-col .csc-textpic dl:hidden').closest('')
    //$('.cols-2-col .csc-textpic dl:hidden').clone().prependTo(popup);


    // padding-top wegen #content{overflow: hidden;}
    $('.cols-2-row:first').css('padding-top','10px');

    // stacking and z-index
    if($.browser.msie) {
        $('.cols-2-col').hover(
            function(){
                $(this).css('z-index', 10)
            },
            function(){
                $(this).css('z-index', 0)
            }
        );
    }

    $('.cols-2-col').each(function () {
        // options
        var distance = 10;
        var time = 250;
        var hideDelay = 500;

        var hideDelayTimer = null;


        // tracker
        var beingShown = false;
        var shown = false;


        var trigger = $('.js-trigger', this);

        $('dl:hidden').addClass('js-popup');
        //großes Bild raussuchen und an die andere Stelle kopieren
        //$('dl:hidden', this).remove().appendTo(trigger).hide().addClass('js-popup');

        var popup = $('.js-popup', this).css('opacity', 0);
        //popup.css('display', 'block');


        // kopiere Überschrift
        //trigger.clone().prependTo(popup).before('<div class="wrap" />').removeClass('js-pfeil');

        // set the mouseover and mouseout on both element
        $([trigger.get(0), popup.get(0)]).mouseover(function () {
          // stops the hide event if we move from the trigger to the popup element
          if (hideDelayTimer) clearTimeout(hideDelayTimer);

          // don't trigger the animation again if we're being shown, or already visible
          if (beingShown || shown) {
            return;
          } else {
            beingShown = true;

            popup.addClass('shown');
            var popupHeight = popup.height();

            // reset position of popup box
            popup.css({
              top: '-10px',
              left: '95px',
              height: popupHeight + 'px',
              display: 'block' // brings the popup back in to view
            });

            // some browsers are more equal than others and align from the bottom
            /*
            if($.browser.msie || $.browser.safari){
              popup.css({
                marginTop: '-' + popupHeight + 'px'
              });
            }
              */
            // (we're using chaining on the popup) now animate it's opacity and position
            popup.animate({
              top: '+=' + distance + 'px',
              opacity: 1
            }, time, 'swing', function() {
              // once the animation is complete, set the tracker variables
              beingShown = false;
              shown = true;
            });
          }
        }).mouseout(function () {
          // reset the timer if we get fired again - avoids double animations
          if (hideDelayTimer) clearTimeout(hideDelayTimer);

          // store the timer so that it can be cleared in the mouseover if required
          hideDelayTimer = setTimeout(function () {
            hideDelayTimer = null;
            popup.animate({
              top: '-=' + distance + 'px',
              opacity: 0
            }, time, 'swing', function () {
              // once the animate is complete, set the tracker variables
              shown = false;
              // hide the popup entirely after the effect (opacity alone doesn't do the job)
              popup.css({
                  display: 'none'
              });
              popup.removeClass('shown');


            });
          }, hideDelay);
        });
    });

    $('#navi-belly #callback').show();

    // Pagebrowser News Listenansicht
    if ($('.browseLinks') != '') {
        $(".pagebrowser-top .browseLinks-inner li:first, .pagebrowser-top .browseLinks-inner li:last").remove();
        $(".pagebrowser-bottom .browseLinks-inner li:first, .pagebrowser-bottom .browseLinks-inner li:last").remove();

        $(".pagebrowser-top .browseLinks-inner li:first, .pagebrowser-bottom .browseLinks-inner li:first").addClass("prev");
        $(".pagebrowser-top .browseLinks-inner li:last, .pagebrowser-bottom .browseLinks-inner li:last").addClass("next");
    }
});
