﻿
// Menü
menuStep = 10;
menuStepDelayClose = 10;
menuStepDelayOpen = 30;
menuCloseDelay = 200;
menuHeight = 0;
timeoutId = false;
ie7deviation = 0;

// Menü: Fadeout des Hauptteils:
selectedFadeOutPages = false; // wenn selektiv ausfaden, dann z.B.: new Array('buero.html', 'index.html', 'profil.html');
fadeOutNonSelectedPages = true;
opacityMainFieldWhenRolledOut = 0.3;
fadeOutStepDelay = 50;
fadeInStepDelay = 50;
fadeStep = 0.1;
aOpacElemsAll = new Array('.content');
aOpacElemsIE9 = new Array('#left', '#middle', '#right', '.content a[target="_blank"]', '#scrollbar_content a');
aOpacElems = new Array();
busyOutFading = false;
busyInFading = false;
cancelFadeIn = false;
cancelFadeOut = false;

// Scrollbar
mouseIsDown = false;
initTimeStep = 40;
finalTimeStep = 10;
accel = 20;

document.observe('dom:loaded',function(){
        // Menü
        menuHeight = (-1) * parseInt($('projectLinks').getStyle('bottom'));
        
        // Link zur Non-JS-Seite projekte.html blind machen
        $$('#projects a')[0].writeAttribute({href: '#'});
        
        // herausfinden, ob auf einer Textseite, auf der das durchscheinende Menü schlecht lesbar ist, weil auf Textseite:
        if (typeof selectedFadeOutPages == 'object') {
            for( i = 0; i < selectedFadeOutPages.length; i++ ) {
                if ( -1 != window.location.href.indexOf(selectedFadeOutPages[i]) ) {
                    fadeOutNonSelectedPages = true;
                }
            }
        }
        
        // check of IE7 patch applies:
        /*
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
            var ieversion=new Number(RegExp.$1);
            if (ieversion>=7)
                 ie7deviation = 2;
        }
        */

        // Event-Handler:
        $('projects').observe('mouseover', function(event){
            if ( timeoutId ) {
                window.clearTimeout(timeoutId);
                timeoutId = false;
            }
            rolloutProjectLinks();
            event.stop();
        });
        $('projects').observe('mouseout', function(event){
            timeoutId = window.setTimeout('rollinProjectLinks()', menuCloseDelay);
            event.stop();
        });
        $('projectLinks').observe('mouseover', function(event){
            if ( timeoutId ) {
                window.clearTimeout(timeoutId);
                timeoutId = false;
            }
            event.stop();
        });
        $('projectLinks').observe('mouseout', function(event){
            timeoutId = window.setTimeout('rollinProjectLinks()', menuCloseDelay);
            event.stop();
        });
        
        // In den Projekten zu Beginn das Bild des erstgelisteten Projekts setzen
        if ( (firstA = $$('div.project.content a[href*="javascript:changePhoto("]')).size() >= 1 ) {
            firstImageFileName = firstA[0].href.match(/'(.*)'/)[1];
            changePhoto(firstImageFileName);
        }

        // Scrollbar
        scrollbar = new Control.ScrollBar('scrollbar_content','scrollbar_track');

        $('up').observe('mousedown',function(event){
            mouseIsDown = true;
            scroll('up');
            event.stop();
        });
        $('up').observe('mouseup',function(event){
            mouseIsDown = false;
            event.stop();
        });

        $('down').observe('mousedown',function(event){
            mouseIsDown = true;
            scroll('down');
            event.stop();
        });
        $('down').observe('mouseup',function(event){
            mouseIsDown = false;
            event.stop();
        });

});

function rollinProjectLinks() {
    if ( fadeOutNonSelectedPages ) {
        // Hauptteil transparent machen:
        cancelFadeOut = true;
        cancelFadeIn = false;
        fadeInStart();
    }
    
    currentHeight = parseInt($('projectLinks').getStyle('height')) - menuStep;
    if ( currentHeight > 0) {
        bottom = currentHeight + ie7deviation;
        $('projectLinks').setStyle({
            height: currentHeight + 'px',
            bottom: '-' + bottom + 'px'});
        window.setTimeout('rollinProjectLinks()', menuStepDelayClose);
    }
    else {
        $('projectLinks').setStyle({
            height: 0,
            bottom: 0});
    }
}

