$(document).ready(function () {

    $('#mainnavi').children('ul').children('li').not('.active').hover(function () {
        var navId = $(this).attr('rel');
        var hoverImg = window[navId];
        $(this).children('a').children('img').attr('src', hoverImg[1]);
    },
        function () {
            var old_name = $(this).children('a').children('img').attr('src');
            var navId = $(this).attr('rel');
            var hoverImg = window[navId];
            if (old_name === hoverImg[1]) {
                $(this).children('a').children('img').attr('src', hoverImg[0]);
            }
            else {
                return false;
            }
        }
    );

    $('input').live('focus', function () {
        value = $(this).attr('value');
        if ($(this).attr('alt') == value) {
            $(this).attr('value', '');
            $(this).css('color', '#000000');
        }
    });

    $('input').live('blur', function () {
        if ($(this).attr('value') == 0) {
            value = $(this).attr('alt')
            $(this).attr('value', value);
            $(this).css('color', '#BEC3C3');
        }
    });

    $('textarea').live('focus', function () {
        value = $(this).attr('value');
        if ($(this).attr('title') == value) {
            $(this).attr('value', '');
        }
    });

    $('textarea').live('blur', function () {
        if ($(this).attr('value') == 0) {
            value = $(this).attr('title')
            $(this).attr('value', value);
        }
    });

    /*
    Sitemap function
    */
    $('.openSitemap').click(function (e) {
        e.preventDefault();
        $('#share_container').slideUp('fast');
        $('.openSitemap').addClass("on");
        $('.shareaction').removeClass("on");
        $('#sitemap_container').slideToggle('fast', function () {
            if ($(this).is(':hidden')) {
                $('.openSitemap').removeClass("on");
            }
        });
    });
    $('.sitemap_close').click(function (e) {
        e.preventDefault();
        $('#sitemap_container').slideToggle('fast', function () {
            if ($(this).is(':hidden')) {
                $('.openSitemap').removeClass("on");
            }
        });
    });

    /*
    Weiterleiten function
    */
    $('.shareaction').click(function (e) {
        e.preventDefault();
        $('#sitemap_container').slideUp('fast');
        $('.shareaction').addClass("on");
        $('.openSitemap').removeClass("on");
        $('#share_container').slideToggle('fast', function () {
            if ($(this).is(':hidden')) {
                $('.shareaction').removeClass("on");
            }
        });
    });
    $('.share_close').click(function (e) {
        e.preventDefault();
        $('#share_container').slideToggle('fast', function () {
            if ($(this).is(':hidden')) {
                $('.shareaction').removeClass("on");
            }
        });
    });
    /*
    h4
    */
    $('h4').wrap('<div class="h4_wrap" />');
    $('h4').before('<div class="h4_line"></div>');


    /*
    * Jobbörse navi
    */
    $('.themebox_navi').children().click(function () {
        $(this).parent().next().next().children('.jobbörse_box_login').hide();
        $(this).parent().children('.active').removeClass('active');
        $(this).addClass('active');
        newpage = $(this).attr('rel');
        newpage = '.' + newpage;
        $(this).parent().next().next().children(newpage).show();
    });


    /*
    * rewe_map
    */
    $('#map_navi').children('a').hover(function () {
        /*        $(this).click(function (e) { e.preventDefault() }); */
        $(this).parent().children('.active').removeClass('active');
        $(this).addClass('active');
        newmap = $(this).attr('rel');
        newmap = '#' + newmap.substring(5);
        oldmap = $('.rewe_map:visible');
        $(oldmap).hide();
        $(newmap).show();
    }, function () {
        $(this).parent().children('.active').removeClass('active');
        $('#map1').hide();
        $('#map2').hide();
        $('#map0').show();
    });

    /*
    * themebox auto height
    * try several times
    */
    for (var i = 0; i < 3; i++) {
        AdjustThemeBoxHeight(i);
    }

    /*
    * AGB lightboox
    */
    $('.agb_label').children('a').click(function (e) {
        e.preventDefault();
        $('.lb_wrapper_agb').children('.lb_bg').fadeTo(200, 0.8, function () {
            $('.lb_wrapper_agb').children('.lb_content').show();
        });
        //        $('.lb_wrapper_agb').children('.lb_content').fadeIn(); //not working with IE & 24Bit-PNG, Transparency Error
    });
    $('.lb_close').click(function () {
        //        $(this).parent().parent('.lb_content').fadeOut(); //not working with IE & 24Bit-PNG, Transparency Error
        $(this).parent().parent('.lb_content').hide();
        $(this).parent().parent().prev().fadeOut();
    });

    /*
    * Contact PrivacyPolicy lightboox
    */
    $('.privacy_label').children('a').click(function (e) {
        e.preventDefault();
        $('.lb_wrapper_PrivacyPolicy').children('.lb_bg').fadeTo(200, 0.8, function () {
            $('.lb_wrapper_PrivacyPolicy').children('.lb_content').show();
        });
        //        c.fadeIn();//not working with IE & 24Bit-PNG, Transparency Error
    });
    $('lb_wrapper_PrivacyPolicy').find('.lb_close').click(function () {
        //        $(this).parent().parent('.lb_content').fadeOut();//not working with IE & 24Bit-PNG, Transparency Error
        $(this).parent().parent('.lb_content').hide();
        $(this).parent().parent().prev().fadeOut();
    });

    /*
    * call-to-action box auto height
    */
    var currentTallest = 0;
    $('.cta_col').each(function () {
        if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
    });

    $('.cta_box').children('.cta_separator').css({ 'height': currentTallest });

    if ($.browser.msie && $.browser.version == 6.0) {
        $('.cta_box').children('.cta_separator').css({ 'height': currentTallest });
    }
    $('.cta_box').children('.cta_separator').css({ 'min-height': currentTallest });

    SetSelectsExpandable();
});

