var onload_funcs = new Array();
var is_ie = false;

function __onload_handler()
{
    // Adapted from Dean Edwards's code @ http://dean.edwards.name/my/busted3.html
    // Safari
    if (/WebKit/i.test(navigator.userAgent)) {
        var _timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) {
                clearInterval(_timer);
                delete _timer;
                initNakama(); // call the onload handler
            }
        }, 10);
    } // Mozilla, Opera 9
    else if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", initNakama, false);
    }

    // Internet Explorer (using conditional comments)
    /*@cc_on @*/
    /*@if (@_win32)
    is_ie = true;
    document.write("<script id=__ie_onload defer src=//:><\/script>");
    document.all.__ie_onload.onreadystatechange = function() {
        if (this.readyState == "complete") {
            initNakama(); // call the onload handler
        }
    };
    /*@end @*/

    /* Other browsers */
    var __func = window.onload;
    if (__func)
    {
        window.onload = function() { __func(); initNakamaWrapper() };
    }
    else
    {
        window.onload = initNakamaWrapper;
    }
}

/*
    Base, version 1.0.2
    Copyright 2006, Dean Edwards
    License: http://creativecommons.org/licenses/LGPL/2.1/
*/

var Base = function() {
    if (arguments.length) {
        if (this == window) { // cast an object to this class
            Base.prototype.extend.call(arguments[0], arguments.callee.prototype);
        } else {
            this.extend(arguments[0]);
        }
    }
};

Base.version = "1.0.2";

Base.prototype = {
    extend: function(source, value) {
        var extend = Base.prototype.extend;
        if (arguments.length == 2) {
            var ancestor = this[source];
            // overriding?
            if ((ancestor instanceof Function) && (value instanceof Function) &&
                ancestor.valueOf() != value.valueOf() && /\bbase\b/.test(value)) {
                var method = value;
            //    var _prototype = this.constructor.prototype;
            //    var fromPrototype = !Base._prototyping && _prototype[source] == ancestor;
                value = function() {
                    var previous = this.base;
                //    this.base = fromPrototype ? _prototype[source] : ancestor;
                    this.base = ancestor;
                    var returnValue = method.apply(this, arguments);
                    this.base = previous;
                    return returnValue;
                };
                // point to the underlying method
                value.valueOf = function() {
                    return method;
                };
                value.toString = function() {
                    return String(method);
                };
            }
            return this[source] = value;
        } else if (source) {
            var _prototype = {toSource: null};
            // do the "toString" and other methods manually
            var _protected = ["toString", "valueOf"];
            // if we are prototyping then include the constructor
            if (Base._prototyping) _protected[2] = "constructor";
            for (var i = 0; (name = _protected[i]); i++) {
                if (source[name] != _prototype[name]) {
                    extend.call(this, name, source[name]);
                }
            }
            // copy each of the source object's properties to this object
            for (var name in source) {
                if (!_prototype[name]) {
                    extend.call(this, name, source[name]);
                }
            }
        }
        return this;
    },

    base: function() {
        // call this method from any other method to invoke that method's ancestor
    }
};

Base.extend = function(_instance, _static) {
    var extend = Base.prototype.extend;
    if (!_instance) _instance = {};
    // build the prototype
    Base._prototyping = true;
    var _prototype = new this;
    extend.call(_prototype, _instance);
    var constructor = _prototype.constructor;
    _prototype.constructor = this;
    delete Base._prototyping;
    // create the wrapper for the constructor function
    var klass = function() {
        if (!Base._prototyping) constructor.apply(this, arguments);
        this.constructor = klass;
    };
    klass.prototype = _prototype;
    // build the class interface
    klass.extend = this.extend;
    klass.implement = this.implement;
    klass.toString = function() {
        return String(constructor);
    };
    extend.call(klass, _static);
    // single instance
    var object = constructor ? klass : _prototype;
    // class initialisation
    if (object.init instanceof Function) object.init();
    return object;
};

Base.implement = function(_interface) {
    if (_interface instanceof Function) _interface = _interface.prototype;
    this.prototype.extend(_interface);
};

// onload "document"
function initNakamaWrapper()
{
    // IE6 pue tellement qu'il arrive à appeler deux fois trop vite, donc on le ralentit un peu
    setTimeout(initNakama, 1);
}

// onload "domcontent"
function initNakama()
{
    var len = onload_funcs.length;

    for (var i = 0; i < len; i++)
    {
        func = onload_funcs[i];
        if (typeof func != 'function'
         && typeof func[0] != 'undefined'
         && typeof func[0] == 'function')
        {
            onload_funcs[i] = func[0];
        }
        else if (func)
        {
            onload_funcs[i].call();
            onload_funcs[i] = false;
        }
    }
}

function popup(page,largeur,hauteur,options) {
    var top=(screen.height-hauteur)/2;
    var left=(screen.width-largeur)/2;
    window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}

function closePopup(){
    window.close(this);
}

