/*******************************************************************************
 * sort of var_dump() for JS
 ******************************************************************************/
var debug = function(elem){
    if (window.console) {
        console.debug(elem);
    }
} //Ende debug
function log(){
    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 1.3+ !!!!!!
 *
 * @return void
 ******************************************************************************/
$(function(){
    init();
}); // ende $(document).ready();
/****************************************
 * GOOGLE MAP laden
 ****************************************/
var map;
var gdir;
var geocoder = null;
var addressMarker;
var address = 'Bacchusstraße 20, D-79235 Vogtsburg-Bischoffingen';

function loadGoogleMap(){
    geocoder = new GClientGeocoder();
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"), {
            size: new GSize(450, 500)
        });
        map.setUIToDefault();
        var myDiv = document.getElementById("directions");
        gdir = new GDirections(map, myDiv);
        GEvent.addListener(gdir, "error", handleErrors);
        if (geocoder) {
            geocoder.getLatLng(address, function(point){
                if (!point) {
                    alert(address + " not found");
                }
                else {
                    map.setCenter(point, 14);
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml(address);
                }
            });
        }
    }
}


function setDirections(fromAddress){
    if (address && gdir) {
        $("#directions").html("");
        if (address.length > 1) {
            gdir.load("from: " + fromAddress + " to: " + address, {
                "locale": "de"
            });
        }
    }
}


function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
        $("#directions").html("Es konnte keine entsprechende geografische Position für die angegebene Adresse gefunden werden. Dies kann daran liegen, dass die Adresse relativ neu oder möglicherweise falsch ist.");
    }
    else 
        if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
            $("#directions").html("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
        }
        else 
            if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
                $("#directions").html("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
            }
            else 
                if (gdir.getStatus().code == G_GEO_BAD_KEY) {
                    $("#directions").html("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) {
                        $("#directions").html("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
                    }
                    else {
                        $("#directions").html("An unknown error occurred.");
                    }
    
}

/*****
 * Ende GMAP
 */
function init(){
    try {
        $("a.fancybox, a.lightbox, a.popup, a.thickbox").lightBox({
            imageBtnClose: "../../assets/jquery/plugins/lightbox/0.5/images/lightbox-btn-close.gif"
        });
    } 
    catch (e) {
    }
    try {
        $("#content3 .info-start:even").addClass("info-even");
        $("#content3 .info-start:odd").addClass("info-odd");
    } 
    catch (e) {
    }
    try {
        loadGoogleMap();
        $("#route-submit").live("click", function(){
            fromAddress = $("#route").val();
            setDirections(fromAddress);
        });
        $("#route").keyup(function(eo){
            if (eo.keyCode == 13 || eo.charCode == 13) {
                fromAddress = $("#route").val();
                setDirections(fromAddress);
            }
        });
    } 
    catch (e) {
    }
    try {
        $("#tabs").tabs({
            cookie: {
                expires: 30,
                path: "/"
            }
        });
    } 
    catch (e) {
    }
    try {
        $("#kopfbild, .home_box, #top_start .suche").dropShadow({
            left: 0,
            top: 1,
            blur: 2,
            color: '#666666'
        });
    } 
    catch (e) {
    }
    try {
    
        $("a.extern").live("click", function(){
            window.open($(this).attr("href"));
            return false;
        });
    } 
    catch (e) {
    }
    try {
    
        $("a.back-link").live("click", function(){
            if ($(this).attr("href").length < 1) {
                history.back();
                return false;
            }
        });
    } 
    catch (e) {
    }
    try {
    
        $("a.galerie_v2_link").lightBox({
            fixedNavigation: true,
            imageLoading: '../../assets/jquery/plugins/lightbox/0.5/images/lightbox-ico-loading.gif',
            imageBtnPrev: '../../assets/jquery/plugins/lightbox/0.5/images/lightbox-btn-prev.gif',
            imageBtnNext: '../../assets/jquery/plugins/lightbox/0.5/images/lightbox-btn-next.gif',
            imageBtnClose: '../../assets/jquery/plugins/lightbox/0.5/images/lightbox-btn-close.gif',
            imageBlank: '../../assets/jquery/plugins/lightbox/0.5/images/lightbox-blank.gif',
            txtImage: "Bild",
            txtOf: "von"
        });
    } 
    catch (e) {
    }
    try {
        $("#ambit-form").submit(function(){
            $.ajax({
                url: $(this).attr("action"),
                data: {
                    zip: $("input[name=zip]").val(),
                    country: $("select[name=country]").val(),
                    rad: $("select[name=rad]").val(),
                    t: $("input[name=t]").val(),
                    nav: $("input[name=nav]").val(),
                    lang: $("input[name=lang]").val()
                },
                success: function(data){
                    $("div#ambit-return").html(data);
                },
                beforeSend: function(){
                    $("div#ambit-return").html('<img src="../../assets/common/loader.gif" alt="Loading" />');
                }
            });
            return false;
        });
    } 
    catch (e) {
    }
    try {
        $(".letters").tabs();
        $("a.letter").first().click();
    } 
    catch (e) {
    }
}

function setContentHeight(){
    try {
        var contentarea = $("#contentarea").height();
        var subnavi = $("#subnavi").height();
        var custom = $("#custom").height();
        if (isNaN(custom) || custom == 0) {
            custom = $("#custom_start").height();
        }
        log(contentarea, subnavi, custom);
        if (contentarea > custom && contentarea > subnavi) {
            $("#subnavi, #custom").height(contentarea);
            $("#custom_start").height(contentarea + 17);
        }
        else {
            if (subnavi > contentarea && subnavi > custom) {
                $("#contentarea, #custom").height(subnavi);
                $("#custom_start").height(subnavi + 17);
            }
            else {
                if (custom > contentarea && custom > subnavi) {
                    $("#subnavi, #contentarea").height(custom);
                }
            }
        }
    } 
    catch (e) {
    }
}

