$(document).ready(function(){
    init();
}); //Ende $(document).ready();
/*******************************************************************************
 * log() : sort of var_dump() for JS
 ******************************************************************************/
var log = function(){
    if (window && window.console && window.console.log) {
        for (var i = 0, len = arguments.length; i < len; i++) {
            console.log(arguments[i]);
        }
    }
}//Ende log
/*******************************************************************************
 * all the things that have to be done on document load
 * requires JQuery
 *
 * @return void
 ******************************************************************************/
var init = function(){
    $(".hidden").hide();
    $("a[href^=#]").each(function(){
        $(this).click(function(){
            var $href = $(this).attr("href");
            $href = $href.split("#");
            $href = "#" + $href[1];
            window.location.hash = $href;
            return false;
        });
    });
    var $content_height = $("#content, #content2, #content3, #content4, #content5").height();
    var $subnavi_height = $("#subnavi-content").height();
    var $custom_height = $("#custom").height() - 44; //44 i don't know why ... :-D
    if ($custom_height > $content_height) {
        $content_height = $custom_height;
        log($content_height, $custom_height);
        $("#content, #content2, #content3, #content4, #content5").height($custom_height);
    }
    if ($content_height > $subnavi_height) {
        $("#subnavi-content").height($content_height - 50);
    }
    //Empty Search-Field on focus, refill on blur 
    var $val;
    $("#such_input, .empty-on-focus").focus(function(){
        $val = $(this).val();
        $(this).val("");
    }).blur(function(){
        if ($(this).val().length <= 0) {
            $(this).val($val);
        }
    });
    address = $("#adresse").html();
    if (address) {
        if (address.length > 0) {
            loadGoogleMap(address);
        }
    }
    $("#print").click(function(){
        $("#content, #content2, #content3, #content4, #content5").print();
    });
    dsOptions = {
        left: -1,
        top: -1,
        blur: 3,
        opacity: 0.3
    };
    $("#content, #content2, #content3, #content4, #content5").dropShadow(dsOptions);
    //IE6 PNG-Fix
    if ($.browser.msie && parseInt($.browser.version) == 6) {
        var text = 'Sie benutzen einen veralteten Browser (Internet Explorer 6).';
        text += ' Wir empfehlen einen Update auf einen Browser der neuen Generation<br />';
        text += '(<a href="http://www.mozilla-europe.org/de/firefox/">Firefox 3.5</a>,';
        text += ' <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx">Internet Explorer 8</a>, ...).';
        $("#center").prepend('<div id="ie6-msg">' + text + '</div>');
        $("#ie6-msg").show("slow");
        DD_belatedPNG.fix('body, img, #subnavi-top-left, #subnavi-bottom-left ');
    }
}

/****************************************
 * GOOGLE MAP laden
 ****************************************/
function loadGoogleMap(address, description){
    geocoder = new GClientGeocoder();
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("google-map"));
        if (geocoder) {
            geocoder.getLatLng(address, function(point){
                if (!point) {
                    alert(address + " not found");
                }
                else {
                    map.setCenter(point, 13);
                    map.addControl(new GLargeMapControl());
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml("<h3>" + (description ? description : address) + "</h3>");
                }
            });
        }
    }
}

/*************************************
 * JQuery Print by OT
 *************************************/
jQuery.fn.print = function(){
    var styles = '';
    $("head link").each(function(){
        styles += '<link rel="stylesheet" type="text/css" href="' + $(this).attr("href") + '" />' + "\n";
    })
    var frameName = ("printer-" + (new Date()).getTime());
    var frameTitle = document.title;
    $("<iframe name='" + frameName + "' id=" + frameName + ">").css({
        "width": "1px",
        "height": "1px",
        "position": "absolute",
        "left": "-10px"
    }).appendTo($("body"));
    var frameDoc = window.frames[frameName].document;
    frameDoc.open();
    frameDoc.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
    frameDoc.write("<html>");
    frameDoc.write("<head>");
    frameDoc.write("<style type=\"text/css\">#content_bottom{display:none;}</style>");
    frameDoc.write("<title>");
    frameDoc.write(frameTitle);
    frameDoc.write("</title>");
    frameDoc.write("</head>");
    frameDoc.write("<body>");
    frameDoc.write(styles);
    frameDoc.write(this.html());
    frameDoc.write("</body>");
    frameDoc.write("</html>");
    frameDoc.close();
    window.frames[frameName].focus();
    window.frames[frameName].print();
}
