/**********
*   Initialisation
**********/
$(document).ready(function () {

    //enable li.hover on submenu
    startList();

    //cufon replacements
    Cufon.replace('ul.menu li a', {fontFamily: 'Helvetica Neue Std', fontWeight: 'bold', fontSize: '12px'});
    Cufon.replace('ul.menu li.searchButton', {fontFamily: 'Helvetica Neue Std', fontWeight: 'bold', fontSize: '12px'});
    Cufon.replace('ul.submenu li a', {fontFamily: 'Helvetica Neue Cond', fontSize: '11px', color: '#ffffff'});
    Cufon.replace('ul.subsubmenu li a', {fontFamily: 'Helvetica Neue Cond', fontSize: '11px', color: '#ffffff'});
    Cufon.replace('div.zoek_een_winkel p', {fontFamily: 'Helvetica Neue Std', fontSize: '13px'});

    Cufon.replace('h2', {fontFamily: 'Helvetica Neue std', fontSize: '14px'});
    Cufon.replace('div.subcat h2', {fontFamily: 'Helvetica Neue Light', fontSize: '32px', fontWeight: 'normal'});
    Cufon.replace('div.vraag h2', {fontFamily: 'Helvetica Neue Light', fontSize: '32px', fontWeight: 'normal'});
    Cufon.replace('div.productpagina h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.product p.kop', {fontFamily: 'Helvetica Neue Std', fontSize: '18px', fontWeight: 'normal'});
    Cufon.replace('div.tekstpagina h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.winkels h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.routeplanner h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.zoeken h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.ondernemer_home h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.ondernemer_nieuws h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.ondernemer_nieuwsbericht h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.ondernemer_acties h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.ondernemer_productinformatie h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.voorjaarsactie_home h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});
    Cufon.replace('div.voorjaarsactie_winnen h2', {fontFamily: 'Helvetica Neue Light', fontSize: '24px', fontWeight: 'normal'});

    Cufon.replace('h3', {fontFamily: 'Helvetica Neue Light', fontSize: '18px'});
    Cufon.replace('div.zoek_een_winkel_groot h3', {fontFamily: 'Helvetica Neue Light', fontSize: '20px'});
    Cufon.replace('div.zoek_een_winkel_groot span', {fontFamily: 'Helvetica Neue Std', fontSize: '13px'});
    Cufon.replace('div.zoek_een_winkel_klein p', {fontFamily: 'Helvetica Neue Std', fontSize: '13px'});

    Cufon.replace('div.zoek_een_winkel_product h3', {fontFamily: 'Helvetica Neue Light', fontSize: '20px'});
    Cufon.replace('div.zoek_een_winkel_product span', {fontFamily: 'Helvetica Neue Std', fontSize: '13px'});

    Cufon.replace('p.bekijk_producten', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.terug', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.alle_winkels', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.routeplanner', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.print', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.vorige', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.vorige_disabled', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.volgende', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.volgende_disabled', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.voorjaarsactie', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.folder_button', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('p.button_leeg', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('span.home', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('span.folder', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});

    Cufon.replace('.wkactie_content .popup .tekst', {fontFamily: 'Helvetica Neue Std', fontSize: '14px'});
    Cufon.replace('.wkactie_content .popup .tekst .klein', {fontFamily: 'Helvetica Neue Std', fontSize: '14px'});
    Cufon.replace('.wkactie_content .popup .tekst .kleinst', {fontFamily: 'Helvetica Neue Std', fontSize: '10px'});
    Cufon.replace('.wkactie_content .popup .inloggen', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('.wkactie_content .prijzen h3', {fontFamily: 'Helvetica Neue Std', fontSize: '14px', fontWeight: 'normal'});
    Cufon.replace('.wkactie_content .prijzen h3 span.groter', {fontFamily: 'Helvetica Neue Std', fontSize: '20px', fontWeight: 'normal'});
    Cufon.replace('.wkactie_content .aanbiedingen h3', {fontFamily: 'Helvetica Neue Std', fontSize: '13px', fontWeight: 'normal'});
    Cufon.replace('.wkactie_content .aanbiedingen h3 span.oranje', {fontFamily: 'Helvetica Neue Std', fontSize: '13px', fontWeight: 'normal', color: '#EB7B25'});
    Cufon.replace('.wkactie_content .aanbiedingen .rechts p', {fontFamily: 'Helvetica Neue Std', fontSize: '12px', fontWeight: 'normal'});
    Cufon.replace('.wkactie_content .own_score', {fontFamily: 'Helvetica Neue Std', fontSize: '14px', fontWeight: 'normal'});
    Cufon.replace('.wkactie_content .own_score .naam', {fontFamily: 'Helvetica Neue Std', fontSize: '18px', fontWeight: 'normal'});
    Cufon.replace('.wkactie_content .own_score .loguit', {fontFamily: 'Helvetica Neue Std', fontSize: '12px', fontWeight: 'normal'});
    Cufon.replace('.addthis_container span.tekst', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});
    Cufon.replace('.addthis_container span.tekst_klein', {fontFamily: 'Helvetica Neue Std', fontSize: '10px'});

    Cufon.replace('.wkactie_content_winnaars span.cufon', {fontFamily: 'Helvetica Neue Std', fontWeight: 'normal'});

    Cufon.replace('#WKwrapper #counter .vervang', {fontFamily: 'Score Board', fontSize: '21px'});
    /*Cufon.replace('#WKwrapper #counter .kleiner', {fontFamily: 'Score Board', fontSize: '14px'});*/
    Cufon.replace('.wkactie_content .highscores', {fontFamily: 'Score Board', fontSize: '16px'});

    //png fixen
    $('.ondernemer_acties').pngFix();
    $('.product .afbeelding .foto').pngFix();
    $('.wkactie_content').pngFix();
    $('p.game_button').pngFix();

    //slideshow servicepunten
    if ($(".servicepunten").length > 0) {
        slideServicepunten();
    }

    //slideshow producten/vragen
    if ($('.homepage_slideshow').length > 0) {
        slideVragen();
    }

    //winkelpagina
    if ($('.winkelSummary').length > 0) {
        $(".winkelSummary").bind("click", displayWinkelInfo);
    }

    //tooltips prijzen voorjaarsactie
    if ($('a.tooltip').length > 0) {
        $('a.tooltip').bstip({
            sticky: 'move',
            color: 'voorjaarsactie_tooltip',
            speed: 'fast',
            type: 'tip',
            tack: 0,
            keep: 2000,
            ynudge: 15,
            xnudge: -30,
            hook: "bottom-right",
            opacity: 1
        });
    }

    //tooltips merken bij producten
    if ($('a.tooltip_merken').length > 0) {
        $('a.tooltip_merken').bstip({
            sticky: 'none',
            color: 'merken_tooltip',
            speed: 'fast',
            type: 'tip',
            tack: 3000,
            keep: 2000,
            ynudge: 15,
            xnudge: -30,
            opacity: 1
        });
    }

    //tips verbergen/tonen voor voorjaarsactie
    if ($('.tips_knoppen a').length > 0) {
        $('.tips_knoppen a').bind('click', function() {

            //wat moet er verdwijnen?
            var sVerdwijnKey = $('.tips_knoppen a.active').attr('rel');

            //wat moet er verschijnen?
            var sVerschijnKey = $(this).attr('rel');

            //knoppen uitzetten
            $('.tips_knoppen a').removeClass('active');

            //knop op active zetten
            $(this).addClass('active');

            //verberg alle tips
            $('#tips_'+sVerdwijnKey).fadeOut('normal', function() {

                //toon benodigde tip als andere verdwenen is
                $('#tips_'+sVerschijnKey).fadeIn();

            });

            return false;
        });
    }

    // Links met rel="external" moeten in een nieuw venster worden geopend
    $('A[rel="external"]').click( function() {
        window.open(jQuery(this).attr('href') );
        return false;
    } );

    //home animatie
    if ( $("#voorjaarsactie_aanjager").length > 0 ) {

        var flashvars = {};
        var params = {
            wmode: "transparent"
        };
        var attributes = {};
        swfobject.embedSWF("flash/bannert_schoonmaken.swf", "voorjaarsactie_aanjager", "208", "319", "8", null, flashvars, params, attributes);

    }
    if ( $("#voorjaarsactie_gereed_aanjager").length > 0 ) {

        var flashvars = {};
        var params = {
            wmode: "transparent"
        };
        var attributes = {};
        swfobject.embedSWF("flash/bannert_schoonmaken_gereed.swf", "voorjaarsactie_gereed_aanjager", "208", "319", "8", null, flashvars, params, attributes);

    }
    if ( $("#wkactie_aanjager").length > 0 ) {

        var flashvars = {};
        var params = {
            wmode: "transparent"
        };
        var attributes = {};
        swfobject.embedSWF("flash/bannert_wkactie.swf", "wkactie_aanjager", "208", "319", "8", null, flashvars, params, attributes);

    }

    //slider van aanbiedingen
    if ( $("#aanbiedingen_slider").length > 0 ) {
        $("#aanbiedingen_slider").easySlider({
            continuous: true,
            prevText: '<img src="images/icon_vorige.png" alt="vorige">',
            nextText: '<img src="images/icon_volgende.png" alt="volgende">',
            auto: true,
            pause: 2000
        });
    }

    if ( $(".simpleModal").length > 0 ) {
        $(".simpleModal").click( function() {
            var options = {
                            minWidth: 398,
                            maxWidth: 398,
                            minHeight: 308,
                            maxHeight: 308,
                            opacity: 50,
                            close: true,
                            closeHTML: "<a class='modalClose' title='Sluiten'><img src='images/icon_close.gif' alt='Sluiten'></a>",
                            escClose: true,
                            overlayClose: true,
                            overlayCss: {
                                            backgroundColor: "#339933"
                                        },
                            containerCss: {
                                            background: "transparent url(images/bg_wkactie_aanbiedingpopup.png) top left no-repeat",
                                            padding: "40px 20px 20px 20px",
                                            textAlign: "left",
                                            heigth: 368,
                                            width: 438
                                          },
                            persist: true
                          }
            var rel = $(this).attr('rel');
            $("#"+rel).modal(options);
        });
    }

});

/*****
*   Menu
*****/

function startList() {
    if (document.getElementById("submenu") != null) {
        var sfEls = document.getElementById("submenu").getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover=function() {
                this.style.width = this.offsetWidth + 'px';
                //console.log(this);
                parentEl = this.parentNode;
                if (parentEl.className != 'subsubmenu') {
                    this.className+=" hover";
                }
            }
            sfEls[i].onmouseout=function() {
                this.className=this.className.replace(new RegExp(" hover\\b"), "");
            }
        }
    }
}

/*****
*   Search field
*****/

function clearSearchField() {

    var searchField = $('.menu .searchField input');
    var data = searchField.val();

    if (data == 'waar wilt u meer over weten?') {
        searchField.val('');
    }

}

function fillSearchField() {

    var searchField = $('.menu .searchField input');
    var data = searchField.val();

    if (data == '') {
        searchField.val('waar wilt u meer over weten?');
    }

}

function formatPostcode(formId) {

    //postcode trimmen
    sPostcode = $("input[name='postcode']").val();
    sPostcode = sPostcode.replace(/\s+/g,'');

    //letters toevoegen
    if (sPostcode.match(/[1-9][0-9]{3}/)==sPostcode) {

        $.getJSON('ajax/getPostcodeLetters.php', {postcode: sPostcode}, function(json) {

            //letters plakken
            if (json != null) {
                sPostcode = sPostcode+json;
            }

            //geformat terugzetten
            $("input[name='postcode']").val(sPostcode);
            //formulier opnieuw versturen
            $(formId).submit();

        });

        //formulier niet laten verzenden tot de postcode is aangepast
        return false;

    } else {

        //geformat terugzetten
        $("input[name='postcode']").val(sPostcode);
        return true;

    }

}

function checkPostcode(formId) {

    if (validatePostalCode($("#"+formId+" input[name='postcode']").val())) {
        //alles is goed, return true
        return true;
    } else {
        //postcode klopt niet, genereer foutmelding en return false
        $("#zoek_winkel_postcode_error").html('U heeft een verkeerde postcode ingevuld.');
        return false;
    }

}

function validatePostalCode(string) {

    if (string.match(/[1-9][0-9]{3} ?[a-zA-Z]{2}/)==string) {
        return true;
    } else {
        return false;
    }

}

function ucwords (str) {
    // Uppercase the first character of every word in a string
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/ucwords    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Waldo Malqui Silva
    // +   bugfixed by: Onno Marsman
    // *     example 1: ucwords('kevin van zonneveld');
    // *     returns 1: 'Kevin Van Zonneveld'    // *     example 2: ucwords('HELLO WORLD');
    // *     returns 2: 'HELLO WORLD'
    return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase( ); } );
}


