function lancer(fct) {
    if (window.addEventListener)
        window.addEventListener('load', fct, false);
    else
        window.attachEvent('onload', fct);
}

/*FUNC CLASS*/


function serialize(mixed_value) {
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'

    var _getType = function(inp) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';

    switch (type) {
        case "function":
            val = "";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            /*
             if (type == "object") {
             var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
             if (objname == undefined) {
             return;
             }
             objname[1] = serialize(objname[1]);
             val = "O" + objname[1].substring(1, objname[1].length - 1);
             }
             */
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") {
                    continue;
                }

                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += serialize(okey) +
                    serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
            val = "N";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";
    }
    return val;
}


function hasClass(element, value) {
    return element.className.match(new RegExp('(\\s|^)' + value + '(\\s|$)'));
}

function addClass(element, value) {
    if (!element.className) {
        element.className = value;
    } else {
        newClassName = element.className;
        newClassName += " ";
        newClassName += value;
        element.className = newClassName;
    }
}

function removeClass(element, value) {
    if (hasClass(element, value)) {
        var reg = new RegExp('(\\s|^)' + value + '(\\s|$)');
        element.className = element.className.replace(reg, ' ');
    }
}


if (Prototype.Browser.IE) {
    Prototype.Browser.IEVersion = parseFloat(navigator.appVersion.split(';')[1].strip().split(' ')[1]);
    Prototype.Browser.IE6 = Prototype.Browser.IEVersion == 6;
    Prototype.Browser.IE7 = Prototype.Browser.IEVersion == 7;
}

if (Prototype.Browser.Gecko)
    addClass(document.getElementsByTagName('html')[0], 'mozilla');
else if (Prototype.Browser.IE6)
    addClass(document.getElementsByTagName('html')[0], 'ie6');
else if (Prototype.Browser.IE7)
    addClass(document.getElementsByTagName('html')[0], 'ie7');

/*FUNC CLASS*/




/*FUNC EVENTS*/

function isset() {
    var a = arguments;
    var l = a.length;
    var i = 0;
    if (l == 0) {
        throw new Error('Empty isset');
    }
    while (i != l) {
        if (typeof(a[i]) == 'undefined' || a[i] === null) {
            return false;
        } else {
            i++;
        }
    }
    return true;
}

