jQuery(document).ready(function() {
    convertHrefToPopIn(function(data) {
        jQuery(this).colorbox({
            transition      : 'elastic',
            iframe          : true,
            width           : data.width,
            height          : data.height,
            opacity         : 0.3,
            overlayClose    : (data.alwaysRaised == 'yes' ? false : true)
        });
   });
});

/*
 * @brief : Parsage des liens de la page pour transformation des WindowVisu en ouverture type pop-in
 * @param fct_callback : Fonction qui va afficher la popin en fonction du plugin choisi
 */
function convertHrefToPopIn(fct_callback) {

    //href="javascript:WindowVisu('http://cms.lyon.novius.fr/demo-publinova/index/demo-formulaire.html', 'popup', 'alwaysRaised=no,dependent=yes,directories=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=yes,width=600,height=500');"
    myRegExp = new RegExp("javascript:WindowVisu\\((.*)\\);", "g");

    jQuery('a[href^="javascript:WindowVisu("]').each(function() {

        var href          = jQuery(this).attr('href');
        var tabWindowVisu = myRegExp.exec(href);
        myRegExpQuotes    = new RegExp("\\'([^\\']*)\\'", "gi");

        var args = new Array();
        var i    = 0;
        while (arg = myRegExpQuotes.exec(tabWindowVisu[1])) {
            // On parcourt chaque argument de la fonction WindowVisu()
            args[i] = new String(arg[1]);
            i++;
        }
        // Le tableau args contient 3 éléments :
        // args[0] : href
        // args[1] : id popup
        // args[2] : liste des paramètres

        var href         = args[0];
        var liste_params = args[2];

        if (liste_params != undefined) {
            var params = liste_params.split(',');
            var data   = new Object();
            for (i = 0; i <= params.length; i++) {
                // Récupération de chaque paramètre sous la forme nom_param=valeur

                var param  = jQuery.trim(params[i]);
                var param2 = param.split('=');
                if (param2.length == 2) {
                    var k = param2[0];
                    var v = param2[1];

                    if (k == 'width' || k == 'height') {
                        // On convertit la valeur en type entier et non chaine (ex : Fancybox ne comprend pas les dimensions '500' mais juste 500)
                        v = parseInt(v);
                    }

                    // On ajoute chaque paramètre prédéfini de la popup dans le tableau data
                    data[k] = v;
                }
            }
            jQuery(this).attr('href', href);
            fct_callback.call(this, data);
        }
    });
}