function menu(classMenu){
    document.getElementById("menu_profil").className = classMenu;
}

/* HEADER PERSO */

var showheader = new Boolean(false);
function showinfo(element) {
    var test = showheader.toString();
    if ( test == 'false') {
                document.getElementById(element).style.display = 'block';
                document.getElementById('header_hide').style.display = 'inline';
                document.getElementById('header_show').style.display = 'none';
        showheader = 'true';
        } else {
                document.getElementById(element).style.display = 'none';
                document.getElementById('header_hide').style.display = 'none';
                document.getElementById('header_show').style.display = 'inline';
        showheader = 'false';
    }
}

/* INFOBULLES */

function show(element){
        document.getElementById(element).style.visibility = 'visible';
}
function displayblock(element){
        document.getElementById(element).style.display = 'block';
}
function hide(element){
        document.getElementById(element).style.visibility = 'hidden';
}
function displaynone(element){
        document.getElementById(element).style.display = 'none';
}

function shuffle_array(arr)
{
    var i = arr.length, j, t;
    while (i--)
    {
            j = Math.floor((i + 1) * Math.random());
            t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
    }
    return arr
}

function array_filter(arr, func)
{
    var list = [];
    for (var i = 0, l = arr.length; i < l; i++)
    {
        if (func(arr[i]))
        {
            list.push(arr[i]);
        }
    }

    return list;
}

function getLabel(parent,elmt) {
    if(!elmt || !elmt.id) return false;
    
    var myLabel = parent.getElementsByTagName("label");
    for(var i=0; i<myLabel.length; i++) {
        if(myLabel[i].htmlFor == elmt.id) return myLabel[i];
    }
    return false;
}
                            
function hasClass(elm, className)
{
    if (!elm || !elm.className)
    {
        return false;
    }
    var re = new RegExp('(^|\\s)' + className + '(\\s|$)');
    return elm.className.match(re)
}

function addClass(elm, className)
{
    if (!elm)
    {
        return false;
    }
    if (!hasClass(elm, className))
    {
        elm.className += (elm.className ? " " : "") + className;
    }
}

function removeClass(elm, className)
{
    if (!elm || !elm.className)
    {
        return false;
    }
    var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)", "g");
    elm.className = elm.className.replace(regexp, "$2");
}

function toggleClass(elm, className)
{
    if (hasClass(elm, className))
    {
        removeClass(elm, className);
    } else {
        addClass(elm, className);
    }
}

function swapNodes(node1, node2)
{
    var nextSibling = node1.nextSibling;
    var parentNode = node1.parentNode;
    node2.parentNode.replaceChild(node1, node2);
    parentNode.insertBefore(node2, nextSibling);
}

