$(function() {
    $('a.media').media({
        caption: false
    });
});

function closeParentWin() {
    top.$('#ex2').jqmHide();
}


$(document).ready(function() {

    var zIndexNumber = 1000;
    $('div').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });

    var mouseOverActive;

    $(function() {
        $.nyroModalSettings({
            minWidth: 230,
            minHeight: 26,
            swf: { // Swf player options if swf type is used.
                wmode: 'opaque'
            },
            padding: 5
        });

        $('#fldDatumVanaf').datePicker();
        $('#fldDatumTotEnMet').datePicker();

        $("li.liMainMenuCurrent").find("div.subMenu").show();
    });

    /* START MENU */

    /* Main menu */

    $("div#navSubMenu").find("li.current").find("div.subMenu").show();
    if ($("li.current").parent().parent().parent().hasClass("subSubMenu")) {
        $("div#navSubMenu").find("li.current").find("div.subMenu").find("div.subSubMenu").find("li.current").parent().parent().parent().parent().find("div.subSubMenu").show();
        $("div#navSubMenu").find("li.current").find("div.subMenu").css("width", "339px");
    }

    $("li.liMainMenuCurrent").find("a.current").mouseover(function() {
        mouseOverActive = true;

        $("li.liMainMenu").find("div.subMenu").hide();
        $("li.liMainMenu").find("div.subMenu").removeClass("subMenuCurrent");
        $("li.liMainMenuCurrent").find("div.subMenu").show();
        $(this).parent().find("div.subMenu").addClass("subMenuCurrent");
        $(this).parent().find("div.subMenu").show();

    });

    $("li.liMainMenu").find("a").mouseover(function() {
        mouseOverActive = true;

        $("li.liMainMenu").find("div.subMenu").hide();
        $("li.liMainMenu").find("div.subMenu").removeClass("subMenuCurrent");
        $("li.liMainMenuCurrent").find("div.subMenu").hide();
        $(this).parent().find("div.subMenu").addClass("subMenuCurrent");
        $(this).parent().find("div.subMenu").show();

    });

    $("ul#nav > li.liMainMenu").mouseover(function() {
        $(this).bind("mouseleave", function() {
            $("ul#nav > li.liMainMenu").each(function(i) {
                if ($(this).hasClass("current")) {
                    $("div.subMenu").hide();
                    $("ul#nav > li.liMainMenu").eq(i).find("div.subMenu").css("width", "130px").show();
                }
            });
        });

    });



    /* Main menu end*/
    /* Sub menu */

    $("div.subMenu").mouseover(function() {
        $(this).show();
    });

    $(".subMenu").find("a.level3").mouseover(function() {
        $(this).parent().parent().find("div.mo").fadeOut("fast");
        $(this).parent().parent().parent().parent().stop();
        $(this).parent().parent().parent().parent().css("width", "130");
        $(this).parent().parent().find("div.subSubMenu").hide();

        if ($(this).parent().find("div.subSubMenu").length > 0) {
            $(this).addClass("mo");
            $(this).parent().find("div.subSubMenu").fadeIn("fast");
            $(this).parent().parent().parent().parent().animate({
                width: "339"
            }, 500);
        }
        else {
            $(this).parent().find("div.subSubMenu").hide();
            $(this).parent().parent().parent().parent().animate({
                width: "130"
            }, 500);
        }
    });

    $(".subMenu").find("a.level2").mouseout(function() {
        $(this).removeClass("mo");
    });

    /* eind submenu */

    /* close button */

    $("div.closeMenu").click(function() {
        $("div.subMenu").find("div.subSubMenu").stop().hide();
        $("ul#nav > li.liMainMenu").unbind("mouseleave");
        $("div.subMenu").width(130);
        $("div.subMenu").stop();
        $("div.subMenu").hide();
    });

    /* eind close button*/

    /*      EINDE MENU       */


    //Op pagina 'management programmes > aanvragen' het menu standaard verbergen
    if ($("body").hasClass("aanvragen_mp")) {
        $("div.closeMenu").click();
    }

    $('img[@src$=.png]').ifixpng();
    $('div.subMenu').ifixpng();



    $("ul.gallery").find("li").not(".current").hide();


    $("a.prev").click(function() {
        var num = $("ul.gallery li.current").prevAll().length - 1;
        if (num < 0) {
            num = $("ul.gallery").find("li").length - 1;
        }

        $("ul.gallery li.current").hide();
        $("ul.gallery li.current").removeClass("current");
        $("ul.gallery li").eq(num).addClass("current");
        $("ul.gallery li").eq(num).show();

        $("a#schermvullend").attr("href", ($("ul.gallery li.current img").attr("src")));
        12
    });
    $("a.next").click(function() {
        var num = $("ul.gallery li.current").prevAll().length + 1;
        if (num > $("ul.gallery").find("li").length - 1) {
            num = 0;
        }

        $("ul.gallery li.current").fadeOut(500).hide();
        $("ul.gallery li.current").removeClass("current");
        $("ul.gallery li").eq(num).addClass("current");
        $("ul.gallery li").eq(num).fadeIn(500).show();

        $("a#schermvullend").attr("href", ($("ul.gallery li.current img").attr("src")));

    });

    $("div.scrollable1").scrollable({
        clickable: false,
        keyboard: false,
        prev: ".prev1",
        next: ".next1",
        size: 2
    });

    $("div.scrollable2").scrollable({
        clickable: false,
        keyboard: false,
        prev: ".prev2",
        next: ".next2",
        size: 2
    });

    if ($("#frmShout").find("#naam").val() != "naam")
        $("#frmShout").find("#naam").css("color", "#000000");

    if ($("#frmShout").find("#tekst").val() != "reactie (max. 120 tekens)")
        $("#frmShout").find("#tekst").css("color", "#000000");

    if ($("#frmShout").find("#naam").val() == "")
        $("#frmShout").find("#naam").val("naam");

    $("#frmShout").find("#naam").focus(function() {
        if ($(this).val() == "naam") {
            $(this).val("");
            $(this).css("color", "#000000");
        }
    });

    $("#frmShout").find("#naam").blur(function() {
        if ($(this).val() == "") {
            $(this).val("naam");
            $(this).css("color", "#cccccc");
        }
    });

    if ($("#frmShout").find("#tekst").val() == "")
        $("#frmShout").find("#tekst").val("reactie (max. 120 tekens)");

    $("#frmShout").find("#tekst").focus(function() {
        if ($(this).val() == "reactie (max. 120 tekens)") {
            $(this).val("");
            $(this).css("color", "#000");
        }
    });

    $("#frmShout").find("#tekst").blur(function() {
        if ($(this).val() == "") {
            $(this).val("reactie (max. 120 tekens)");
            $(this).css("color", "#cccccc");
        }
    });

    //validate shoutbox
    $("#frmShout").validate({
        rules: {
            naam: "required",
            tekst: {
                required: true,
                minlength: 5,
                maxlength: 120
            }
        },
        errorClass: "invalid"
    });

    //post shoutbox
    $("#frmShout").find("#btn_verstuur").click(function() {
        if ($("#naam").val() == "naam") {
            $("#naam").val("");
        }
        if ($("#tekst").val() == "reactie (max. 120 tekens)") {
            $("#tekst").val("");
        }
        if ($("#frmShout").valid()) {
            var randomnumber = Math.floor(Math.random() * 100001);
            var send = $.post("/Ajax/saveShout.aspx?randomnumber=" + randomnumber,
                    {
                        naam: $("#naam").val(),
                        tekst: $("#tekst").val()
                    },
                    function(data) {
                        $.prompt("Bedankt voor je reactie!", { callback: reloadShoutbox });
                    }
                );
            return false;
        }
        else {
            if ($("#naam").val() == "") {
                $("#naam").val("naam");
            }
            if ($("#tekst").val() == "") {
                $("#tekst").val("reactie (max. 120 tekens)");
            }
            $.prompt("Naam en reactie zijn verplicht. De reactie mag maximaal 120 tekens bevatten.");
        }
        return false;
    });

    //validate googlemaps route
    $("#frmGoogleMap").validate({
        rules: {
            vertrekpunt: "required"
        },
        errorClass: "invalid"
    });

    //post googlemaps route
    $("#frmGoogleMap").find("#btn_verstuur").click(function() {
        if ($("#frmGoogleMap").valid()) {

            var url = "http://maps.google.nl/maps?f=d&source=s_d&saddr=" + $("#vertrekpunt").val() + "&daddr=Overstag,+Lelystad&geocode=&hl=nl&mra=ls&ie=UTF8";
            window.open(url);
        }
        else {
            $.prompt("Voer eerst een vertrekpunt in om de route te berekenen.");
        }
        return false;
    });

    //extranet routebeschrijving
    $("#ulEmmeloord").hide();
    $("#ulEnkhuizen").hide();
    $("#ulAmsterdam").hide();

    $("#linkAmsterdam").click(function() {
        $("#ulEmmeloord").hide();
        $("#ulEnkhuizen").hide();
        $("#ulAmsterdam").show();
        return false;
    });
    $("#linkEmmeloord").click(function() {
        $("#ulEmmeloord").show();
        $("#ulEnkhuizen").hide();
        $("#ulAmsterdam").hide();
        return false;
    });
    $("#linkEnkhuizen").click(function() {
        $("#ulEmmeloord").hide();
        $("#ulEnkhuizen").show();
        $("#ulAmsterdam").hide();
        return false;
    });

});

function onAfter(curr, next, opts) {
    var index = $(this).parent().children().index(this);
    $('#middelScrollDown')[index == 0 ? 'hide' : 'show']();
    $('#middelScrollUp')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

function reloadShoutbox() {
    setTimeout('document.location="'+document.location+'"', 0);
}

function limitChars(textid, limit, infodiv) {
    var text = $('#' + textid).val();
    var textlength = text.length;
    if (textlength > limit) {
        $('#' + infodiv).html('Je kunt max. ' + limit + ' tekens gebruiken.');
        $('#' + infodiv).show();
        $('#' + textid).val(text.substr(0, limit));
        return false;
    }
    else {
        $('#' + infodiv).hide();
        return true;
    }
}

function limitCharsSimple(textid, limit) {
    var text = $('#' + textid).val();
    var textlength = text.length;
    if (textlength > limit) {
        $('#' + textid).val(text.substr(0, limit));
        return false;
    }
}