window.addEvent('domready', function() {
    $$('a').each(function(a){
        if (a.getProperty('rel') == 'external') {
            a.setProperty('target', '_blank');
        }
    });
    
});

var history_timeout = false;

function openFirst() {
    $('years').getFirst('a').fireEvent('click');
}

function initHistory() {
    $$('#years a').each(function(a) {
        $(a).oheight = $(a).getStyle('height');
        $(a).fx = new Fx.Morph($(a), {
            duration: 240, 
            transition: Fx.Transitions.Sine.easeInOut,
            onComplete: function() {
                if ($(a).hasClass('top-active') || $(a).hasClass('bottom-active')) {
                    $(a).getParent().getChildren('div.active-con').removeClass('active-con');
                    var id = $(a).getProperty('id').split('_');
                    id = id[1];
                    var con = $('con_'+id);
                    $(con).addClass('active-con').setStyles({opacity:0, display:'block' });
                    
                    if ($(a).hasClass('top')) {
                        var h = - (($(con).getSize().y.toInt() / 2) + 122);
                        $(con).setStyle('top', h);
                    } else {
                        var h = (132 - ($(con).getSize().y.toInt() / 2));
                        $(con).setStyle('top', h);
                    }
                    
                    $(con).tween('opacity', 1);
                }
            }
        });
        $(a).addEvent('click', function() {
            $clear(history_timeout);
            if (!$(a).hasClass('top-active') && !$(a).hasClass('bottom-active')) {
                var cl = $(this).hasClass('bottom') ? 'bottom' : 'top';
                var oth = cl == 'top' ? 'bottom' : 'top';
                $$('.active-con').tween('opacity',0);
                $(this).getParent().getChildren('.top-active').removeClass('top-active');
                $(this).getParent().getChildren('.bottom-active').removeClass('bottom-active');
                $(this).getParent().getChildren('a').each(function(c) {
                    if ($(c) != $(a)) {
                        if ($(c).hasClass(cl)) {
                            $(c).fx.start({'height': 30});
                        } else {
                            $(c).fx.start({'height': $(c).oheight});
                        }
                    }
                });
                $(this).addClass(cl + '-active').fx.start({'height': 130});
            } else {
                $(this).tween('opacity', [0.65, 1]);
            }
        });
    });
    
    history_timeout = openFirst.delay(500);
}

var map;
var gdir;
var geocoder = null;
var addressMarker;
var latlng = [51.202756,7.58054];

function loadMap() {
    if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setMapType(G_HYBRID_MAP);
        
        var pnt = new GLatLng(latlng[0], latlng[1]);

        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        GEvent.addListener(gdir, "addoverlay", correctSize);

        setDirections("A45 Lüdenscheid", "de");
    }
}

function correctSize() {
    //alert($('directions').getSize().y.toInt());
    if ($('directions').getSize().y.toInt() > 248) {
        $('map').setStyle('height', ($('directions').getSize().y.toInt() + 44) + 'px');
    } else {
        $('map').setStyle('height', '290px');
    }
    map.checkResize();
}

function setDirections(fromAddress, locale) {
    gdir.load("from: " + fromAddress + " to: " + latlng[0] + "," + latlng[1],
    { "locale": locale });
    return false;
}





function handleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
    alert("Zu der eingegebenen Adresse konnten keine geographischen Daten gefunden werden. Möglicherweise haben Sie eine relativ neue oder falsche Adresse eingegeben.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
    alert("Die Geocoding-Anfrage konnte nicht erfolgreich ausgeführt werden. Eine genaue Ursache ist nicht bekannt.\n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
    alert("Der HTTP q Parameter fehlt oder hat keinen Wert. Möglicherweise wurde ein leeres Adressfeld übermittelt.\n Error code: " + gdir.getStatus().code);

    //   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
    //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
    alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
    alert("Die Anfrage konnte nicht vollständig verarbeitet werden.\n Error code: " + gdir.getStatus().code);

    else alert("Ein unbekannter Fehler ist aufgetreten.");

}

function onGDirectionsLoad() { 
    
}

var t,b;
h_class = 1;
var fadeHeader = function() { t.fade('toggle'); }

function placeImage(src, c) {
    c.setStyle('background-image', 'url(' + src + ')');
    fadeHeader.delay(4000);
}

function loadImage(i, c) {
    if (images[i][1] === true) {
        placeImage(images[i][0], c);
    } else {
        new Asset.images([images[i][0]], {
            onComplete: function(){
                images[i][1] = true;
                placeImage(images[i][0], c);
            }
        });
    }
}

window.addEvent('load', function() {
    if (images.length >= 2) {
        t = $('motiv');
        b = $('header');
        t.set(
            'tween', 
            { 
                duration: 2400,
                onComplete: function() {
                    if (images.length > 2) {
                        var c = t.getStyle('opacity') < 1 ? t : b;
                        h_class = (h_class + 1) < images.length ? (h_class + 1) : 0;
                        loadImage(h_class, c);
                    }
                }
            }
        );
        fadeHeader.delay(3600);
    } else {
        $('header').hide();
        $('motiv').show();
    }
})
