$(function() {
    sc = $("#s").html();
    //$('#groot_beoordeling_div').voting(<?=$f[0]["id"]?>,<?=$avg?>,<?=$voted?>);

    overlabel();
    $(".overlabel").focus(function() {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val('').css('color', '').removeClass('overlabel_inactief');
        }
    });
    $(".overlabel").blur(function() {
        if ($(this).val() == '') {
            $(this).val($(this).attr("title")).css('color', '#666').addClass('overlabel_inactief');
        }
    });

    $(".home .stervote").voting();
    $(".profiel .stuurbericht").click(function() {
        if ($(".profiel .plaatsreactie").is(":hidden")) {
            $(".profiel .plaatsreactie").slideDown();
            document.location = '#reacties';
        }
        else {
            $(".profiel .plaatsreactie").slideUp();
        }
        return false;
    });

    $("a[href*='/reageer']").click(function() {
        if (!sc) {
            notices('error', 'Log in om een reactie te plaatsen!');
            return false;
        }
    });

    $(".meer_emoticons").click(function() {
        $(".meer_emoticons_venster").fadeIn();
    });
    $(".meer_emoticons_venster .sluit").click(function() {
        $(".meer_emoticons_venster").fadeOut();
    });


    //frames
    parent.document.title = $("title").html();

    $(".vriendje_toevoegen").click(function() {
        loading(1);
        c = $(this).attr("id").substr(2);
        $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'vriendje',c:c},success:function(data) {
            t = data.split('|');
            if (t[0] == "q") {
                if (!confirm('Je hebt toestemming nodig van deze gebruiker, toestemming vragen?')) {
                    loading();
                    return false;
                }
                $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'vriendje_toestemming',c:c},success:function(data) {
                    o = data.split('|');
                    notices(o[0], o[1]);
                    loading();
                }});

            }
            else {
                notices(t[0], t[1]);
                loading();
            }

        }});
    });

    if ($(".profiel .show_reactie").size()) {
        $(".profiel .plaatsreactie").slideDown();
    }

    linkblur(); // links blurren

    $(".zalen_selectiebalk input").click(function() {
        var zalen = Array();
        if ($(this).attr("id") == "check_allezalen") {
            $(".zalen_selectiebalk input[type='checkbox']").attr("checked", "checked");
        }
        $(".zalen_selectiebalk input[type='checkbox']:checked").each(function() {
            if ($(this).attr("name") == "check_zaal") {
                zalen.push($(this).val());
            }
        });
        zalen = zalen.join(',');
        loading(1);
        try {
            $.ajax({url:'/ajax/actions.php',type:"POST",data:{
                action:'favoriete_zalen',zalen:zalen},success:function() {
                location.reload(true);
            }});
        } catch(err) {
        }
    });

    if ($("#gallery_foto").size()) {
        nr = parseInt($(".foto_nr").html());
        laad_foto(nr);
    }
    $(".foto_vorige").click(function() {
        switch_foto('vorige');
    });
    $(".foto_volgende,#gallery_foto").click(function() {
        switch_foto('volgende');
    });
    $("#gallery_foto").click(function() {
        laad_foto;
    });
    $(".slideshow_knop").click(function() {
        start_slideshow();
        return false;
    });
    $(".gallery .knop_ecard").click(function() {
        if (!sc) {
            notices('error', 'Je moet ingelogd zijn om een ecard te versturen');
        } else {
            $(".gallery .ecard").fadeIn();
        }
    });
    $(".gallery .annuleren_ecard").click(function() {
        $(".gallery .ecard").fadeOut();
    });
    $("#gallery_reactie").focus(function() {
        setTimeout("$('#gallery_reactie').animate({height:'120px'}, 300)", 50);
    });
    $(".switchvideo").click(function() {
        if ($('.video').is(':hidden')) {
            $('.video').show();
            $('.updates').hide();
            $('.switchvideo').html('bekijk je updates &gt;&gt;');
            init_youtube();
            setTimeout('ytplayer.playVideo()', 100);
        }
        else {
            $('.video').hide();
            $('.updates').show();
            $('.switchvideo').html('bekijk video van deze zaal &gt;&gt;');
            ytplayer.stopVideo();
        }

    });
    $(".members .zoek_kader .knop_klein").click(function() {
        $(".members .zoek_kader .zoekform").submit();
    });
    $(".members .zoek_kader .zoekform").submit(function() {
        naam = $(".zoekform [name=naam]").val();
        if (naam == $(".zoekform [name=naam]").attr("title")) {
            naam = '';
        }
        plaats = $(".zoekform [name=plaats]").val();
        if (plaats == $(".zoekform [name=plaats]").attr("title")) {
            plaats = '';
        }
        geslacht = '';
        if ($(".zoekform #g_m").is(":checked")) {
            geslacht += 'm';
        }
        if ($(".zoekform #g_v").is(":checked")) {
            geslacht += 'v';
        }
        online = '';
        if ($(".zoekform #online").is(":checked")) {
            online += '1';
        }
        searchstring = escape(naam) + ',' + escape(plaats) + ',' + geslacht + ',' + online;
        document.location = '/members/1/' + searchstring;

        return false;
    });

    if ($("#notices div").size() > 0) {
        $("#notices .ok").each(function() {
            notices('ok', $(this).html());
        });
        $("#notices .error").each(function() {
            notices('error', $(this).html());
        });

    }

    //periode
    $("input[name=toon_periode]").bind("click change keyup", function(e) {

        if ($("input[name=toon_periode]").is(":checked")) {
            $(".toon_periode").fadeIn();
        }
        else {
            $(".toon_periode").fadeOut();
        }

    });


    //sponsor imgs
    $(".sponsor .thumbs img").click(function() {
        src = $(this).attr("src");
        newsrc = src.replace(/klein/, "groot");
        $(".sponsor .sponsorimg").attr("src", newsrc);
    });

    //laad_fotolijst
    if ($("#flashheader").size() > 0) {
        ingelogd1 = 0;
        if ($("#s").html() != '') {
            ingelogd1 = 1;
        }
        uniq = $("#player_id").html();
        zaal1 = $("#zaal").html();
        u1 = $("#u").html();
        var params = { allowScriptAccess: "always", wmode: "transparent" };
        var vars = { pagina: $("#pag").html(),zaal: zaal1, rand: uniq, ingelogd: ingelogd1,u: u1  };
        swfobject.embedSWF("/swf/header/swf/zaal" + zaal1 + ".swf", "flashheader", "970", "352", "8", null, vars, params);
    }


    $(".emoticon").click(function() {

        $("#forumpost").trigger("focus");
        code = $(this).attr("title");
        insertAtCursor('forumpost', ' ' + code + ' ');
        $(".meer_emoticons_venster").fadeOut();
    });

    new function($) {
        $.fn.setCursorPosition = function(pos) {
            if ($(this).get(0).setSelectionRange) {
                $(this).get(0).setSelectionRange(pos, pos);
            } else if ($(this).get(0).createTextRange) {
                var range = $(this).get(0).createTextRange();
                range.collapse(true);
                range.moveEnd('character', pos);
                range.moveStart('character', pos);
                range.select();
            }
        }
    }(jQuery);


    //gallery
    $(".gallery .album").click(function() {
        id = $(this).attr("id").split("_")[1];
        document.location = '/gallery/fotos/' + id + '/';
    });

    monthNames = ["Januari","Februari","Maart","April","Mei","Juni","Juli","Augustus","September","Oktober","November","December"];
    monthNamesShort = ["Jan","Feb","Ma","Apr","Mei","Jun","Jul","Aug","Sep","Okt","Nov","Dec"];
    dayNames = ["Zondag","Maandag","Dinsdag","Woensdag","Donderdag","Vrijdag","Zaterdag"];
    dayNamesShort = ["Zon","Maa","Din","Woe","Don","Vri","Zat"];
    dayNamesMin = ["Zo","Ma","Di","Wo","Do","Vr","Za"];

    $("#album_datum").datepicker({ altFormat:'dd-mm-yy',
        dayNamesMin:dayNamesMin,
        closeText:'sluit',
        monthNames:monthNames,
        altField: "#album_datum"});

    $("#datum_van").datepicker({ altFormat:'dd-mm-yy',
        dayNamesMin:dayNamesMin,
        closeText:'sluit',
        monthNames:monthNames,
        altField: "#datum_van"});

    $("#datum_tot").datepicker({ altFormat:'dd-mm-yy',
        dayNamesMin:dayNamesMin,
        closeText:'sluit',
        monthNames:monthNames,
        altField: "#datum_tot"});

    //form
    $(".tijdmask").mask("99:99");
    $(".datummask").mask("99-99-9999");
    if ($(".ie").size() == 0) {
        $(".input_gebruikersnaam").alphanumeric({allow:"-._"});
        ;
        $(".input_email").alphanumeric({allow:"!#$%&'*+-/=?^_`{|}~@."});
        $(".input_wachtwoord").alphanumeric({allow:".-_"});
        $(".numeric").numeric({allow:"- "});
    }

    $(".email").each(function() {
        v = $(this).html().split('|');
        if (v[1]) {
            m = v[0] + '@' + v[1];
            $(this).html('<a href="mailto:' + m + '" title="Stuur een e-mail">' + m + '</a>');
        }
    });


    $(".verwijder").click(function() {
        if (!confirm('Verwijderen?')) {
            return false;
        }
        $.post("/ajax/actions.php", {action:'verwijderen',str:$(this).attr("id") }, function(data) {
            location.reload(true)
        });
        return false;
    });

    $(".gallery .jaarselect a").click(function() {
        if (!sc) {
            notices('error', 'Je moet ingelogd zijn om andere jaren te kunnen bekijken');
            return false
        }

    });

    //open tweets in new frame
    $(".tweet_wrapper a").attr("target", "_blank");

    $("#contact_verzenden").click(function() {
        naam = $("[name='naam']").val();
        bedrijfsnaam = $("[name='bedrijfsnaam']").val();
        email = $("[name='email']").val();
        telefoon = $("[name='telefoon']").val();
        bericht = $("[name='bericht']").val();

        if (!naam) {
            alert('Vul je naam in.');
            return false;
        }
        if ((!email) && (!telefoon)) {
            alert('Vul een e-mail adres of telefoonnummer in.');
            return false;
        }
        if (email) {
            if (!validate_email(email)) {
                alert('Het opgegeven e-mail adres is niet geldig.');
                return false;
            }
        }
        if (!bericht) {
            alert('Vul een bericht in.');
            return false;
        }

        $("#contact_form").submit();
    });

    if ($(".home .actiematige_content img").size() > 1) {
        actiematig();
    }

    $(".blok").click(function() {
        this_id = $(this).attr("id");
        id = this_id.substr(5);
        this_src = $("#" + this_id).attr("src");
        if (this_src.indexOf('geblokkeerd') > -1) {
            nstatus = 'ok';
        } else {
            nstatus = 'geblokkeerd';
        }
        $.post("/ajax/actions.php", {action:'blok',id:id,status:nstatus}, function(data) {
        });
        $("#" + this_id).attr("src", '/img/ico/' + nstatus + '.png');
    });

    $(".blok1").click(function() {
        notices('error', 'Je kunt je eigen status niet aanpassen');
    });


    $(".zoekgebr").click(function() {
        zs = $(".zoekgebruiker").val();
        document.location = '/beheer/gebruikers/1/' + escape(zs);
    });

    $(".a_form").submit(function() {
        this_id = $(this).attr("id")
        if ($("#" + this_id).hasClass('form_submitting')) {
            return false;
        }
        $(this).addClass('form_submitting');

        $('#' + this_id + ' .overlabel_inactief').val('');

        //tinymce
        if (typeof tinyMCE != "undefined") {
            tinyMCE.triggerSave(true, true);
        }
        val = $(this).serialize();
        $('.overlabel_inactief').trigger('blur');
        loading(1);
        $.post("/ajax/forms.php", {form:this_id,val:val}, function(data) {
            loading();
            $("#" + this_id).removeClass('form_submitting');
            errors = data.error,ok_red = data.ok_red,ok_func = data.ok_func,error_text = '';
            if (errors) {
                error_text = '\n - ' + errors.join('\n - ');
                alert('Er ging iets fout:\n' + error_text);
                return false;
            }
            else {
                try {
                    if (ok_red) {
                        window.location = ok_red;
                        return false;
                    }
                    //if (ok_red)	{ return false; }
                    if (ok_func) {
                        eval(ok_func);
                        return false;
                    }
                }
                catch(err) {
                }
            }
            alert('Er is iets fout gegaan bij het opslaan van het formulier. Probeer het op een later tijdstip opnieuw');
        }, "json");
        return false;
    });

    $(".a_submit").click(function() {
        $(this).parents('.a_form').submit();
    });

    if ($("#fotolijst").size()) {
        laad_fotolijst();
    }

    //start youtube
    if ($(".ytvideo").size()) {
        init_youtube();
    }


    if ($("#gmap").size() > 0) {

        function createMarker(point, icon, tooltip, html) {
            var marker = new GMarker(point, {title:tooltip});
            //GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
            return marker;
        }

        groenestip = new GIcon();
        groenestip.image = "http://wandelen.wiredev3.nl/img/poi/stip.png";
        groenestip.shadow = "http://wandelen.wiredev3.nl/img/googlemaps/schaduw.png";
        groenestip.iconSize = new GSize(31, 31);
        groenestip.shadowSize = new GSize(39, 39);
        groenestip.iconAnchor = new GPoint(10, 10);
        groenestip.infoWindowAnchor = new GPoint(9, 14);
        groenestip.infoShadowAnchor = new GPoint(18, 25);

        map = new GMap2(document.getElementById("gmap"));
        map.setUIToDefault();
        bruinslocatie = new GLatLng(52.33036790548537, 6.806164383888245);
        map.setCenter(bruinslocatie, 16);
        map.setMapType(G_HYBRID_MAP);

        var marker = createMarker(bruinslocatie, groenestip, '', '');
        map.addOverlay(marker);

    }

    $(".routebeschrijving_knop").click(function() {
        if ($(".routebeschrijving_input").hasClass("overlabel_inactief")) {
            notices('error', 'Vul jouw adres/postcode in');
            return false;
        }
        adres = escape($(".routebeschrijving_input").val());
        window.open('http://maps.google.nl/maps?f=d&source=s_d&saddr=' + adres + '&daddr=Gravenallee+2+7597+LR');

    });

    if (($("#fv").html() == "1") && ($("#muz_afsp").html() == "1")) {
        setTimeout("header_play()", 1000);
        setTimeout("header_play()", 3000);
        setTimeout("header_play()", 5000);
    }


    //beer
    $("#beer_overlay .close").click(function() {

        $("#beer_overlay,#overlay").fadeOut();
        return false;
    })


});

