var ribbonTimer = 0;
var ribbonMaster = null;
var firstRibbonTab = true;

function importRibbonData(url) {
    var HTMLTabs=[];
    var data = eval('('+getFileContent(url)+')');
    var l = data.length;
    for (var i=0; i<l; i++) {
        var tabContent=[];
        var tab = data[i];
        var tabName = tab[0];
        var items = tab[1];
        var l2 = items.length;
        for (var i2=0; i2<l2; i2++) {
            var item = items[i2];
            tabContent[tabContent.length]=getData('ribbonTabItem', {
                content: item[0], url: item[1], target:item[2]||'frameBody'
            });
        }
        HTMLTabs[HTMLTabs.length]=getData('ribbonTab', { content:tabContent.join(''), index:i });
        (function(i) { addToRibbon(tabName, function() { changeTab(i); }) })(i.toString());
    }
    document.getElementById("menu").innerHTML=HTMLTabs.join('');
}

function addToRibbon(name, onactivate) {
    if (!ribbonMaster) { ribbonMaster=document.getElementById('ribbonMaster'); }
    var spacer = document.createElement("td");
    spacer.innerHTML=('<div style="width: 6px"></div>');
    var tab = document.createElement("td");
    tab.innerHTML = ('<div class="border left"></div><div style="padding-left: 7px; padding-right: 8px; float: left;">' + name + '</div><div class="border right"></div>');
    if (firstRibbonTab) {
        firstRibbonTab=false;
        tab.setAttribute("active", "true")
        tab.className="activeRibbonTab";
    }
    if (tab.attachEvent) {
        tab.attachEvent("onclick", function() {
            ribbonActive(tab, true);
        }, true);
    } else {
        tab.addEventListener("click", function() {
            ribbonActive(tab, true);
        }, true);
    }
    tab.onactivate = (function(e) { try { onactivate(e) } catch (ex) {} });
    tab.onmouseover = (function() { ribbonOver(tab); });
    tab.onmouseout  = (function() { ribbonOut (tab); });
    ribbonMaster.appendChild(spacer);
    ribbonMaster.appendChild(tab);
}
function ribbonOver(o) {
    if (o.getAttribute('active')!='true') { o.className='hoverRibbonTab'; }
    ribbonTimer=setTimeout(function() { ribbonActive(o); }, 750)
}
function ribbonOut(o) {
    o.className=((o.getAttribute('active')=='true')?'activeRibbonTab':'');
    clearTimeout(ribbonTimer);
}
function ribbonActive(o, generatePage) {
    var all = ribbonMaster.getElementsByTagName("TD");
    for (var i=0; i<all.length; i++) {
        all[i].setAttribute("active","false");
        all[i].setAttribute("class", "");
        all[i].className="";
    }
    o.setAttribute("active", "true")
    o.className="activeRibbonTab";
    try {
        var e = window.event;
        if (!e) {
            try { e=document.createEventObject(); } catch (ex) {}
        }
        o.onactivate(e);
    } catch (ex) {}
    try {
        if (!generatePage) { return true; }
        var w = (bodyFrame.window||bodyFrame.contentWindow);
        var doc = w.document;
        var str = [];
        str[str.length]='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        str[str.length]='<html>\n<head>\n<base href="'+location.href+'"/>\n<title>'+o.getElementsByTagName("DIV")[1].firstChild.data+'</title>\n<style type="text/css"><\!--\n    @import url(\'style.css\'); \n-\-></style>\n<script><!--\n    var desc="Contenu de la catégorie sélectionnée"; \n-\-><\/script>\n</head>\n<body>\n';
        str[str.length]='<div class="box"><div class="title">Voici les pages contenues dans cette catégorie :</div><div class="content links">'
        var all = document.getElementById("tab_"+currentTab).getElementsByTagName("A");
        var l = all.length;
        for (var i=0; i<l; i++) {
            var c = all[i];
            str[str.length]='\n<a onclick="location.href=this.href" href="'+c.href+'"><div><table height="100%" style="height: 100%; font-size: 15pt;"><tr><td>'+c.innerHTML+'</td></tr></table></div></a>';
        }
        str[str.length]='<div style="clear: both; height: 0px; font-size: 0px; line-height: 0px;"></div></div>';
        str[str.length]='</body></html>';
        doc.open();
        doc.write(str.join(''));
        doc.close();
        return true;
    } catch (ex) {}
}

var currentTab = "0";
function changeTab(newTab) {
    // Unactivate old tab
    document.getElementById("tab_"+currentTab).style.display="none";
    // Change current tab
    currentTab = newTab;
    // Activate current tab
    document.getElementById("tab_"+currentTab).style.display="block";
}

var bodyFrame = null;
var content = null;
var toolSize = (128);
function resizeFrame() {
    if (!bodyFrame) { 
        bodyFrame = document.getElementById("frameBody");
        if (!bodyFrame) {
            setTimeout(resizeFrame, 1);
            return false;
        }
    }
    if (window.p) {
        bodyFrame.style.height = content.document.documentElement.scrollHeight+"px";
    } else {
        var currentH = bodyFrame.style.height;
        var newH     = document.documentElement.offsetHeight;
        if (window.innerHeight) {
            newH = window.innerHeight;
        }
        if ((bodyFrame.contentWindow||bodyFrame.window) && (bodyFrame.contentWindow||bodyFrame.window).hideVistabar) {
            document.getElementById('vistabar').style.display='none';
            newH -= (55);
        } else {    
            document.getElementById('vistabar').style.display='block';
            newH -= (toolSize);
        }
        newH = newH.toString() + "px";
        if (newH != currentH) { bodyFrame.style.height=newH; }
    }
}

if (window.attachEvent) {
    window.attachEvent("onload", resizeFrame, true);
    window.attachEvent("onresize", resizeFrame, true);
}
else {
    window.addEventListener("load", resizeFrame, true);
    window.addEventListener("resize", resizeFrame, true);
}

function autoSwithShareSpace(currentUrl) {
    try {
        if (currentUrl.indexOf("/sharespace")!=-1) {
            /* A CHANGER SI ON AJOUTE UN ONGLET APERS SHARESPACE */
            var arr = ribbonMaster.getElementsByTagName("TD");
            ribbonActive(arr[arr.length-1], false);
        }   
    } catch (ex) {}
}

var lastContentWindow = null;
var currentUrl = "";
setInterval(function() {
    if (!bodyFrame) { bodyFrame = document.getElementById("frameBody"); }
    try {
        var contentWindow = bodyFrame.window ? bodyFrame.window : bodyFrame.contentWindow;
        content=lastContentWindow=contentWindow;
        if (!contentWindow.document.body.isNowWhite) {
            location.hash = contentWindow.location.pathname;
            var body = contentWindow.document.body
            
            //body.bgColor="white";
            //body.background="";
            body.tabIndex = -1;
            body.style.scrollbarBaseColor="#dbe9fc";
            
            body=body.parentNode;
            
            //body.bgColor="white";
            //body.background="";
            body.tabIndex = -1;
            body.style.scrollbarBaseColor="#dbe9fc";
            
            currentUrl = location.hash;
            contentWindow.document.body.isNowWhite=true;
            try { resizeFrame(); } catch (ex) {}
        }
        if (contentWindow && (location.hash != "") && (location.hash != "#") && (location.hash != currentUrl) && (location.hash != "#"+currentUrl)) {
            if (location.hash.charAt(0)=="#") {
                contentWindow.location.href=location.hash.substr(1);
                currentUrl=location.hash.substr(1);
                autoSwithShareSpace(currentUrl);
            } else {
                contentWindow.location.href=location.hash;
                currentUrl=location.hash;
                autoSwithShareSpace(currentUrl);
            }
        }
    } catch (ex) { }
}, 400)
