


//---------------------------------------------------------------------------------------
window.onload = function()
{
    writeChildContext();
    setupImageFlow();
}








var menucontext = new Array();
menucontext["actives"] = new Array();
menucontext["other"] = new Array();
var allmenus = new Array();

function in_array(a, b)
{
    for(var x=0; x<b.length; x++)
        if(a == b[x])
            return true;
    return false;
}

function getOthers(a, b)
{
    var ret = new Array();
    for(var x=0; x<allmenus.length; x++){
        if(!in_array(allmenus[x], a) && !in_array(allmenus[x], b))
            ret[ret.length] = allmenus[x];
    }
    return ret;
}

function packChilds(id)
{
    var ret = new Array();
    if(document.getElementById(id)){

        ret[ret.length] = id;
        var naviglinks = document.getElementById(id).getElementsByTagName("a");
        for(var x=0; x<naviglinks.length; x++){
            if(naviglinks[x].id){
                var nl = naviglinks[x].id.replace("navlink_", "sub_");
                if(document.getElementById(nl)){
                    if(!in_array(nl, ret))
                    ret[ret.length] = nl;
                }
                nl = packChilds(nl);
                if(nl.length > 0){
                    ret.push(nl);
                }
            }
            if(id == "navlink_moderation") alert(ret.join("\n"));
        }
    }
    return ret;
}

function array_unique(_arr)
{
   var ret = new Array();
   for(var x=0; x<_arr.length; x++){
       if(!in_array(_arr[x], ret))
           ret[ret.length] = _arr[x];
   }
   return ret;
}

function writeChildContextSubs(id, parent_array)
{
    if(document.getElementById(id)){
        if(parent_array == null)
            var par = new Array();
        else
            var par = new Array(parent_array);
        par[par.length] = id;
        var naviglinks = document.getElementById(id).getElementsByTagName("a");
        for(var x=0; x<naviglinks.length; x++){
            if(naviglinks[x].id){
                var chlds = packChilds(naviglinks[x].id.replace("navlink_", "sub_"));
                menucontext[naviglinks[x].id] = new Array();
                menucontext[naviglinks[x].id]["container"] = id;
                menucontext[naviglinks[x].id]["parents"] = par;
                menucontext[naviglinks[x].id]["childs"] = array_unique(chlds);
                menucontext[naviglinks[x].id]["other"] = getOthers(par, chlds);
                menucontext[naviglinks[x].id]["classname"] = "home";
                writeChildContextSubs(naviglinks[x].id.replace("navlink_", "sub_"), par);
            }
        }
    }
}


function writeChildContext()
{
    var hiddenmenue = document.getElementById("hiddenmenu").getElementsByTagName("div");
    for(var x=0; x<hiddenmenue.length; x++){
        if(hiddenmenue[x].id){
            allmenus[allmenus.length] = hiddenmenue[x].id;
        }
    }
    var naviglinks = document.getElementById("navigation").getElementsByTagName("a");
    for(var x=0; x<naviglinks.length; x++){
        if(naviglinks[x].id){
            var chlds = packChilds(naviglinks[x].id.substr(0, naviglinks[x].id.length-1))
            menucontext[naviglinks[x].id] = new Array();
            menucontext[naviglinks[x].id]["container"] = "navigation";
            menucontext[naviglinks[x].id]["parents"] = new Array();
            menucontext[naviglinks[x].id]["childs"] = array_unique(chlds);
            menucontext[naviglinks[x].id]["other"] = getOthers(new Array(), chlds);
            menucontext[naviglinks[x].id]["classname"] = "home";
            writeChildContextSubs(naviglinks[x].id.substr(0, naviglinks[x].id.length-1), null);
        }else{
            naviglinks[x].onmouseover = closeMenues;
        }
    }
    writeMenuEvents();
}


var activemenuetime = false;
function writeMenuEvents()
{
    for (var id in menucontext){
        if(id != "actives" && id != "other"){
            if(document.getElementById(id)){
                var link = document.getElementById(id);
                link.onmouseover = function(){
                    var actives = menucontext[this.id]['parents'];
                    actives[menucontext["actives"].length] = menucontext[this.id]['container'];
                    actives[menucontext["actives"].length] = menucontext[this.id]['childs'][0];
                    actives = array_unique(actives);
                    var other = menucontext[this.id]['other'];
                    menueOver(this,  actives,  other, menucontext[this.id]['childs'][0]);
                }
                link.onmouseout = function(){
                    var actives = menucontext[this.id]['parents'];
                    actives[menucontext["actives"].length] = menucontext[this.id]['container'];
                    actives[menucontext["actives"].length] = menucontext[this.id]['childs'][0];
                    actives = array_unique(actives);
                    var other = menucontext[this.id]['other'];
                    activemenuetime = window.setTimeout("closeMenues()", 1000);
                }
            }
        }
    }
}

