// hieronder alle functies na DOM-load
window.addEvent('domready', function() {
    remove_link_lines();
    init_languageselection();
    init_navmain();
    init_calendar();
    init_usplist();
    usplist_setheight();
    init_dienstenbeschrijvingen();
    init_dienstenlijsten();
    init_vestigingen();
    init_homeslideshow();
    init_tijd();
    init_searchbox();
});

// de caroussel mag pas worden geïnitialiseerd nadat ook alle images geladen zijn
window.addEvent('load', function() {    
    insertJWplayer();
    init_caroussel();

});


// deze functie verwijdert de stippellijntjes bij aangeklikte a-tags
function remove_link_lines() {
    document.getElements('a').addEvent('focus', function() { this.blur() });
}


// zorgt ervoor dat de tijd dropdowns in het contact formulier kloppen en regelt de validatie van het telefoonnummer veld
function init_tijd() {
    if (window.netprefix == null) return;

    // get elements
    var pre = netprefix; // .NET element prefix
    var el_contact = $(pre + 'pnl_contact');
    var el_contact_0 = $(pre + 'in_contact_0');
    var el_contact_1 = $(pre + 'in_contact_1');
    var el_van = $(pre + 'in_tijd_van');
    var el_tot = $(pre + 'in_tijd_tot');
    var el_tel_req = $(pre + 'txt_telefoon_required');
    var el_req_tel = $('req_telefoon');
    // show time
    if (el_contact && el_contact_0) {
        el_contact.style.display = 'none';
        el_contact_0.addEvent('click', function() {
            el_contact.style.display = 'none';
            if (el_tel_req) {
                el_tel_req.enabled = false;
                el_tel_req.style.display = 'none';
            }
            if (el_req_tel) el_req_tel.innerHTML = '';
        });
        el_contact_1.addEvent('click', function() {
            el_contact.style.display = '';
            if (el_tel_req) el_tel_req.enabled = true;
            if (el_req_tel) el_req_tel.innerHTML = '*';
        });
        // make sure time makes sence 
        if (el_van && el_tot) {
            el_van.addEvent('change', function() {
                if (el_van.selectedIndex >= el_tot.selectedIndex) el_tot.selectedIndex = (el_van.selectedIndex < el_tot.options.length - 1) ? el_van.selectedIndex + 1 : 0;
            });
            el_tot.addEvent('change', function() {
                if (el_van.selectedIndex >= el_tot.selectedIndex) el_van.selectedIndex = (el_tot.selectedIndex == 0) ? el_van.options.length - 1 : el_tot.selectedIndex - 1;
            });
        }
    }
}


// deze functie verzorgt het in- uitklappen van de taalkeuze-box
function init_languageselection() {
    $E('a', 'selected_language').addEvent('click', function() {
        if ($('lang_dropdown').className == 'collapsed') {
            $('lang_dropdown').className = '';
        } else {
            $('lang_dropdown').className = 'collapsed';
        }
    });
}


// deze functie verzorgt het in- uitklappen van de hoofdnavigatie
function init_navmain() {
    $ES('li', 'nav_main').each(function(item) {
        item.addEvents({
            'mouseover': function() {
                if ($E('ul', item)) { $E('ul', item).style.display = 'block'; }
                if ($E('div', item)) { $E('div', item).style.display = 'block'; }
            },
            'mouseout': function() {
                if ($E('ul', item)) { $E('ul', item).style.display = 'none'; }
                if ($E('div', item)) { $E('div', item).style.display = 'none'; }
            }
        });

        if ($E('div', item)) { $E('div', item).setStyle('opacity', 0.5); }
        if ($E('ul', item)) { $E('ul', item).style.display = 'none'; }
    });
}


//deze functie verzorgt het in- uitklappen van de financiele kalender
function init_calendar() {
    if (!$('calendar')) { return; }
    var accordion = new Accordion('h4.calendar_header', 'div.calendar_details', {
        show: 0,
        opacity: false,
        duration: 300,
        onActive: function(toggler, element) {
            $ES('span', toggler)[1].className = 'title_open';
        },

        onBackground: function(toggler, element) {
            $ES('span', toggler)[1].className = 'title';
        }
    }, $('calendar'));
}


