$(document).ready(function(){

    // land en taal id. Op de corporate site bestaan de hidden fields met landid en taalid niet, dus daarvoor krijgen de vars default 0
    iLandId = 0;
    iTaalId = 0;
    if($('#landid').length > 0){
        iLandId = $('#landid').val();
        iTaalId = $('#taalid').val();
    }

    // Links met rel="external" moeten in een nieuw venster worden geopend
    $('A[rel="external"]').click( function() {
        window.open(jQuery(this).attr('href') );
        return false;
    });


    // zoekformulier
    if($('#q').length){
        sDefaultQ = $('#q')[0].value;
    }
    $('#q').focus(function(){
        if(this.value == sDefaultQ) this.value = '';
    });

    $('#q').blur(function(){
        if(this.value == '') this.value = sDefaultQ;
    });

    /*
    $('#zoekForm').submit(function(){
        if($('#q')[0].value == ''){
            alert('Please type in something to search for');
            $('#q').focus();
            return false;
        }

    });
    */

    // Edit in place toolbar
    $('#eipToolbar').css({'opacity': "0.9"});

    // Breadcrumb
    $('#breadcrumbs').css({'opacity': "0.8"});

    // Animatie (hiervoor is jquery.cycle.lite.min.js nodig!)
    $('#animationimgs').cycle();
    $('#airlinelogos').cycle();


    $('#peopleWrap').cycle({
        prev:   '#peoplePrev',
        next:   '#peopleNext',
        pause:   1

    });

    // Zoekveld voor medewerkers
    if($('#searchname').length){
        sDefaultSearchname = $('#searchname')[0].value;
    }
    $('#searchname').focus(function(){
        if(this.value == sDefaultSearchname) this.value = '';
    });

    $('#searchname').blur(function(){
        if(this.value == '') this.value = sDefaultSearchname;
    });

    // medewerkers zoekformulier verwerken dmv ajax request
    $('#peopleSearch').submit(function(event){
        event.preventDefault();

        if($('#searchname')[0].value == sDefaultSearchname || $('#searchname')[0].value == ''){
            // als er geen naam is ingevuld...
            alert('Please type in a name to search for');
            $('#searchname').focus();
        } else {

            // iLandId & iTaalId zijn global vars die aan het begin van main.js zijn gedefinieerd

            // als alles goed is ingevuld...
            $.post('ajax/peoplesearch.php',{name: $('#searchname')[0].value, land: iLandId, taal: iTaalId}, function(data, textStatus){
                if(textStatus == 'success'){
                    $('#peopleSearchResults').html(data.html);
                } else {
                    $('#peopleSearchResults').html('An error occurred while processing your search. Please excuse us for the inconvenience.');
                }
            },"json");
        }
    });

    // Verjaardagen tonen
    $('#upcomingBirthdays').click(function(e){
        e.preventDefault();

        $.post('ajax/upcomingbirthdays.php',{land: iLandId, taal: iTaalId}, function(data, textStatus){
            if(textStatus == 'success'){
                $('#peopleSearchResults').html(data.html);
            } else {
                $('#peopleSearchResults').html('An error occurred while processing your request. Please excuse us for the inconvenience.');
            }
        },"json");

    });

    // Go directly to jumpbox
    $('#godirectlyto').change(function(){
        sUrl = $("#godirectlyto").val();

        if(sUrl != 0){
            document.location.href = sUrl;
        }
    });

    $('#godirectlyto2').change(function(){
        sUrl = $("#godirectlyto2").val();

        if(sUrl != 0){
            document.location.href = sUrl;
        }
    });

    // Office select: Contact informatie van een kantoor tonen
    $('#contact_officeselect').change(function(){

        iKantoor = $('#contact_officeselect').val(); // kantoor id

        // iLandId & iTaalId zijn global vars die aan het begin van main.js zijn gedefinieerd
        $.post('ajax/kantoordetails.php',{id: iKantoor, type: 'contactinfo', land: iLandId, taal: iTaalId}, function(data, textStatus){
            if(textStatus == 'success'){
                $('#officeDetails').html(data.html);
            } else {
                $('#officeDetails').html('An error occurred while processing your request. Please excuse us for the inconvenience.');
            }
        },"json");

    });


    // Office select: Contact informatie van een kantoor tonen
    $('#24h_phone_officeselect').change(function(){

        iKantoor = $('#24h_phone_officeselect').val(); // kantoor id

        $.post('ajax/kantoordetails.php',{id: iKantoor, type: '24phone'}, function(data, textStatus){
            if(textStatus == 'success'){
                $('#office24hphone').html(data.html);
            } else {
                $('#office24hphone').html('An error occurred while processing your request. Please excuse us for the inconvenience.');
            }
        },"json");

    });

    // nieuwsbrief aanmelden
    $('#nieuwsbriefformsubmit').click(function(e){
        e.preventDefault();
        $('#nieuwsbriefform').submit();
    });


    // office map: kaart met alle kantoren
    if (jQuery.browser.msie && jQuery.browser.version.substr(0,1)=="6") {
        // IE6 fix: png's werken niet in ie6. Dus in plaats van de interactieve kaart krijg je in ie6 een statisch gifje te zien
        $('#officemap .markers').attr('src','images/map/ie6_map.gif');
    } else {
        // Alle landen highlights verbergen
        //$('#officemap .highlight').hide();

        // Als je over een area hovert, wordt het onderliggen land opgelicht
        $('#officemap map area').hover(function(){
            sCountryCode = $(this).attr('class');
            $('#officemap #highlight_'+sCountryCode).fadeIn();
        },
        function(){
            $('#officemap #highlight_'+sCountryCode).fadeOut();
        });

        // De lelijke outlines rondom de area's onzichtbaar maken in IE
        $('#officemap map area').focus(function(){
            $(this).blur();
        });

    }

});

