var loading = false, loaded = [];
var correct_sidecontact = false;
var latlng = [51.219314,7.626416];
var map, gdir, mrk, directed = false;
var mnu_anim, reinit = false;

function openInfoBox() {
    mrk.openInfoWindow(
        '<div class="map_inner_w">' + 
        '<h3>Latsch <br />Information Management GmbH</h3>' + 
        'Konrad - Adenauer - Haus<br />' + 
        '58507 Lüdenscheid<br /></div>'
    );
}

function setDirections(fromAddress, toAddress) {
    gdir.load("from: " + fromAddress + " to: " + toAddress);
    
    return false;
}

function onGDirectionsLoad() { 
    if (directed === false) {
        mrk.hide();
        mrk.closeInfoWindow();
    }
}

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 renderMap(direction) {
    if (GBrowserIsCompatible()) {
        var con = document.getElementById('map');
        map = new GMap2(con);
        var pnt = new GLatLng(latlng[0], latlng[1]);
        mrk = new GMarker(pnt);

        map.addOverlay(mrk);
        map.addControl(new GMapTypeControl());
    	map.addControl(new GSmallMapControl());
    	map.addControl(new GScaleControl());
        
        if (direction === true) {
            gdir = new GDirections(map, document.getElementById("directions"));
            GEvent.addListener(gdir, "load", onGDirectionsLoad);
            GEvent.addListener(gdir, "error", handleErrors);
            
            setDirections("A45 Lüdenscheid", "Friedrich Ebert Straße 303, 58566 Kierspe");
        } else {
            map.setCenter(pnt, 16, G_HYBRID_MAP);
            GEvent.addListener(mrk, 'click', function() {
                openInfoBox();
            });
            
            $(window).load(function() {
                map.panDirection(0,1);
                openInfoBox();
            });
        }
    }
}

function animBg(el, offset, sprite, frames, time) {
    $(el).show();
    $(el).css('backgroundPosition', '0px ' + (sprite * offset) + 'px');
    if (sprite > frames)
        sprite--;
    else
        sprite++;
    if (sprite < frames || sprite > frames) {
        mnu_anim = window.setTimeout(function() {animBg($(el), offset, sprite, frames, time);}, time);
    }
}

function enlarge(box) {
    if ($(box).width() < 600) {
        shrinkBox($(box).siblings('.box'));
        stretchBox($(box));
    }
}

function stretchBox(box) {
    $(box).animate({
        'width': 717
    }).find('img.title').animate({
        'height': 57,
        'top': 32
    },360).end().find('img.image').animate({
        'height': 290,
        'right': 0
    },360);
}

function shrinkBox(box) {
    $(box).stop(true).animate({
        'width': 231
    }).find('img.title').stop(true).animate({
        'height': 42,
        'top': 32
    },300).end().find('img.image').stop(true).animate({
        'height': 175,
        'right': -65
    },300);
}

function reinitBox(box) {
    $(box).stop(true).animate({
        'width': 474
    }).find('img.title').animate({
        'height': 57,
        'top': 125
    },360).end().find('img.image').animate({
        'height': 220,
        'right': -70
    },360);
}

jQuery().ready(function() {
    $('a').each(function() {
        $(this).bind('click', function() {
            if (this.blur) {this.blur();}
        });
    });
    
    $('#homelink').addClass('nohover').hover(
        function() {
            $(this).stop().css('backgroundPosition', '0 -462px');
            animBg($(this), -73, 0, 10, 26);
        },
        function() {
            clearTimeout(mnu_anim);
            animBg($(this), -73, 9, 0, 19);
        }
    );
    
    $('#rss').addClass('nohover').hover(
        function() {
            $(this).stop().css('backgroundPosition', '0 -462px');
            animBg($(this), -50, 0, 10, 15);
        },
        function() {
            clearTimeout(mnu_anim);
            animBg($(this), -50, 9, 0, 10);
        }
    );
    
    if ($('#rss-top').length) {
        $('#rss-top').addClass('nohover').hover(
            function() {
                $(this).stop().css('backgroundPosition', '0 -462px');
                animBg($(this), -50, 0, 10, 15);
            },
            function() {
                clearTimeout(mnu_anim);
                animBg($(this), -50, 9, 0, 10);
            }
        );
    }
    
    if ($('#header-boxes').length && $('#header-boxes div.box').length) {
        $('#header-boxes div.box').each(function() {
            $(this).hoverIntent(
                function() { 
                    window.clearTimeout(reinit);
                    enlarge($(this)); 
                },
                function() {
                    var b = $(this);
                    reinit = window.setTimeout(function() {
                        reinitBox($(b).siblings('.box').andSelf());
                    }, 2400); 
                }
            );
        });
    }
});