//deze functie verzorgt het wisselen van de usp's op de doelgroepen-pagina
function init_usplist() {
    selected_usp = -1;
    usp_titles = $$('.usptitle');
    if (usp_titles.length < 1) { return; }
    usp_summaries = $$('.usp_summary');
    usp_contents = $$('.usp_content');
    usp_closebuttons = $$('.usp_close');
    usp_titles.each(function(item, index) {
        item.addEvents({
            'click': function() {
                usp_summaries.each(function(item) {
                    item.style.display = 'none';
                });
                usp_contents.each(function(item) {
                    item.style.display = 'none';
                });
                usp_titles.each(function(item) {
                    item.className = 'usptitle';
                });
                usp_titles.getLast().className = 'usptitle right_usp';
                usp_contents[index].style.display = 'block';
                if (index == usp_titles.length - 1) {
                    usp_titles[index].className = 'usptitle right_usp usptitle_active';
                } else {
                    usp_titles[index].className = 'usptitle usptitle_active';
                }
                selected_usp = index;
            },
            'mouseover': function() {
                usp_summaries[index].style.backgroundColor = '#fafafa';
                if (index == usp_titles.length - 1) {
                    usp_titles[index].className = 'usptitle right_usp usptitle_active';
                } else {
                    usp_titles[index].className = 'usptitle usptitle_active';
                }
            },
            'mouseout': function() {
                usp_summaries[index].style.backgroundColor = '#ffffff';
                if (index == usp_titles.length - 1) {
                    usp_titles[index].className = 'usptitle right_usp';
                } else {
                    usp_titles[index].className = 'usptitle';
                }
                switch (selected_usp) {
                    case usp_titles.length - 1:
                        usp_titles[selected_usp].className = 'usptitle right_usp usptitle_active';
                        break;
                    case -1:
                        break;
                    default:
                        usp_titles[selected_usp].className = 'usptitle usptitle_active';
                }
            }
        });
    });

    usp_summaries.each(function(item, index) {
        item.addEvents({
            'click': function() {
                usp_summaries.each(function(item) {
                    item.style.display = 'none';
                });
                usp_contents.each(function(item) {
                    item.style.display = 'none';
                });
                usp_titles.each(function(item) {
                    item.className = 'usptitle';
                });
                usp_titles.getLast().className = 'usptitle right_usp';
                usp_contents[index].style.display = 'block';
                if (index == usp_titles.length - 1) {
                    usp_titles[index].className = 'usptitle right_usp usptitle_active';
                } else {
                    usp_titles[index].className = 'usptitle usptitle_active';
                }
                selected_usp = index;
            },
            'mouseover': function() {
                usp_summaries[index].style.backgroundColor = '#fafafa';
                if (index == usp_titles.length - 1) {
                    usp_titles[index].className = 'usptitle right_usp usptitle_active';
                } else {
                    usp_titles[index].className = 'usptitle usptitle_active';
                }
            },
            'mouseout': function() {
                usp_summaries[index].style.backgroundColor = '#ffffff';
                if (index == usp_titles.length - 1) {
                    usp_titles[index].className = 'usptitle right_usp';
                } else {
                    usp_titles[index].className = 'usptitle';
                }
                switch (selected_usp) {
                    case usp_titles.length - 1:
                        usp_titles[selected_usp].className = 'usptitle right_usp usptitle_active';
                        break;
                    case -1:
                        break;
                    default:
                        usp_titles[selected_usp].className = 'usptitle usptitle_active';
                }
            }
        });
    });

    usp_closebuttons.each(function(item, index) {
        item.addEvent(
			'click', function() {
			    selected_usp = -1;
			    usp_contents.each(function(item) {
			        item.style.display = 'none';
			    });
			    usp_summaries.each(function(item) {
			        item.style.display = 'block';
			    });
			    usp_titles.each(function(item) {
			        item.className = 'usptitle';
			    });
			    usp_titles[usp_titles.length - 1].className = 'usptitle right_usp';
			}
		);
    });
}


