$(document).ready(function() {

    Cufon.replace('h2 a,#navlist>li>a,.continue', { hover: true, fontFamily: 'Lucida Sans' });
    Cufon.replace('h2,h3,h4,#navlist>li>a,.continue', { fontFamily: 'Lucida Sans' });
    Cufon.replace('.teaser h2,.teaser p,.teler-down,.teler-up', { fontFamily: 'Lucida Sans Bold' });
    Cufon.replace('blockquote', { fontFamily: 'Lucida Sans Italic' });
    Cufon.now();

    $('#sidebar .dotted').css('padding-bottom', '1px');

    $('#select-teler').customSelect();
    $('#select-gewas').customSelect();
    $('.AxFormControl').each(function() {
        if ($(this).find('select').length > 0) { $(this).customSelect(); }
    });

    $('.teler-overzicht input[title]').each(function() {
        var defaultText = $(this).attr('title');
        if ($(this).val() == "") {
            $(this).val(defaultText);
        }
        $(this).focus(function() {
            var userInput = $(this).val();
            $(this).addClass('focused');
            if (userInput == defaultText) {
                $(this).val('');
            }
        });
        $(this).blur(function() {
            var userInput = $(this).val();
            if (userInput == '') {
                $(this).val(defaultText);
                $(this).removeClass('focused');
            }
        });
    });

    $('.AxRequired').parent('div, span').prepend('<span class="AxRequiredIcon" />');

    $('.AxRequiredMessage').each(function() {
        $(this).parent('div').find('input').css('background', '#f9f7f6');
    });

    $('.teler-down').click(function() { $('.teler-overzicht').slideDown(); $('.teler-down').css('visibility', 'hidden'); });
    $('.teler-up').click(function() { $('.teler-overzicht').slideUp(); $('.teler-down').css('visibility', 'visible'); });

    $('.employee').each(function() {
        var imageSrc = $(this).find('img').attr('src');
        var imageAlt = $(this).find('img').attr('alt');
        $(this).find('.more-info').prepend('<img src="' + imageSrc + '" alt="' + imageAlt + '" />');
    });

    $('.employee').hover(function() {
        $(this).find('.more-info').fadeIn();
    }, function() {
        $(this).find('.more-info').fadeOut().hide();
    });

    $("#main a[href^='http://'],#sidebar a[href^='http://']").each(function() {
        $(this).addClass('external');
        $(this).attr('target', '_blank');
    });

    $('.blockwrapper').each(function() {
        var dottedEvenHeight = $(this).find('.dotted:even').height();
        var dottedOddHeight = $(this).find('.dotted:odd').height();
        if (dottedEvenHeight > dottedOddHeight) {
            $(this).find('.dotted').height(dottedEvenHeight);
            $(this).find('.dotted .content').height(dottedEvenHeight - 30);
        }
        else {
            $(this).find('.dotted').height(dottedOddHeight);
            $(this).find('.dotted .content').height(dottedOddHeight - 30);
        }
    });

    $('.AxFormControl:has(select)').each(function() {
        var contentOld = $(this).html();
        var contentNew = contentOld.replace('&nbsp;', '');
        $(this).html(contentNew);
        var reqMes = $(this).find('.AxRequiredMessage');
        $(this).find('.AxRequiredMessage').remove();
        $(this).parent().append(reqMes);
        $(this).parent().find('.AxRequiredMessage').addClass('custom-req');
    });

    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

        $('#navlist li').hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });
    }

});

(function($) {
    $.fn.customSelect = function() {
        $(this).addClass('custom-select').find('select').hide();
        $(this).append('<ul/>');
        $(this).append('<a href="#" class="choose" />');
        $(this).find('option').each(function() {
            var optText = $(this).text();
            var optSelected = $(this).attr('selected');
            var optValue = $(this).attr('value');
            $(this).parent().parent().find('ul').append("<li class='" + optSelected + "'>" + optText + "</li>");
        });
        $(this).click(function() {            
            if ($(this).find('ul').find('li:animated').length == 0) {
                if (!$(this).hasClass('open')) {
                    $(this).addClass('open');
                    $(this).find('li').slideDown();
                    $(this).find('ul').addClass('open');
                    $(this).find('li').click(function() {
                        var i = $(this).parent('ul').find('li').index(this);
                        $(this).parent('ul').parent('div').find('option').removeAttr('selected');
                        $(this).parent('ul').parent('div').find('option').eq(i).attr('selected', 'selected');
                        $(this).parent('ul').find('li').removeClass('true').addClass('false'); ;
                        $(this).removeClass('false').addClass('true');
                    });
                }
                else {
                    $(this).find('ul').removeClass('open');
                    $(this).removeClass('open');
                    $(this).find('li').hide();
                    $(this).parent().find('.true').show();
                }
            }
        });
    };
})(jQuery);