/*****
*   Service punten
*****/

var currentPosition;
var slideWidth;
var slides;
var numberOfSlides;

function slideServicepunten() {

    currentPosition = 0;
    slideWidth = 182;
    paddingRight = 10;
    slides = $('.servicepunt');
    numberOfSlides = slides.length;

    // Wrap all .slides with #slideInner div
    slides.wrapAll('<div id="slideInner"></div>').css({
        'float' : 'left',
        'width' : slideWidth,
        'paddingRight' : paddingRight
    });

    // Set #slideInner width equal to total width of all slides
    $('#slideInner').css('width', (slideWidth+paddingRight) * numberOfSlides);

    setInterval('slideServicepuntenLeft()', 8000);

}

function slideServicepuntenLeft() {

    //hoog de slide op
    if (currentPosition == (numberOfSlides-1)) {
        $("#slideInner").css('marginLeft', slideWidth+paddingRight);
        currentPosition = 0;
    } else {
        currentPosition = currentPosition+1;
    }

    //animeer slide uit beeld en nieuwe in beeld
    $('#slideInner').animate({
        'marginLeft' : (slideWidth+paddingRight)*(-currentPosition)
    });

}

/*****
*   Homepage vragen slideshow
*****/

function slideVragen() {

    $('.homepage_slideshow').showcase({
        images: aPictures,
        width: "514px",
        height: "319px",
        border: "0px",
        animation: {type: "fade", interval: 5000, speed: 3000, stopOnHover: false}
    });

    $('.homepage_slideshow img').pngFix();

}