//deze functie stelt de hoogte van de usp's op de doelgroepen-pagina gelijk
function usplist_setheight() {
    if (usp_titles.length < 1) { return; }
    var h = 0;
    for (n = 0; n < usp_summaries.length; n++) {
        if (usp_summaries[n].offsetHeight > h) {
            h = usp_summaries[n].clientHeight;
        }
    }
    for (n = 0; n < usp_summaries.length; n++) {
        usp_summaries[n].setStyle('height', h - 26);
    }
}


//deze functie regelt het wisselen van de beschrijvingen van de dienstenoverzichten
function init_dienstenbeschrijvingen() {
    if (!$('dienstenvoordoelgroep')) { return; }
    diensten = $ES('span.switch', 'dienstenvoordoelgroep');
    diensten.extend($$('.dienst'));
    dienstenbeschrijvingen = $$('.dienst_beschrijving');
    diensten.each(function(item, index) {
        item.addEvent(
			'click', function() {
			    dienstenbeschrijvingen.each(function(item) {
			        item.style.display = 'none';
			    });
			    diensten.each(function(item) {
			        if (item.className != 'productgroep selected') { item.className = ''; }
			    });
			    dienstenbeschrijvingen[index].style.display = 'block';
			    diensten[index].className = 'selected';
			}
		);
    });
    $$('.dienst').each(function(item) {
        item.addEvents({
            'mouseover': function() {
                item.style.backgroundColor = '#f5f5f5';
            },
            'mouseout': function() {
                item.style.backgroundColor = '';
            }
        });
    });
    dienstenbeschrijvingen[0].style.display = 'block';
}


//deze functie verzorgt het in- uitklappen van de dienstenlijsten
function init_dienstenlijsten() {
    if (!$('dienstenvoordoelgroep')) { return; }
    var accordion = new Accordion('li.productgroep', 'ul.dienstengroep', {
        show: 0,
        opacity: false,
        duration: 200,
        onActive: function(toggler, element) {
            toggler.className = 'productgroep selected';
        },

        onBackground: function(toggler, element) {
            toggler.className = 'productgroep';
        }
    }, $('dienstenvoordoelgroep'));
}


//deze functie verzorgt het in- uitklappen van de vestigingen
function init_vestigingen() {
    if (!$('vestigingen')) { return; }
    var vestigingen = $ES('li', 'vestigingen');
    var gegevens = $$('.vestiging');
    gegevens.each(function(item) {
        item.className = 'vestiging vestiging_hidden';
    });
    gegevens[0].className = 'vestiging';
    vestigingen.each(function(item, index) {
        item.addEvent(
			'click', function() {
			    vestigingen.each(function(item) {
			        item.className = '';
			    });
			    vestigingen[index].className = 'active';

			    gegevens.each(function(item) {
			        item.className = 'vestiging vestiging_hidden';
			    });
			    gegevens[index].className = 'vestiging';
			}
		);
    });
}


// deze functie laat de visuals slideshow op de homepage (header) functioneren
function init_homeslideshow() {
    if (!$('home_slideshow')) { return; }
    images = $ES('img', 'home_slideshow');
    captions = $ES('p', 'home_slideshow');
    currentslide = 0;
    images.each(function(item) {
        item.setStyle('opacity', 0);
    });
    captions.each(function(item) {
        item.setStyle('top', -95);
    });
    images[currentslide].setStyle('opacity', 1);
    captions[currentslide].setStyle('top', 0);

    $('nextslide').addEvent('click', function() {
        images[currentslide].effect('opacity', {
            duration: 1500
        }).start(0);
        captions[currentslide].effect('top', {
            duration: 1500
        }).start(0, 95);

        currentslide = (currentslide == images.length - 1) ? 0 : currentslide + 1;

        images[currentslide].effect('opacity', {
            duration: 1500
        }).start(1);
        captions[currentslide].effect('top', {
            duration: 1500
        }).start(-95, 0);
    });
}