function linkblur() {
    $("a,area,map,object").bind("focus", function() {
        if ($(this).blur)$(this).blur();
    });
}


function make_player(zaal) {
    $(".video_wrapper").html('');
    $(".zalen .wrapper").css('height', '260px');
    $(".zalen .wrapper.zaal" + zaal).css('height', '510px');
    $(".zalen .play_video").show();
    $(".zalen .knop" + zaal + ".play_video").hide();

    var ythtml = '	<div class="video">'
            + '         <div class="p1"><div class="p2"><div class="p3">'
            + '       	<div class="video_zaalnr dn">' + zaal + '</div>'
            + '       	<div class="video_wrap"><div id="ytvideo"></div></div>'
            + '			 <div class="knop_play"></div><a href="javascript:;" class="knop_play_a">&nbsp;</a>'
            + '			<div class="knop_pause"></div><a href="javascript:;" class="knop_pause_a">&nbsp;</a>'
            + '          <div class="knop_stop"></div><a href="javascript:;" class="knop_stop_a">&nbsp;</a>'
            + '          <div class="knop_mute"></div><a href="javascript:;" class="knop_mute_a">&nbsp;</a>'
            + '        </div></div></div>'
            + '    </div>';

    $("#video_wrapper" + zaal).html(ythtml);
    linkblur();

    is_init_youtube = 0;
    init_youtube();
}


