$(document).ready(function() {
    loadTabs();
    loadLightboxes();
    loadDatePickers();
    loadTooltips();
    initUpdatePanelCallback();
    //loadFloatingHeader();
    HideMessagesOnChangedContent();
});

/* functie om afbeeldinglijst te verbergen na toevoegen, sorteren of verwijderen van items op beheer_contentitems.aspx */
function HideImageList() {
    if ($('#ctl00_cphBody_hdnContentItemId').length > 0) {
        $('#ctl00_cphBody_hdnContentItemId').val(0);
    }
}

/* function om save-message te verbergen bij een wijziging */
function HideMessagesOnChangedContent() {    
    $('#aspnetForm').children().change(function() {
        if ($('div.eventmessages').length > 0) {
            if ($('div.eventmessages:hidden').length <= 0) { 
                $('div.eventmessages').hide();
            }
        }
    });
    
    //dit werkt niet voor TinyMCE editors, de code daarvoor staat in de "js/tinymce/init_editor" bij de onChange event in de init-functie
}

///*  functie om editpage header te laten floaten */
//function loadFloatingHeader() {
//    if ($('#floating_editheader').length > 0) { 
//        $(window).scroll(function() {
//            $('#floating_editheader').animate({ top: $(window).scrollTop() + "px" }, { queue: false, duration: 0 });
//        });
//    //    }
//}

/*  functie om ui-tabs in te laden */
function loadTabs() {
    //initialize tabs (en zet meteen gekozen tab terug)
    $('#tabs').tabs({
        fxFade: true,
        fxSpeed: 'fast',
        select: function(event, ui) {
            forcePanelRefresh(ui.tab.rel);
        },
        show: function(event, ui) {
            $('#ctl00_hdnSelectedTab').val($('#tabs').tabs('option', 'selected'));
        }
    });

    //next en previous buttons spans nodig omdat kleursettings van standaard ui de kleuren van tabs paging overschrijven (stom é)
    $('#tabs').tabs('paging', { nextButton: "<span style='color: #FFFFFF;'>&#187;</span>", prevButton: "<span style='color: #FFFFFF;'>&#171;</span>" });  //nieuw voor paging
}

//click event voor het verversen van een updatepanel
function forcePanelRefresh(relID) {
    //contentitemid terug op 0 (zodat afbeeldingengridview van contentitembeheer "verwijderd" wordt)
    if ($('#ctl00_cphBody_hdnContentItemId').length > 0) {
        $('#ctl00_cphBody_hdnContentItemId').val(0);
    }

    //eerst deleted terug op 0
    if ($('#ctl00_cphBody_hdnDeleteId').length > 0) {
        $('#ctl00_cphBody_hdnDeleteId').val(0);
    }
    //dan pas postback laten doen
    if ($('#ctl00_cphBody_hdnUpdateListId').length > 0) {
        $('#ctl00_cphBody_hdnUpdateListId').val(relID);
        __doPostBack('ctl00_cphBody_hdnUpdateListId', '');
    }
}

/*  functie om de lightboxes te initialiseren */
function loadLightboxes() {
    $(".lightboxlink").lightBox({
        overlayBgColor: '#626262',
        overlayOpacity: 0.6,
        imageLoading: 'images/lightbox-ico-loading.gif',
        imageBtnClose: 'images/lightbox-btn-close.gif',
        imageBtnPrev: 'images/lightbox-btn-prev.gif',
        imageBtnNext: 'images/lightbox-btn-next.gif',
        containerResizeSpeed: 350,
        txtImage: 'Foto',
        txtOf: 'van',
        imageBlank: 'images/lightbox-blank.gif'
    });
}

/*  functie die controleert of "messages" thickboxes getoond moeten worden */
function checkMessages() {
    if ($('#ctl00_cphBody_hdnDeleteId').length > 0) {
        if ($('#ctl00_cphBody_hdnDeleteId').val() != "0") {
            tb_open_box(450, 110, 'messages', true);
        }
    }

    if ($('#ctl00_cphBody_hdnNoItemsSelected').length > 0) {
        if ($('#ctl00_cphBody_hdnNoItemsSelected').val() != "0") {
            tb_open_box(450, 75, 'messages', true);
        }
    }

    if ($('#ctl00_cphBody_hdnLoginError').length > 0) {
        if ($('#ctl00_cphBody_hdnLoginError').val() != "0") {
            tb_open_box(450, 75, 'messages', true);
        }
    }

    //extra voor de afbeeldingen-gridview bij een contentitem
    if ($('#ctl00_cphBody_hdnDeleteIdAfbeeldingen').length > 0) {
        if ($('#ctl00_cphBody_hdnDeleteIdAfbeeldingen').val() != "0") {
            tb_open_box(450, 110, 'messages', true);
        }
    }

    if ($('#ctl00_cphBody_hdnNoItemsSelectedAfbeeldingen').length > 0) {
        if ($('#ctl00_cphBody_hdnNoItemsSelectedAfbeeldingen').val() != "0") {
            tb_open_box(450, 75, 'messages', true);
        }
    }
}