function getElementsByClassName(className, context)
{
    context = context || document;
    var nodeList;

    if (context == document || context.nodeType == 1)
    {
        if (typeof document.evaluate == 'function')
        {
            var xpath = document.evaluate(".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]",
                                          context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            var els = [];
            for (var i = 0, l = xpath.snapshotLength; i < l; i++)
        {
                els.push(xpath.snapshotItem(i));
        }
            return els;
        }
        else
        {
            nodeList = context.getElementsByTagName('*');
        }
    }
    else
    {
        nodeList = context;
    }
    var arr = array_filter(nodeList, function(node) {
        return hasClass(node, className);
    });
    return arr;
}

function setLocale(locale)
{
    if (locales_redirect)
    {
        return true;
    }
    else
    {
        var form = document.createElement('form');
        form.method = 'POST';
        form.action = '';

        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = 'setLocale';
        input.value = locale;

        form.appendChild(input);

        document.getElementsByTagName('body')[0].appendChild(form);
        form.submit();

        return false;
    }
}

function getcookie(name, defaultvalue)
{
    name += "=";
    if (document.cookie.length > 0)
    {
        offset = document.cookie.indexOf(name);
        if (offset != -1)
        {
            offset += name.length;
            end = document.cookie.indexOf(";", offset)
            return unescape(document.cookie.substring(offset, (end != -1) ? end : document.cookie.length));
        }
    }
    return (defaultvalue);
}

function setcookie(name, value, domain)
{
    var cookie = name + "=" + escape(value);
    var today = new Date();
    var expire = new Date();

    expire.setTime(today.getTime() + 1000 * 60 * 60 * 24 * 365);
    cookie += "; expires=" + expire.toGMTString();
    cookie += "; path=/";
    if (domain)
    {
        cookie += "; domain=" + domain;
    }
    document.cookie = cookie;
}

function openFooterPopup(link)
{
    window.open(link, 'footerPopupSkyrock', 'width=810,height=560,location=no,toolbar=no,resizable=yes,scrollbars=yes');
    return false;
}

function openWindow(page, title, width, height) {
    window.open(page, title, 'location=no,toolbar=no,resizable=yes,scrollbars=yes,width='+width+',height='+height);
    return false;
}

function openAddBanWindow(e) {
    if (this == window)
        var elm = e;
    else
        elm = this;
    window.open(elm.href, 'banadd', 'location=no,toolbar=no,resizable=yes,scrollbars=yes,width=470,height=250');
    return false;
}

function openSendMessageWindow(e, id_skynaute, pseudo)
{
    if (this == window)
        var elm = e;
    else
        elm = this;
    var href = www_path + 'm/messages/popup-write_message.php?dest=' + id_skynaute;
    window.open(href, 'message_' + pseudo.replace(/-/g, '_'), 'location=no,toolbar=no,resizable=yes,scrollbars=yes,width=550,height=500');
    return false;
}

function openAddCommentWindow(elm)
{
    var url = elm.href;

    window.open(url, 'commentaire_' + blog_pseudo.replace(/-/g, '_'),
        'width=680,height=500,location=no,toolbar=no,resizable=yes,scrollbars=yes');
    return false;
}

function displaySessionRefresh()
{
    if (typeof www_path == 'undefined')
    {
        return;
    }
    if (typeof _RIMAGE == 'undefined')
    {
        _RIMAGE = new Image(1, 1);
    }
    _RIMAGE.src = www_path + "common/keepsession.php?" + Math.round(Math.random() * 1000000000);
}

function toggleDisplay(id)
{
    var status = false;
    var elm = document.getElementById(id);
    if(elm)
    {
        if(elm.style.display == 'none')
        {
            elm.style.display = '';
            status = true;
        }
        else
        {
            elm.style.display = 'none';
            status = false;
        }
    }
    return status;
}

function initClickHeatWrapper()
{
    document.write('<script type="text/javascript" src="' + static_js_url + 'js/clickheat.js"></script>');
}

function getHexValueForColor(c)
{
    if (!c)
    {
        return '';
    }
    
    str = c.toLowerCase();
    if (str == "transparent")
    {
        str = '';
    }
    else if (str.indexOf('rgb') === 0)
    {       
        function tohex(inval)
        {
            inval = parseInt(inval);
            var out = '0123456789ABCDEF'.charAt(inval / 16);
            out    += '0123456789ABCDEF'.charAt(inval % 16);
            return out;
        }
   
        var j;

        j = (str.substr(4, str.indexOf(')') - 4)).split(',');
        str = '#' + tohex(j[0]) + tohex(j[1]) + tohex(j[2]);
    }
    if (str.indexOf('#') === 0)
    {
        if (str.length == 4)
        {
            // transformons #xyz en #xxyyzz
            str = '' + str.charAt(1) + str.charAt(1) + str.charAt(2) + str.charAt(2) + str.charAt(3) + str.charAt(3);
        }
        else
        {
            // virons les #
            str = str.substr(1);
        }
    }
    else
    {
        str = '';
    }
    return str.toUpperCase();
}

function getStylePropertyValueWithRoot(root, elm, property)
{
    if (elm)
    {
        if (root.all && elm.currentStyle)
        {
            property = property.replace(/\-([a-z])/g, function(match, chr) { return chr.toUpperCase(); });
            if (elm.currentStyle[property])
            {
                return (elm.currentStyle[property]);
            }
        }
        else if (root.defaultView && root.defaultView.getComputedStyle)
        {
            var cs = root.defaultView.getComputedStyle(elm, null);
            if (!cs)
            {
                return false;
            }
            return cs.getPropertyValue(property, null);
        }
    }
    return false;
}

function getColorValueForProperty(root, elm, property, defaultValue)
{
    var v = getStylePropertyValueWithRoot(root, elm, property);
    var c = getHexValueForColor(v);
    if (!c)
    {
        c = defaultValue;
    }
    return c;
}

function generateVocalPlayer()
{
    var elm = document.getElementById('cybercop');
    var color = false

    if (elm) {
        color = getColorValueForProperty(document, elm, 'background-color', false)
    }

    if (!color) {
        elm = document.getElementById('barleft');
        if (elm) 
            color = getColorValueForProperty(document, elm, 'background-color', '000000')
        else
            color = '000000'
    }


    if (typeof blog_id_skynaute != 'undefined')
        var __id_skynaute = blog_id_skynaute
    else if (typeof profil_id_skynaute != 'undefined')
        var __id_skynaute = profil_id_skynaute
    else
        return;

    elm = document.getElementById('mvocal');

    var str = '<object width="150" height="36" type="application/x-shockwave-flash" data="'+static_img_url+'flash/messagevocal/mvPlayer.swf" id="playervocal">';
    str += '<param name="movie" value="'+static_img_url+'flash/messagevocal/mvPlayer.swf"/>';
    str += '<param name="quality" value="high"/>';
    str += '<param name="wmode" value="transparent"/>';
    str += '<param name="scale" value="noscale" />';
    str += '<param name="flashvars" value="xmlPath='+www_path+'common/announce_vocal_xml.php?param='+__id_skynaute+'&backgroundcolor='+color+'"/>';
    str += '</object>';
   
    elm.innerHTML = str;
}