function is_object(mixed_var) {
    if (mixed_var instanceof Array) {
        return false;
    } else {
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}

function importScript(url) {
    var tag = document.createElement("script");
    tag.type = "text/javascript";
    tag.src = url;
    document.body.appendChild(tag);
}


/*ADDEVENT*/
function addEvent(obj, type, fn) {
    if (is_object(obj)) {
        if (obj.attachEvent) {
            obj['e' + type + fn] = fn;
            obj[type + fn] = function() {
                obj['e' + type + fn](window.event);
            }
            obj.attachEvent('on' + type, obj[type + fn]);
        } else
            obj.addEventListener(type, fn, false);
    }
}

function removeEvent(obj, type, fn) {
    if (obj.detachEvent) {
        obj.detachEvent('on' + type, obj[type + fn]);
        obj[type + fn] = null;
    }
    else
        obj.removeEventListener(type, fn, false);
}
/*FUNC EVENTS*/


function block(id) {
    $(id).style.display = 'block';
}

function none(id) {
    $(id).style.display = 'none';
}

function str_replace(search, replace, subject) {
    var s = subject;
    var ra = r instanceof Array, sa = s instanceof Array;
    var f = [].concat(search);
    var r = [].concat(replace);
    var i = (s = [].concat(s)).length;
    var j = 0;

    while (j = 0,i--) {
        if (s[i]) {
            while (s[i] = (s[i] + '').split(f[j]).join(ra ? r[j] || "" : r[0]),++j in f) {
            }
            ;
        }
    }

    return sa ? s : s[0];
}


/*MENU GAUCHE*/


function ajax_view_content(n, url, pars, method) {
    if (method == '')
        method = 'post';
    var myAjax = new Ajax.Updater(n, url, {method: 'post',parameters: pars, evalScripts:true});

}


//change content + tab activ
var update = 0;
function change_content(idToDisplay, idToActiv) {
    if ($(idToDisplay) != null) {
        var fieldset = $('sf_admin_content');
        var all_fieldset = fieldset.getElementsByTagName('fieldset');

        for (i = 0; i < all_fieldset.length; i++)
            if (all_fieldset[i].id != 'sf_fieldset_attachments')
                all_fieldset[i].style.display = 'none';

        $(idToDisplay).style.display = 'block';


        var menuColonneGaucheActiv = $('menuColonneGaucheActiv');
        var all_menu = menuColonneGaucheActiv.getElementsByTagName('a');

        for (i = 0; i < all_menu.length; i++)
            removeClass(all_menu[i], 'activ')

        if ($(idToActiv) != null) {
            addClass($(idToActiv), 'activ');
        }
        var sf_admin_form = $$('.sf_admin_form')
        sf_admin_form[0].style.display = 'block';

        if (update == 0)
            update = 1;
        else {
            var all_notice = $$('notice');
            for (i = 0; i < all_notice.length; i++)
                all_notice[i].style.display = 'block';
        }
    }
}


var pars = '';
function assigner(i, idmenu) {
    if (($(i) != null) && ($('sf_fieldset_' + idmenu) != null)) {
        addEvent($(i), 'click', function() {
            change_content('sf_fieldset_' + idmenu, $(i).id);
            if (idmenu == 'ajax') {
                /*if(pars == '')                                                                           
                 var pars = '';  */
                executeAjax($(i).href, 'sf_fieldset_ajax', pars, 'post')
            }
        });
    }
}


//noload / ajax
function init_lien(id) {
    if (id != null)
        var tab_lien = id.getElementsByTagName('a');
    else
        var tab_lien = document.getElementsByTagName('a');

    for (var i = 0; i < tab_lien.length; i++) {

        reg = new RegExp("ajax", 'g');
        regtab = reg.exec(tab_lien[i].className);
        if (regtab)
            assigner(tab_lien[i].id, 'ajax');

        reg = new RegExp("noload", 'g');
        regtab = reg.exec(tab_lien[i].className);
        if (regtab) {
            tab_lien[i].storeValue = i + 1;
            tab_lien[i].onclick = function() {
                return false;
            };
        }
    }
}


function executeAjax(url, div, pars, method) {

    if (method == '')
        method = 'post';

    var sf_admin_form = $$('.sf_admin_form')
    sf_admin_form[0].style.display = 'none';
    var myAjax = new Ajax.Updater(div, url, { method: method,
        parameters:pars,
        asynchronous:true,
        evalScripts : true,
        onComplete: function(transport) {
            init_lien($('sf_fieldset_ajax'));
        }
    });

}


//initialize
function EventsMenuGauche(idToDisplay, idToActiv) {


    if ($(idToDisplay) == null)
        idToDisplay = '';
    if ($(idToActiv) == null)
        idToActiv = '';


    reg = new RegExp("edit", 'g');
    regtab = reg.exec(document.referrer);
    if (!regtab) {
        idToDisplay = '';
        idToActiv = '';
    }


    var menuColonneGaucheActiv = $('menuColonneGaucheActiv');
    var all_menu = menuColonneGaucheActiv.getElementsByTagName('a');

    for (i = 0; i < all_menu.length; i++) {
        var idmenu = str_replace('menu_', '', all_menu[i].id);
        var idallmenu = all_menu[i].id;
        assigner(idallmenu, idmenu);

        if (idToDisplay == '')
            idToDisplay = 'sf_fieldset_' + idmenu;
        if (idToActiv == '')
            idToActiv = 'menu_' + idmenu;
    }

    change_content(idToDisplay, idToActiv);
    init_lien(menuColonneGaucheActiv);
}

function send_form(f, div) {
    alert(f);
    executeAjax($(f).action, div, Form.serialize(f) + '&' + pars, $(f).method);
}

function href_valider_ref(id, tag_id) {
    if (id == '' || id == null)
        id = 'retour_menu_ajax_select';
    $(id).href = $(tag_id).href;
    init_lien($('retour_menu_div'));
}
/*MENU GAUCHE*/

function attach_textarea() {
    var container = $('sf_admin_container');
    if (container) {
        var all_textarea = container.getElementsByTagName('textarea');

        for (var i = 0; i < all_textarea.length; i++) {
            if (typeof all_textarea[i].attributes['class'] == "undefined")
                var classname = all_textarea[i].getAttribute("class");
            else
                var classname = all_textarea[i].attributes["class"].nodeValue;
            //alert('+++>'+classname);
            if (classname != 'no_fck') {
                var oFCKeditor1 = new FCKeditor(all_textarea[i].id, 800);

                if (classname) {
                    //alert(all_textarea[i].getAttribute('class'));
                    oFCKeditor1.ToolbarSet = classname;
                }
                else
                    oFCKeditor1.ToolbarSet = 'admin_01';
                if (all_textarea[i].getAttribute('rows') > 200)
                    oFCKeditor1.Height = all_textarea[i].getAttribute('rows');

                oFCKeditor1.BasePath = "/js/fckeditor/";
                oFCKeditor1.ReplaceTextarea();
            }
        }
    }

}
jQuery(function() {
    jQuery.superbox();
});

lancer(attach_textarea);  