var is_init_youtube = 0;
function init_youtube() {
    if (is_init_youtube == 1) {
        return;
    }
    is_init_youtube = 1;
    var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
    var atts = { id: "ytvideo"  };
    swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&version=3",
            "ytvideo", "320", "238", "8", null, null, params, atts);

    bind_yt_knoppen();
}

function bind_yt_knoppen() {
    $(".knop_play_a").click(function() {
        if (!ytplayer) {
            return;
        }
        ytplayer.playVideo();
    });
    $(".knop_pause_a").click(function() {
        if (!ytplayer) {
            return;
        }
        ytplayer.pauseVideo();
    });
    $(".knop_stop_a").click(function() {
        if (!ytplayer) {
            return;
        }
        ytplayer.stopVideo();
    });

    var muted = 0;
    $(".knop_mute_a").click(function() {
        if (!ytplayer) {
            return;
        }
        if (muted == 0) {
            $('.knop_mute').css('background-image', 'url(/img/video/mute.png)');
            ytplayer.mute();
            muted = 1;
        }
        else {
            $('.knop_mute').css('background-image', 'url(/img/video/audio.png)');
            ytplayer.unMute();
            muted = 0;
        }
    });
}
//einde youtube


function overlabel() {
    $(".overlabel").each(function() {
        if ($(this).val() == '') {
            $(this).val($(this).attr("title")).css('color', '#666').addClass('overlabel_inactief');
        }
    });
}

