﻿function registerFancyBox(selectorId, width, height, url, type, classSelection, modal) {
    var params = {
        'overlayColor': '#000',
        'overlayOpacity': 0.75,
        'hideOnOverlayClick': false,
        'titlePosition': 'inside'
    };

    if (width != null) params.width = parseInt(width, 10);
    if (height != null) params.height = parseInt(height, 10);
    if (url != null) params.href = url;
    params.type = (type != null ? type : 'iframe');
    if (classSelection == null) classSelection = false;
    if (modal != null) params.modal = modal;

    if (type == 'image' && $((!classSelection ? "#" : ".") + selectorId).length > 1)
        params.titleFormat = function (itemTitle, itemArray, itemIndex, itemOpts) { return itemTitle + '<span> Bild ' + (itemIndex + 1) + ' av ' + itemArray.length + '</span>' };

    $((!classSelection ? "#" : ".") + selectorId).fancybox(params);
}

/*
    Register fancyboxes for all static links. typically generated "hardcoded" by the administration GUI CMS parts when created pages and articles.
    Unfortunatly, called for every page loaded.
*/
$(document).ready(function () {
    /*
        handle old style thickbox contact links that used thickbox, remove the tb_remove and all following thickbox commands. Only include page and guid.
        also remove title.
        old link: /portal/ORG/Contact.aspx?guid=f42abd91-d239-44a8-860d-4ebb6178d061&tb_remove=aThickBoxRemove?TB_iframe=true&height=400&width=568
        new link: /portal/ORG/Contact.aspx?guid=f42abd91-d239-44a8-860d-4ebb6178d061
    */
    $(".link_contact.thickbox").each(function () {
        $(this).attr("href", ($(this).attr("href").substring(0, $(this).attr('href').indexOf('&tb_remove'))));
        $(this).attr('title', null);
    });

    // link_contact is a static link used to open a "contact me" dialog, generated hardcoded by the user when creating articles
    registerFancyBox('link_contact', 560, 420, null, 'iframe', true);
});