function rolloutProjectLinks() {
    if ( fadeOutNonSelectedPages ) {
        // Hauptteil transparent machen:
        cancelFadeIn = true;
        cancelFadeOut = false;
        fadeOutStart();
    }
        
    if ( (currentHeight = parseInt($('projectLinks').getStyle('height'))) +menuStep <= menuHeight) {
        i = currentHeight+menuStep;
        bottom = i + ie7deviation;
        $('projectLinks').setStyle({
            height: i + 'px',
            bottom: '-' + bottom + 'px'});
        window.setTimeout('rolloutProjectLinks()', menuStepDelayOpen);
    }
    else {
        if ( menuHeight - parseInt($('projectLinks').getStyle('height')) > 0 ) {
            bottom = menuHeight + ie7deviation;
            $('projectLinks').setStyle({
                height:  menuHeight + 'px',
                bottom: '-' + bottom + 'px'});
        }
    }
}

function scroll(dir, step, substep) {
    factor = dir == 'up' ? -1 : 1;
    if (typeof step == 'undefined') {
        step = initTimeStep;
    }

    scrollbar.scrollBy(factor * 4);

    if (mouseIsDown) {
        if (step > finalTimeStep) {
            step = step - step/accel;
        }
        window.setTimeout("scroll('" + dir + "', " + step + ")", step);
    }
}

function fadeOutStart() {
	if ( aOpacElems.length == 0 )
	{
		setOpacElems();
	}
	fadeOut();
}

function fadeOut(currentOpacity) {
    if ( typeof currentOpacity == 'undefined' ) {
        // let subsequent starts of Outfading fail while busy outfading
        if ( busyOutFading ) return;

        currentOpacity = $$('.content')[0].getStyle('opacity');
        busyOutFading = true;
    }
    // secure subtraction against floating point calculation inexactness
    nextOpacity = Math.round(10000*(currentOpacity-fadeStep))/10000;
    if ( nextOpacity >= opacityMainFieldWhenRolledOut && ! cancelFadeOut ) {
		for ( i = 0; i < aOpacElems.length; i++ )
		{
			$$(aOpacElems[i]).invoke('setOpacity', nextOpacity);
		}
        window.setTimeout('fadeOut(' + nextOpacity + ')', fadeOutStepDelay);
    }
    else {
        busyOutFading = false;
    }
}

function fadeInStart() {
	if ( aOpacElems.length == 0 )
	{
		setOpacElems();
	}
	fadeIn();
}

function fadeIn(currentOpacity) {
    if ( typeof currentOpacity == 'undefined' ) {
        // let subsequent starts of Infading fail while busy infading
        if ( busyInFading ) return;

        currentOpacity = $$('.content')[0].getStyle('opacity');
        busyInFading = true;
    }
    // secure addition against floating point calculation inexactness
    nextOpacity = Math.round(10000*(currentOpacity+fadeStep))/10000;
    // bridge last small gap smaller than fadeStep:
    if ( Math.abs(1-nextOpacity) < fadeStep) nextOpacity = 1;
    
    if ( nextOpacity <= 1 && ! cancelFadeIn ) {
		for ( i = 0; i < aOpacElems.length; i++ )
		{
			$$(aOpacElems[i]).invoke('setOpacity', nextOpacity);
		}
        window.setTimeout('fadeIn(' + nextOpacity + ')', fadeInStepDelay);
    }
    else {
        busyInFading = false;
    }
}

function changePhoto(filename) {
    // setz rechts das richtige Bild
    image = 'images/' + filename;
    $('projectPhoto').setStyle({display: 'block'}).src = image;
    
    // hol den Titel für das Bild aus der Überschrift des Absatzes 
    title = $$('#left a[href*="' + filename + '"]')[0].previousSiblings().findAll(function(obj){return obj.tagName == 'P'})[0].select('span')[0].innerHTML;
    title = title.replace(/<br[^>]*>/im, ' ');
    // setz den Titel unter das Bild
    $$('div#imageFrame small')[0].innerHTML = title;
}

function setOpacElems()
{
	aOpacElems = aOpacElemsAll;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		ieversion = new Number(RegExp.$1);
		if (ieversion == 9)
		{
			for ( i = 0; i < aOpacElemsIE9.length; i++ )
			{
				if ( typeof $$(aOpacElemsIE9[i]) == 'object' && typeof $$(aOpacElemsIE9[i])[0] == 'object' )
				{
					aOpacElems.push(aOpacElemsIE9[i]);
				}
			}
			//alert(aOpacElems.toString());
			return;
		}
	}
}