function laad_foto(nr) {
    foto_nrs = $(".foto_nrs").html().split(',');
    aantal_fotos = foto_nrs.length - 1;
    this_album_id = parseInt($(".album_id").html());
    foto_arr_nr = foto_nrs[nr];

    setTimeout("this_preload(" + nr + ")", 1000);

    $.post("/ajax/actions.php", {action:'get_beoordeling',ref_id:this_album_id + "_" + foto_arr_nr}, function(data) {
        t = data.split('_');
        score = t[0];
        this_voted = t[1];
        $(".stervote").html(this_album_id + '_' + foto_arr_nr + ' ' + score + ' ' + this_voted + ' 0');
        $(".stervote").voting();
    });
    $("#foto_reactie_hidden").val(this_album_id + '.' + foto_arr_nr);
    $(".foto_totaal").html(aantal_fotos);
    $(".foto_huidig").html(nr);

    if ($('#gallery_reactie').css('height') != '40px') {
        $('#gallery_reactie').animate({height:'40px'}, 300);
    }

    setTimeout("laad_foto_reacties(1)", 400);
}

function clear_foto_reacties() {
    $("#gallery_reactie").val('');
}

function start_slideshow() {
    if ($(".slideshow_knoptekst").html() == 'Stop slideshow') {
        stop_slideshow();
        return false;
    }
    $(".slideshow_knoptekst").html('Stop slideshow');
    notices('ok', 'De slideshow is gestart');
    slideshow_run();
}