function closeMenues()
{
    for(var x=0; x<allmenus.length; x++){
        if(document.getElementById(allmenus[x]))
            document.getElementById(allmenus[x]).style.display = "none";
    }
    window.clearTimeout(activemenuetime);
}


function menueOver(obj, actives, other, child)
{
    window.clearTimeout(activemenuetime);
    for(var x=0; x<other.length; x++){
        if(document.getElementById(other[x]))
            document.getElementById(other[x]).style.display = "none";
    }
    for(var x=0; x<actives.length; x++){
        if(actives[x] != "navigation" && document.getElementById(actives[x])){
            document.getElementById(actives[x]).style.display = "";
        }
    }
    if(document.getElementById(child)){
        var child = document.getElementById(child);
        if(child.className == "sub"){
            child.style.position = "absolute";
            child.style.top  = (findPosY(obj)+25).toString()+"px";
            child.style.left = (findPosX(obj)+2).toString()+"px";
            child.style.display = "";
        }else if(child.className == "sub_sub"){
            var posx = findPosX(obj)+obj.offsetWidth;
            var posy = findPosY(obj)-11;
            child.style.position = "absolute";
            child.style.left = posx.toString()+"px";
            child.style.top  = posy.toString()+"px";
            child.style.display = "";
        }
    }
}





















//---------------------------------------------------------------------------------------
function setupImageFlow()
{
        if(document.getElementById("imageflow"))
        {
                hide(conf_loading);
                refresh(true);
                show(conf_images);
                show(conf_scrollbar);
                initMouseWheel();
                initMouseDrag();
        }
}

//---------------------------------------------------------------------------------------
function chimage(obj, img)
{
    if(typeof img == "undefined"){
        if(obj.src.search('gray') != -1)
            obj.src = obj.src.replace('gray', 'color');
        else
            obj.src = obj.src.replace('color', 'gray');
    }else{
        obj.src = img;
    }
}





//------------------------------------------------------------------------------------------------------------
function lightboxImage(obj)
{

    document.getElementById("imageflow").style.visibility = 'hidden';
    var imgs = document.getElementById("imageflow").getElementsByTagName("img");
    for(var x=0; x<imgs.length; x++)
        imgs[x].style.visibility = 'hidden';

    document.getElementById("bigtext").style.backgroundImage = 'url('+obj.url+')';

    document.getElementById("bigtext").style.backgroundRepeat = 'no-repeat';
    document.getElementById("bigtext").style.backgroundPosition = 'center center';
    window.setTimeout("resetFunction('lowboxImage')", 500);

}

//------------------------------------------------------------------------------------------------------------
function resetFunction(funcname)
{
    if(funcname == 'lowboxImage')
        document.getElementById("bigtext").onclick = function () {lowboxImage();}
    else
        document.getElementById("bigtext").onclick = function () {nischt();}
}

//------------------------------------------------------------------------------------------------------------
function lowboxImage()
{
    document.getElementById("imageflow").style.visibility = 'visible';
    var imgs = document.getElementById("imageflow").getElementsByTagName("img");
    for(var x=0; x<imgs.length; x++)
        imgs[x].style.visibility = 'visible';
    document.getElementById("bigtext").style.backgroundImage = '';
    window.setTimeout("resetFunction('nischt')", 500);
}

function nischt()
{

}






    //----------------------------------------------------------------------------------------------------
    function findPosX(obj) {
       var curleft = 0;
       if(obj.offsetParent) {
          while(obj.offsetParent) {
             curleft += obj.offsetLeft
             obj = obj.offsetParent;
          }
       }
       else if(obj.x)
         curleft = obj.x;
       return curleft;
    }


    //----------------------------------------------------------------------------------------------------
    function findPosY(obj) {
       var curtop = 0;

       /*if (window.pageYOffset) {
          curtop -= window.pageYOffset;
       } else if (document.body && document.body.scrollTop) {
          curtop -= document.body.scrollTop;
       }*/

       if(obj.offsetParent) {
          while(obj.offsetParent) {
             curtop += obj.offsetTop
             obj = obj.offsetParent;
          }
       }
       else if(obj.y)
          curtop = obj.y;
       return curtop;
    }