/*****
*   Winkels
*****/

function displayWinkelInfo(event) {

    //voorkom doorklik tegen
    event.preventDefault();

    //hide alle infoContainers
    $('.winkel_info').hide();

    //maak alle links unbold
    $('.winkelSummary a').removeClass('bold').removeClass('no_underline').css('color', '#737373');

    //maak deze links bold
    id = $(this).attr('id');
    links = $('#'+ id + ' a').addClass('bold').addClass('no_underline').css('color', '#339933');
    //links = $('#' + id + ' a').css('font-weight', 'bold').css('text-decoration', 'none');

    //laat deze infoContainer zien
    el = $('#' + id + 'Info');
    el.fadeIn('slow');

}


/*****
*   Routeplanner formulier aanpassen
*****/
function setRouteForm() {

    //er is een vertrekadres opgehaald
    if ($('#route_van').val() != '') {

        $('#route_van').unbind('focus');
        $('#route_van').addClass('route_van_vol');
        $('form button.route_submit').removeClass('route_submit').addClass('route_reset');
        $('form button.route_reset').click(function() { clearRouteForm(); return false; });
        return true;

    //er moet nog een vertrekadres ingevuld worden
    } else {

        $('#route_van').removeClass('route_van_vol').val('Vul een vertrekadres en woonplaats in');
        $('#route_van').focus(function() { $('#route_van').val(''); });
        $('form button.route_reset').unbind('click');
        $('form button.route_reset').removeClass('route_reset').addClass('route_submit');
        return false;

    }

}