function stop_slideshow() {
    $(".slideshow_knoptekst").html('Start slideshow');
    notices('error', 'De slideshow is gestopt');
}

$(function() {
    $(".gallery").click(function() {
        if ($(".slideshow_knoptekst").html() == 'Stop slideshow') {
            stop_slideshow();
            return false;
        }
    });
}); //slideshow stoppen bij klikken op pagina

function slideshow_run() {
    if ($(".slideshow_knoptekst").html() == 'Start slideshow') {
        return;
    }
    switch_foto('volgende');
    setTimeout('slideshow_run()', 3000);
}

function laad_foto_reacties(pag) {
    arr_nr = parseInt($(".foto_arr_nr").html());
    album_id = parseInt($(".album_id").html());

    $(".foto_reacties").load("/ajax/gallery_reacties.php?ref_id=" + album_id + "_" + arr_nr + '&pagina=' + pag, function() {
        $(".gallery .bekijk_foto .nav_i").click(function() {
            selectie = $(this).html();
            pag = parseInt($(".gallery .bekijk_foto .nav_a").html());
            if (selectie == '&gt;&gt;') {
                pag = pag + 1;
            }
            else if (selectie == '&lt;&lt;') {
                pag = pag - 1;
            }
            else {
                pag = parseInt(selectie);
            }
            laad_foto_reacties(pag);
            return false;
        });

    });
}
function switch_foto(richting) {
    foto_nrs = $(".foto_nrs").html().split(',');
    aantal_fotos = foto_nrs.length - 1;
    huidig_nr = parseInt($(".foto_huidig").html());

    this_album_id = parseInt($(".album_id").html());

    if (richting == 'vorige') {
        if (huidig_nr == 1) {
            huidig_nr = aantal_fotos;
        } else {
            huidig_nr = huidig_nr - 1;
        }
    }
    if (richting == 'volgende') {
        if (huidig_nr == aantal_fotos) {
            huidig_nr = 1;
        } else {
            huidig_nr = huidig_nr + 1;
        }
    }

    foto_arr_nr = foto_nrs[huidig_nr];

    foto_title = $('.fotonaam').html();
    foto_title = foto_title.replace('[huidig]', huidig_nr);
    foto_title = foto_title.replace('[totaal]', aantal_fotos);
    downloadlink = $('.downloadlinkurl').html();
    downloadlink = downloadlink.replace('[huidig]', foto_arr_nr);
    $('.downloadlink').attr("href", downloadlink);
    parent.document.title = foto_title;
    $("input[name=foto]").val(this_album_id + '.' + foto_arr_nr);

    $("#foto_wrap").prepend('<img src="/gfx/foto/' + this_album_id + '/' + foto_nrs[huidig_nr] + '.jpg" alt="" class="gi"/>');
    $("#foto_wrap .gi:last").fadeOut(500, function() {
        $("#foto_wrap .gi:last").remove()
    });
    $(".ghost").attr("src", "/gfx/foto/" + this_album_id + "/" + foto_nrs[huidig_nr] + ".jpg");
    $(".foto_huidig_nr").html(huidig_nr);
    $(".foto_arr_nr").html(foto_arr_nr);

    laad_foto(huidig_nr);
    set_share_icons();

}

function set_share_icons() {

    this_album_id = $("input[name=foto]").val();
    foto_arr_nr = parseInt($(".foto_arr_nr").html());
    target_url = document.location.protocol + '//' + document.domain + '/gallery/foto/' + this_album_id.split('.').join('/');
    body_bg = colors['content_bg'];
    $(".share_icons").html('<iframe width="330" height="50" frameborder="0" scrolling="no" src="/share.php?url=' + target_url + '&amp;bgcolor=' + body_bg + '" class="like"></iframe>');
}

$(function() {
 if ($("body.gallery div.foto_wrapper").length) {
        set_share_icons();
    }
})

function this_preload(nr) {
    foto_nrs = $(".foto_nrs").html().split(',');
    aantal_fotos = foto_nrs.length - 1;
    this_album_id = parseInt($(".album_id").html());
    if (nr == 1) {
        vorig_nr = aantal_fotos;
    } else {
        vorig_nr = nr - 1;
    }
    if (nr == aantal_fotos) {
        volgend_nr = 1;
    } else {
        volgend_nr = nr + 1;
    }

    $('.foto_preload').html('<img src="/gfx/foto/' + this_album_id + "/" + foto_nrs[vorig_nr] + '.jpg" alt=""/>');
    $('.foto_preload').append('<img src="/gfx/foto/' + this_album_id + "/" + foto_nrs[volgend_nr] + '.jpg" alt=""/>');
}

