var menuout = false;
var loginout = false;
var mouseX = 0;
var mouseY = 0;
var loaded = false;

function pageloaded() {
    loaded = true;
    // You need to tell Mozilla to start listening:
    if (window.Event && document.captureEvents)
        document.captureEvents(Event.MOUSEMOVE);
    // Then assign the mouse handler
    document.onmousemove = getMousePos;
    var height = getWindowSize()[1];
    height -= 130;
    try {
        if (welcome) {
            $('bgimages').style.height = "auto";
            $('bgimages').show();
            document.getElementById('bgimages').style.height = height + 30 + "px";
            if (loadedimages == 9) {
                $('bga').show();
                $('bgb').hide();
            }
        }
    }
    catch (e) {
        document.getElementById('bgimages').style.display = "none";
    }
    getMousePos();
    return false;
}

var menueffect = null;
var menuoff = null;

function showmenu() {
    if (menuoff != null) {
        clearTimeout(menuoff);
        menuoff = null;
    }
    if (menuout) return false;
    menuout = true;
    if (menueffect) menueffect.cancel();
    menueffect = new Effect.Morph('menu', { style: 'top: 0px;', duration: 0.3, queue: 'end' });
    return false;
}

function hidemenu() {
    menuoff = null;
    if (!menuout) return false;
    menuout = false;
    if (menueffect) menueffect.cancel();
    new Effect.Morph('menu', { style: 'top: -40px', duration: 0.3, queue: 'end' });
    return false;
}

function showlogin() {
    if (!loginout) {
        loginout = true;
        var litw = parseInt(document.getElementById('login_tab').offsetWidth);
        var lifw = parseInt(document.getElementById('login_form').offsetWidth);
        new Effect.Morph('login_tab', { style: 'right: ' + (-litw - 40).toString() + 'px', duration: 0.3, queue: 'end' });
        new Effect.Morph('login_form', { style: 'right: 0px', duration: 0.3, queue: 'end' });
    }
    return false;
}

function hidelogin() {
    if (loginout) {
        loginout = false;
        var lifw = document.getElementById('login_form').getWidth();
        new Effect.Morph('login_form', { style: 'right: ' + (-lifw - 40).toString() + 'px', duration: 0.3, queue: 'end' });
        new Effect.Morph('login_tab', { style: 'right: 0px', duration: 0.3, queue: 'end' });
    }
    if (menuout && menuoff == null) {
        menuoff = setTimeout(hidemenu, 1500);
    }
    return false;
}

function getMousePos(e) {
    if (!loaded)
        return false;
    if (!e)
        var e = window.event || window.Event;
    if ('undefined' != typeof e.pageX) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }
    else {
        mouseX = e.clientX + document.body.scrollLeft;
        mouseY = e.clientY + document.body.scrollTop;
    }
    var t = document.getElementById("tooltip");
    if (t != null) {
        t.style.top = 5 + mouseY + "px";
        t.style.left = 20 + mouseX + "px";
    }

    var h=document.getElementById('header');
    var xmin = parseInt(h.offsetLeft);
    var xmax = parseInt(h.offsetWidth) + xmin;

    if (xmin <= mouseX && xmax >= mouseX) {
        if (mouseY <= 100)
            showmenu();

        if (mouseY > 100/* && menuout*/)
            hidelogin();

        if (mouseY <= 100 && mouseY > 60 && menuout) {
            var l = document.getElementById('login_tab');
            if (l) {
                var lleft = xmax + parseInt(l.offsetLeft) - parseInt(l.offsetWidth);
                if (lleft < mouseX)
                    showlogin();
            }
        }
    }
    var height = getWindowSize()[1];
    //height -= document.getElementById('header').clientHeight;
    //height -= document.getElementById('footer').clientHeight;
    height -= 130;
    //document.getElementById('bgimages').style.height = (height+30) + "px";
    document.getElementById('content').style.height = height + "px";
    
    try {
        if (welcome) {
            document.getElementById('bgimages').style.height = height + 30 + "px";
        }
    }
    catch (e) {
        document.getElementById('bgimages').style.display = "none";
    }
    
    //    ct = parseInt(document.getElementById('content').style.top);
    //    ct -= height;
    //    document.getElementById('content').style.top = ct + "px;";
    return false;
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function getWindowSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}

function growimage(img) {
    new Effect.BlindDown(img, { duration: 0.2, queue: 'end' });
    return false;
}

function fadeimage(img) {
    new Effect.Fade(img, { duration: 0.5, from: 0, to: 1 });
    return false;
}

function ShowTooltip(s, i) {
    if (!loaded) return false;
    t = document.getElementById("tooltip");
    if (t != null) {
        s=s.replace(/NL/gi, "<br />")
        t.innerHTML = s;
        if (i != "") {
            t.innerHTML += '<br /><img src="' + i + '" tag="" />';
        }
        t.style.display = "block";
    }
    return false;
}

function HideTooltip() {
    document.getElementById("tooltip").style.display = "none";
    return false;
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function ShowMore(i)
{
	var r="rest"+i.toString();
	var m=document.getElementById("more"+i.toString());
	var l=document.getElementById("less"+i.toString());
	var h = m.clientHeight;
	var re = document.getElementById(r);
	re.style.height = "auto";
	var h = re.clientHeight;
	re.style.height = "0px";
	
	new Effect.Morph(r, { style: 'height: '+h.toString()+'px;', duration: 1, queue: 'end' }); 
    m.style.display="none"; 
    l.style.display="block";
}
function ShowLess(i)
{
	var r="rest"+i.toString();
	var m=document.getElementById("more"+i.toString());
	var l=document.getElementById("less"+i.toString());
	new Effect.Morph(r, { style: 'height: 0;', duration: 1, queue: 'end' }); 
    m.style.display="block"; 
    l.style.display="none";
}

addLoadEvent(pageloaded);