/*
function setRouteForm() {

    //er is een vertrekadres opgehaald
    if ($('#route_van').val() != '') {

        //
        $('#route_van').unbind('focus');
        $('#route_van').addClass('route_van_vol');

        //verander submit-button in reset-button

        //$('form button.route_submit p.routeplanner a').text('Route aanpassen');
        //$('form button.route_submit').removeClass('route_submit').addClass('route_reset');
        //Cufon.replace('p.routeplanner', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});


        //hang een reset-functie aan de nieuwe reset-button
        $('form button.route_reset').click(function() { clearRouteForm(); return false; });
        return true;

    //er moet nog een vertrekadres ingevuld worden
    } else {

        //maak een inputveld zichtbaar en het veld leeg
        $('#route_van').removeClass('route_van_vol').val('Vul een adres en woonplaats in');
        $('#route_van').focus(function() { $('#route_van').val(''); });

        //haal de reset-functie weg
        $('form button.route_reset').unbind('click');

        //maak van de reset button een submit button
        $('form button.route_submit p.routeplanner a').text('Plan route');
        $('form button.route_reset').removeClass('route_reset').addClass('route_submit');
        Cufon.replace('p.routeplanner', {fontFamily: 'Helvetica Neue Std', fontSize: '12px'});

        return false;

    }

}
*/

function clearRouteForm() {

    $('#route_van').removeClass('route_van_vol').val('Vul een vertrekadres en woonplaats in');
    $('#route_van').focus(function() { $('#route_van').val(''); });
    $('form button.route_reset').unbind('click');
    $('form button.route_reset').removeClass('route_reset').addClass('route_submit');

}


/*****
*   Optin Formulier
*****/

function checkOptForm() {

    var bReturn = true;
    var sError = '';
    $('.error').html(sError);


    if ($('input[name="andere_acties"]').is(':checked') == false) {

        bReturn = false;
        sError = 'U moet akkoord gaan met de voorwaarden.'

    }

    //foutmelding weergeven
    if (sError != '') {
        $('.error').html(sError);
    }

    return bReturn;

}