function toestemming(jn, code) {
    $.ajax({url:'/ajax/actions.php',type:"POST",data:{
        action:'vriendje_bevestig',jn:jn,code:code},success:function(data) {
        if (data == 'geweigerd') {
            notices('ok', 'Het verzoek is geweigerd');
            $('#toestemming_' + code).fadeOut();
        }
        if (data == 'toegevoegd') {
            $('toestemming_' + code).hide();
            location.reload(true);
        }

    }});
}

function ecard_verstuurd() {
    $("#verstuur_ecard").clearForm();
    overlabel();
    notices('ok', 'De ecard is verstuurd!');
    $(".gallery .ecard").fadeOut();
}

video_ids = Array('', 'edtPWSX4-kQ', 'QH_WePrkIAM', '-CaMyvx2_V8', 'qg6orwL0XCU', 'hw_tDqP6QWo', 'Jjhx5PU_nIU', 'JqjqVK71LXg');

function onYouTubePlayerReady(playerId) {
    if ($(".zalen").size()) {
        zaalnr = parseInt($(".video_zaalnr").html());
        ytplayer = document.getElementById("ytvideo");
        ytplayer.loadVideoById(video_ids[zaalnr]);
        ytplayer.addEventListener("onStateChange", "youtube_state_change");
        ytplayer.playVideo();
    }

    if ($(".home").size()) {
        this_zaal = $("#zaal").html();
        this_zaal_fv = $("#z_fv").html();
        video_id = '';
        if (this_zaal < 0) {
            video_id = video_ids[this_zaal];
        }

        ytplayer = document.getElementById("ytvideo");
        ytplayer.loadVideoById(video_id);
        ytplayer.addEventListener("onStateChange", "youtube_state_change");

        if (this_zaal_fv == 1) {
            ytplayer.playVideo();
            audio_pause();
        }
        else {
            ytplayer.pauseVideo();
            audio_start();
        }

        header_play();
    }
}

function youtube_state_change(state) {
    if (!ytplayer) {
        audio_start();
        return false;
    }
    if ((state == 3) || (state == 1)) {
        audio_pause();
    } else {
        audio_start();
    }
}

function audio_start() {
    if ($('#flashheader').length) { //notices('ok','header play');
            document.getElementById('flashheader').sendTextToFlash('play');
    }
}

function audio_pause() {
        document.getElementById('flashheader').sendTextToFlash('pause');
}

function header_play() {
    if ($("#ytplayer").length==0) {
        audio_start();
        return;
    }
    if (!ytplayer) {
        audio_start();
        return;
    }
    var state = ytplayer.getPlayerState();

    youtube_state_change(state);
}

function loading(start) {
    if (start == 1) {
        $("#loading").fadeIn('fast');
        $("input [type=submit],.a_submit").attr("disabled", "disabled");
    }
    else {
        $("#loading").fadeOut('fast');
        $("input [type=submit],.a_submit").removeAttr("disabled");
    }
}

function notices(type, msg) {
    rand = Math.random().toString().substr(2);
    if ($("#notices div").size() > 5) {
        return;
    }
    tijd = 500 + msg.length * 80;

    if (type == 'error') {
        $("#notices").append("<div class='error' id='a" + rand + "'>" + msg + "</div>");
    }

    if (type == 'ok') {
        $("#notices").append("<div class='ok' id='a" + rand + "'>" + msg + "</div>");
    }

    $("#a" + rand).slideDown();
    setTimeout('$("#a' + rand + '").slideUp(300);', tijd);
    setTimeout('$("#a' + rand + '").remove();', tijd + 500);
}

function actiematig() {
    //html = $(this).html();
    html = $(".actiematige_content div:last").html();
    $(".actiematige_content img:last").fadeOut(400, function() {
        $(".actiematige_content").prepend('<div>' + html + '</div>');
        $(".actiematige_content div:last").remove();//remove();

        setTimeout("actiematig()", 5000);
    });
}