function tb_open_box(width, heigth, inlineId, modal) {
    tb_show(null, '#TB_inline?height=' + heigth + '&width=' + width + '&inlineId=' + inlineId + '&modal=' + modal, null);
}

/*  functie om datepickers in te laden */
function loadDatePickers() {
    //Jquery UI -> datepickers
    var arrMonthNames = ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'];
    var arrDayNames = ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag']; //let op, beginnen met zondag!
    var arrShortDayNames = ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za']; //let op, beginnen met zondag!

    $("#ctl00_cphBody_txtFrom").datepicker({ dateFormat: "dd/mm/yy", monthNames: arrMonthNames, dayNames: arrDayNames, dayNamesMin: arrShortDayNames, firstDay: 1, showWeeks: true });
    $("#ctl00_cphBody_txtTo").datepicker({ dateFormat: "dd/mm/yy", monthNames: arrMonthNames, dayNames: arrDayNames, dayNamesMin: arrShortDayNames, firstDay: 1, showWeeks: true });

    $("#ctl00_cphBody_txtZichtbaarVanaf").datepicker({ dateFormat: "dd/mm/yy", monthNames: arrMonthNames, dayNames: arrDayNames, dayNamesMin: arrShortDayNames, firstDay: 1, showWeeks: true });
    $("#ctl00_cphBody_txtZichtbaarTot").datepicker({ dateFormat: "dd/mm/yy", monthNames: arrMonthNames, dayNames: arrDayNames, dayNamesMin: arrShortDayNames, firstDay: 1, showWeeks: true });
  
//    //jquery ui -> datepicker events
//    //aangezien de datepicker met javascript werkt gebeurt er standaard geen postback bij het veranderen van de datum
//    //daarom hebben we een knop die met css verborgen is die de nodige .net code uitvoert wanneer de tekst in de textbox verandert
//    $("#ctl00_cphBody_txtFrom").change(function() {
//        $("#ctl00_cphBody_btnRefresh").click();
//    });
//    $("#ctl00_cphBody_txtFrom").change(function() {
//        $("#ctl00_cphBody_btnRefresh").click();
//    });
}

/*  functie om de tooltips te initialiseren */
function loadTooltips() {
    $('#edit_wrapper *').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - "
    });

    $('#wrapper *').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - "
    });
}

/*  functie om thickboxes te (her)initialiseren */
function initThickboxes()
{
        tb_init('a.thickbox, area.thickbox, input.thickbox');
}

/*  functies om een callback aan de refresh van een updatepanel te hangen
    op deze manier kunnen we bijvoorbeeld lightboxes herinitialiseren bij een ververste gridview */
function EndRequestHandler(sender, args) {
    if (args.get_error() == undefined) {
        //hier de functies plaatsen die na updatepanel refresh uitgevoerd moeten worden
        loadLightboxes();
        checkMessages();
        initThickboxes();
        loadTooltips();
    }
    else {
        //alert("An error occured: " + args.get_error().message);
    }
}

function initUpdatePanelCallback() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

///*  functie om update te stoppen */
//var postBackElement;

//function InitializeRequest(sender, args) {
//    var prm = Sys.WebForms.PageRequestManager.getInstance();

//    if (prm.get_isInAsyncPostBack()) {
//        args.set_cancel(true);
//    }
//    postBackElement = args.get_postBackElement();
//    if (postBackElement.id == 'ButtonTrigger') {
//        $get('UpdateProgress1').style.display = "block";
//    }
//}
//function EndRequest(sender, args) {
//    if (postBackElement.id == 'ButtonTrigger') {
//        $get('UpdateProgress1').style.display = "none";
//    }
//}
//function AbortPostBack() {
//    var prm = Sys.WebForms.PageRequestManager.getInstance();

//    if (prm.get_isInAsyncPostBack()) {
//        prm.abortPostBack();
//    }
//}