// hieronder de caroussels van 'historie' en 'interviews'
function init_caroussel() {
    if (!$('carousselcontainer')) { return; }
    carousseloverlays = $$('.carousseloverlay');
    carousselitems = $$('.carousselitem');
    caroussel_largeitems = $$('.historie_item').length > 0 ? $$('.historie_item') : $$('.interview_item');

    // stel de breedte in op de juiste waarde, afhankelijk van de inhoud van alle items, met aanpassing voor afwijking in ie6 en ie7
    carousselWidth = 0;
    for (n = 0; n < $$('.carousselitem').length; n++) {
        carousselWidth = carousselWidth + $$('.carousselitem')[n].scrollWidth;
    }
    carousselWidth = window.ie ? carousselWidth + 10 : carousselWidth;
    carousselPos = -10; 	// offset om linkerborder en padding te verbergen
    $('caroussel').setStyle('width', carousselWidth);
    $('caroussel').setStyle('left', carousselPos);

    $('moveleft').addEvents({
        'mouseover': function() {
            caroussel_moveleft();
        },
        'mouseout': function() {
            caroussel_stop();
        },
        'click': function() {
            caroussel_gotostart();
        }
    });
    $('moveright').addEvents({
        'mouseover': function() {
            caroussel_moveright();
        },
        'mouseout': function() {
            caroussel_stop();
        },
        'click': function() {
            caroussel_gotoend();
        }
    });

    // stel de juiste breedte in van elke transparante overlay
    carousseloverlays.each(function(item) {
        var targetWidth = item.parentNode.clientWidth - 20;
        item.setStyle('width', targetWidth);
    });
    carousselitems_removeOverlays();

    // regelt de overlay mouseover-effecten van de caroussel items
    carousselitems.each(function(item, index) {
        item.addEvents({
            'mouseenter': function() {
                carousselitem_setOverlay(index);
            },
            'mouseleave': function() {
                carousselitems_removeOverlays();
            },
            'click': function() {
                carousselitem_show(index);
            }
        });
    });

    // toon standaard de onderliggende content bij het eerste item
    carousselitem_show(0);
}

function caroussel_moveleft() {
    $('moveleft').setStyle('background-image', 'url(/siteimg/caroussel_left_hover.gif)');

    // zorg ervoor dat de snelheid altijd gelijk is, door duration af te laten hangen van af te leggen afstand
    effect_duration = (750 - (carousselWidth + carousselPos - 600)) * 10;
    moveleft_effect = $('caroussel').effect('left', {
        duration: effect_duration,
        transition: Fx.Transitions.linear
    });
    moveleft_effect.start(-10);
}
function caroussel_moveright() {
    $('moveright').setStyle('background-image', 'url(/siteimg/caroussel_right_hover.gif)');

    // zorg ervoor dat de snelheid altijd gelijk is, door duration af te laten hangen van af te leggen afstand
    effect_duration = (carousselWidth + carousselPos - 600) * 10;
    moveright_effect = $('caroussel').effect('left', {
        duration: effect_duration,
        transition: Fx.Transitions.linear
    });
    moveright_effect.start(600 - carousselWidth);
}
function caroussel_stop() {
    $('moveleft').setStyle('background-image', 'url(/siteimg/caroussel_left.gif)');
    $('moveright').setStyle('background-image', 'url(/siteimg/caroussel_right.gif)');

    // check of er een effect wordt uitgevoerd en zo ja, stop deze
    if (typeof (moveright_effect) != 'undefined') { moveright_effect.stop(); }
    if (typeof (moveleft_effect) != 'undefined') { moveleft_effect.stop(); }

    carousselPos = $('caroussel').getStyle('left').toInt();
}