function selecteerallepics() {
    $("#fotolijst li").addClass("ui-selected").removeClass("unselected");
}
function deselecteerallepics() {
    $("#fotolijst li").removeClass("ui-selected").addClass("unselected");
}
function selectpicsreverse() {
    $("#fotolijst li").each(function() {
        if ($(this).hasClass('ui-selected')) {
            $(this).removeClass('ui-selected').addClass('unselected');
        }
        else {
            $(this).addClass('ui-selected').removeClass('unselected');
        }
    });
}
function selectpicsdelete() {
    ids = [];
    nr = $(".ui-selected img:visible").size();
    if (nr < 1) {
        alert('Er zijn geen foto\'s geselecteerd');
        return;
    }
    if (!confirm(nr + ' foto\'s verwijderen?')) {
        return
    }
    $("#fotolijst li:visible").each(function() {
        if ($(this).hasClass('ui-selected')) {
            attr = $(this).attr("id").substr(2);
            if (attr) {
                ids.push(attr);
            }
            $(this).fadeOut('fast');
        }
    });
    $.ajax({url:'/ajax/fotoalbum.php',type:"POST",data:{
        action:'foto_lijst_verwijderen',album_id:$('#album_id').val(),pag:$('.album_pag').html(),ids:ids.join(',')}});
}
function select_album_img() {
    ids = [];
    if ($("#fotolijst img").size() == 0) {
        alert('Er moeten eerst foto\'s worden geupload voordat een album-afbeelding kan worden geselecteerd.');
        return;
    }
    nr = $(".ui-selected img:visible").size();
    if (nr != 1) {
        alert('Selecteer ��n afbeelding om als album-afbeelding te gebruiken.');
        return;
    }
    selected_id = $(".ui-selected img:visible").parent().attr("id").substr(2);
    $.ajax({url:'/ajax/fotoalbum.php',type:"POST",beforeSend:function() {
        loading(1)
    },data:{ action:'set_album_img',album_id:$('#album_id').val(),pag:$('.album_pag').html(),id:selected_id},
        success:function() {
            $("#album_img").attr("src", "/gfx/" + $('.album_pag').html() + "img/" + $('#album_id').val() + ".jpg?" + Math.random());
            setTimeout('loading();', 1800);
        }
    });
}
function laad_fotolijst() {
    album_id1 = $("#album_id").val();
    pag1 = $('.album_pag').html();
    $.ajax({url:'/ajax/fotoalbum.php',data:{ action:'foto_lijst',pag:pag1,album_id:album_id1 },success:function(data) {
        $("#fotolijst").html(data);
        $("#fotolijst").selectable();
    }});
}

function verwijder_vriendje(id) {
    if (!confirm('Weet je zeker dat je deze gebruiker wilt wissen uit je vriendenlijst?')) {
        return false;
    }

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'vriendje_verwijderen',id:id},success:function(data) {
        $("#vriendje_" + id).fadeOut();
        t = data.split('|');
        notices(t[0], t[1]);
        loading();

    }});
    return false;
}

function del_profiel_reactie(id) {
    if (!confirm('Weet je zeker dat je deze reactie wilt verwijderen?')) {
        return false;
    }

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'del_profiel_reactie',id:id},success:function(data) {
        t = data.split('|');
        notices(t[0], t[1]);
        if (t[0] == 'ok') {
            $("#prof_reactie_" + id).fadeOut();
        }
    }});
    return false;
}

function del_gallery_reactie(id) {
    if (!confirm('Weet je zeker dat je deze reactie wilt verwijderen?')) {
        return false;
    }

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'del_gallery_reactie',id:id},success:function(data) {
        t = data.split('|');
        notices(t[0], t[1]);
        if (t[0] == 'ok') {
            $("#reactie_gallery_" + id).fadeOut();
        }
    }});
    return false;
}

function del_forum_reactie(id, onderwerp) {
    if (onderwerp == 1) {
        if (!confirm('Weet je zeker dat je deze reactie wilt verwijderen?\n\nLet op: Alle posts binnen dit onderwerp worden ook verwijderd!')) {
            return false;
        }
    }
    if (onderwerp != 1) {
        if (!confirm('Weet je zeker dat je deze reactie wilt verwijderen?')) {
            return false;
        }
    }

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'del_forum_reactie',id:id},success:function(data) {
        if (onderwerp == 1) {
            notices('ok', 'Het bericht is verwijderd, een moment geduld...');
            setTimeout("document.location='/forum/';", 3000);
            return;
        }

        t = data.split('|');
        notices(t[0], t[1]);
        if (t[0] == 'ok') {
            $("#reactie_forum_" + id).fadeOut();
        }
    }});
    return false;
}

var temp_reacties = Array();
function edit_forum_reactie(id) {
    if ($("#reactie_forum_" + id + " textarea").size()) {
        return false;
    }
    loading(1);
    $.ajax({url:'/ajax/edit_bericht.php',type:"GET",data:{ action:'edit_forum_reactie',id:id},success:function(data) {
        temp_reacties[id] = $("#reactie_forum_" + id + " .tekst").html();
        $("#reactie_forum_" + id + " .tekst").html(data);
        $(".edit_reactie_textarea").autogrow();
        loading(0);
    }});
}

function edit_forum_annuleren(id) {
    $("#reactie_forum_" + id + " .tekst").html(temp_reacties[id]);
}


function reactie_edit_opslaan(id, reactie) {
    loading(1);
    reactie_contents = $("#edit_reactie_textarea_" + id).val();

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'update_reactie',reactie:reactie_contents,id:id},success:function(data) {
        data = unescape(data);
        $("#reactie_forum_" + id + " .tekst").html(data);
        loading(0);
    }});
}

