// FONCTIONS GÃ?NÃ?RALES

var OPE = (window.opera) ? true : false;
var IE  = (document.all && !OPE) ? true : false;
var MOZ = (!IE && !OPE) ? true : false;

/**
 *  incFile(file)
 *  Inclusion d'un appel Ã  fichier js ou css dans le head
 *  RNB, d'aprÃ©s http://www.phpied.com/javascript-include, aout 2005 / dÃ©c 2006
 */
function incFile()
{
    var inc_array = Array();
    var head = document.getElementsByTagName('head')[0];
    
    this.include = function(file)
    {
        if (!this.isInclude(file)) {
            var t = file.substring(file.lastIndexOf('.')+1);
            switch (t) {
	            case 'js':
                    var head_inc = document.createElement('script');
                    head_inc.setAttribute('type','text/javascript');
                    head_inc.setAttribute('src',root+file);
	                break;
	            case 'css':
                    var head_inc = document.createElement('link');
                    head_inc.setAttribute('type','text/css');
                    head_inc.setAttribute('rel','stylesheet');
                    head_inc.setAttribute('href',root+file);
	                break;
            }
            head.appendChild(head_inc);
            inc_array.push(file);
        }
        
    };
    this.isInclude = function(file)
    {
        var n = inc_array.length;
        for (var i=0; i<n; i++) {
            if (inc_array[i]==file) {
                return true;
            }
        }
        return false;
    };
    
}

/**
 *  className : setClass
 *  Ajouter / supprimer une classe Ã  un Ã©lÃ©ment
 *  RNB, Aout 2005 / dÃ©cembre 2006
 *  @param  char    mode : a (ajouter) / r (remove)
 *  @param  String  c   la classe
 *  @param  DomElement  node    ElÃ©ment Ã  traiter
 */
function setClass(mode,c,node)
{
    var re = new RegExp('(^| )'+c+'( |$)');
    switch (mode) {
        case 'a':
            if (!re.test(node.className)) {
                node.className += ' ' + c;
            }
            break;
        case 'r':
            if (re.test(node.className)) {
                node.className = node.className.replace(c,'');
                node.className = node.className.replace('  ',' ');
            }
            break;
    }
}
/**
 *  GetElBy(tag,attr,val)
 *  Tableau des Ã©lÃ©ments 'tag' dont l'attribut 'attr' a la valeur 'val'.
 *  RNB, aout 2005
 *  @param  String  id : identifiant du noeud parent (vide si body)
 *  @param  String  tag : nom du tag (* pour tous les tags)
 *  @param  String  attr : attribut recherchÃ©
 *  @param  String  val : valeur de l'attribut
 *  @return array   tableau d'Ã©lÃ©ments dom
 */
function getElBy(id,tag,attr,val)
{
     var dbRes = [];
    var re = new RegExp('(^| )'+val+'( |$)');
    if (id=='') {
         var dbEl = document.getElementsByTagName(tag);
    }
    else {
        var dbEl = document.getElementById(id).getElementsByTagName(tag);
    }
     for (e=0; e<dbEl.length; e++) {
          if (attr == 'class') {
            if (re.test(dbEl[e].className)) { dbRes.push(dbEl[e]); }
        }
          else {
            if (re.test(dbEl[e].getAttribute(attr))) { dbRes.push(dbEl[e]); }
        }     
     }
     return dbRes;
}

/**
 *  mousePos(e)
 *  DÃ©termine la position de la souris.
 */
function mousePos(e) {
    var pos = new Array();
    pos[0] = (!e) ? event.x : e.pageX - 200;
    pos[1] = (!e) ? event.y + 10 : e.pageY - 50;   
    return pos;
}

/**
 *  formatData
 *  Mise en forme des balises strong dans les metas.
 *  RNB, septembre 2005 / dÃ©cembre 2006
 *  @param  String  tag : balise html Ã  analyser
 *  @param  String  val : classe de la balise
 */
function formatData(tag,val)
{
    var list = getElBy('c-centre',tag,'class',val);
    var nItem = list.length;
    for (var i=0; i<nItem; i++) {
        var lStrong = list[i].getElementsByTagName('strong');
        var n = lStrong.length;
        // Trouver le texte le plus long
        var mStrong = lStrong[0].childNodes[0].length;
        for (var j=1; j<n; j++) {
            if (lStrong[j].childNodes[0].length > mStrong) {
                mStrong = lStrong[j].childNodes[0].length;
            }
        }
        // Appliquer le formatage
        for (var j=0; j<n; j++) {
            var str = lStrong[j].childNodes[0].nodeValue;
            str = str.substring(0,str.lastIndexOf(':')-1);
            while (str.length<mStrong) { str += '.'; }
            lStrong[j].childNodes[0].nodeValue = str + ': ';
        }
    }
}

/**
 *  ONLOAD
 */
window.onload = function() {
    var inc = new incFile();
    root = 'http://localhost/textpattern420/';
    bodyId = document.body.id;
    if (bodyId=="front") {
        inc.include('themes/switch.utils.js');
        inc.include('themes/switch.cols.js');
    }
    if (bodyId=='article') {
        formatData('ul','meta');
        inc.include('themes/switch.utils.js');
		inc.include('themes/switch.cols.js');
        inc.include('themes/switch.comments.js');
        if (document.getElementById('update')) {
            inc.include('themes/switch.update.js');
        }
    }
    if (bodyId=="sous-rubrique") {
        inc.include('themes/switch.utils.js');
		inc.include('themes/switch.cols.js');
        inc.include('themes/switch.tree.js');
    }
}
