﻿var firstBreadcrumbItem;
var secondBreadcrumbItem;
var thirdBreadcrumbItem;

$(document).ready(function () {

    initFlash();
    initFlashActions();
    initPopups();
    
    if ($('body').attr('class') == 'nieuws') { showNieuws(); initNieuwsDetail(); }
    if ($('body').attr('class') == 'nieuwsdetail') { $('#popupnieuws').show(); }
    if ($('body').attr('class') == 'contact') { showContact(); }
});

function initPopups() {

    $('#linknieuws').click(function() {
        showNieuws();
        trackPageView('nieuws');
        return false;
    });

    $('#linkcontact').click(function() {
        showContact();
        trackPageView('contact');
        return false;
    });

    $('.close').click(function() {
        hidePopups();
        return false;
    });

    $('a.grootpopup').click(function() {
        window.open($(this).attr('href'));
        return false;
    });    
    
}

function showNieuws() {

    hidePopups();

    $('#popupnieuws .content').load(
        $('#linknieuws').attr('href') + ' #nyro',
        function() {
            $('#popupnieuws').show();
            initNieuwsPager();
            initNieuwsDetail();
            $('#contactlinknieuws').click(function() {
                showContact();
                return false;
            });
        }
    );
}

function initNieuwsPager() {

    $('.nieuwspager').click(
        function() {
            $('#popupnieuws .content').load(
                $(this).attr('href') + ' #nyro',
                function() {
                    initNieuwsPager();
                    initNieuwsDetail();
                }
            );
            return false;

        }
    );
}

function initNieuwsDetail() {

    $('.nieuwsdetail').click(
        function() {
            $('#popupnieuws .content').load(
                $(this).attr('href') + ' #nyro',
                function() {
                    initNieuwsDetailBack();
                }
            );
            return false;
        }
    );
}

function initNieuwsDetailBack() {

    $('a.grootpopup').click(function() {
        window.open($(this).attr('href'));
        return false;
    });   

    $('.nieuwsterug').click(
        function() {
            $('#popupnieuws .content').load(
                $(this).attr('href') + ' #nyro',
                function() {
                    initNieuwsPager();
                    initNieuwsDetail();
                }
            );
            return false;

        }
    );
}

function showContact() {

    hidePopups();

    $('#popupcontact .content').load(
        $('#linkcontact').attr('href') + ' #nyro',
        function() {
            $('#popupcontact').show();
            initContactForm();
        }
    );   
}

function hidePopups() {
    $('#popupnieuws').hide();
    $('#popupcontact').hide();
}

function initContactForm() {

    var strErrorMessageVerplicht = 'verplicht veld'
    var strErrorMessageEmail = 'geen geldig e-mailadres'

    switch (taal) {
        case 'nl':
            strErrorMessageVerplicht = 'verplicht veld';
            strErrorMessageEmail = 'geen geldig e-mailadres';
            break;
        case 'en':
            strErrorMessageVerplicht = 'required field';
            strErrorMessageEmail = 'not a valid email address';
            break;
        case 'de':
            strErrorMessageVerplicht = 'erforderliches Feld';
            strErrorMessageEmail = 'keine gültige EMail-Adresse';
            break;
        case 'fr':
            strErrorMessageVerplicht = 'champ requis';
            strErrorMessageEmail = 'adresse email n\'est pas valide';
            break;
    }

    var validator = $('#frmContact').validate({
        messages: {
            naam: { required: strErrorMessageVerplicht },
            email: { required: strErrorMessageVerplicht, email: strErrorMessageEmail },
            bericht: { required: strErrorMessageVerplicht }
        }
    });

    $('#sendContactForm').click(function() {

        if ($('#frmContact').valid()) {
            var naam = $('#txtNaam').val();
            var email = $('#txtEmail').val();
            var bericht = $('#txtBericht').val();
            var taal = $('#hiddenTaal').val();
            var nieuwsbrief = $('#chkDigitaleNieuwsbrief:checked').val() == 'on';
            $.post(
            '/mail.aspx?taal=' + taal,
            { txtNaam: naam, txtEmail: email, txtBericht: bericht, hiddenTaal: taal, chkDigitaleNieuwsbrief: nieuwsbrief },
            function(data) {
            $('#formulier').html(data);
            }
            )
        }
    }
    );
}

function updateBreadcrumbs(levelOneName, levelOnePath, levelTwoName, levelTwoPath, levelThreeName, levelThreePath) {
    
    var breadcrumbHtmlOutput;

    if (levelThreeName) {
        firstBreadcrumbItem = new BreadcrumbItem(levelOneName, levelOnePath);
        secondBreadcrumbItem = new BreadcrumbItem(levelTwoName, levelTwoPath);  
        thirdBreadcrumbItem = new BreadcrumbItem(levelThreeName);  
    } else if (levelTwoName) {
        firstBreadcrumbItem = new BreadcrumbItem(levelOneName, levelOnePath);
        secondBreadcrumbItem = new BreadcrumbItem(levelTwoName);
        thirdBreadcrumbItem = new BreadcrumbItem();  
    } else {
        firstBreadcrumbItem = new BreadcrumbItem(levelOneName);
        secondBreadcrumbItem = new BreadcrumbItem();        
        thirdBreadcrumbItem = new BreadcrumbItem();  
    }
    
    $('#breadcrumbs div').html(firstBreadcrumbItem.getHTML() + secondBreadcrumbItem.getHTML() + thirdBreadcrumbItem.getHTML());
}

function updateBreadcrumbSection(sectionName) {
    alert(sectionName);    
}

function updateBreadcrumbTitle(title) {
     $('#breadcrumbs span').html(title);
}


function SetMenu(taal) {
    var breadcrumbsTitle = $('#breadcrumbs span').html();
    var breadcrumbs = $('#breadcrumbs div').html();
    $("#menu").load('/default.aspx?taal=' + taal + '&template=inc/menu.htm', function () {
        initFlashActions();
        initPopups();
        $('#breadcrumbs span').html(breadcrumbsTitle);
        $('#breadcrumbs div').html(breadcrumbs);
    });
}

function initFlashActions() {

    $('a.flashAction').click(function() {
        var strPageKey = $(this).attr('rel')
        setSWFAddress(strPageKey);
        if(strPageKey == 'nieuws' || strPageKey == 'contact') trackPageView(strPageKey);
        return false;
    });
}

function trackPageView(pageKey){
    var strLink = '/' + taal + '/' + pageKey
    pageTracker._trackPageview(strLink);
    secondTracker._trackPageview(strLink);
}

function initFlash() {

    var so = new SWFObject("/framework.swf", "lookolook", "100%", "100%", "8", "#ffffff");
    so.addParam("wmode", "opaque");
    so.addVariable("starteiland", starteiland);
    so.addVariable("curTaal", flashTaalIndex);
    so.addVariable("bikestarActieVisibility", bikestarActieVisibility);
    so.addVariable("actie1", "active");
    so.addVariable("actie2", "inactive");
    so.addVariable("actie3", "inactive");
    so.write("flashmovie");
   
}
function setSWFAddress(page) {   
    SWFAddress.setValue('/' + page + '/');
}

// Elk item in de breadcrumbs is een BreadCrumbItem
function BreadcrumbItem(name, path) {
    if (name) {
        this.name = name;
    } else {
    this.name = '';
    }
    this.path = path;
}

// Geeft óf de titel óf HTML-anchor terug
BreadcrumbItem.prototype.getHTML = function () {
    if (this.path) {
        return '<a href="'+this.path+'">'+this.name+'</a> | ';
    } 
    return this.name
}