function reageren(id) {
    loading(1);
    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'quote',id:id},success:function(data) {
        loading(0);
        data = unescape(data);
        //$("#reactie_forum_"+id+" .tekst").html(data);
        document.location = '#reageer';
        if ($("#forumpost").val() == data) {
            return false;
        }
        $("#forumpost").focus().val($("#forumpost").val() + data);
        document.getElementById('forumpost').scrollTop = document.getElementById('forumpost').scrollHeight;

    }});
}

function fav_foto_del(album, nr) {
    if (!confirm('Weet je zeker dat je deze foto wilt wissen uit je favorieten?')) {
        return false;
    }

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'fav_foto_verwijderen',album:album,nr:nr},success:function(data) {
        $("#fav_foto_" + album + "_" + nr).fadeOut();
        t = data.split('|');
        notices(t[0], t[1]);
        setTimeout('location.reload(true);', 300);
    }});

    return false;
}

function gespot_del(album, nr) {
    if (!confirm('Weet je zeker dat je deze foto wilt wissen uit je gespotte foto\'s?')) {
        return false;
    }

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'gespot_verwijderen',album:album,nr:nr},success:function(data) {
        $("#gespot_" + album + "_" + nr).fadeOut();
        t = data.split('|');
        notices(t[0], t[1]);
        setTimeout('location.reload(true);', 300);
    }});
    return false;
}

function fav_foto() {
    if (!sc) {
        notices('error', 'Je moet ingelogd zijn om een favoriete foto toe te voegen');
    }
    album = $(".album_id").html();
    arr_nr = parseInt($(".foto_arr_nr").html());

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'fav_foto',album:album,nr:arr_nr},success:function(data) {
        t = data.split('|');
        notices(t[0], t[1]);
    }});
    return false;
}

function gespot() {
    if (!sc) {
        notices('error', 'Je moet ingelogd zijn om jezelf te kunnen spotten');
    }
    album = $(".album_id").html();
    arr_nr = parseInt($(".foto_arr_nr").html());

    $.ajax({url:'/ajax/actions.php',type:"POST",data:{ action:'gespot',album:album,nr:arr_nr},success:function(data) {
        t = data.split('|');
        notices(t[0], t[1]);
    }});
    return false;
}

$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};


function add_coin(post_id,type) {
    if (!post_id) return false;
    var type_text = '';
    if (type=='b') { type_text = 'bier'; }
    if (type=='w') { type_text = 'wijn'; }
    if (!confirm('Deze post een '+type_text+'munt toekennen?')) {
        return false;
    }
    $.post("/ajax/actions.php", {action:'coin_add',post_id:post_id,type:type }, function(data) {
        alert(data);
    });
    return false;

}

function frame_upload(actie, file) {
    loading();
    src = $("#uploadframe").attr("src");

    if (actie == 'laden') {
        loading(1);
    }

    if (actie == 'annuleren') {
        $("#uploadframe").attr('src', src);

    }
    if (actie == 'error') {
        alert('Het bestand kon niet worden geupload. Probeer een andere');
        $("#uploadframe").attr('src', src);
    }
    if (actie == 'laden_klaar') {
        setTimeout("$('.upload_mask').css('background','url(" + file + "&a=" + Math.random() + ")')", 300);
        $("#uploadframe").attr('src', src);
    }
}


function validate_email(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(email)) {
        return true;
    } else {
        return false;
    }
}


function insertAtCursor(myField, myValue) {
    //IE support
    myField = document.getElementById(myField);
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }

    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
                + myValue
                + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

function css_browser_selector(u) {
    var ua = u.toLowerCase(),is = function(t) {
        return ua.indexOf(t) > -1;
    },g = 'gecko',w = 'webkit',s = 'safari',o = 'opera',h = document.documentElement,b = [(!(/opera|webtv/i.test(ua)) && /msie\s(\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('firefox/2') ? g + ' ff2' : is('firefox/3.5') ? g + ' ff3 ff3_5' : is('firefox/3') ? g + ' ff3' : is('gecko/') ? g : is('opera') ? o + (/version\/(\d+)/.test(ua) ? ' ' + o + RegExp.$1 : (/opera(\s|\/)(\d+)/.test(ua) ? ' ' + o + RegExp.$2 : '')) : is('konqueror') ? 'konqueror' : is('chrome') ? w + ' chrome' : is('iron') ? w + ' iron' : is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/.test(ua) ? ' ' + s + RegExp.$1 : '') : is('mozilla/') ? g : '',is('j2me') ? 'mobile' : is('iphone') ? 'iphone' : is('ipod') ? 'ipod' : is('mac') ? 'mac' : is('darwin') ? 'mac' : is('webtv') ? 'webtv' : is('win') ? 'win' : is('freebsd') ? 'freebsd' : (is('x11') || is('linux')) ? 'linux' : '','js'];
    c = b.join(' ');
    h.className += ' ' + c;
    return c;
}
;
css_browser_selector(navigator.userAgent);
