jQuery(document).ready(function($) {

    // Liste für interne Links in Spalten aufteilen
    if($('.csc-menu-def').length > 0)
    {
        //$('.csc-menu-def').columnizeList({ cols:2, constrainWidth:1 });
        $('.csc-menu-def').makeacolumnlists({cols:2,colWidth:0,equalHeight:true,startN:1});
    }
        
    // initialize fancybox
    $("a.lightbox").fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true
    });

    // empty input fields on click
    $('#search-input').focus(function () {
        $(this).attr('value','');
    });

    // align Heights of newsboxes on homepage
    $('.twocol .news-latest-container .news-latest-item').alignHeights();
    
    // Tooltip für Kontakt national
    $('#content-text .tooltip').css({'display':'none'});
    $('.tx-billitonemployeesearch-pi1 .contact-person .image a').bind('click', function(){
        $('#content-text .tx-billitonemployeesearch-pi1 .contact-person .image a').each(function() {
            $(this).qtip("hide");
        });
        return false;
    });
    
    $('#content-text .tx-billitonemployeesearch-pi1 .contact-person .image a').each(function() {
        $(this).qtip({
            content: $(this).parent().next('.tooltip').html(),
            prerender: true,
            position: {
                corner: {
                    target: 'topMiddle',
                    tooltip: 'bottomMiddle'
                }
            },
            style: {
                width: 200,
                padding: 7,
                background: '#6a6666',
                color: '#fff',
                textAlign: 'left',
                border: {
                    width: 2,
                    radius: 5,
                    color: '#0f4b94'
                },
                tip: 'bottomMiddle',
                name: 'dark' // Inherit the rest of the attributes from the preset dark style
            },
            hide: { effect: { type: 'slide' }, when: { event: 'inactive' }, delay: '10000' },
            show: { effect: { type: 'slide' }, when: { event: 'click' } }
        });
    });
    
    $('#content-text .tx-billitonemployeesearch-pi1 .contact-person .tooltip h2 strong').each(function() {
            var personName = $(this).html();
            $(this).closest('.tooltip').prev('.image').append(personName);
    });

    // accordion für FAQ
    $("#content-text .accordion .csc-header").css({'cursor':'pointer'});
    $("#content-text .accordion .csc-header").click(function() {
        $(this).siblings().removeClass('active');
        $(this).addClass('active');
        $(this).next('.csc-texttext, .csc-textpic').toggle().siblings('.csc-texttext, .csc-textpic').slideUp('fast');
    }).next().hide();
    
    // Styling für Sprachauswahl-Dropdown
    //$("form#languageselector").jqTransform();
    $('form#languageselector .submit').hide();
    $('form#languageselector select').bind('change', function() { $('#languageselector').submit() });
    
    // Target _blank für PDF Downloads erzwingen
    $('#sidebar, #content-text').find('a').each(function() {
        var $a = $(this);
        var href = $a.attr('href');
        // get the extension from the href
        var hrefArray = href.split('.');
        var extension = hrefArray[hrefArray.length - 1];
        if(extension == 'pdf')
        {
            $a.attr({'target':'_blank'});
        }
    });
    
    
    // leeren li-Tag aus Sprachauswahl entfernen
    $('.language-select-js li').each(function() {
        if(!$(this).find('a').length)
        {
            $(this).detach();
        }
    });
/*
    // Label ein- und ausklappen
    jQuery('#konstantin-label').bind('mouseover', function() {
        jQuery(this)
            .css( {
                'background-position' : 'left top'
            } )
            .animate( {
                'marginLeft' : '-190px',
                'width' : '190px'
            },
            { queue: false, duration: 1500 } );
    });
    jQuery('#konstantin-label').bind('mouseleave', function() {
        jQuery(this)
            .css( {
                'background-position' : 'left bottom'
            } )
            .animate( {
                'marginLeft' : '-51px',
                'width' : '51px'
            },
            { queue: false, duration: 1500 } );
    });
*/
    // Slideshow im header triggern
    $('#header').cycle({ slideExpr: '> img' });
   
    /*
        Langauge Selector
    */
    // Set active language to the beginning of the language selector
    /*var myActElem = $('#language-select-mask ul li.act').detach();
    $('#language-select-mask ul').prepend(myActElem);*/
    // Animate language selector
    /*$('#language-select-mask').width(17).hover(function() {
      $(this).animate(
          { 'width': 145 },
          { duration: 1000, queue: false }
      );
    },
    function() {
      $(this).animate(
        { 'width': 17 },
        { duration: 1000, queue: false }
      );
    });*/
    
});

jQuery.fn.alignHeights = function()
{
    function alignHeights(group)
    {
        var maxHeight = null;
        jQuery.each(group, function()
        {
            var height = jQuery(this).height();
            if ((maxHeight === null) || (height > maxHeight))
            {
                maxHeight = height;
            }
        });
        jQuery(group).height(maxHeight);
    }

    var group = [];
    var lastTop = null;
    jQuery.each(this, function()
    {
        group.push(this);

        lastTop = top;
    });

    if (group.length)
    {
       alignHeights(group);
    }

    return this;
}