/*
* calculate talest height of given themebox row, and adjust all boxes
*/
function AdjustThemeBoxHeight(rowIndex) {
    var rowSelect = '.row_' + rowIndex;
    var currentTallest = 0;
    var selector1 = rowSelect + '.themebox_wrapper';
    var selector2 = rowSelect + '.themebox_wrapper2';

    /* get tallest height */
    $(rowSelect).each(function () {
        $(selector1).each(function () {
            if ($(this).height() > currentTallest) {
                currentTallest = $(this).height();
            }
        });
        $(selector2).each(function () {
            if ($(this).height() > currentTallest) {
                currentTallest = $(this).height();
            }
        });
    });

    /* adjust all boxes to calculated height */
    $(selector1).children('.themebox_border_mid').css({ 'height': currentTallest });
    $(selector2).children('.themebox_border_mid').css({ 'height': currentTallest });

    if ($.browser.msie && $.browser.version == 6.0) {
        $(selector1).children('.themebox_border_mid').css({ 'height': currentTallest });
    }
    $(selector1).children('.themebox_border_mid').css({ 'min-height': currentTallest });

    if ($.browser.msie && $.browser.version == 6.0) {
        $(selector2).children('.themebox_border_mid').css({ 'height': currentTallest });
    }
    $(selector2).children('.themebox_border_mid').css({ 'min-height': currentTallest });
}

/*
* Select-Box auto text-width-adjusting for IE
* http://stackoverflow.com/questions/73960/dropdownlist-width-in-ie
*/
function SetSelectsExpandable() {
    if ($.browser.msie && $.browser.version < 9) {
        if ($.browser.version < 8) {
            // workaround for IE <= 7: insert spaces to first option text of each select, to mimic min-width
            $('.search_inputs').find('select.wide').each(function () {
                var widest = 0;
                $(this).children('option').first().append('                                      ');
            });
        }
        $('.search_inputs').find('select.wide').bind('focus mouseover', function () {
            $(this).addClass('expand').removeClass('clicked');
        }).bind('click', function () {
            $(this).toggleClass('clicked');
        }).bind('mouseout', function () {
            if (!$(this).hasClass('clicked')) {
                $(this).removeClass('expand');
            }
        }).bind('blur', function () {
            $(this).removeClass('expand clicked');
        });
    }
}