function caroussel_gotostart() {
    moveleft_effect.stop();
    $('caroussel').effect('left', {
        duration: 200,
        onComplete: function() { carousselPos = $('caroussel').getStyle('left').toInt(); }
    }).start(-10);
}
function caroussel_gotoend() {
    moveright_effect.stop();
    $('caroussel').effect('left', {
        duration: 200,
        onComplete: function() { carousselPos = $('caroussel').getStyle('left').toInt(); }
    }).start(600 - carousselWidth);
}

// leg een semitransparante overlay over alle items, behalve degene die mouseover heeft
function carousselitem_setOverlay(index) {
    carousseloverlays.remove(carousseloverlays[index]);
    carousseloverlays.each(function(item) {
        item.setStyle('opacity', '0.8');
    });
    carousseloverlays = $$('.carousseloverlay');
}
// maak alle overlays weer volledig transparant
function carousselitems_removeOverlays() {
    carousseloverlays.each(function(item) {
        item.setStyle('opacity', '0');
    });
}

// onderstaande wisselt de content bijbehorend bij de carousselitems
function carousselitem_show(index) {
    caroussel_largeitems.each(function(item) {
        item.setStyle('display', 'none');
    });
    caroussel_largeitems[index].setStyle('display', 'block');
}


function init_searchbox() {
    if (!$('search')) { return }
    search_focused = false;
    $E('input', 'search').addEvents({
        'focus': function() {
            if (search_focused == false) { $E('input', 'search').value = '' }
            search_focused = true;
        },
        'keyup': function(event) {
            var event = new Event(event);
            //if (event.key == 'enter') alert ('a');
        }
    });
}

function doSearch(el) {
    if (!el) el = $('q');
    if (el && el.value != '') top.location = '/search.aspx?q=' + el.value;
    return false;
}

function opEnterZoek(e, dit) {
    var keynum;
    var waarde;

    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }

    if (keynum == 13) {
        window.event.cancelBubble = true;
        doSearch();
        window.onunload = function() {
            doSearch();
        }

    }
}

/**
* insertJWplayer
*
* replaces hyperlinks to flv media (in the Sitecore medialibrary) with instance of JWplayer,
* adopting the source videofile plus the width and height of the placeholder image inside the hyperlink
*
* @author Klaas Dieleman <klaas{AT}efocus.nl>
* 
*/

function insertJWplayer() {

    var requiredFlashVersion = 9;
    var jwlinks = new Array;

    //    document.getElements('a').each(function(item) {

    //        var str = item.getProperty('href');
    //        document.write(str);
    //        var pos = str.indexOf("~/media/video/");
    //        if (pos >= 0) {
    //            jwlinks.include(item);
    //        }
    //    });

    document.getElements('a').each(function(item) {

        if (item.getProperty('href') && item.getElement('img'))

        //        var str = item.getProperty('href');
        //        document.write(str);

            var pos = item.getProperty('href').indexOf("~/media/Video/");
        if (pos >= 0) {
            jwlinks.include(item);
        }
    });

    if ($$('div.f33_accordion').length > 0 && Browser.Engine.webkit == false) {
        $$('div.slides div.slidevideo').setStyle('display', 'block');
        $$('div.slides div.slidevideo').setStyle('position', 'absolute');
        $$('div.slides div.slidevideo').setStyle('left', -99999);
    }

    constructMovieObject = function(item) {

        var jwFile = item.getProperty('href');
        jwContainer = new Element('span');
        jwContainer.inject(item, 'after');

        var s1 = new SWFObject("/flash/player.swf", "player", item.getElement('img').getStyle('width'), item.getElement('img').getStyle('height'), requiredFlashVersion);
        s1.addParam("allowfullscreen", "true");
        s1.addParam("allowscriptaccess", "always");
        s1.addParam("wmode", "transparent");
        s1.addVariable("file", jwFile);
        s1.addVariable("type", "video");
        s1.addVariable('backcolor', '00402e');
        s1.addVariable('autostart', 'true');
        s1.write(jwContainer);
        item.empty();        
    };

    jwlinks.each(function(item) {
        constructMovieObject(item);
    });

    if ($$('div.f33_accordion').length > 0) {
        $$('div.slides div.slidevideo').removeProperty('style');
    }

}
